diff options
| author | Philipp A | 2019-12-08 20:02:58 +0100 |
|---|---|---|
| committer | Philipp A | 2019-12-08 20:02:58 +0100 |
| commit | 008b29ea8a15b28df5b84efaf124bd79aa1f8fa3 (patch) | |
| tree | 12967419978835cf42749279dba38aef7f1dabdd /src/parser | |
| parent | f1c1422d0ae17f46999735bbb787cca188ecfa54 (diff) | |
| download | rust-rst-008b29ea8a15b28df5b84efaf124bd79aa1f8fa3.tar.bz2 | |
Render simple section IDs and numbers
Diffstat (limited to 'src/parser')
| -rw-r--r-- | src/parser/conversion.rs | 8 | ||||
| -rw-r--r-- | src/parser/conversion/block.rs | 20 | ||||
| -rw-r--r-- | src/parser/conversion/inline.rs | 2 |
3 files changed, 22 insertions, 8 deletions
diff --git a/src/parser/conversion.rs b/src/parser/conversion.rs index 32461bf..f9e2a78 100644 --- a/src/parser/conversion.rs +++ b/src/parser/conversion.rs @@ -5,9 +5,10 @@ use failure::Error; use pest::iterators::Pairs; use crate::document_tree::{ - HasChildren, + Element,HasChildren, elements as e, element_categories as c, + attribute_types as at, }; use super::pest_rst::Rule; @@ -63,7 +64,10 @@ pub fn convert_document(pairs: Pairs<Rule>) -> Result<e::Document, Error> { None => kinds.push(kind), } let super_level = get_level(&mut toplevel, §ion_idxs); - super_level.push(e::Section::with_children(vec![title.into()]).into()); + let slug = title.names().iter().next().map(|at::NameToken(name)| at::ID(name.to_owned())); + let mut section = e::Section::with_children(vec![title.into()]); + section.ids_mut().extend(slug.into_iter()); + super_level.push(section.into()); section_idxs.push(Some(super_level.len() - 1)); }, Ssubel(elem) => get_level(&mut toplevel, §ion_idxs).push(elem), diff --git a/src/parser/conversion/block.rs b/src/parser/conversion/block.rs index 1e5d88f..b14c2b5 100644 --- a/src/parser/conversion/block.rs +++ b/src/parser/conversion/block.rs @@ -29,7 +29,7 @@ pub(super) fn convert_ssubel(pair: Pair<Rule>) -> Result<Option<TitleOrSsubel>, use self::TitleOrSsubel::*; Ok(Some(match pair.as_rule() { Rule::title => { let (t, k) = convert_title(pair)?; Title(t, k) }, - //TODO: subtitle, dectoration, docinfo + //TODO: subtitle, decoration, docinfo Rule::EOI => return Ok(None), _ => Ssubel(convert_substructure(pair)?.into()), })) @@ -38,7 +38,8 @@ pub(super) fn convert_ssubel(pair: Pair<Rule>) -> Result<Option<TitleOrSsubel>, fn convert_substructure(pair: Pair<Rule>) -> Result<c::SubStructure, Error> { Ok(match pair.as_rule() { - // todo: Topic, Sidebar, Transition, Section + // todo: Topic, Sidebar, Transition + // no section here, as it’s constructed from titles _ => convert_body_elem(pair)?.into(), }) } @@ -58,21 +59,30 @@ fn convert_body_elem(pair: Pair<Rule>) -> Result<c::BodyElement, Error> { fn convert_title(pair: Pair<Rule>) -> Result<(e::Title, TitleKind), Error> { - let mut title: Option<Vec<c::TextOrInlineElement>> = None; + let mut title: Option<String> = None; + let mut title_inlines: Option<Vec<c::TextOrInlineElement>> = None; let mut adornment_char: Option<char> = None; // title_double or title_single. Extract kind before consuming let inner_pair = pair.into_inner().next().unwrap(); let kind = inner_pair.as_rule(); for p in inner_pair.into_inner() { match p.as_rule() { - Rule::line => title = Some(convert_inlines(p)?), + Rule::line => { + title = Some(p.as_str().to_owned()); + title_inlines = Some(convert_inlines(p)?); + }, Rule::adornments => adornment_char = Some(p.as_str().chars().next().expect("Empty adornment?")), rule => unimplemented!("Unexpected rule in title: {:?}", rule), }; } // now we encountered one line of text and one of adornments // TODO: emit error if the adornment line is too short (has to match title length) - let elem = e::Title::with_children(title.expect("No text in title")); + let mut elem = e::Title::with_children(title_inlines.expect("No text in title")); + if let Some(title) = title { + //TODO: slugify properly + let slug = title.to_lowercase().replace("\n", "").replace(" ", "-"); + elem.names_mut().push(at::NameToken(slug)); + } let title_kind = match kind { Rule::title_double => TitleKind::Double(adornment_char.unwrap()), Rule::title_single => TitleKind::Single(adornment_char.unwrap()), diff --git a/src/parser/conversion/inline.rs b/src/parser/conversion/inline.rs index bdabbf0..52803ef 100644 --- a/src/parser/conversion/inline.rs +++ b/src/parser/conversion/inline.rs @@ -2,11 +2,11 @@ use failure::Error; use pest::iterators::Pair; use crate::document_tree::{ + HasChildren, elements as e, element_categories as c, extra_attributes as a, attribute_types as at, - element_categories::HasChildren, }; use crate::parser::{ |
