tweak html generation, rename treehouse-incubator to treehouse
This commit is contained in:
parent
0a185250da
commit
ad84a79335
18
Cargo.lock
generated
18
Cargo.lock
generated
|
@ -264,15 +264,7 @@ dependencies = [
|
|||
]
|
||||
|
||||
[[package]]
|
||||
name = "treehouse-format"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"log",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "treehouse-incubator"
|
||||
name = "treehouse"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"codespan-reporting",
|
||||
|
@ -282,6 +274,14 @@ dependencies = [
|
|||
"treehouse-format",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "treehouse-format"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"log",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.16.0"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "treehouse-incubator"
|
||||
name = "treehouse"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
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,
|
||||
|
@ -46,9 +49,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
|
||||
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);
|
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…
Reference in a new issue