diff options
| -rw-r--r-- | src/collect_files.rs | 2 | ||||
| -rw-r--r-- | src/generate_html.rs | 33 | ||||
| -rw-r--r-- | src/string_utils.rs | 18 |
3 files changed, 37 insertions, 16 deletions
diff --git a/src/collect_files.rs b/src/collect_files.rs index d6d7cc2..cf2f307 100644 --- a/src/collect_files.rs +++ b/src/collect_files.rs @@ -344,7 +344,7 @@ impl Website { if parents.is_empty() { // This is the index file for the whole site. self.pages.push(Page { - name: "Home".into(), + name: self.name.clone(), url: String::from("index"), parents, parent_url: String::from(""), diff --git a/src/generate_html.rs b/src/generate_html.rs index 467feeb..3b6ee62 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -125,11 +125,11 @@ pub fn get_html_head(page: &Page, website: &Website) -> String { pub fn get_table_of_contents(page: &Page) -> String { let mut toc = String::from("<ul>\n"); - let site_name = sanitize_text(&page.name.plain(), true); - toc.push_str(&format!("<li class='l1'><a href='#title'>{site_name}</a></li>\n")); + let page_name = sanitize_text(&page.name.plain(), true); + toc.push_str(&format!("<li class='l1'><a href='#title'>{page_name}</a></li>\n")); for heading in &page.headings { - let name = &heading.name; + let name = sanitize_text(&heading.name.plain(), true); let url = &heading.slug(); let class = match heading.level { Level::Heading1 => "l1", @@ -168,22 +168,35 @@ pub fn document_to_html(page: &Page, website: &Website) -> String { html!("</article>"); html!("<article>"); prefix = Some(to_slug(&line.to_string())); - // html!("<nav class='return'><a href='#'></a></nav>"); }; // Find namespaced heading ID from headings list. let url = match page.headings.iter().find(|h| h.block_id == i) { Some(heading) => heading.slug(), None => unreachable!("Couldn't find heading in headings list"), }; - // let url = to_slug(strip_appendix(&line.to_string())); let heading_tag = match level { Level::Heading1 => "h1", Level::Heading2 => "h2", Level::Heading3 => "h3", }; - wrap!(heading_tag, format!("id='{url}'"), { - tag!("a", line, format!("href='#{url}'")); - }); + // Find out whether line contains a link. + let mut contains_link = false; + for token in &line.tokens { + if let Token::InternalLink { .. } | Token::ExternalLink { .. } = token { + contains_link = true; + break; + } + } + if !contains_link { + wrap!(heading_tag, format!("id='{url}'"), { + tag!("a", line, format!("href='#{url}'")); + }); + } else { + // Leave off the heading <a> tag if the heading already contains + // a link (<a> tags cannot be nested). + tag!(heading_tag, line, format!("id='{url}'")); + } + } Block::Paragraph(line) => tag!("p", line), Block::Math(content) => html!("<div class='math'>{}</div>", sanitize_text(content, false)), @@ -199,8 +212,8 @@ pub fn document_to_html(page: &Page, website: &Website) -> String { depth += 1; } else if c == '/' && prev == '<' { depth -= 2; // 2 because prev was a '<' as well. - } else if c == ':' && depth == 0 { - output.pop(); output.push_str("<br>"); + } else if c == ' ' && prev == ':' && depth == 0 { + output.pop(); output.pop(); output.push_str("<br>"); class.push_str("extended"); depth += 99; } prev = c; diff --git a/src/string_utils.rs b/src/string_utils.rs index 2061110..cceb575 100644 --- a/src/string_utils.rs +++ b/src/string_utils.rs @@ -66,11 +66,19 @@ impl From<&str> for Name { // Turn a string into a tidy URL slug. pub fn to_slug(text: &str) -> String { - text.to_ascii_lowercase().chars().filter_map(|c| - if c.is_alphanumeric() || "-_~.+/#".contains(c) { Some(c) } - else if c == ' ' { Some('-') } - else { None } ) - .collect() + let mut string = String::new(); + let mut prev = ' '; + for c in text.to_lowercase().chars() { + let c = match c == ' ' { + true => '-', + false => c, + }; + if c.is_alphanumeric() { string.push(c) } + if "_~.+/#".contains(c) { string.push(c) } + if c == '-' && prev != '-' { string.push(c) } + prev = c; + } + return string; } // Prevent link hrefs from breaking out of quotations. |
