From 4a6948cde767578b4a91dbfab20f874ccabafb9a Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 27 Oct 2023 15:46:16 -0400 Subject: [PATCH] Add paragraph. --- src/context/element.rs | 15 +++++++++++++-- src/context/mod.rs | 1 + src/context/paragraph.rs | 32 ++++++++++++++++++++++++++++++++ src/intermediate/element.rs | 9 +++++++-- src/intermediate/mod.rs | 2 ++ src/intermediate/object.rs | 8 ++++---- src/intermediate/paragraph.rs | 24 ++++++++++++++++++++++++ 7 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 src/context/paragraph.rs create mode 100644 src/intermediate/paragraph.rs diff --git a/src/context/element.rs b/src/context/element.rs index 3825439..7a09a62 100644 --- a/src/context/element.rs +++ b/src/context/element.rs @@ -6,9 +6,13 @@ use crate::config::Config; use crate::error::CustomError; use crate::intermediate::IElement; +use super::paragraph::RenderParagraph; + #[derive(Debug, Serialize)] #[serde(untagged)] -pub(crate) enum RenderElement {} +pub(crate) enum RenderElement { + Paragraph(RenderParagraph), +} impl RenderElement { pub(crate) fn new, F: AsRef>( @@ -17,6 +21,13 @@ impl RenderElement { output_file: F, element: &IElement, ) -> Result { - todo!() + match element { + IElement::Paragraph(inner) => Ok(RenderElement::Paragraph(RenderParagraph::new( + config, + output_directory, + output_file, + inner, + )?)), + } } } diff --git a/src/context/mod.rs b/src/context/mod.rs index 3aadf32..d95cbee 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -4,6 +4,7 @@ mod element; mod global_settings; mod heading; mod object; +mod paragraph; mod plain_text; mod section; mod target; diff --git a/src/context/paragraph.rs b/src/context/paragraph.rs new file mode 100644 index 0000000..b2760f0 --- /dev/null +++ b/src/context/paragraph.rs @@ -0,0 +1,32 @@ +use std::path::Path; + +use serde::Serialize; + +use crate::config::Config; +use crate::error::CustomError; +use crate::intermediate::IParagraph; + +use super::RenderObject; + +#[derive(Debug, Serialize)] +#[serde(tag = "type")] +#[serde(rename = "heading")] +pub(crate) struct RenderParagraph { + children: Vec, +} + +impl RenderParagraph { + pub(crate) fn new, F: AsRef>( + config: &Config, + output_directory: D, + output_file: F, + paragraph: &IParagraph, + ) -> Result { + let children = paragraph + .children + .iter() + .map(|obj| RenderObject::new(config, &output_directory, &output_file, obj)) + .collect::, _>>()?; + Ok(RenderParagraph { children }) + } +} diff --git a/src/intermediate/element.rs b/src/intermediate/element.rs index 36bc9b6..1b1370c 100644 --- a/src/intermediate/element.rs +++ b/src/intermediate/element.rs @@ -1,9 +1,12 @@ use crate::error::CustomError; use super::registry::Registry; +use super::IParagraph; #[derive(Debug)] -pub(crate) enum IElement {} +pub(crate) enum IElement { + Paragraph(IParagraph), +} impl IElement { pub(crate) fn new<'parse>( @@ -11,7 +14,9 @@ impl IElement { elem: &organic::types::Element<'parse>, ) -> Result { match elem { - organic::types::Element::Paragraph(_) => todo!(), + organic::types::Element::Paragraph(inner) => { + Ok(IElement::Paragraph(IParagraph::new(registry, inner)?)) + } organic::types::Element::PlainList(_) => todo!(), organic::types::Element::CenterBlock(_) => todo!(), organic::types::Element::QuoteBlock(_) => todo!(), diff --git a/src/intermediate/mod.rs b/src/intermediate/mod.rs index 95185d7..eedc589 100644 --- a/src/intermediate/mod.rs +++ b/src/intermediate/mod.rs @@ -5,6 +5,7 @@ mod element; mod heading; mod object; mod page; +mod paragraph; mod plain_text; mod registry; mod section; @@ -17,6 +18,7 @@ pub(crate) use element::IElement; pub(crate) use heading::IHeading; pub(crate) use object::IObject; pub(crate) use page::BlogPostPage; +pub(crate) use paragraph::IParagraph; pub(crate) use plain_text::IPlainText; pub(crate) use section::ISection; pub(crate) use target::ITarget; diff --git a/src/intermediate/object.rs b/src/intermediate/object.rs index c40059b..5b74111 100644 --- a/src/intermediate/object.rs +++ b/src/intermediate/object.rs @@ -22,8 +22,8 @@ impl IObject { organic::types::Object::StrikeThrough(_) => todo!(), organic::types::Object::Code(_) => todo!(), organic::types::Object::Verbatim(_) => todo!(), - organic::types::Object::PlainText(plain_text) => { - Ok(IObject::PlainText(IPlainText::new(registry, plain_text)?)) + organic::types::Object::PlainText(inner) => { + Ok(IObject::PlainText(IPlainText::new(registry, inner)?)) } organic::types::Object::RegularLink(_) => todo!(), organic::types::Object::RadioLink(_) => todo!(), @@ -40,8 +40,8 @@ impl IObject { organic::types::Object::InlineBabelCall(_) => todo!(), organic::types::Object::InlineSourceBlock(_) => todo!(), organic::types::Object::LineBreak(_) => todo!(), - organic::types::Object::Target(target) => { - Ok(IObject::Target(ITarget::new(registry, target)?)) + organic::types::Object::Target(inner) => { + Ok(IObject::Target(ITarget::new(registry, inner)?)) } organic::types::Object::StatisticsCookie(_) => todo!(), organic::types::Object::Subscript(_) => todo!(), diff --git a/src/intermediate/paragraph.rs b/src/intermediate/paragraph.rs new file mode 100644 index 0000000..e40a47f --- /dev/null +++ b/src/intermediate/paragraph.rs @@ -0,0 +1,24 @@ +use crate::error::CustomError; + +use super::registry::Registry; +use super::IObject; + +#[derive(Debug)] +pub(crate) struct IParagraph { + pub(crate) children: Vec, +} + +impl IParagraph { + pub(crate) fn new<'parse>( + registry: &mut Registry<'parse>, + paragraph: &organic::types::Paragraph<'parse>, + ) -> Result { + let children = paragraph + .children + .iter() + .map(|obj| IObject::new(registry, obj)) + .collect::, _>>()?; + + Ok(IParagraph { children }) + } +}