From ee28abce78ffa3b53ff039ff8640a3b37dc5348b Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Thu, 9 Jan 2025 22:15:55 +1300 Subject: Rewrite link handling and add navigation features to generated HTML --- src/generate_html.rs | 105 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 24 deletions(-) (limited to 'src/generate_html.rs') diff --git a/src/generate_html.rs b/src/generate_html.rs index 84c3bdb..dd08885 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -4,44 +4,94 @@ use markdown::*; pub fn generate_html(document: &MarkdownDocument, page: &Page, website: &Website) -> String { + let root = page.root(); + let page_name = &page.name; + let site_name = &website.name; + let mut parent_url = String::new(); + for segment in &page.parents { + parent_url.push_str(&make_url_safe(segment)); parent_url.push('/'); + } + parent_url.pop(); + let parent_name = match page.parents.get(page.parents.len()-1) { + Some(parent) => parent.to_string(), + None => String::new(), + }; + + let head = get_html_head(document, page); let head = head.trim(); + let mut home = format!("{site_name}"); + let mut parent = format!("{parent_name}"); + let mut title = format!("

{page_name}

"); + let mut toc = get_table_of_contents(page); + let main = document_to_html(document, page, website); let main = main.trim(); + + if page.parents.is_empty() { + parent.clear(); + if page.name_url == "index" { + home.clear(); + title.clear(); + toc.clear(); + } + } + format!("\ -{} — {} +{page_name} — {site_name} -{} +{head} +
+ +{title} +{toc} +
-{} +{main}
- \ -", - page.name, website.name, - get_html_head(document, page).trim(), - document_to_html(document, page, website).trim() - ) +") } - pub fn get_html_head(document: &MarkdownDocument, page: &Page) -> String { if let Some(Block::Fragment { language, content }) = document.blocks.first() { if language == "embed-html-head" { return content.to_string(); } } - let back = page.back_string(); + let root = page.root(); format!("\ - - - \ + + + \ ") } +pub fn get_table_of_contents(page: &Page) -> String { + if page.headings.len() < 3 { + return String::new(); + } + let mut toc = String::from("\n"); + return toc; +} + pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Website) -> String { let mut html = String::new(); @@ -54,9 +104,10 @@ pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Webs ($t:expr,$l:expr,$c:expr) => { html!("<{} {}>{}", $t, $c, line_to_html!($l), $t) }; ($t:expr,$l:expr) => { html!("<{}>{}", $t, line_to_html!($l), $t) }; } macro_rules! wrap { - ($t:expr,$f:expr) => {{ html!("<{}>", $t); $f; html!("", $t); }}; - } + ($t:expr,$c:expr,$f:expr) => {{ html!("<{} {}>", $t, $c); $f; html!("", $t); }}; + ($t:expr,$f:expr) => {{ html!("<{}>", $t); $f; html!("", $t); }}; } + let root = page.root(); for block in &document.blocks { match block { Block::Heading { level, line } => match level { @@ -96,11 +147,17 @@ pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Webs }), Block::Note(lines) => wrap!("aside", for line in lines { tag!("p", line) }), Block::Embedded { label, path } => match path.rsplit_once('.') { - Some((_, extension)) => match extension.to_lowercase().as_str() { - "jpg"|"jpeg"|"png"|"webp"|"gif"|"tiff" => html!( - "
{label}
"), - "mp3"|"wav"|"m4a" => html!("