summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-03-08 21:16:22 +0100
committerHampusM <hampus@hampusmat.com>2023-03-08 21:16:22 +0100
commitb336066d6067a0eb9ff9fc34c5aa062b86e56c62 (patch)
tree01468c8e9fa952a21cc4ebf6648cf0fc2f8848b3 /src
parentc49f62a8c369e58ee5cbffe853a436e724789d2f (diff)
feat: add support for emphasis roles
Diffstat (limited to 'src')
-rw-r--r--src/description.rs79
1 files changed, 76 insertions, 3 deletions
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<TableError>),
+
+ /// 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<Self, EmphasisError>
+ {
+ 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,
}