tweak html generation, rename treehouse-incubator to treehouse

This commit is contained in:
liquidex 2023-08-18 13:39:08 +02:00
parent 0a185250da
commit ad84a79335
4 changed files with 46 additions and 10 deletions

18
Cargo.lock generated
View file

@ -264,15 +264,7 @@ dependencies = [
] ]
[[package]] [[package]]
name = "treehouse-format" name = "treehouse"
version = "0.1.0"
dependencies = [
"log",
"thiserror",
]
[[package]]
name = "treehouse-incubator"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"codespan-reporting", "codespan-reporting",
@ -282,6 +274,14 @@ dependencies = [
"treehouse-format", "treehouse-format",
] ]
[[package]]
name = "treehouse-format"
version = "0.1.0"
dependencies = [
"log",
"thiserror",
]
[[package]] [[package]]
name = "typenum" name = "typenum"
version = "1.16.0" version = "1.16.0"

View file

@ -1,5 +1,5 @@
[package] [package]
name = "treehouse-incubator" name = "treehouse"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View file

@ -1,8 +1,11 @@
mod tree_html;
use codespan_reporting::{ use codespan_reporting::{
diagnostic::{Diagnostic, Label, LabelStyle, Severity}, diagnostic::{Diagnostic, Label, LabelStyle, Severity},
files::SimpleFile, files::SimpleFile,
term::termcolor::{ColorChoice, StandardStream}, term::termcolor::{ColorChoice, StandardStream},
}; };
use tree_html::branches_to_html;
use treehouse_format::{ use treehouse_format::{
ast::{Branch, Roots}, ast::{Branch, Roots},
pull::Parser, pull::Parser,
@ -46,9 +49,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
match parse_result { match parse_result {
Ok(roots) => { Ok(roots) => {
let mut html = String::from("<!DOCTYPE html><html><head></head><body>");
for root in &roots.branches { for root in &roots.branches {
print_branch(root, &root_file); 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) => { Err(error) => {
let writer = StandardStream::stderr(ColorChoice::Auto); let writer = StandardStream::stderr(ColorChoice::Auto);

View 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>");
}