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