refactor: move all state to struct, add footnotes handling

This commit is contained in:
DarkCat09 2024-11-21 22:43:08 +04:00
parent 959581d0b3
commit 852aabd1b2
Signed by: DarkCat09
GPG key ID: BD3CE9B65916CD82

View file

@ -12,13 +12,17 @@ pub fn compile_markdown(
) -> std::io::Result<()> {
let src_text = std::fs::read_to_string(src)?;
let mut html = create_file(html)?;
let mut gmi = create_file(gmi)?;
let mut p = ParserVars {
html: Box::new(create_file(html)?),
gmi: Box::new(create_file(gmi)?),
let mut state = State::Start;
let mut ol = false; // ordered list
let mut counter: u64 = 0;
let mut links: Vec<GmiLink<'_>> = vec![];
state: State::Start,
ol: false,
counter: 0,
links: vec![],
notes: vec![],
};
for event in Parser::new_ext(&src_text, Options::all()) {
use pulldown_cmark::Event::*;
@ -31,143 +35,145 @@ pub fn compile_markdown(
match tag {
Paragraph => {
html.write_all(b"<p>")?;
write_paragraph_start(&mut gmi, state)?;
p.html.write_all(b"<p>")?;
write_paragraph_start(&mut p)?;
}
Heading { level, id, .. } => {
if let Some(id) = id {
html.write_fmt(format_args!("<{} id=\"{}\">", level, id))?;
p.html
.write_fmt(format_args!("<{} id=\"{}\">", level, id))?;
} else {
html.write_fmt(format_args!("<{}>", level))?;
p.html.write_fmt(format_args!("<{}>", level))?;
}
write_paragraph_start(&mut gmi, state)?;
write_paragraph_start(&mut p)?;
let hashes = match level {
HeadingLevel::H1 => "# ",
HeadingLevel::H2 => "## ",
_ => "### ",
};
gmi.write_all(hashes.as_bytes())?;
state = State::Paragraph;
p.gmi.write_all(hashes.as_bytes())?;
p.state = State::Paragraph;
}
BlockQuote(_kind) => {
html.write_all(b"<blockquote>")?;
p.html.write_all(b"<blockquote>")?;
write_paragraph_start(&mut gmi, state)?;
gmi.write_all(b"> ")?;
state = State::Quote;
write_paragraph_start(&mut p)?;
p.gmi.write_all(b"> ")?;
p.state = State::Quote;
}
CodeBlock(CodeBlockKind::Fenced(lang)) => {
// TODO: highlighting with syntect
html.write_all(b"<pre><code>")?;
p.html.write_all(b"<pre><code>")?;
write_paragraph_start(&mut gmi, state)?;
gmi.write_fmt(format_args!("```{}\r\n", lang))?;
write_paragraph_start(&mut p)?;
p.gmi.write_fmt(format_args!("```{}\r\n", lang))?;
}
CodeBlock(CodeBlockKind::Indented) => {
html.write_all(b"<pre><code>")?;
p.html.write_all(b"<pre><code>")?;
write_paragraph_start(&mut gmi, state)?;
gmi.write_all(b"```")?;
write_paragraph_start(&mut p)?;
p.gmi.write_all(b"```")?;
}
List(None) => {
html.write_all(b"<ul>")?;
write_paragraph_start(&mut gmi, state)?;
state = State::Start;
p.html.write_all(b"<ul>")?;
write_paragraph_start(&mut p)?;
p.state = State::Start;
}
List(Some(counter_start)) => {
counter = counter_start;
ol = true;
p.counter = counter_start;
p.ol = true;
if counter_start == 1 {
html.write_all(b"<ol>")?;
p.html.write_all(b"<ol>")?;
} else {
html.write_fmt(format_args!("<ol start=\"{}\">", counter_start))?;
p.html
.write_fmt(format_args!("<ol start=\"{}\">", counter_start))?;
}
write_paragraph_start(&mut gmi, state)?;
state = State::Start;
write_paragraph_start(&mut p)?;
p.state = State::Start;
}
Item => {
html.write_all(b"<li>")?;
p.html.write_all(b"<li>")?;
if state != State::Start {
gmi.write_all(b"\r\n")?;
state = State::Paragraph;
if p.state != State::Start {
p.gmi.write_all(b"\r\n")?;
p.state = State::Paragraph;
}
if ol {
gmi.write_fmt(format_args!("{}. ", counter))?;
counter += 1;
if p.ol {
p.gmi.write_fmt(format_args!("{}. ", p.counter))?;
p.counter += 1;
} else {
gmi.write_all(b"* ")?;
p.gmi.write_all(b"* ")?;
}
}
Strong => {
html.write_all(b"<b>")?;
write_inline(&mut gmi, state, "**", &mut links)?;
p.html.write_all(b"<b>")?;
write_inline(&mut p, "**")?;
}
Emphasis => {
html.write_all(b"<i>")?;
write_inline(&mut gmi, state, "*", &mut links)?;
p.html.write_all(b"<i>")?;
write_inline(&mut p, "*")?;
}
Strikethrough => {
html.write_all(b"<s>")?;
write_inline(&mut gmi, state, "~", &mut links)?;
p.html.write_all(b"<s>")?;
write_inline(&mut p, "~")?;
}
Link { dest_url, .. } => {
html.write_fmt(format_args!("<a href=\"{}\">", dest_url))?;
links.push(GmiLink {
title: String::new(),
url: dest_url,
});
state = State::Link;
p.html
.write_fmt(format_args!("<a href=\"{}\">", dest_url))?;
p.links.push(GmiLink::from_url(dest_url));
p.state = State::Link;
}
Image { dest_url, .. } => {
links.push(GmiLink {
title: String::new(),
url: dest_url,
});
state = State::Link;
p.links.push(GmiLink::from_url(dest_url));
p.state = State::Link;
}
Table(_align) => {
html.write_all(b"<table>")?;
p.html.write_all(b"<table>")?;
write_paragraph_start(&mut gmi, state)?;
write_paragraph_start(&mut p)?;
// gmi ??
}
TableHead => {
html.write_all(b"<thead><tr>")?;
state = State::TableHead;
p.html.write_all(b"<thead><tr>")?;
p.state = State::TableHead;
}
TableRow => {
html.write_all(b"<tr>")?;
p.html.write_all(b"<tr>")?;
}
TableCell => {
if state == State::TableHead {
html.write_all(b"<th>")?;
if p.state == State::TableHead {
p.html.write_all(b"<th>")?;
} else {
html.write_all(b"<td>")?;
p.html.write_all(b"<td>")?;
}
}
FootnoteDefinition(id) => {
p.notes.push(Footnote::from_id(id));
p.state = State::Footnote;
}
MetadataBlock(_) => {
state = State::Metadata(state != State::Start);
p.state = State::Metadata(p.state != State::Start);
}
other => {
@ -181,67 +187,67 @@ pub fn compile_markdown(
match tag {
Paragraph => {
html.write_all(b"</p>")?;
p.html.write_all(b"</p>")?;
if !links.is_empty() {
gmi.write_all(b"\r\n")?;
for (i, link) in links.iter().enumerate() {
gmi.write_fmt(format_args!(
if !p.links.is_empty() {
p.gmi.write_all(b"\r\n")?;
for (i, link) in p.links.iter().enumerate() {
p.gmi.write_fmt(format_args!(
"\r\n=> {} [{}]: {}", // => https://... [1]: example
link.url, i, link.title,
))?;
}
links.clear();
p.links.clear();
}
}
Heading(level) => html.write_fmt(format_args!("<{}>", level))?,
BlockQuote(_) => html.write_all(b"</blockquote>")?,
Heading(level) => p.html.write_fmt(format_args!("<{}>", level))?,
BlockQuote(_) => p.html.write_all(b"</blockquote>")?,
CodeBlock => {
html.write_all(b"</code></pre>")?;
gmi.write_all(b"```")?;
p.html.write_all(b"</code></pre>")?;
p.gmi.write_all(b"```")?;
}
List(ordered) => {
if ordered {
html.write_all(b"</ol>")?;
ol = false;
p.html.write_all(b"</ol>")?;
p.ol = false;
} else {
html.write_all(b"</ul>")?;
p.html.write_all(b"</ul>")?;
}
}
Item => html.write_all(b"</li>")?,
Item => p.html.write_all(b"</li>")?,
Strong => {
html.write_all(b"</b>")?;
write_inline(&mut gmi, state, "**", &mut links)?;
p.html.write_all(b"</b>")?;
write_inline(&mut p, "**")?;
}
Emphasis => {
html.write_all(b"</i>")?;
write_inline(&mut gmi, state, "*", &mut links)?;
p.html.write_all(b"</i>")?;
write_inline(&mut p, "*")?;
}
Strikethrough => {
html.write_all(b"</s>")?;
write_inline(&mut gmi, state, "~", &mut links)?;
p.html.write_all(b"</s>")?;
write_inline(&mut p, "~")?;
}
Link | Image => {
gmi.write_fmt(format_args!(
p.gmi.write_fmt(format_args!(
"{}[{}]", // example[1] ...\r\n => https://... [1]: example
links.last().unwrap().title,
links.len(),
p.links.last().unwrap().title,
p.links.len(),
))?;
}
TableHead => html.write_all(b"</tr></thead><tbody>")?,
TableRow => html.write_all(b"</tr>")?,
TableHead => p.html.write_all(b"</tr></thead><tbody>")?,
TableRow => p.html.write_all(b"</tr>")?,
TableCell => {
if state == State::TableHead {
html.write_all(b"</th>")?
if p.state == State::TableHead {
p.html.write_all(b"</th>")?
} else {
html.write_all(b"</td>")?
p.html.write_all(b"</td>")?
}
}
Table => html.write_all(b"</tbody></table>")?,
Table => p.html.write_all(b"</tbody></table>")?,
_ => {}
}
state = if state == State::Metadata(false) {
p.state = if p.state == State::Metadata(false) {
State::Start
} else {
State::Paragraph
@ -249,7 +255,7 @@ pub fn compile_markdown(
}
Text(text) => {
match state {
match p.state {
State::Metadata(_) => {
// TODO: parse yaml
continue;
@ -257,48 +263,61 @@ pub fn compile_markdown(
_ => {}
}
html.write_all(text.as_bytes())?;
write_inline(&mut gmi, state, &text, &mut links)?;
p.html.write_all(text.as_bytes())?;
write_inline(&mut p, &text)?;
}
Code(code) => {
html.write_all(b"<code>")?;
html.write_all(code.as_bytes())?;
html.write_all(b"</code>")?;
p.html.write_all(b"<code>")?;
p.html.write_all(code.as_bytes())?;
p.html.write_all(b"</code>")?;
write_inline(&mut gmi, state, "`", &mut links)?;
write_inline(&mut gmi, state, &code, &mut links)?;
write_inline(&mut gmi, state, "`", &mut links)?;
write_inline(&mut p, "`")?;
write_inline(&mut p, &code)?;
write_inline(&mut p, "`")?;
}
SoftBreak => {
html.write_all(b" ")?;
write_inline(&mut gmi, state, " ", &mut links)?;
p.html.write_all(b" ")?;
write_inline(&mut p, " ")?;
}
HardBreak => {
html.write_all(b"<br>")?;
p.html.write_all(b"<br>")?;
gmi.write_all(b"\r\n")?;
if state == State::Quote {
gmi.write_all(b"> ")?;
write_inline(&mut p, "\r\n")?;
if p.state == State::Quote {
p.gmi.write_all(b"> ")?;
}
}
Rule => {
html.write_all(b"<hr>")?;
p.html.write_all(b"<hr>")?;
write_paragraph_start(&mut gmi, state)?;
gmi.write_all(b"---")?;
write_paragraph_start(&mut p)?;
p.gmi.write_all(b"---")?;
}
FootnoteReference(id) => {
p.html.write_fmt(format_args!(
"<sup><a href=\"#ref-{}\">[{}]</a></sup>",
id, id,
))?;
// [^1]
p.gmi.write_all(b"[^")?;
p.gmi.write_all(id.as_bytes())?;
p.gmi.write_all(b"]")?;
}
TaskListMarker(done) => {
if done {
html.write_all(b"<input type=checkbox checked disabled>")?;
gmi.write_all(b"[x] ")?;
p.html
.write_all(b"<input type=checkbox checked disabled>")?;
p.gmi.write_all(b"[x] ")?;
} else {
html.write_all(b"<input type=checkbox disabled>")?;
gmi.write_all(b"[ ] ")?;
p.html.write_all(b"<input type=checkbox disabled>")?;
p.gmi.write_all(b"[ ] ")?;
}
}
@ -308,8 +327,10 @@ pub fn compile_markdown(
}
}
html.flush()?;
gmi.flush()?;
// TODO: footnotes rendering
p.html.flush()?;
p.gmi.flush()?;
Ok(())
}
@ -324,33 +345,50 @@ fn create_file(path: impl AsRef<Path>) -> std::io::Result<BufWriter<std::fs::Fil
}
#[inline(always)]
fn write_paragraph_start(gmi: &mut impl std::io::Write, state: State) -> std::io::Result<()> {
if state != State::Start && state != State::Quote {
gmi.write_all(b"\r\n\r\n")?;
fn write_paragraph_start(p: &mut ParserVars) -> std::io::Result<()> {
match p.state {
State::Start | State::Quote => {}
_ => {
p.gmi.write_all(b"\r\n\r\n")?;
}
}
Ok(())
}
#[inline(always)]
fn write_inline(
gmi: &mut impl std::io::Write,
state: State,
text: &str,
links: &mut Vec<GmiLink>,
) -> std::io::Result<()> {
if state == State::Link {
links.last_mut().unwrap().title.push_str(text);
} else {
gmi.write_all(text.as_bytes())?;
fn write_inline(p: &mut ParserVars, text: &str) -> std::io::Result<()> {
match p.state {
State::Link => {
p.links.last_mut().unwrap().title.push_str(text);
}
State::Footnote => {
p.notes.last_mut().unwrap().content.push_str(text);
}
_ => {
p.gmi.write_all(text.as_bytes())?;
}
}
Ok(())
}
struct ParserVars<'v> {
html: Box<dyn std::io::Write>,
gmi: Box<dyn std::io::Write>,
state: State,
ol: bool,
counter: u64,
links: Vec<GmiLink<'v>>,
notes: Vec<Footnote<'v>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
Start,
Paragraph,
Link,
Footnote,
Quote,
TableHead,
Metadata(bool),
@ -360,3 +398,26 @@ struct GmiLink<'link> {
title: String,
url: CowStr<'link>,
}
impl<'link> GmiLink<'link> {
fn from_url(value: CowStr<'link>) -> Self {
Self {
title: String::new(),
url: value,
}
}
}
struct Footnote<'note> {
id: CowStr<'note>,
content: String,
}
impl<'note> Footnote<'note> {
fn from_id(value: CowStr<'note>) -> Self {
Self {
id: value,
content: String::new(),
}
}
}