diff options
| author | Philipp A | 2019-12-08 16:05:52 +0100 | 
|---|---|---|
| committer | Philipp A | 2019-12-08 16:05:52 +0100 | 
| commit | 588f2474c4f13417c9dae18defc657fcb6c0f07e (patch) | |
| tree | 9bcedef291ee4f0a0dca61ce364c8d966614ec50 /src | |
| parent | 2f19fa55606306964f5e4d6a08b6957f547f7066 (diff) | |
| download | rust-rst-588f2474c4f13417c9dae18defc657fcb6c0f07e.tar.bz2 | |
Convert bullet list
Diffstat (limited to 'src')
| -rw-r--r-- | src/parser/conversion/block.rs | 57 | ||||
| -rw-r--r-- | src/parser/conversion/inline.rs | 4 | ||||
| -rw-r--r-- | src/renderer/html.rs | 39 | 
3 files changed, 75 insertions, 25 deletions
| diff --git a/src/parser/conversion/block.rs b/src/parser/conversion/block.rs index cc2d7d1..1e5d88f 100644 --- a/src/parser/conversion/block.rs +++ b/src/parser/conversion/block.rs @@ -13,7 +13,7 @@ use crate::parser::{  	pest_rst::Rule,  	pair_ext_parse::PairExt,  }; -use super::{whitespace_normalize_name, inline::convert_inline}; +use super::{whitespace_normalize_name, inline::convert_inlines};  #[derive(PartialEq)] @@ -28,18 +28,35 @@ pub(super) enum TitleOrSsubel {  pub(super) fn convert_ssubel(pair: Pair<Rule>) -> Result<Option<TitleOrSsubel>, Error> {  	use self::TitleOrSsubel::*;  	Ok(Some(match pair.as_rule() { -		Rule::title            => { let (t, k) = convert_title(pair)?; Title(t, k) }, -		Rule::paragraph        => Ssubel(convert_paragraph(pair)?.into()), -		Rule::target           => Ssubel(convert_target(pair)?.into()), -		Rule::substitution_def => Ssubel(convert_substitution_def(pair)?.into()), -		Rule::admonition_gen   => Ssubel(convert_admonition_gen(pair)?.into()), -		Rule::image            => Ssubel(convert_image::<e::Image>(pair)?.into()), -		Rule::EOI              => return Ok(None), -		rule => panic!("unknown rule {:?}", rule), +		Rule::title => { let (t, k) = convert_title(pair)?; Title(t, k) }, +		//TODO: subtitle, dectoration, docinfo +		Rule::EOI   => return Ok(None), +		_           => Ssubel(convert_substructure(pair)?.into()),  	}))  } +fn convert_substructure(pair: Pair<Rule>) -> Result<c::SubStructure, Error> { +	Ok(match pair.as_rule() { +		// todo: Topic, Sidebar, Transition, Section +		_ => convert_body_elem(pair)?.into(), +	}) +} + + +fn convert_body_elem(pair: Pair<Rule>) -> Result<c::BodyElement, Error> { +	Ok(match pair.as_rule() { +		Rule::paragraph        => convert_paragraph(pair)?.into(), +		Rule::target           => convert_target(pair)?.into(), +		Rule::substitution_def => convert_substitution_def(pair)?.into(), +		Rule::admonition_gen   => convert_admonition_gen(pair)?.into(), +		Rule::image            => convert_image::<e::Image>(pair)?.into(), +		Rule::bullet_list      => convert_bullet_list(pair)?.into(), +		rule => unimplemented!("unhandled rule {:?}", rule), +	}) +} + +  fn convert_title(pair: Pair<Rule>) -> Result<(e::Title, TitleKind), Error> {  	let mut title: Option<Vec<c::TextOrInlineElement>> = None;  	let mut adornment_char: Option<char> = None; @@ -48,7 +65,7 @@ fn convert_title(pair: Pair<Rule>) -> Result<(e::Title, TitleKind), Error> {  	let kind = inner_pair.as_rule();  	for p in inner_pair.into_inner() {  		match p.as_rule() { -			Rule::line => title = Some(p.into_inner().map(convert_inline).collect::<Result<_,_>>()?), +			Rule::line => title = Some(convert_inlines(p)?),  			Rule::adornments => adornment_char = Some(p.as_str().chars().next().expect("Empty adornment?")),  			rule => unimplemented!("Unexpected rule in title: {:?}", rule),  		}; @@ -66,8 +83,7 @@ fn convert_title(pair: Pair<Rule>) -> Result<(e::Title, TitleKind), Error> {  fn convert_paragraph(pair: Pair<Rule>) -> Result<e::Paragraph, Error> { -	let children = pair.into_inner().map(convert_inline).collect::<Result<_,_>>()?; -	Ok(e::Paragraph::with_children(children)) +	Ok(e::Paragraph::with_children(convert_inlines(pair)?))  } @@ -105,7 +121,7 @@ fn convert_substitution_def(pair: Pair<Rule>) -> Result<e::SubstitutionDefinitio  fn convert_replace(pair: Pair<Rule>) -> Result<Vec<c::TextOrInlineElement>, Error> {  	let mut pairs = pair.into_inner();  	let paragraph = pairs.next().unwrap(); -	paragraph.into_inner().map(convert_inline).collect() +	convert_inlines(paragraph)  }   fn convert_image<I>(pair: Pair<Rule>) -> Result<I, Error> where I: Element + ExtraAttributes<a::Image> { @@ -159,3 +175,18 @@ fn convert_admonition_gen(pair: Pair<Rule>) -> Result<c::BodyElement, Error> {  		typ         => panic!("Unknown admontion type {}!", typ),  	})  } + +fn convert_bullet_list(pair: Pair<Rule>) -> Result<e::BulletList, Error> { +	Ok(e::BulletList::with_children(pair.into_inner().map(convert_bullet_item).collect::<Result<_, _>>()?)) +} + +fn convert_bullet_item(pair: Pair<Rule>) -> Result<e::ListItem, Error> { +	let mut iter = pair.into_inner(); +	let mut children: Vec<c::BodyElement> = vec![ +		convert_paragraph(iter.next().unwrap())?.into() +	]; +	for p in iter { +		children.push(convert_body_elem(p)?); +	} +	Ok(e::ListItem::with_children(children)) +} diff --git a/src/parser/conversion/inline.rs b/src/parser/conversion/inline.rs index 79e6f12..ed118c3 100644 --- a/src/parser/conversion/inline.rs +++ b/src/parser/conversion/inline.rs @@ -26,6 +26,10 @@ pub fn convert_inline(pair: Pair<Rule>) -> Result<c::TextOrInlineElement, Error>  	})  } +pub fn convert_inlines(pair: Pair<Rule>) -> Result<Vec<c::TextOrInlineElement>, Error> { +	pair.into_inner().map(convert_inline).collect() +} +  fn convert_reference(pair: Pair<Rule>) -> Result<c::TextOrInlineElement, Error> {  	let name;  	let refuri; diff --git a/src/renderer/html.rs b/src/renderer/html.rs index b3ee343..c892e5f 100644 --- a/src/renderer/html.rs +++ b/src/renderer/html.rs @@ -48,18 +48,33 @@ macro_rules! impl_html_render_cat {($cat:ident { $($member:ident),+ }) => {  	}  }} -macro_rules! impl_html_render_simple {( $($type:ident => $tag:ident),+ ) => { $( -	impl HTMLRender for e::$type { -		fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write { -			write!(stream, "<{}>", stringify!($tag))?; -			for c in self.children() { -				(*c).render_html(stream)?; +macro_rules! impl_html_render_simple { +	( +		$type1:ident => $tag1:ident $( [$($post1:tt)+] )?, +		$( $type:ident => $tag:ident $( [$($post:tt)+] )? ),+ +	) => { +		impl_html_render_simple!($type1 => $tag1 $([$($post1)+])?); +		$( impl_html_render_simple!($type => $tag $([$($post)+])?); )+ +	}; +	( $type:ident => $tag:ident ) => { +		impl_html_render_simple!($type => $tag[""]); +	}; +	( $type:ident => $tag:ident [$post:expr] ) => { +		impl_html_render_simple!($type => $tag["", $post]); +	}; +	( $type:ident => $tag:ident [ $post1:expr, $post2:expr ] ) => { +		impl HTMLRender for e::$type { +			fn render_html<W>(&self, stream: &mut W) -> Result<(), Error> where W: Write { +				write!(stream, concat!("<{}>", $post1), stringify!($tag))?; +				for c in self.children() { +					(*c).render_html(stream)?; +				} +				write!(stream, concat!("</{}>", $post2), stringify!($tag))?; +				Ok(())  			} -			write!(stream, "</{}>", stringify!($tag))?; -			Ok(())  		} -	} -)+ }} +	}; +}  macro_rules! impl_html_render_simple_nochildren {( $($type:ident => $tag:ident),+ ) => { $(  	impl HTMLRender for e::$type { @@ -118,7 +133,7 @@ impl HTMLRender for e::Topic {  }  impl_html_render_cat!(BodyElement { Paragraph, LiteralBlock, DoctestBlock, MathBlock, Rubric, SubstitutionDefinition, Comment, Pending, Target, Raw, Image, Compound, Container, BulletList, EnumeratedList, DefinitionList, FieldList, OptionList, LineBlock, BlockQuote, Admonition, Attention, Hint, Note, Caution, Danger, Error, Important, Tip, Warning, Footnote, Citation, SystemMessage, Figure, Table }); -impl_html_render_simple!(Paragraph => p, LiteralBlock => pre, MathBlock => math, Rubric => a, Compound => p, Container => div, BulletList => ul, EnumeratedList => ol, DefinitionList => dl, FieldList => dl, OptionList => pre, LineBlock => div, BlockQuote => blockquote, Admonition => aside, Attention => aside, Hint => aside, Note => aside, Caution => aside, Danger => aside, Error => aside, Important => aside, Tip => aside, Warning => aside, Figure => figure); +impl_html_render_simple!(Paragraph => p, LiteralBlock => pre, MathBlock => math, Rubric => a, Compound => p, Container => div, BulletList => ul["\n", "\n"], EnumeratedList => ol["\n", "\n"], DefinitionList => dl["\n", "\n"], FieldList => dl["\n", "\n"], OptionList => pre, LineBlock => div["\n", "\n"], BlockQuote => blockquote, Admonition => aside, Attention => aside, Hint => aside, Note => aside, Caution => aside, Danger => aside, Error => aside, Important => aside, Tip => aside, Warning => aside, Figure => figure);  impl_html_render_simple_nochildren!(Table => table);  //TODO: after implementing the table, move it to elems with children  impl<I> HTMLRender for I where I: e::Element + a::ExtraAttributes<a::Image> { @@ -282,7 +297,7 @@ impl HTMLRender for e::RawInline {  impl_html_render_cat!(SubTopic { Title, BodyElement });  impl_html_render_cat!(SubSidebar { Topic, Title, Subtitle, BodyElement }); -impl_html_render_simple!(ListItem => li); +impl_html_render_simple!(ListItem => li["\n"]);  impl HTMLRender for e::DefinitionListItem {  	fn render_html<W>(&self, _stream: &mut W) -> Result<(), Error> where W: Write { | 
