Skip to content

Commit

Permalink
refactor: roll own Attributes struct
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcur committed May 18, 2024
1 parent 2bee9ec commit c37ceaf
Show file tree
Hide file tree
Showing 2 changed files with 313 additions and 154 deletions.
89 changes: 50 additions & 39 deletions src/djot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use jotdown::{Alignment, Attributes, Container, Event, ListKind, OrderedListNumb
use std::borrow::Cow;

use crate::ir_markup::{
Alignment as IrAlignment, Container as IrContainer, ContainerEnd as IrContainerEnd, Event as IrEvent,
ListKind as IrListKind, MathKind as IrMathKind, OrderedListNumbering as IrOrderedListNumbering,
Alignment as IrAlignment, Attributes as IrAttributes, Container as IrContainer, ContainerEnd as IrContainerEnd,
Event as IrEvent, ListKind as IrListKind, MathKind as IrMathKind, OrderedListNumbering as IrOrderedListNumbering,
};

/// Iterates from an Event::Start to a matching Event::End. The resulting iterator yields all
Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Blockquote, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Blockquote,
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -121,7 +121,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::DescriptionList, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::DescriptionList,
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -134,7 +134,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::DescriptionTerm, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::DescriptionTerm,
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -147,7 +147,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::DescriptionDetails, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::DescriptionDetails,
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -168,7 +168,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
) => {
co.yield_(IrEvent::Start {
container: IrContainer::Heading { level, id },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -178,14 +178,17 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
id: _,
}) => {
co.yield_(IrEvent::End {
container: IrContainerEnd::Heading { level },
container: IrContainerEnd::Heading {
// TODO: remove unwrap
level: level.try_into().unwrap(),
},
})
.await
}
Event::Start(Container::Section { id }, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Section { id },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -198,7 +201,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Div { class: _ }, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Div,
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -211,7 +214,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Paragraph, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Paragraph,
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -226,15 +229,15 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
co.yield_(IrEvent::Image {
destination,
alt,
attributes,
attributes: attributes.into(),
})
.await
}
Event::End(Container::Image(_, _)) => unreachable!(),
Event::Start(Container::Link(destination, _link_type), attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Link { destination },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -250,7 +253,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
co.yield_(IrEvent::CodeBlock {
language: language.into(),
code,
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -264,7 +267,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
IrMathKind::Inline
},
math,
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -273,14 +276,22 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::RawInline { format }, attributes) => {
let content = render_to_raw_string(iter_container_from_inside(&mut djot));
if matches!(format, "html" | "HTML") {
co.yield_(IrEvent::HtmlInline { content, attributes }).await
co.yield_(IrEvent::HtmlInline {
content,
attributes: attributes.into(),
})
.await
}
}
Event::End(Container::RawInline { .. }) => unreachable!(),
Event::Start(Container::RawBlock { format }, attributes) => {
let content = render_to_raw_string(iter_container_from_inside(&mut djot));
if matches!(format, "html" | "HTML") {
co.yield_(IrEvent::HtmlBlock { content, attributes }).await
co.yield_(IrEvent::HtmlBlock {
content,
attributes: attributes.into(),
})
.await
}
}
Event::End(Container::RawBlock { .. }) => unreachable!(),
Expand All @@ -291,7 +302,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
kind: kind.into(),
tight,
},
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -304,7 +315,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::ListItem, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::ListItem,
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -317,7 +328,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::TaskListItem { checked }, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::TaskListItem { checked },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -331,12 +342,12 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Table, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Table,
attributes,
attributes: attributes.into(),
})
.await;
co.yield_(IrEvent::Start {
container: IrContainer::TableBody,
attributes: jotdown::Attributes::new(),
attributes: IrAttributes::new(),
})
.await;
}
Expand All @@ -353,7 +364,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::TableRow { head: _ }, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::TableRow,
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -369,7 +380,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
alignment: alignment.into(),
head,
},
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -383,7 +394,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Footnote { label }, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Footnote { label: label.into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -400,7 +411,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Caption, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Other { tag: "caption".into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -413,7 +424,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Verbatim, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Other { tag: "code".into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -426,7 +437,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Span, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Other { tag: "span".into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -439,7 +450,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Subscript, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Other { tag: "sub".into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -452,7 +463,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Superscript, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Other { tag: "sup".into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -465,7 +476,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Insert, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Other { tag: "ins".into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -478,7 +489,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Delete, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Other { tag: "del".into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -491,7 +502,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Strong, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Other { tag: "strong".into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -504,7 +515,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Emphasis, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Other { tag: "em".into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -517,7 +528,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Start(Container::Mark, attributes) => {
co.yield_(IrEvent::Start {
container: IrContainer::Other { tag: "mark".into() },
attributes,
attributes: attributes.into(),
})
.await
}
Expand All @@ -533,15 +544,15 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::Softbreak => co.yield_(IrEvent::Str("\n".into())).await,
Event::Hardbreak => {
co.yield_(IrEvent::HtmlInline {
content: "<br />".into(),
attributes: jotdown::Attributes::new(),
content: "<br>".into(),
attributes: IrAttributes::new(),
})
.await
}
Event::NonBreakingSpace => {
co.yield_(IrEvent::HtmlInline {
content: "&nbsp;".into(),
attributes: jotdown::Attributes::new(),
attributes: IrAttributes::new(),
})
.await
}
Expand All @@ -557,7 +568,7 @@ pub fn djot_to_ir<'s>(mut djot: impl Iterator<Item = Event<'s>>) -> impl Iterato
Event::ThematicBreak(attributes) => {
co.yield_(IrEvent::TagWithAttribute {
tag: "hr".into(),
attributes,
attributes: attributes.into(),
})
.await
}
Expand Down
Loading

0 comments on commit c37ceaf

Please sign in to comment.