diff options
-rw-r--r-- | Cargo.lock | 4 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/collect_files.rs | 2 | ||||
-rw-r--r-- | src/generate_html.rs | 43 |
4 files changed, 21 insertions, 30 deletions
@@ -4,8 +4,8 @@ version = 4 [[package]] name = "markdown" -version = "2.1.2" -source = "git+git://benbridle.com/markdown?tag=v2.1.2#5c9ff7c79fec508017ea6c15b2612bc99276a9b6" +version = "3.1.0" +source = "git+git://benbridle.com/markdown?tag=v3.1.0#7933e285f9df67141094935ed4d1e525a920315b" [[package]] name = "recipe" @@ -5,7 +5,7 @@ edition = "2021" [dependencies] vagabond = { git = "git://benbridle.com/vagabond", tag = "v1.0.3" } -markdown = { git = "git://benbridle.com/markdown", tag = "v2.1.2" } +markdown = { git = "git://benbridle.com/markdown", tag = "v3.1.0" } recipe = { git = "git://benbridle.com/recipe", tag = "v1.4.0" } xflags = "0.4.0-pre.1" diff --git a/src/collect_files.rs b/src/collect_files.rs index df4ac5e..14bf0c3 100644 --- a/src/collect_files.rs +++ b/src/collect_files.rs @@ -296,7 +296,7 @@ impl Website { pub fn has_static(&self, from: &impl LinkFrom, path: &str) -> Option<String> { // Attach parent if not an absolute path. let path = match !path.starts_with('/') { - true => collapse_path(&format!("{}{path}", make_url_safe(from.parent_url()))), + true => collapse_path(&format!("{}{path}", from.parent_url())), false => collapse_path(path), }; for file in &self.static_files { 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!("<div class='math'>{stripped}</div>"); - continue; - } - } - tag!("p", line); - } + Block::Paragraph(line) => tag!("p", line), + Block::Math(content) => html!("<div class='math'>{content}</div>"), Block::List(lines) => wrap!("ul", for line in lines { // Insert a <br> 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!("<li class='{class}'>{output}</li>") }), 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!("<code>{text}</code>")) } Token::Math(text) => { let text = &sanitize_text(text); html.push_str(&format!("<span class='math'>{text}</span>")) } - 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!("<a href='{path}' class='{class}'>{label}</a>")) @@ -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!("<a href='{path}' class='external'>{label}</a>")); } |