planting a seed
This commit is contained in:
commit
e69dcdc197
11 changed files with 561 additions and 0 deletions
11
crates/treehouse-incubator/Cargo.toml
Normal file
11
crates/treehouse-incubator/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "treehouse-incubator"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
handlebars = "4.3.7"
|
||||
pulldown-cmark = { version = "0.9.3", default-features = false }
|
||||
thiserror = "1.0.47"
|
||||
treehouse-format = { workspace = true }
|
||||
|
45
crates/treehouse-incubator/src/main.rs
Normal file
45
crates/treehouse-incubator/src/main.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use tree_html::HtmlGenerator;
|
||||
|
||||
mod tree_html;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
enum Error {
|
||||
#[error("I/O error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("treehouse parsing error: {0}")]
|
||||
Parse(#[from] treehouse_format::ParseError),
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let _ = std::fs::remove_dir_all("target/site");
|
||||
std::fs::create_dir_all("target/site")?;
|
||||
|
||||
let root_file = std::fs::read_to_string("content/tree/root.tree")?;
|
||||
|
||||
let mut parser = treehouse_format::Parser {
|
||||
input: &root_file,
|
||||
position: 0,
|
||||
};
|
||||
let mut generator = HtmlGenerator::default();
|
||||
while let Some(branch) = parser.next_branch()? {
|
||||
for _ in 0..branch.indent_level {
|
||||
print!(" ");
|
||||
}
|
||||
println!(
|
||||
"{} {:?}",
|
||||
branch.kind.char(),
|
||||
&root_file[branch.content.clone()]
|
||||
);
|
||||
generator.add(&root_file, &branch);
|
||||
}
|
||||
std::fs::write(
|
||||
"target/site/index.html",
|
||||
format!(
|
||||
"<!DOCTYPE html><html><head></head><body>{}</body></html>",
|
||||
generator.finish()
|
||||
),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
30
crates/treehouse-incubator/src/tree_html.rs
Normal file
30
crates/treehouse-incubator/src/tree_html.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
use treehouse_format::Branch;
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct HtmlGenerator {
|
||||
buffer: String,
|
||||
indent_level_stack: Vec<usize>,
|
||||
}
|
||||
|
||||
impl HtmlGenerator {
|
||||
pub fn add(&mut self, source: &str, branch: &Branch) {
|
||||
if Some(&branch.indent_level) > self.indent_level_stack.last() {
|
||||
self.indent_level_stack.push(branch.indent_level);
|
||||
self.buffer.push_str("<ul>");
|
||||
}
|
||||
while Some(&branch.indent_level) < self.indent_level_stack.last() {
|
||||
self.indent_level_stack.pop();
|
||||
self.buffer.push_str("</ul>");
|
||||
}
|
||||
self.buffer.push_str("<li>");
|
||||
self.buffer.push_str(&source[branch.content.clone()]);
|
||||
self.buffer.push_str("</li>");
|
||||
}
|
||||
|
||||
pub fn finish(mut self) -> String {
|
||||
for _ in self.indent_level_stack.drain(..) {
|
||||
self.buffer.push_str("</ul>");
|
||||
}
|
||||
self.buffer
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue