From fbaf9c0e7357e9701a072963c59359e35a021a57 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 26 Feb 2023 20:55:46 +0100 Subject: feat: add inline equation support --- src/description.rs | 14 ++++++++++++++ src/xml/element.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) (limited to 'src') diff --git a/src/description.rs b/src/description.rs index 31eff1e..e436e40 100644 --- a/src/description.rs +++ b/src/description.rs @@ -201,6 +201,9 @@ pub enum ParagraphPart /// Code part. Code(String), + /// Inline equation part. + InlineEquation(String), + /// Reference entry citation part. Entry(String), @@ -241,6 +244,7 @@ impl ParagraphPart "parameter" => Self::Parameter, "emphasis" => Self::Emphasis, "code" => Self::Code, + "inlineequation" => Self::InlineEquation, "citerefentry" => Self::Entry, "variablelist" | "itemizedlist" => |_| { unreachable!(); @@ -280,6 +284,16 @@ impl ParagraphPart return Ok(Self::ItemizedList(itemized_list)); } + if tagged_element.name() == "inlineequation" { + return Ok(Self::InlineEquation( + tagged_element + .child_elements() + .into_iter() + .map(ToString::to_string) + .collect::(), + )); + } + let text_element = tagged_element .child_elements() .get_first_text_element() diff --git a/src/xml/element.rs b/src/xml/element.rs index 91e4130..0f44182 100644 --- a/src/xml/element.rs +++ b/src/xml/element.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + #[derive(Debug, PartialEq, Eq, Clone)] pub struct Elements { @@ -144,6 +146,20 @@ pub enum Element Comment(String), } +impl Display for Element +{ + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + match self { + Self::Tagged(tagged) => tagged.fmt(formatter), + Self::Text(text) => text.fmt(formatter), + Self::Comment(comment) => { + formatter.write_fmt(format_args!("")) + } + } + } +} + #[derive(Debug, PartialEq, Eq, Clone)] pub struct Tagged { @@ -152,6 +168,30 @@ pub struct Tagged attributes: Vec, } +impl Display for Tagged +{ + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + let name = &self.name; + + let attrs = self + .attributes + .iter() + .map(|attr| { + format!("{}=\"{}\" ", attr.key, String::from_utf8_lossy(&attr.value)) + }) + .collect::(); + + let child_elements = self + .child_elements + .into_iter() + .map(ToString::to_string) + .collect::(); + + formatter.write_fmt(format_args!("<{name} {attrs}>{child_elements}",)) + } +} + impl Tagged { pub fn new( -- cgit v1.2.3-18-g5258