2024-10-25 22:43:55 +04:00
|
|
|
use std::{
|
|
|
|
io::{BufWriter, Write},
|
|
|
|
path::Path,
|
|
|
|
};
|
2024-10-25 17:02:00 +04:00
|
|
|
|
2024-10-25 22:43:55 +04:00
|
|
|
use pulldown_cmark::{CowStr, Options, Parser};
|
2024-10-25 17:02:00 +04:00
|
|
|
|
|
|
|
pub fn compile_markdown(
|
|
|
|
src: impl AsRef<Path>,
|
|
|
|
html: impl AsRef<Path>,
|
|
|
|
gmi: impl AsRef<Path>,
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
let src_text = std::fs::read_to_string(src)?;
|
|
|
|
|
2024-10-25 22:43:55 +04:00
|
|
|
let mut html = create_file(html)?;
|
|
|
|
let mut gmi = create_file(gmi)?;
|
|
|
|
|
|
|
|
let mut state = State::Start;
|
2024-11-21 16:24:59 +04:00
|
|
|
let mut ol = false; // ordered list
|
2024-10-25 22:43:55 +04:00
|
|
|
let mut counter: u64 = 0;
|
|
|
|
let mut links: Vec<GmiLink<'_>> = vec![];
|
2024-10-25 17:02:00 +04:00
|
|
|
|
|
|
|
for event in Parser::new_ext(&src_text, Options::all()) {
|
|
|
|
use pulldown_cmark::Event::*;
|
|
|
|
|
2024-11-21 16:24:59 +04:00
|
|
|
dbg!(&event);
|
|
|
|
|
2024-10-25 17:02:00 +04:00
|
|
|
match event {
|
|
|
|
Start(tag) => {
|
2024-10-25 22:43:55 +04:00
|
|
|
use pulldown_cmark::{CodeBlockKind, HeadingLevel, Tag::*};
|
|
|
|
|
|
|
|
match tag {
|
|
|
|
Paragraph => {
|
|
|
|
html.write_all(b"<p>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
write_paragraph_start(&mut gmi, state)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Heading { level, id, .. } => {
|
|
|
|
if let Some(id) = id {
|
|
|
|
html.write_fmt(format_args!("<{} id=\"{}\">", level, id))?;
|
|
|
|
} else {
|
|
|
|
html.write_fmt(format_args!("<{}>", level))?;
|
|
|
|
}
|
|
|
|
|
2024-11-21 16:24:59 +04:00
|
|
|
write_paragraph_start(&mut gmi, state)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
let hashes = match level {
|
|
|
|
HeadingLevel::H1 => "# ",
|
|
|
|
HeadingLevel::H2 => "## ",
|
|
|
|
_ => "### ",
|
|
|
|
};
|
|
|
|
gmi.write_all(hashes.as_bytes())?;
|
2024-11-21 16:24:59 +04:00
|
|
|
state = State::Paragraph;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockQuote(_kind) => {
|
|
|
|
html.write_all(b"<blockquote>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
|
|
|
|
write_paragraph_start(&mut gmi, state)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
gmi.write_all(b"> ")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
state = State::Quote;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeBlock(CodeBlockKind::Fenced(lang)) => {
|
|
|
|
// TODO: highlighting with syntect
|
2024-11-21 16:24:59 +04:00
|
|
|
html.write_all(b"<pre><code>")?;
|
|
|
|
|
|
|
|
write_paragraph_start(&mut gmi, state)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
gmi.write_fmt(format_args!("```{}\r\n", lang))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeBlock(CodeBlockKind::Indented) => {
|
|
|
|
html.write_all(b"<pre><code>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
|
|
|
|
write_paragraph_start(&mut gmi, state)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
gmi.write_all(b"```")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
List(None) => {
|
|
|
|
html.write_all(b"<ul>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
write_paragraph_start(&mut gmi, state)?;
|
|
|
|
state = State::Start;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
List(Some(counter_start)) => {
|
|
|
|
counter = counter_start;
|
2024-11-21 16:24:59 +04:00
|
|
|
ol = true;
|
|
|
|
|
2024-10-25 22:43:55 +04:00
|
|
|
if counter_start == 1 {
|
|
|
|
html.write_all(b"<ol>")?;
|
|
|
|
} else {
|
|
|
|
html.write_fmt(format_args!("<ol start=\"{}\">", counter_start))?;
|
|
|
|
}
|
2024-11-21 16:24:59 +04:00
|
|
|
|
|
|
|
write_paragraph_start(&mut gmi, state)?;
|
|
|
|
state = State::Start;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Item => {
|
|
|
|
html.write_all(b"<li>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
|
|
|
|
if state != State::Start {
|
|
|
|
gmi.write_all(b"\r\n")?;
|
|
|
|
state = State::Paragraph;
|
|
|
|
}
|
|
|
|
if ol {
|
|
|
|
gmi.write_fmt(format_args!("{}. ", counter))?;
|
|
|
|
counter += 1;
|
|
|
|
} else {
|
|
|
|
gmi.write_all(b"* ")?;
|
|
|
|
}
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Strong => {
|
|
|
|
html.write_all(b"<b>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
write_inline(&mut gmi, state, "**", &mut links)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Emphasis => {
|
|
|
|
html.write_all(b"<i>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
write_inline(&mut gmi, state, "*", &mut links)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Strikethrough => {
|
|
|
|
html.write_all(b"<s>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
write_inline(&mut gmi, state, "~", &mut links)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
2024-11-21 16:24:59 +04:00
|
|
|
Link { dest_url, .. } => {
|
|
|
|
html.write_fmt(format_args!("<a href=\"{}\">", dest_url))?;
|
2024-10-25 22:43:55 +04:00
|
|
|
links.push(GmiLink {
|
2024-11-21 16:24:59 +04:00
|
|
|
title: String::new(),
|
2024-10-25 22:43:55 +04:00
|
|
|
url: dest_url,
|
|
|
|
});
|
2024-11-21 16:24:59 +04:00
|
|
|
state = State::Link;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
2024-10-25 17:02:00 +04:00
|
|
|
|
2024-11-21 16:24:59 +04:00
|
|
|
Image { dest_url, .. } => {
|
2024-10-25 22:43:55 +04:00
|
|
|
links.push(GmiLink {
|
2024-11-21 16:24:59 +04:00
|
|
|
title: String::new(),
|
2024-10-25 22:43:55 +04:00
|
|
|
url: dest_url,
|
|
|
|
});
|
2024-11-21 16:24:59 +04:00
|
|
|
state = State::Link;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Table(_align) => {
|
|
|
|
html.write_all(b"<table>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
|
|
|
|
write_paragraph_start(&mut gmi, state)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
// gmi ??
|
|
|
|
}
|
|
|
|
|
|
|
|
TableHead => {
|
|
|
|
html.write_all(b"<thead><tr>")?;
|
|
|
|
state = State::TableHead;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableRow => {
|
|
|
|
html.write_all(b"<tr>")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableCell => {
|
|
|
|
if state == State::TableHead {
|
|
|
|
html.write_all(b"<th>")?;
|
|
|
|
} else {
|
|
|
|
html.write_all(b"<td>")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-21 16:24:59 +04:00
|
|
|
MetadataBlock(_) => {
|
|
|
|
state = State::Metadata(state != State::Start);
|
|
|
|
}
|
|
|
|
|
2024-10-25 22:43:55 +04:00
|
|
|
other => {
|
|
|
|
eprintln!("Unsupported tag: {:?}", other);
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
End(tag) => {
|
2024-10-25 22:43:55 +04:00
|
|
|
use pulldown_cmark::TagEnd::*;
|
|
|
|
|
|
|
|
match tag {
|
|
|
|
Paragraph => {
|
|
|
|
html.write_all(b"</p>")?;
|
2024-10-25 17:02:00 +04:00
|
|
|
|
2024-10-25 22:43:55 +04:00
|
|
|
if !links.is_empty() {
|
2024-11-21 16:24:59 +04:00
|
|
|
gmi.write_all(b"\r\n")?;
|
2024-10-25 22:43:55 +04:00
|
|
|
for (i, link) in links.iter().enumerate() {
|
|
|
|
gmi.write_fmt(format_args!(
|
2024-11-21 16:24:59 +04:00
|
|
|
"\r\n=> {} [{}]: {}", // => https://... [1]: example
|
2024-10-25 22:43:55 +04:00
|
|
|
link.url, i, link.title,
|
|
|
|
))?;
|
|
|
|
}
|
|
|
|
links.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Heading(level) => html.write_fmt(format_args!("<{}>", level))?,
|
|
|
|
BlockQuote(_) => html.write_all(b"</blockquote>")?,
|
|
|
|
CodeBlock => {
|
|
|
|
html.write_all(b"</code></pre>")?;
|
|
|
|
gmi.write_all(b"```")?;
|
|
|
|
}
|
|
|
|
List(ordered) => {
|
|
|
|
if ordered {
|
|
|
|
html.write_all(b"</ol>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
ol = false;
|
2024-10-25 22:43:55 +04:00
|
|
|
} else {
|
|
|
|
html.write_all(b"</ul>")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Item => html.write_all(b"</li>")?,
|
|
|
|
Strong => {
|
|
|
|
html.write_all(b"</b>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
write_inline(&mut gmi, state, "**", &mut links)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
Emphasis => {
|
|
|
|
html.write_all(b"</i>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
write_inline(&mut gmi, state, "*", &mut links)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
Strikethrough => {
|
|
|
|
html.write_all(b"</s>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
write_inline(&mut gmi, state, "~", &mut links)?;
|
|
|
|
}
|
|
|
|
Link | Image => {
|
|
|
|
gmi.write_fmt(format_args!(
|
|
|
|
"{}[{}]", // example[1] ...\r\n => https://... [1]: example
|
|
|
|
links.last().unwrap().title,
|
|
|
|
links.len(),
|
|
|
|
))?;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
TableHead => html.write_all(b"</tr></thead><tbody>")?,
|
|
|
|
TableRow => html.write_all(b"</tr>")?,
|
|
|
|
TableCell => {
|
|
|
|
if state == State::TableHead {
|
|
|
|
html.write_all(b"</th>")?
|
|
|
|
} else {
|
|
|
|
html.write_all(b"</td>")?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Table => html.write_all(b"</tbody></table>")?,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2024-11-21 16:24:59 +04:00
|
|
|
match state {
|
|
|
|
State::Metadata(false) => {
|
|
|
|
state = State::Start;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
state = State::Paragraph;
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Text(text) => {
|
2024-11-21 16:24:59 +04:00
|
|
|
match state {
|
|
|
|
State::Metadata(_) => {
|
|
|
|
// TODO: parse yaml
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2024-10-25 22:43:55 +04:00
|
|
|
html.write_all(text.as_bytes())?;
|
2024-11-21 16:24:59 +04:00
|
|
|
write_inline(&mut gmi, state, &text, &mut links)?;
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Code(code) => {
|
2024-10-25 22:43:55 +04:00
|
|
|
html.write_all(b"<code>")?;
|
|
|
|
html.write_all(code.as_bytes())?;
|
|
|
|
html.write_all(b"</code>")?;
|
|
|
|
|
2024-11-21 16:24:59 +04:00
|
|
|
write_inline(&mut gmi, state, "`", &mut links)?;
|
|
|
|
write_inline(&mut gmi, state, &code, &mut links)?;
|
|
|
|
write_inline(&mut gmi, state, "`", &mut links)?;
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
2024-11-21 16:24:59 +04:00
|
|
|
SoftBreak => {
|
|
|
|
html.write_all(b" ")?;
|
|
|
|
write_inline(&mut gmi, state, " ", &mut links)?;
|
|
|
|
}
|
2024-10-25 17:02:00 +04:00
|
|
|
|
|
|
|
HardBreak => {
|
2024-10-25 22:43:55 +04:00
|
|
|
html.write_all(b"<br>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
|
2024-10-25 22:43:55 +04:00
|
|
|
gmi.write_all(b"\r\n")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
if state == State::Quote {
|
|
|
|
gmi.write_all(b"> ")?;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Rule => {
|
2024-10-25 22:43:55 +04:00
|
|
|
html.write_all(b"<hr>")?;
|
2024-11-21 16:24:59 +04:00
|
|
|
|
|
|
|
write_paragraph_start(&mut gmi, state)?;
|
2024-10-25 22:43:55 +04:00
|
|
|
gmi.write_all(b"---")?;
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
2024-11-21 16:24:59 +04:00
|
|
|
TaskListMarker(done) => {
|
|
|
|
if done {
|
2024-10-25 22:43:55 +04:00
|
|
|
html.write_all(b"<input type=checkbox checked disabled>")?;
|
2024-11-21 16:46:52 +04:00
|
|
|
gmi.write_all(b"[x] ")?;
|
2024-10-25 22:43:55 +04:00
|
|
|
} else {
|
|
|
|
html.write_all(b"<input type=checkbox disabled>")?;
|
2024-11-21 16:46:52 +04:00
|
|
|
gmi.write_all(b"[ ] ")?;
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
other => {
|
2024-10-25 22:43:55 +04:00
|
|
|
eprintln!("Unsupported event: {:?}", other);
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 22:43:55 +04:00
|
|
|
html.flush()?;
|
|
|
|
gmi.flush()?;
|
|
|
|
|
2024-10-25 17:02:00 +04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn create_file(path: impl AsRef<Path>) -> std::io::Result<BufWriter<std::fs::File>> {
|
|
|
|
let f = std::fs::OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
|
|
|
.open(path)?;
|
|
|
|
Ok(BufWriter::new(f))
|
|
|
|
}
|
2024-10-25 22:43:55 +04:00
|
|
|
|
2024-11-21 16:24:59 +04:00
|
|
|
#[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")?;
|
|
|
|
}
|
|
|
|
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())?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-10-25 22:43:55 +04:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
enum State {
|
|
|
|
Start,
|
|
|
|
Paragraph,
|
2024-11-21 16:24:59 +04:00
|
|
|
Link,
|
|
|
|
Quote,
|
2024-10-25 22:43:55 +04:00
|
|
|
TableHead,
|
2024-11-21 16:24:59 +04:00
|
|
|
Metadata(bool),
|
2024-10-25 22:43:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct GmiLink<'link> {
|
2024-11-21 16:24:59 +04:00
|
|
|
title: String,
|
2024-10-25 22:43:55 +04:00
|
|
|
url: CowStr<'link>,
|
|
|
|
}
|