/// A (recursive) table of contents
#[derive(Debug, PartialEq)]
-crate struct Toc {
+pub(crate) struct Toc {
/// The levels are strictly decreasing, i.e.
///
/// `entries[0].level >= entries[1].level >= ...`
}
#[derive(Debug, PartialEq)]
-crate struct TocEntry {
+pub(crate) struct TocEntry {
level: u32,
sec_number: String,
name: String,
/// Progressive construction of a table of contents.
#[derive(PartialEq)]
-crate struct TocBuilder {
+pub(crate) struct TocBuilder {
top_level: Toc,
/// The current hierarchy of parent headings, the levels are
/// strictly increasing (i.e., `chain[0].level < chain[1].level <
}
impl TocBuilder {
- crate fn new() -> TocBuilder {
+ pub(crate) fn new() -> TocBuilder {
TocBuilder { top_level: Toc { entries: Vec::new() }, chain: Vec::new() }
}
/// Converts into a true `Toc` struct.
- crate fn into_toc(mut self) -> Toc {
+ pub(crate) fn into_toc(mut self) -> Toc {
// we know all levels are >= 1.
self.fold_until(0);
self.top_level
/// Push a level `level` heading into the appropriate place in the
/// hierarchy, returning a string containing the section number in
/// `<num>.<num>.<num>` format.
- crate fn push(&mut self, level: u32, name: String, id: String) -> &str {
+ pub(crate) fn push(&mut self, level: u32, name: String, id: String) -> &str {
assert!(level >= 1);
// collapse all previous sections into their parents until we
impl Toc {
fn print_inner(&self, v: &mut String) {
+ use std::fmt::Write as _;
+
v.push_str("<ul>");
for entry in &self.entries {
// recursively format this table of contents
- v.push_str(&format!(
+ let _ = write!(
+ v,
"\n<li><a href=\"#{id}\">{num} {name}</a>",
id = entry.id,
num = entry.sec_number,
name = entry.name
- ));
+ );
entry.children.print_inner(&mut *v);
v.push_str("</li>");
}
v.push_str("</ul>");
}
- crate fn print(&self) -> String {
+ pub(crate) fn print(&self) -> String {
let mut v = String::new();
self.print_inner(&mut v);
v