1
Fork 0

Move Toc printing from fmt::Display

This commit is contained in:
Mark Rousskov 2019-09-13 08:41:27 -04:00
parent 8a9dab3a20
commit 3f144e119e
2 changed files with 17 additions and 22 deletions

View file

@ -752,7 +752,7 @@ impl MarkdownWithToc<'_> {
html::push_html(&mut s, p); html::push_html(&mut s, p);
} }
format!("<nav id=\"TOC\">{}</nav>{}", toc.into_toc(), s) format!("<nav id=\"TOC\">{}</nav>{}", toc.into_toc().print(), s)
} }
} }

View file

@ -1,10 +1,7 @@
//! Table-of-contents creation. //! Table-of-contents creation.
use std::fmt;
use std::string::String;
/// A (recursive) table of contents /// A (recursive) table of contents
#[derive(PartialEq)] #[derive(Debug, PartialEq)]
pub struct Toc { pub struct Toc {
/// The levels are strictly decreasing, i.e. /// The levels are strictly decreasing, i.e.
/// ///
@ -28,7 +25,7 @@ impl Toc {
} }
} }
#[derive(PartialEq)] #[derive(Debug, PartialEq)]
pub struct TocEntry { pub struct TocEntry {
level: u32, level: u32,
sec_number: String, sec_number: String,
@ -165,25 +162,23 @@ impl TocBuilder {
} }
} }
impl fmt::Debug for Toc { impl Toc {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn print_inner(&self, v: &mut String) {
fmt::Display::fmt(self, f) v.push_str("<ul>");
}
}
impl fmt::Display for Toc {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "<ul>")?;
for entry in &self.entries { for entry in &self.entries {
// recursively format this table of contents (the // recursively format this table of contents
// `{children}` is the key). v.push_str(&format!("\n<li><a href=\"#{id}\">{num} {name}</a>",
write!(fmt,
"\n<li><a href=\"#{id}\">{num} {name}</a>{children}</li>",
id = entry.id, id = entry.id,
num = entry.sec_number, name = entry.name, num = entry.sec_number, name = entry.name));
children = entry.children)? entry.children.print_inner(&mut *v);
v.push_str("</li>");
} }
write!(fmt, "</ul>") v.push_str("</ul>");
}
crate fn print(&self) -> String {
let mut v = String::new();
self.print_inner(&mut v);
v
} }
} }