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;
|
|
|
|
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::*;
|
|
|
|
|
|
|
|
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>")?;
|
|
|
|
if state != State::Start {
|
|
|
|
gmi.write_all(b"\r\n\r\n")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Heading { level, id, .. } => {
|
|
|
|
if let Some(id) = id {
|
|
|
|
html.write_fmt(format_args!("<{} id=\"{}\">", level, id))?;
|
|
|
|
} else {
|
|
|
|
html.write_fmt(format_args!("<{}>", level))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let hashes = match level {
|
|
|
|
HeadingLevel::H1 => "# ",
|
|
|
|
HeadingLevel::H2 => "## ",
|
|
|
|
_ => "### ",
|
|
|
|
};
|
|
|
|
gmi.write_all(hashes.as_bytes())?;
|
|
|
|
state = State::GmiPrefix(hashes);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockQuote(_kind) => {
|
|
|
|
html.write_all(b"<blockquote>")?;
|
|
|
|
gmi.write_all(b"> ")?;
|
|
|
|
state = State::GmiPrefix("> ");
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeBlock(CodeBlockKind::Fenced(lang)) => {
|
|
|
|
html.write_all(b"<pre><code>")?;
|
|
|
|
// TODO: highlighting with syntect
|
|
|
|
gmi.write_fmt(format_args!("```{}\r\n", lang))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeBlock(CodeBlockKind::Indented) => {
|
|
|
|
html.write_all(b"<pre><code>")?;
|
|
|
|
gmi.write_all(b"```")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
List(None) => {
|
|
|
|
html.write_all(b"<ul>")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
List(Some(counter_start)) => {
|
|
|
|
counter = counter_start;
|
|
|
|
if counter_start == 1 {
|
|
|
|
html.write_all(b"<ol>")?;
|
|
|
|
} else {
|
|
|
|
html.write_fmt(format_args!("<ol start=\"{}\">", counter_start))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Item => {
|
|
|
|
html.write_all(b"<li>")?;
|
|
|
|
gmi.write_fmt(format_args!("{}. ", counter))?;
|
|
|
|
counter += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Strong => {
|
|
|
|
html.write_all(b"<b>")?;
|
|
|
|
gmi.write_all(b"**")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Emphasis => {
|
|
|
|
html.write_all(b"<i>")?;
|
|
|
|
gmi.write_all(b"*")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Strikethrough => {
|
|
|
|
html.write_all(b"<s>")?;
|
|
|
|
gmi.write_all(b"~")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Link {
|
|
|
|
dest_url, title, ..
|
|
|
|
} => {
|
|
|
|
html.write_fmt(format_args!("<a href=\"{}\">{}</a>", dest_url, title))?;
|
|
|
|
|
|
|
|
gmi.write_fmt(format_args!("{}[{}]", &title, links.len() + 1))?;
|
|
|
|
links.push(GmiLink {
|
|
|
|
title,
|
|
|
|
url: dest_url,
|
|
|
|
});
|
|
|
|
}
|
2024-10-25 17:02:00 +04:00
|
|
|
|
2024-10-25 22:43:55 +04:00
|
|
|
Image {
|
|
|
|
dest_url, title, ..
|
|
|
|
} => {
|
|
|
|
html.write_fmt(format_args!(
|
|
|
|
"<img src=\"{}\" alt=\"{}\">",
|
|
|
|
dest_url, title
|
|
|
|
))?;
|
|
|
|
|
|
|
|
gmi.write_fmt(format_args!("{}[{}]", &title, links.len() + 1))?;
|
|
|
|
links.push(GmiLink {
|
|
|
|
title,
|
|
|
|
url: dest_url,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Table(_align) => {
|
|
|
|
html.write_all(b"<table>")?;
|
|
|
|
// 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>")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
gmi.write_all(b"\r\n\r\n")?;
|
|
|
|
for (i, link) in links.iter().enumerate() {
|
|
|
|
gmi.write_fmt(format_args!(
|
|
|
|
"=> {} [{}]: {}",
|
|
|
|
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>")?;
|
|
|
|
} else {
|
|
|
|
html.write_all(b"</ul>")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Item => html.write_all(b"</li>")?,
|
|
|
|
Strong => {
|
|
|
|
html.write_all(b"</b>")?;
|
|
|
|
gmi.write_all(b"**")?;
|
|
|
|
}
|
|
|
|
Emphasis => {
|
|
|
|
html.write_all(b"</i>")?;
|
|
|
|
gmi.write_all(b"*")?;
|
|
|
|
}
|
|
|
|
Strikethrough => {
|
|
|
|
html.write_all(b"</s>")?;
|
|
|
|
gmi.write_all(b"~")?;
|
|
|
|
}
|
|
|
|
Link => {}
|
|
|
|
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>")?,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
state = State::Paragraph;
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Text(text) => {
|
2024-10-25 22:43:55 +04:00
|
|
|
html.write_all(text.as_bytes())?;
|
|
|
|
gmi.write_all(text.as_bytes())?;
|
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>")?;
|
|
|
|
|
|
|
|
gmi.write_all(b"`")?;
|
|
|
|
gmi.write_all(code.as_bytes())?;
|
|
|
|
gmi.write_all(b"`")?;
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SoftBreak => {}
|
|
|
|
|
|
|
|
HardBreak => {
|
2024-10-25 22:43:55 +04:00
|
|
|
html.write_all(b"<br>")?;
|
|
|
|
gmi.write_all(b"\r\n")?;
|
|
|
|
if let State::GmiPrefix(prefix) = state {
|
|
|
|
gmi.write_all(prefix.as_bytes())?;
|
|
|
|
}
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Rule => {
|
2024-10-25 22:43:55 +04:00
|
|
|
html.write_all(b"<hr>")?;
|
|
|
|
gmi.write_all(b"---")?;
|
2024-10-25 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TaskListMarker(state) => {
|
2024-10-25 22:43:55 +04:00
|
|
|
if state {
|
|
|
|
html.write_all(b"<input type=checkbox checked disabled>")?;
|
|
|
|
gmi.write_all(b"[x]")?;
|
|
|
|
} else {
|
|
|
|
html.write_all(b"<input type=checkbox disabled>")?;
|
|
|
|
gmi.write_all(b"[ ]")?;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
enum State {
|
|
|
|
Start,
|
|
|
|
Paragraph,
|
|
|
|
GmiPrefix(&'static str),
|
|
|
|
TableHead,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GmiLink<'link> {
|
|
|
|
title: CowStr<'link>,
|
|
|
|
url: CowStr<'link>,
|
|
|
|
}
|