From 245c90841003c2382150e583104ea5d85594cd31 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 17 Dec 2020 17:15:58 +0100 Subject: [PATCH] Fix nested anchors and placeholders Signed-off-by: trivernis --- src/parser/block.rs | 5 +++-- src/parser/line.rs | 3 +++ src/parser/mod.rs | 4 +++- src/references/placeholders.rs | 12 ++++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/parser/block.rs b/src/parser/block.rs index e25bc61..c2c5a40 100644 --- a/src/parser/block.rs +++ b/src/parser/block.rs @@ -102,6 +102,8 @@ impl ParseBlock for Parser { header.size = size; self.section_nesting = size; self.sections.push(size); + self.section_anchors.push(header.anchor.clone()); + let mut section = Section::new(header); section.metadata = metadata; self.ctm.seek_whitespace(); @@ -111,6 +113,7 @@ impl ParseBlock for Parser { } self.sections.pop(); + self.section_anchors.pop(); if let Some(sec) = self.sections.last() { self.section_nesting = *sec } else { @@ -337,8 +340,6 @@ impl ParseBlock for Parser { .map(|m| m.get_string_map()) .unwrap_or(HashMap::new()); - self.ctm.seek_whitespace(); - match self.import(path.clone(), &metadata) { ImportType::Document(Ok(anchor)) => Ok(Some(Import { path, anchor })), ImportType::Stylesheet(_) => Ok(None), diff --git a/src/parser/line.rs b/src/parser/line.rs index 151224e..13cdeef 100644 --- a/src/parser/line.rs +++ b/src/parser/line.rs @@ -53,6 +53,9 @@ impl ParseLine for Parser { self.ctm.get_text()[start_index..self.ctm.get_index()] .iter() .for_each(|e| anchor.push(*e)); + if let Some(last) = self.section_anchors.last() { + anchor = format!("{}-{}", last, anchor); + } anchor.retain(|c| !c.is_whitespace()); log::trace!("Line::Header"); Ok(Header::new(line, anchor)) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index f082081..a05d390 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -88,6 +88,7 @@ pub struct Parser { pub(crate) ctm: CharTapeMachine, section_nesting: u8, sections: Vec, + section_anchors: Vec, section_return: Option, wg: WaitGroup, pub(crate) block_break_at: Vec, @@ -111,6 +112,7 @@ impl Parser { Self { options, sections: Vec::new(), + section_anchors: Vec::new(), section_nesting: 0, section_return: None, wg: WaitGroup::new(), @@ -140,7 +142,7 @@ impl Parser { text_unil.reverse(); let mut inline_pos = 0; - while text_unil[inline_pos] != LB { + while inline_pos < text_unil.len() && text_unil[inline_pos] != LB { inline_pos += 1; } if let Some(path) = &self.options.path { diff --git a/src/references/placeholders.rs b/src/references/placeholders.rs index 9376fc7..14f8950 100644 --- a/src/references/placeholders.rs +++ b/src/references/placeholders.rs @@ -35,6 +35,8 @@ const P_GLS: &str = "gls"; const P_DATE: &str = "date"; const P_TIME: &str = "time"; const P_DATETIME: &str = "datetime"; +const P_AUTHOR: &str = "author"; +const P_TITLE: &str = "title"; impl ProcessPlaceholders for Document { /// parses all placeholders and assigns values to them @@ -65,6 +67,16 @@ impl ProcessPlaceholders for Document { P_DATETIME => pholder.set_value(inline!(Inline::Plain(PlainText { value: format!("{} {}", get_date_string(), get_time_string()) }))), + P_AUTHOR => { + if let Some(value) = self.config.lock().metadata.author.clone() { + pholder.set_value(inline!(Inline::Plain(PlainText { value }))) + } + } + P_TITLE => { + if let Some(value) = self.config.lock().metadata.title.clone() { + pholder.set_value(inline!(Inline::Plain(PlainText { value }))) + } + } _ => { if let Some(value) = self .config