From d0186cc90e8e73bdf1ea95f98da200d4aa0784a7 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 17 Dec 2020 16:52:45 +0100 Subject: [PATCH] Fix toc not using plain text Signed-off-by: trivernis --- src/elements/mod.rs | 89 ++++++++++++++++++++++++++++++++++++++++++--- src/parser/block.rs | 1 + 2 files changed, 85 insertions(+), 5 deletions(-) diff --git a/src/elements/mod.rs b/src/elements/mod.rs index d64c704..631de81 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -252,7 +252,7 @@ pub struct Placeholder { #[derive(Clone, Debug)] pub struct RefLink { - pub(crate) description: Box, + pub(crate) description: TextLine, pub(crate) reference: String, } @@ -352,7 +352,7 @@ impl Document { list.ordered = ordered; self.elements.iter().for_each(|e| match e { Block::Section(sec) => { - if !sec.get_hide_in_toc() { + if !sec.is_hidden_in_toc() { let mut item = ListItem::new(Line::RefLink(sec.header.get_anchor()), 1, ordered); item.children.append(&mut sec.get_toc_list(ordered).items); @@ -486,9 +486,10 @@ impl Section { pub fn get_toc_list(&self, ordered: bool) -> List { let mut list = List::new(); + self.elements.iter().for_each(|e| { if let Block::Section(sec) = e { - if !sec.get_hide_in_toc() { + if !sec.is_hidden_in_toc() { let mut item = ListItem::new(Line::RefLink(sec.header.get_anchor()), 1, ordered); item.children.append(&mut sec.get_toc_list(ordered).items); @@ -500,7 +501,7 @@ impl Section { list } - pub(crate) fn get_hide_in_toc(&self) -> bool { + pub(crate) fn is_hidden_in_toc(&self) -> bool { if let Some(meta) = &self.metadata { meta.get_bool("toc-hidden") } else { @@ -556,7 +557,7 @@ impl Header { pub fn get_anchor(&self) -> RefLink { RefLink { - description: Box::new(self.line.clone()), + description: self.line.as_raw_text().as_plain_line(), reference: self.anchor.clone(), } } @@ -612,6 +613,16 @@ impl TextLine { pub fn add_subtext(&mut self, subtext: Inline) { self.subtext.push(subtext) } + + pub fn as_plain_line(&self) -> TextLine { + TextLine { + subtext: self + .subtext + .iter() + .map(|s| Inline::Plain(s.as_plain_text())) + .collect(), + } + } } impl Table { @@ -814,3 +825,71 @@ impl MetadataValue { } } } + +impl Line { + pub fn as_raw_text(&self) -> TextLine { + match self { + Line::Text(t) => t.clone(), + Line::Ruler(_) => TextLine::new(), + Line::RefLink(r) => r.description.clone(), + Line::Anchor(a) => a.inner.as_raw_text().as_plain_line(), + Line::Centered(c) => c.line.clone(), + Line::BibEntry(_) => TextLine::new(), + } + } +} + +impl Inline { + pub fn as_plain_text(&self) -> PlainText { + match self { + Inline::Plain(p) => p.clone(), + Inline::Bold(b) => b.value.iter().fold( + PlainText { + value: String::new(), + }, + |a, b| PlainText { + value: format!("{} {}", a.value, b.as_plain_text().value), + }, + ), + Inline::Italic(i) => i.value.iter().fold( + PlainText { + value: String::new(), + }, + |a, b| PlainText { + value: format!("{} {}", a.value, b.as_plain_text().value), + }, + ), + Inline::Underlined(u) => u.value.iter().fold( + PlainText { + value: String::new(), + }, + |a, b| PlainText { + value: format!("{} {}", a.value, b.as_plain_text().value), + }, + ), + Inline::Striked(s) => s.value.iter().fold( + PlainText { + value: String::new(), + }, + |a, b| PlainText { + value: format!("{} {}", a.value, b.as_plain_text().value), + }, + ), + Inline::Monospace(m) => PlainText { + value: m.value.clone(), + }, + Inline::Superscript(s) => s.value.iter().fold( + PlainText { + value: String::new(), + }, + |a, b| PlainText { + value: format!("{} {}", a.value, b.as_plain_text().value), + }, + ), + Inline::Colored(c) => c.value.as_plain_text(), + _ => PlainText { + value: String::new(), + }, + } + } +} diff --git a/src/parser/block.rs b/src/parser/block.rs index c85683a..e25bc61 100644 --- a/src/parser/block.rs +++ b/src/parser/block.rs @@ -78,6 +78,7 @@ impl ParseBlock for Parser { fn parse_section(&mut self) -> ParseResult
{ let start_index = self.ctm.get_index(); self.ctm.seek_whitespace(); + if self.ctm.check_char(&HASH) { let mut size = 1; while let Some(_) = self.ctm.next_char() {