From 5e7209f4a669aa59947a923ea031c0be6c5abee0 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sun, 19 Jan 2025 20:01:51 +1300 Subject: Update supported markdown syntax Added math blocks, and changed link syntax, embedded file syntax, and bold line element syntax. --- src/generate_html.rs | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) (limited to 'src/generate_html.rs') diff --git a/src/generate_html.rs b/src/generate_html.rs index 86341e6..383015a 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -115,15 +115,8 @@ pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Webs Level::Heading2 => tag!("h2", line, format!("id='{}'", make_url_safe(&line_to_html!(line)))), Level::Heading3 => tag!("h3", line, format!("id='{}'", make_url_safe(&line_to_html!(line)))), } - Block::Paragraph(line) => { - if let Some(stripped) = line.to_string().strip_prefix("$$ ") { - if let Some(stripped) = stripped.strip_suffix(" $$") { - html!("
{stripped}
"); - continue; - } - } - tag!("p", line); - } + Block::Paragraph(line) => tag!("p", line), + Block::Math(content) => html!("
{content}
"), Block::List(lines) => wrap!("ul", for line in lines { // Insert a
tag directly after the first untagged colon. let mut depth = 0; @@ -157,7 +150,7 @@ pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Webs html!("
  • {output}
  • ") }), Block::Note(lines) => wrap!("aside", for line in lines { tag!("p", line) }), - Block::Embedded { label, path } => match path.rsplit_once('.') { + Block::Embed { label, path } => match path.rsplit_once('.') { Some((_, extension)) => { let path = match path.strip_prefix('/') { Some(stripped) => format!("{root}{stripped}"), @@ -247,6 +240,7 @@ pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Webs fn line_to_html(line: &Line, page: &Page, website: &Website) -> String { + let from = &page.name; let mut html = String::new(); for line_element in &line.tokens { match line_element { @@ -260,30 +254,29 @@ fn line_to_html(line: &Line, page: &Page, website: &Website) -> String { let text = &sanitize_text(text); html.push_str(&format!("{text}")) } Token::Math(text) => { let text = &sanitize_text(text); html.push_str(&format!("{text}")) } - Token::InternalLink(path) => { - let (label, class, path) = match path.split_once('#') { - Some(("", section)) => (section, "heading", format!("#{section}")), - Some((page, section)) => (section, "page", format!("{page}.html#{section}")), - _ => (path.as_str(), "page", format!("{path}.html")), + Token::InternalLink(name) => { + let (class, label, path) = match name.split_once('#') { + Some(("", heading)) => ("heading", heading, format!("#{heading}")), + Some((page, heading)) => ("page", heading, format!("{page}.html#{heading}")), + _ => ("page", name.as_str(), format!("{name}.html")), }; let mut path = make_url_safe(&path); - let full_label = sanitize_text(label); - let label = match full_label.rsplit_once('/') { - Some((_parent, label)) => label.trim(), - None => &full_label, + let label = match label.rsplit_once('/') { + Some((_, label)) => sanitize_text(label.trim()), + None => sanitize_text(label.trim()), }; // Check that the linked internal page exists. if class == "page" { match website.has_page(page, &path, "html") { - Some(resolved_path) => path = resolved_path, - None => warn!("Page {:?} contains link to nonexistent page {path:?}", page.name), + Some(resolved) => path = resolved, + None => warn!("Page {from:?} contains link to nonexistent page {path:?}"), } } // Check that the heading exists. if class == "heading" { let heading = path.strip_prefix('#').unwrap().to_string(); if !page.headings.iter().any(|h| h.url == heading) { - warn!("Page {:?} contains link to nonexistent internal heading {heading:?}", page.name); + warn!("Page {from:?} contains link to nonexistent internal heading {heading:?}"); } } html.push_str(&format!("{label}")) @@ -291,7 +284,6 @@ fn line_to_html(line: &Line, page: &Page, website: &Website) -> String { Token::ExternalLink { label, path } => { let mut path = path.to_owned(); let mut label = label.to_string(); - let mut is_internal = true; for protocol in ["mailto:", "http://", "https://"] { if let Some(stripped) = path.strip_prefix(protocol) { @@ -299,14 +291,14 @@ fn line_to_html(line: &Line, page: &Page, website: &Website) -> String { if label.is_empty() { label = stripped.to_string(); } + break; } - } if is_internal { // Check that the linked static file exists. match website.has_static(page, &path) { Some(resolved_path) => path = resolved_path, - None => warn!("Page {:?} contains link to nonexistent static file {path:?}", page.name), + None => warn!("Page {from:?} contains link to nonexistent static file {path:?}"), } // Take the file name as the label if the link is unlabeled. if label.is_empty() { @@ -316,7 +308,6 @@ fn line_to_html(line: &Line, page: &Page, website: &Website) -> String { }; } } - let label = sanitize_text(&label); html.push_str(&format!("{label}")); } -- cgit v1.2.3-70-g09d2