add: flag to toggle including top level header in outline
This commit is contained in:
parent
04ebd38691
commit
aa7b1d0a63
2 changed files with 21 additions and 7 deletions
21
src/lib.rs
21
src/lib.rs
|
|
@ -10,6 +10,7 @@ use log::info;
|
||||||
pub fn compile_article(
|
pub fn compile_article(
|
||||||
article_dir: &PathBuf,
|
article_dir: &PathBuf,
|
||||||
prepend: &Option<PathBuf>,
|
prepend: &Option<PathBuf>,
|
||||||
|
include_title: bool,
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
info!("compiling {} ...", article_dir.display());
|
info!("compiling {} ...", article_dir.display());
|
||||||
|
|
||||||
|
|
@ -56,7 +57,7 @@ pub fn compile_article(
|
||||||
|
|
||||||
let mut outline = EcoString::new();
|
let mut outline = EcoString::new();
|
||||||
let mut curr_level = 1;
|
let mut curr_level = 1;
|
||||||
parse_outline(&mut doc.root, &mut outline, &mut curr_level);
|
parse_outline(&mut doc.root, &mut outline, &mut curr_level, include_title);
|
||||||
for i in (1..curr_level).rev() {
|
for i in (1..curr_level).rev() {
|
||||||
outline.push_str(" ".repeat(i as usize - 1).as_str());
|
outline.push_str(" ".repeat(i as usize - 1).as_str());
|
||||||
outline.push_str("</ul>\n");
|
outline.push_str("</ul>\n");
|
||||||
|
|
@ -108,11 +109,20 @@ pub fn compile_article(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_outline(elem: &mut HtmlElement, outline: &mut EcoString, curr_level: &mut u32) {
|
fn parse_outline(
|
||||||
|
elem: &mut HtmlElement,
|
||||||
|
outline: &mut EcoString,
|
||||||
|
curr_level: &mut u32,
|
||||||
|
include_title: bool,
|
||||||
|
) {
|
||||||
if matches!(
|
if matches!(
|
||||||
elem.tag.to_string().as_str(),
|
elem.tag.to_string().as_str(),
|
||||||
"<h2>" | "<h3>" | "<h4>" | "<h5>" | "<h6>"
|
"<h2>" | "<h3>" | "<h4>" | "<h5>" | "<h6>"
|
||||||
) {
|
) {
|
||||||
|
if !include_title && elem.tag.to_string().as_str() == "<h2>" {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let mut header_text = EcoString::new();
|
let mut header_text = EcoString::new();
|
||||||
|
|
||||||
for child in &elem.children {
|
for child in &elem.children {
|
||||||
|
|
@ -167,7 +177,7 @@ fn parse_outline(elem: &mut HtmlElement, outline: &mut EcoString, curr_level: &m
|
||||||
|
|
||||||
for child in elem.children.make_mut().iter_mut() {
|
for child in elem.children.make_mut().iter_mut() {
|
||||||
match child {
|
match child {
|
||||||
HtmlNode::Element(e) => parse_outline(e, outline, curr_level),
|
HtmlNode::Element(e) => parse_outline(e, outline, curr_level, include_title),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -176,16 +186,17 @@ fn parse_outline(elem: &mut HtmlElement, outline: &mut EcoString, curr_level: &m
|
||||||
pub fn compile_all(
|
pub fn compile_all(
|
||||||
root_dir: &PathBuf,
|
root_dir: &PathBuf,
|
||||||
prepend: &Option<PathBuf>,
|
prepend: &Option<PathBuf>,
|
||||||
|
include_title_in_outline: bool,
|
||||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
for entry in fs::read_dir(root_dir)? {
|
for entry in fs::read_dir(root_dir)? {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
|
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
compile_all(&path, prepend)?;
|
compile_all(&path, prepend, include_title_in_outline)?;
|
||||||
} else if path.file_name().is_some_and(|n| n == "index.typ") {
|
} else if path.file_name().is_some_and(|n| n == "index.typ") {
|
||||||
let dir = path.parent().unwrap().to_path_buf();
|
let dir = path.parent().unwrap().to_path_buf();
|
||||||
compile_article(&dir, prepend)?;
|
compile_article(&dir, prepend, include_title_in_outline)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,9 @@ struct Args {
|
||||||
|
|
||||||
#[arg(short)]
|
#[arg(short)]
|
||||||
recursive: bool,
|
recursive: bool,
|
||||||
|
|
||||||
|
#[arg(long)]
|
||||||
|
include_title_in_outline: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
@ -28,9 +31,9 @@ fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
let result = if args.recursive {
|
let result = if args.recursive {
|
||||||
compile_all(&args.dir, &args.prepend)
|
compile_all(&args.dir, &args.prepend, args.include_title_in_outline)
|
||||||
} else {
|
} else {
|
||||||
compile_article(&args.dir, &args.prepend)
|
compile_article(&args.dir, &args.prepend, args.include_title_in_outline)
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue