From 9ca6cc8c0acdd68d2c79f1f990c9dd81dd9fef4b Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 29 Aug 2022 15:43:02 +0200 Subject: a --- tools/Cargo.lock | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ tools/Cargo.toml | 2 ++ tools/src/main.rs | 45 +++++++++++++++++++++++++++++++----- tools/src/markdown.rs | 54 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 tools/src/markdown.rs diff --git a/tools/Cargo.lock b/tools/Cargo.lock index 4867c09..b9776eb 100644 --- a/tools/Cargo.lock +++ b/tools/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1485d4d2cc45e7b201ee3767015c96faa5904387c9d87c6efdd0fb511f12d305" + [[package]] name = "atty" version = "0.2.14" @@ -29,8 +44,10 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" name = "blog-tool" version = "0.1.0" dependencies = [ + "anyhow", "clap", "laby", + "markdown", ] [[package]] @@ -142,12 +159,35 @@ dependencies = [ "syn", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" +[[package]] +name = "markdown" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef3aab6a1d529b112695f72beec5ee80e729cb45af58663ec902c8fac764ecdd" +dependencies = [ + "lazy_static", + "pipeline", + "regex", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + [[package]] name = "once_cell" version = "1.13.1" @@ -160,6 +200,12 @@ version = "6.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" +[[package]] +name = "pipeline" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d15b6607fa632996eb8a17c9041cb6071cb75ac057abd45dece578723ea8c7c0" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -211,6 +257,23 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" + [[package]] name = "ryu" version = "1.0.11" diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 55d550f..62fbb09 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" [dependencies] laby = "0.2.4" clap = { version = "3.2.17", features = ["derive"] } +anyhow = "1.0.62" +markdown = "0.3.0" \ No newline at end of file diff --git a/tools/src/main.rs b/tools/src/main.rs index 0167e3c..396d148 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -1,17 +1,50 @@ -use clap::Parser; -use laby::{html, Render}; + +use std::{ + fs::{read_to_string, File}, + io::Write, +}; + +use clap::{Parser, Subcommand}; +use laby::{html, internal::Buffer, Render}; #[derive(Parser)] struct Args { - input: String, - output: String, + #[clap(short, long)] + output: Option, + #[clap(subcommand)] + action: ArgAction, +} + +#[derive(Subcommand)] +enum ArgAction { + RenderArticle { input: String }, } fn main() { let args = Args::parse(); - + match args.action { + ArgAction::RenderArticle { input } => { + let md_source = read_to_string(input).unwrap(); + let mut out = Buffer::new(); + article(md_source).render(&mut out); + write_output(&args.output, out.into_string()); + } + } +} + +fn write_output(t: &Option, o: String) { + if let Some(f) = t { + let mut f = File::create(f).unwrap(); + f.write_fmt(format_args!("{o}")).unwrap() + } else { + println!("{o}") + } } fn scaffold(title: String, body: impl Render) -> impl Render { - html!(head!(title!(title)), body!()) + html!(head!(title!(title)), body!(body)) +} + +fn article(md_source: String) { + } diff --git a/tools/src/markdown.rs b/tools/src/markdown.rs new file mode 100644 index 0000000..af3798e --- /dev/null +++ b/tools/src/markdown.rs @@ -0,0 +1,54 @@ +pub struct Markdown(Vec); + +pub enum MdElement { + Heading(u8, Vec), + Paragraph(Vec), + Code { syntax: String, content: String }, +} + +pub enum RichText { + Text(String), + Bold(Box), + Strike(Box), + Italic(Box), + Code(Box), + Link(String, Vec), +} + +impl Markdown { + pub fn parse(s: &str) -> anyhow::Result { + let mut c = vec![]; + let mut lines = s.lines(); + while let Some(line) = lines.next() { + if line.starts_with("#") { + let (hashes, h) = line.split_once(' ').unwrap(); + c.push(MdElement::Heading(hashes.len() as u8, RichText::parse(h)?)); + } else if line.starts_with("```") { + todo!() + } else { + let mut block = line.to_string(); + while let Some(line) = lines.next() { + if line == "" { + break; + } + block += line; + } + c.push(MdElement::Paragraph(RichText::parse(&block)?)) + } + } + Ok(Markdown(c)) + } +} +impl RichText { + pub fn parse(s: &str) -> anyhow::Result> { + let mut before = String::new(); + let mut after = String::new(); + let mut after_el = false; + for c in s.chars() { + match c { + _ => {} + } + } + Ok(segs) + } +} -- cgit v1.2.3-70-g09d2