From ad84a79335fd396dd07243fe7625fb420cda3981 Mon Sep 17 00:00:00 2001 From: lqdev Date: Fri, 18 Aug 2023 13:39:08 +0200 Subject: [PATCH] tweak html generation, rename treehouse-incubator to treehouse --- Cargo.lock | 18 ++++++------ .../Cargo.toml | 2 +- .../src/main.rs | 7 +++++ crates/treehouse/src/tree_html.rs | 29 +++++++++++++++++++ 4 files changed, 46 insertions(+), 10 deletions(-) rename crates/{treehouse-incubator => treehouse}/Cargo.toml (89%) rename crates/{treehouse-incubator => treehouse}/src/main.rs (90%) create mode 100644 crates/treehouse/src/tree_html.rs diff --git a/Cargo.lock b/Cargo.lock index 5232b1c..c7d3057 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/crates/treehouse-incubator/Cargo.toml b/crates/treehouse/Cargo.toml similarity index 89% rename from crates/treehouse-incubator/Cargo.toml rename to crates/treehouse/Cargo.toml index d81a51f..5d54819 100644 --- a/crates/treehouse-incubator/Cargo.toml +++ b/crates/treehouse/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "treehouse-incubator" +name = "treehouse" version = "0.1.0" edition = "2021" diff --git a/crates/treehouse-incubator/src/main.rs b/crates/treehouse/src/main.rs similarity index 90% rename from crates/treehouse-incubator/src/main.rs rename to crates/treehouse/src/main.rs index a8a28b9..48897b3 100644 --- a/crates/treehouse-incubator/src/main.rs +++ b/crates/treehouse/src/main.rs @@ -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> { match parse_result { Ok(roots) => { + let mut html = String::from(""); 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("") } Err(error) => { let writer = StandardStream::stderr(ColorChoice::Auto); diff --git a/crates/treehouse/src/tree_html.rs b/crates/treehouse/src/tree_html.rs new file mode 100644 index 0000000..16b77eb --- /dev/null +++ b/crates/treehouse/src/tree_html.rs @@ -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("
  • "); + { + if !branch.children.is_empty() { + s.push_str(match branch.kind { + BranchKind::Expanded => "
    ", + BranchKind::Collapsed => "
    ", + }); + s.push_str(""); + } + s.push_str(&source[branch.content.clone()]); + if !branch.children.is_empty() { + s.push_str(""); + branches_to_html(s, &branch.children, source); + s.push_str("
    "); + } + } + s.push_str("
  • "); +} + +pub fn branches_to_html(s: &mut String, branches: &[Branch], source: &str) { + s.push_str(""); +}