tweak html generation, rename treehouse-incubator to treehouse
This commit is contained in:
parent
0a185250da
commit
ad84a79335
4 changed files with 46 additions and 10 deletions
12
crates/treehouse/Cargo.toml
Normal file
12
crates/treehouse/Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "treehouse"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
codespan-reporting = "0.11.1"
|
||||
handlebars = "4.3.7"
|
||||
pulldown-cmark = { version = "0.9.3", default-features = false }
|
||||
thiserror = "1.0.47"
|
||||
treehouse-format = { workspace = true }
|
||||
|
105
crates/treehouse/src/main.rs
Normal file
105
crates/treehouse/src/main.rs
Normal file
|
@ -0,0 +1,105 @@
|
|||
mod tree_html;
|
||||
|
||||
use codespan_reporting::{
|
||||
diagnostic::{Diagnostic, Label, LabelStyle, Severity},
|
||||
files::SimpleFile,
|
||||
term::termcolor::{ColorChoice, StandardStream},
|
||||
};
|
||||
use tree_html::branches_to_html;
|
||||
use treehouse_format::{
|
||||
ast::{Branch, Roots},
|
||||
pull::Parser,
|
||||
};
|
||||
|
||||
#[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 print_branch(branch: &Branch, source: &str) {
|
||||
fn inner(branch: &Branch, source: &str, indent_level: usize) {
|
||||
for _ in 0..indent_level {
|
||||
print!(" ");
|
||||
}
|
||||
println!(
|
||||
"{} {:?}",
|
||||
branch.kind.char(),
|
||||
&source[branch.content.clone()]
|
||||
);
|
||||
for child in &branch.children {
|
||||
inner(child, source, indent_level + 1);
|
||||
}
|
||||
}
|
||||
inner(branch, source, 0);
|
||||
}
|
||||
|
||||
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 parse_result = Roots::parse(&mut Parser {
|
||||
input: &root_file,
|
||||
position: 0,
|
||||
});
|
||||
|
||||
match parse_result {
|
||||
Ok(roots) => {
|
||||
let mut html = String::from("<!DOCTYPE html><html><head></head><body>");
|
||||
for root in &roots.branches {
|
||||
print_branch(root, &root_file);
|
||||
}
|
||||
branches_to_html(&mut html, &roots.branches, &root_file);
|
||||
std::fs::write("target/site/index.html", &html)?;
|
||||
html.push_str("</body></html>")
|
||||
}
|
||||
Err(error) => {
|
||||
let writer = StandardStream::stderr(ColorChoice::Auto);
|
||||
let config = codespan_reporting::term::Config::default();
|
||||
let files = SimpleFile::new("root.tree", &root_file);
|
||||
let diagnostic = Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: None,
|
||||
message: error.kind.to_string(),
|
||||
labels: vec![Label {
|
||||
style: LabelStyle::Primary,
|
||||
file_id: (),
|
||||
range: error.range,
|
||||
message: String::new(),
|
||||
}],
|
||||
notes: vec![],
|
||||
};
|
||||
codespan_reporting::term::emit(&mut writer.lock(), &config, &files, &diagnostic)?;
|
||||
}
|
||||
}
|
||||
|
||||
// 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(())
|
||||
}
|
29
crates/treehouse/src/tree_html.rs
Normal file
29
crates/treehouse/src/tree_html.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
use treehouse_format::{ast::Branch, pull::BranchKind};
|
||||
|
||||
pub fn branch_to_html(s: &mut String, branch: &Branch, source: &str) {
|
||||
s.push_str("<li>");
|
||||
{
|
||||
if !branch.children.is_empty() {
|
||||
s.push_str(match branch.kind {
|
||||
BranchKind::Expanded => "<details open>",
|
||||
BranchKind::Collapsed => "<details>",
|
||||
});
|
||||
s.push_str("<summary>");
|
||||
}
|
||||
s.push_str(&source[branch.content.clone()]);
|
||||
if !branch.children.is_empty() {
|
||||
s.push_str("</summary>");
|
||||
branches_to_html(s, &branch.children, source);
|
||||
s.push_str("</details>");
|
||||
}
|
||||
}
|
||||
s.push_str("</li>");
|
||||
}
|
||||
|
||||
pub fn branches_to_html(s: &mut String, branches: &[Branch], source: &str) {
|
||||
s.push_str("<ul>");
|
||||
for child in branches {
|
||||
branch_to_html(s, child, source);
|
||||
}
|
||||
s.push_str("</ul>");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue