Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add missing documentation #4

Merged
merged 35 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
313433d
docs: add documentation for `MathMl` and its attributes
nfejzic Dec 6, 2023
93d3c06
docs: add documentation for global attributes
nfejzic Dec 6, 2023
8da3e2d
docs: document `Renderer` and `Writer` traits
nfejzic Dec 6, 2023
f42692e
fix: improve API of `Writer` trait
nfejzic Dec 6, 2023
07757ef
docs: document marker types
nfejzic Dec 6, 2023
1ba99b8
docs: document the `src/lib.rs`
nfejzic Dec 6, 2023
0e23bad
docs: document the default renderer
nfejzic Dec 6, 2023
427847e
docs: document `src/elements.rs`
nfejzic Dec 6, 2023
a2cecba
fix: expose attributes and buf writer to public API
nfejzic Dec 6, 2023
462426a
docs: document global attributes
nfejzic Dec 6, 2023
a6f53b4
docs: document buf_writer
nfejzic Dec 6, 2023
8439332
fix: remove dead code
nfejzic Dec 6, 2023
40729d2
fix: warn about missing docs
nfejzic Dec 6, 2023
0703fd4
docs: document the `mi` module and element
nfejzic Dec 6, 2023
6f102f6
docs: document the `mo` element
nfejzic Dec 6, 2023
ca7c9dd
docs: document the annotation elements
nfejzic Dec 6, 2023
c3f8b22
docs: document the `maction` element
nfejzic Dec 6, 2023
93f7aef
fix: make `DisplayAttr` globally available
nfejzic Dec 6, 2023
d26ed0c
fix: remove the `math` element
nfejzic Dec 6, 2023
f83ba3d
docs: document the `merror` element
nfejzic Dec 6, 2023
39eac9f
docs: document the `mfrac` module
nfejzic Dec 6, 2023
9aef959
docs: document the `mmultiscripts` module
nfejzic Dec 6, 2023
74581e6
docs: document the `mn` module
nfejzic Dec 6, 2023
8fa5b8e
docs: document the `mpadded` module
nfejzic Dec 6, 2023
e8c7598
docs: document the `mphantom` module
nfejzic Dec 6, 2023
bf5abd0
docs: document the `mroot` module
nfejzic Dec 6, 2023
d38417d
docs: document the `mrow` module
nfejzic Dec 6, 2023
820dd75
docs: document the `ms` module
nfejzic Dec 6, 2023
db3f387
docs: document the `mspace` module
nfejzic Dec 6, 2023
6b5e4f5
docs: document the `mstyle` module
nfejzic Dec 6, 2023
9adc26b
docs: document the `msubsup` module
nfejzic Dec 6, 2023
26bf8ae
docs: document the `mtable` module
nfejzic Dec 6, 2023
76fb3b8
docs: document the `mtext` module
nfejzic Dec 6, 2023
a6d6f4e
docs: document the `munderover` module
nfejzic Dec 6, 2023
9848417
docs: add missing reference to `Infallible`
nfejzic Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,65 @@
//! Global attributes of MathML elements.

use std::fmt::Display;

/// Direction for [`Attribute::Dir`].
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Dir {
/// `rtl` direction.
RightToLeft,
/// `ltr` direction.
LeftToRight,
}

/// ScriptLevel for [`Attribute::ScriptLevel`].
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ScriptLevel {
/// A positive number.
Add(usize),
/// A negative number.
Sub(usize),
/// A (positive) number.
Num(usize),
}

/// MathVariant for [`Attribute::MathVariant`].
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum MathVariant {
/// Normal variant.
Normal,
/// Bold variant.
Bold,
/// Italic variant.
Italic,
/// Bold italic variant.
BoldItalic,
/// Double struck variant.
DoubleStruck,
/// Bold fraktur variant.
BoldFraktur,
/// Script variant.
Script,
/// Bold script variant.
BoldScript,
/// Fraktur variant.
Fraktur,
/// Sans serif variant.
SansSerif,
/// Bold sans serif variant.
BoldSansSerif,
/// Sans serif italic variant.
SansSerifItalic,
/// Sans serif bold italic variant.
SansSerifBoldItalic,
/// Monospace variant.
Monospace,
/// Initial variant.
Initial,
/// Tailed variant.
Tailed,
/// Looped variant.
Looped,
/// Stretched variant.
Stretched,
}

Expand Down Expand Up @@ -121,6 +147,22 @@ pub enum Attribute {
/// The `tabindex` attribute, same as in HTML.
TabIndex(i16),

/// Event handler function, e.g. `onclick="..."`.
///
/// # Example
/// ```rust
/// use alemat::{Attribute, BufMathMlWriter, Writer};
///
/// let handler = Attribute::OnHandler {
/// name: "click".to_string(),
/// handler: "console.log('Clicked!')".to_string(),
/// };
///
/// let mut writer = BufMathMlWriter::default();
/// writer.write_attr(&handler);
/// let output = writer.finish();
///
/// assert_eq!(output, "onclick=\"console.log('Clicked!')\"");
OnHandler {
/// Name of the event.
name: String,
Expand Down
15 changes: 13 additions & 2 deletions src/buf_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use crate::{
AnnotationAttr, AnnotationContent, FracAttr, Num, OperatorAttr, PaddedAttr, SpaceAttr,
TableAttr, TableCellAttr,
},
Element, MathMlAttr, Renderer, Writer,
DisplayAttr, Element, MathMlAttr, Renderer, Writer,
};

/// Default implementation of MathMl [`Writer`].
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct BufMathMlWriter {
buf: String,
Expand Down Expand Up @@ -580,7 +581,13 @@ impl Writer for BufMathMlWriter {
self.write_str(" ")?;

match attr {
MathMlAttr::Display(d) => write!(self, r#"display="{d}""#)?,
MathMlAttr::Display(d) => {
write!(self, r#"display=""#)?;
match d {
DisplayAttr::Block => write!(self, r#"block""#)?,
DisplayAttr::Inline => write!(self, r#"inline""#)?,
}
}
MathMlAttr::AltText(alt_t) => write!(self, r#"alttext="{alt_t}""#)?,
MathMlAttr::Global(a) => self.write_attr(a)?,
}
Expand All @@ -600,6 +607,10 @@ impl Writer for BufMathMlWriter {
fn into_inner(self) -> Self::Buffer {
self.buf
}

fn finish(&mut self) -> Self::Buffer {
std::mem::take(&mut self.buf)
}
}

impl Renderer for BufMathMlWriter {
Expand Down
8 changes: 6 additions & 2 deletions src/default_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use crate::{
AnnotationAttr, AnnotationContent, FracAttr, Num, OperatorAttr, PaddedAttr, SpaceAttr,
TableAttr, TableCellAttr,
},
Element, MathMlAttr, Renderer,
DisplayAttr, Element, MathMlAttr, Renderer,
};

/// Default MathMl [`Renderer`] implementation.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct MathMlFormatter;

Expand Down Expand Up @@ -521,7 +522,10 @@ impl Renderer for MathMlFormatter {
.attr
.iter()
.map(|a| match a {
MathMlAttr::Display(d) => Ok(format!(r#"display="{d}""#)),
MathMlAttr::Display(d) => match d {
DisplayAttr::Block => Ok(String::from(r#"display="block""#)),
DisplayAttr::Inline => Ok(String::from(r#"display="inline""#)),
},
MathMlAttr::AltText(alt_t) => Ok(format!(r#"alttext="{alt_t}""#)),
MathMlAttr::Global(a) => self.render_attr(a),
})
Expand Down
53 changes: 51 additions & 2 deletions src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
//! elements.

mod maction;
mod math;
mod merror;
mod mmultiscripts;
mod mphantom;
mod mrow;
mod mstyle;

/// The grouping elements are `maction`, `math`, `merror`, `mphantom`, `mprescripts`, `mrow`,
/// `mstyle`, `semantics` and unknown MathML elements.
pub mod grouping {
pub use super::maction::*;
pub use super::math::*;
pub use super::merror::*;
pub use super::mmultiscripts::Prescripts;
pub use super::mphantom::*;
Expand All @@ -21,13 +21,17 @@ pub mod grouping {

mod mroot;

/// The radical elements are `mroot` and `msqrt`. In this implementation both are constructed using
/// the [`Radical`] struct.
pub mod radicals {
pub use super::mroot::*;
}

mod msubsup;
mod munderover;

/// The scripted elements are `mmultiscripts`, `mover`, `msub`, `msubsup`, `msup`, `munder` and
/// `munderover`.
pub mod scripted {
pub use super::mmultiscripts::Multiscripts;
pub use super::msubsup::*;
Expand Down Expand Up @@ -66,31 +70,74 @@ use self::{
scripted::UnderOver,
};

/// The MathML elements.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Element {
/// `maction` element.
Action(Action),

/// `annotation` and `annotation-xml` elements.
Annotation(Annotation),

/// `merror` element.
Error(Error),

/// `mfrac` element.
Frac(Frac),

/// `mi` element.
Ident(Ident),

/// `mmultiscripts` element.
Multiscripts(Multiscripts),

/// `mprescripts` element.
Prescripts(Prescripts),

/// `mn` element.
Num(Num),

/// `mo` element.
Operator(Operator),

/// `mpadded` element.
Padded(Padded),

/// `mphantom` element.
Phantom(Phantom),

/// `mroot` and `msqrt` elements.
Radical(Radical),

/// `mrow` element.
Row(Row),

/// `msemantics` element.
Semantics(Semantics),

/// `mspace` element.
Space(Space),

/// `ms` element.
StrLiteral(StrLiteral),

/// `mstyle`
Style(Style),

/// `msub`, `msup` and `msubsup` elements.
SubSup(SubSup),

/// `mtable` element.
Table(Table),

/// `mtext` element.
Text(Text),

/// `munder`, `mover` and `munderover` elements.
UnderOver(UnderOver),
}

/// A list of [`Element`]s.
#[repr(transparent)]
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Elements(pub(crate) Vec<Element>);
Expand All @@ -109,6 +156,7 @@ impl DerefMut for Elements {
}

impl Elements {
/// Consumes the [`Elements`] and returns a [`Vec`] of [`Element`]s.
pub fn into_inner(self) -> Vec<Element> {
self.0
}
Expand All @@ -130,6 +178,7 @@ macro_rules! children {
}
}

/// Trait for conversion into [`Elements`].
pub trait IntoElements {
/// Converts the type into elements.
fn into_elements(self) -> Elements;
Expand Down
17 changes: 17 additions & 0 deletions src/elements/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use super::IntoElements;
/// The content of `annotation` element, either text or MathML.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum AnnotationContent {
/// Text content of the `annotation` element. Implies the `annotation` variant.
Text(String),
/// MathML content of the `annotation` element. Implies the `annotation-xml` variant.
Nested(Elements),
}

Expand Down Expand Up @@ -42,6 +44,7 @@ impl From<Element> for AnnotationContent {
/// [`Attribute`]: crate::attributes::Attribute
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum AnnotationAttr {
/// One of the global [`Attribute`]s.
Global(Attribute),

/// NOTE: Authors can use the encoding attribute to distinguish annotations for HTML
Expand Down Expand Up @@ -78,19 +81,23 @@ pub struct Annotation {
crate::element_from_type!(Annotation => Annotation);

impl Annotation {
/// Create a builder for [`Annotation`] element.
pub fn builder() -> AnnotationBuilder<Uninit> {
AnnotationBuilder::default()
}

/// Get a reference to the inner content of the [`Annotation`] element.
pub fn content(&self) -> &AnnotationContent {
&self.content
}

/// Get a reference to all attributes of the [`Annotation`] element.
pub fn attributes(&self) -> &[AnnotationAttr] {
&self.attributes
}
}

/// Builder of the [`Annotation`] element.
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct AnnotationBuilder<T> {
content: Option<AnnotationContent>,
Expand All @@ -100,6 +107,7 @@ pub struct AnnotationBuilder<T> {
}

impl<T> AnnotationBuilder<T> {
/// Set the content of the [`Annotation`] element.
pub fn content(self, content: impl Into<AnnotationContent>) -> AnnotationBuilder<Init> {
AnnotationBuilder {
content: Some(content.into()),
Expand All @@ -108,6 +116,7 @@ impl<T> AnnotationBuilder<T> {
}
}

/// Add attributes.
pub fn attr<I, A>(mut self, attr: I) -> Self
where
I: IntoIterator<Item = A>,
Expand All @@ -119,6 +128,7 @@ impl<T> AnnotationBuilder<T> {
}

impl AnnotationBuilder<Init> {
/// Build the [`Annotation`] element.
pub fn build(self) -> Annotation {
Annotation {
content: self
Expand All @@ -143,21 +153,25 @@ pub struct Semantics {
}

impl Semantics {
/// Create a builder for [`Semantics`] element.
pub fn builder() -> SemanticsBuilder<Uninit> {
SemanticsBuilder::default()
}

/// Get a reference to the inner content of the [`Semantics`] element.
pub fn children(&self) -> &[Element] {
&self.children
}

/// Get a reference to all attributes of the [`Semantics`] element.
pub fn attributes(&self) -> &[Attribute] {
&self.attr
}
}

crate::element_from_type!(Semantics => Semantics);

/// Builder of the [`Semantics`] element.
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct SemanticsBuilder<T> {
content: Option<Elements>,
Expand All @@ -167,6 +181,7 @@ pub struct SemanticsBuilder<T> {
}

impl<T> SemanticsBuilder<T> {
/// Set the content of the [`Semantics`] element.
pub fn content(self, content: impl IntoElements) -> SemanticsBuilder<Init> {
SemanticsBuilder {
content: Some(content.into_elements()),
Expand All @@ -175,6 +190,7 @@ impl<T> SemanticsBuilder<T> {
}
}

/// Add attributes.
pub fn attr<A>(mut self, attr: A) -> Self
where
A: IntoIterator<Item = Attribute>,
Expand All @@ -185,6 +201,7 @@ impl<T> SemanticsBuilder<T> {
}

impl SemanticsBuilder<Init> {
/// Build the [`Semantics`] element.
pub fn build(self) -> Semantics {
Semantics {
children: self
Expand Down
Loading
Loading