From b336066d6067a0eb9ff9fc34c5aa062b86e56c62 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 8 Mar 2023 21:16:22 +0100 Subject: feat: add support for emphasis roles --- src/description.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/description.rs b/src/description.rs index 4f3b08a..d55737d 100644 --- a/src/description.rs +++ b/src/description.rs @@ -262,7 +262,7 @@ pub enum ParagraphPart Parameter(String), /// Emphasis part. - Emphasis(String), + Emphasis(Emphasis), /// Code part. Code(String), @@ -329,14 +329,13 @@ impl ParagraphPart "constant" => Self::Constant, "function" => Self::Function, "parameter" => Self::Parameter, - "emphasis" => Self::Emphasis, "code" => Self::Code, "inlineequation" => Self::InlineEquation, "programlisting" => Self::ProgramListing, "citerefentry" => Self::Entry, "superscript" => Self::Superscript, "variablelist" | "itemizedlist" | "informaltable" | "para" | "footnote" - | "table" | "informalequation" => |_| { + | "table" | "informalequation" | "emphasis" => |_| { unreachable!(); }, _ => { @@ -422,6 +421,12 @@ impl ParagraphPart )); } + if tagged_element.name() == "emphasis" { + return Ok(Self::Emphasis(Emphasis::from_tagged_element( + tagged_element, + )?)); + } + let text_element = tagged_element .child_elements() .get_first_text_element() @@ -474,4 +479,72 @@ pub enum ParagraphPartError /// Invalid table. #[error("Invalid table")] InvalidTable(#[source] Box), + + /// Invalid emphasis. + #[error("Invalid emphasis")] + InvalidEmphasis(#[from] EmphasisError), +} + +/// Emphasis. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Emphasis +{ + /// The emphasised text. + pub text: String, + + /// Emphasis role. + pub role: EmphasisRole, +} + +impl Emphasis +{ + fn from_tagged_element(tagged_element: &Tagged) -> Result + { + return Ok(Emphasis { + text: tagged_element + .child_elements() + .get_first_text_element() + .cloned() + .unwrap_or_default(), + role: tagged_element + .attributes() + .iter() + .find(|attr| attr.key == "role") + .map(|attr| { + let value = String::from_utf8(attr.value.clone()) + .map_err(|_| EmphasisError::EmphasisRoleNotUTF8)?; + + if value == "bold" { + return Ok(EmphasisRole::Bold); + } + + Err(EmphasisError::UnknownEmphasisRole(value)) + }) + .unwrap_or(Ok(EmphasisRole::None))?, + }); + } +} + +/// [`Emphasis`] error. +#[derive(Debug, thiserror::Error)] +pub enum EmphasisError +{ + /// Emphasis role is not valid UTF-8. + #[error("Emphasis role is not valid UTF-8")] + EmphasisRoleNotUTF8, + + /// Unknown emphasis role. + #[error("Unknown emphasis role '{0}'")] + UnknownEmphasisRole(String), +} + +/// Emphasis role. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum EmphasisRole +{ + /// Bold. + Bold, + + /// None. + None, } -- cgit v1.2.3-18-g5258