From 80244990691ad1ee7e5face5baf9f5f6ee6a8c87 Mon Sep 17 00:00:00 2001 From: Noah Hellman Date: Sun, 15 Jan 2023 20:03:25 +0100 Subject: [PATCH] html: output attributes --- src/html.rs | 136 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 90 insertions(+), 46 deletions(-) diff --git a/src/html.rs b/src/html.rs index 1faca82..c3d1e04 100644 --- a/src/html.rs +++ b/src/html.rs @@ -68,69 +68,108 @@ impl<'s, I: Iterator>, W: std::fmt::Write> Writer { fn write(&mut self) -> std::fmt::Result { for e in &mut self.events { match e { - Event::Start(c, _attrs) => { + Event::Start(c, attrs) => { if c.is_block() { self.out.write_char('\n')?; } if self.text_only && !matches!(c, Container::Image(..)) { continue; } - match c { - Container::Blockquote => self.out.write_str("
")?, + match &c { + Container::Blockquote => self.out.write_str(" todo!(), - Container::ListItem => self.out.write_str("
  • ")?, - Container::DescriptionList => self.out.write_str("
    ")?, - Container::DescriptionDetails => self.out.write_str("
    ")?, + Container::ListItem => self.out.write_str(" self.out.write_str(" self.out.write_str(" todo!(), - Container::Table => self.out.write_str("")?, - Container::TableRow => self.out.write_str("")?, - Container::Div { class } => { - if let Some(c) = class { - write!(self.out, r#"
    "#, c)?; - } else { - self.out.write_str("
    ")?; - } - } - Container::Paragraph => self.out.write_str("

    ")?, - Container::Heading { level } => write!(self.out, "", level)?, - Container::TableCell => self.out.write_str("

    ")?, Container::DescriptionTerm => self.out.write_str("")?, Container::CodeBlock { .. } => self.out.write_str("")?, - Container::Span | Container::Math { .. } => { - self.out.write_str("")?; - } + Container::Span => self.out.write_str("")?, Container::Link(..) => self.out.write_str("")?, Container::Image(src, ..) => { self.text_only = false; @@ -167,6 +204,13 @@ impl<'s, I: Iterator>, W: std::fmt::Write> Writer { } } Container::Verbatim => self.out.write_str("")?, + Container::Math { display } => { + self.out.write_str(if display { + r#"\]"# + } else { + r#"\)"# + })?; + } Container::RawBlock { .. } | Container::RawInline { .. } => { self.raw = Raw::None; }
    ")?, - Container::DescriptionTerm => self.out.write_str("
    ")?, - Container::CodeBlock { lang } => { - if let Some(l) = lang { - write!(self.out, r#"
    "#, l)?;
    -                            } else {
    -                                self.out.write_str("
    ")?;
    -                            }
    -                        }
    -                        Container::Span => self.out.write_str("")?,
    -                        Container::Link(dst, ..) => write!(self.out, r#""#, dst)?,
    +                        Container::Table => self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" write!(self.out, " self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" write!(self.out, r#" {
                                 self.text_only = true;
                                 self.out.write_str(" self.out.write_str("")?,
    -                        Container::Math { display } => self.out.write_str(if display {
    -                            r#"\["#
    -                        } else {
    -                            r#"\("#
    -                        })?,
    +                        Container::Verbatim => self.out.write_str(" {
    -                            self.raw = if format == "html" {
    +                            self.raw = if format == &"html" {
                                     Raw::Html
                                 } else {
                                     Raw::Other
    -                            }
    +                            };
    +                            continue;
                             }
    -                        Container::Subscript => self.out.write_str("")?,
    -                        Container::Superscript => self.out.write_str("")?,
    -                        Container::Insert => self.out.write_str("")?,
    -                        Container::Delete => self.out.write_str("")?,
    -                        Container::Strong => self.out.write_str("")?,
    -                        Container::Emphasis => self.out.write_str("")?,
    -                        Container::Mark => self.out.write_str("")?,
    +                        Container::Subscript => self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str(" self.out.write_str("‘")?,
                             Container::DoubleQuoted => self.out.write_str("“")?,
                         }
    +
    +                    if attrs.iter().any(|(a, _)| a == "class")
    +                        || matches!(
    +                            c,
    +                            Container::Div { class: Some(_) } | Container::Math { .. }
    +                        )
    +                    {
    +                        self.out.write_str(r#" class=""#)?;
    +                        let mut classes = attrs
    +                            .iter()
    +                            .filter(|(a, _)| a == &"class")
    +                            .map(|(_, cls)| cls);
    +                        let has_attr = if let Container::Math { display } = c {
    +                            self.out.write_str(if display {
    +                                "math display"
    +                            } else {
    +                                "math inline"
    +                            })?;
    +                            true
    +                        } else if let Some(cls) = classes.next() {
    +                            self.out.write_str(cls)?;
    +                            for cls in classes {
    +                                self.out.write_char(' ')?;
    +                                self.out.write_str(cls)?;
    +                            }
    +                            true
    +                        } else {
    +                            false
    +                        };
    +                        if let Container::Div { class: Some(cls) } = c {
    +                            if has_attr {
    +                                self.out.write_char(' ')?;
    +                            }
    +                            self.out.write_str(cls)?;
    +                        }
    +                        self.out.write_char('"')?;
    +                    }
    +
    +                    match c {
    +                        Container::CodeBlock { lang } => {
    +                            if let Some(l) = lang {
    +                                write!(self.out, r#">"#, l)?;
    +                            } else {
    +                                self.out.write_str(">")?;
    +                            }
    +                        }
    +                        Container::Image(..) => {
    +                            self.out.write_str(r#" alt=""#)?;
    +                        }
    +                        Container::Math { display } => {
    +                            self.out
    +                                .write_str(if display { r#">\["# } else { r#">\("# })?;
    +                        }
    +                        _ => self.out.write_char('>')?,
    +                    }
                     }
                     Event::End(c) => {
                         if c.is_block_container() && !matches!(c, Container::Footnote { .. }) {
    @@ -154,9 +193,7 @@ impl<'s, I: Iterator>, W: std::fmt::Write> Writer {
                             Container::TableCell => self.out.write_str("