From 7853c8a551806007c3697a38fa5f929ca45a0985 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 29 Aug 2022 16:59:57 +0200 Subject: rename tools to code --- code/Cargo.lock | 356 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ code/Cargo.toml | 10 ++ code/makefile | 20 ++++ code/src/main.rs | 140 ++++++++++++++++++++++ 4 files changed, 526 insertions(+) create mode 100644 code/Cargo.lock create mode 100644 code/Cargo.toml create mode 100644 code/makefile create mode 100644 code/src/main.rs (limited to 'code') diff --git a/code/Cargo.lock b/code/Cargo.lock new file mode 100644 index 0000000..b9776eb --- /dev/null +++ b/code/Cargo.lock @@ -0,0 +1,356 @@ +# This file is automatically @generated by Cargo. +# 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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "blog-tool" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "laby", + "markdown", +] + +[[package]] +name = "clap" +version = "3.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29e724a68d9319343bb3328c9cc2dfde263f4b3142ee1059a9980580171c954b" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13547f7012c01ab4a0e8f8967730ada8f9fdf419e8b6c792788f39cf4e46eefa" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "itoap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9028f49264629065d057f340a86acb84867925865f73bbf8d47b4d149a7e88b8" + +[[package]] +name = "laby" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4194d4f3a50193a0d5840579b0e6836ac085bb538551575085f684bb22f508a" +dependencies = [ + "laby_common", + "laby_macros", +] + +[[package]] +name = "laby_common" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aca8e6a5dcbdc8646d54ccc02312551b14aa52b65e2cf225ed68b8bdec2cd65" +dependencies = [ + "itoap", + "raw-cpuid", + "ryu", +] + +[[package]] +name = "laby_macros" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d25610c9e241b2e09ab9ad5394eed42b52bbf6469b54c9a796f671ed16cb280" +dependencies = [ + "laby_common", + "proc-macro2", + "quote", + "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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" + +[[package]] +name = "os_str_bytes" +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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "raw-cpuid" +version = "10.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6aa2540135b6a94f74c7bc90ad4b794f822026a894f3d7bcd185c100d13d4ad6" +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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/code/Cargo.toml b/code/Cargo.toml new file mode 100644 index 0000000..62fbb09 --- /dev/null +++ b/code/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "blog-tool" +version = "0.1.0" +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/code/makefile b/code/makefile new file mode 100644 index 0000000..fa1464d --- /dev/null +++ b/code/makefile @@ -0,0 +1,20 @@ + +TOOL := ../code/target/debug/blog-tool + +SRC_ARTICLES := $(shell find articles -type f) +OUT_ARTICLES := $(SRC_ARTICLES:articles/%.md=out/%.html) + +all: $(OUT_ARTICLES) out/index.html + +out/style.css: style.css + cp $< $@ + +out/index.html: $(TOOL) $(SRC_ARTICLES) + $(TOOL) render-index ./articles > $@ + +out/%.html: articles/%.md $(TOOL) out/style.css + mkdir -p out + $(TOOL) render-article $< > $@ + +$(TOOL): $(shell find ../code/src -type f) + sh -c 'cd ../code; cargo build' diff --git a/code/src/main.rs b/code/src/main.rs new file mode 100644 index 0000000..09ce3d8 --- /dev/null +++ b/code/src/main.rs @@ -0,0 +1,140 @@ +use std::{ + fs::{read_to_string, File}, + io::{BufRead, BufReader, Write}, + path::PathBuf, +}; + +use clap::{Parser, Subcommand}; +use laby::{html, internal::Buffer, iter, li, raw, ul, Render}; +use markdown::{Block, Span}; + +#[derive(Parser)] +struct Args { + #[clap(short, long)] + output: Option, + #[clap(subcommand)] + action: ArgAction, +} + +#[derive(Subcommand)] +enum ArgAction { + RenderArticle { input: String }, + RenderIndex { root: 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()); + } + ArgAction::RenderIndex { root } => { + let mut out = Buffer::new(); + index(root).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!( + nav!(h2!("metamuffin's blog"), a!(href = "./index.html", "index")), + article!(body) + ) + ) +} + +fn index(root: String) -> impl Render { + scaffold( + "index".to_string(), + ul!(iter!(std::fs::read_dir(root) + .unwrap() + .map(|e| e.unwrap()) + .map(|e| ( + e.file_name().to_str().unwrap().to_string(), + article_title(e.path()) + )) + .map(|(path, title)| li!( + path.as_str()[0..10].to_string(), + ": ", + a!(href = format!("./{}", path.replace(".md", ".html")), title) + )))), + ) +} + +fn article_title(path: PathBuf) -> String { + let f = File::open(path).unwrap(); + let mut f = BufReader::new(f); + let mut buf = String::new(); + f.read_line(&mut buf).unwrap(); // assume the 1st line has the title + String::from(&buf[2..]) +} + +fn article(md_source: String) -> impl Render { + scaffold( + "blub".to_string(), + raw!(blocks_to_html(markdown::tokenize(&md_source))), + ) +} + +fn span_to_html(ss: Vec) -> String { + let mut out = String::new(); + for s in ss { + out += match s { + Span::Break => format!("
"), + Span::Text(t) => escape(&t), + Span::Code(c) => format!("
{}
", escape(&c)), + Span::Link(text, url, _) => { + format!("{}", escape(&url), escape(&text)) + } + Span::Image(_, _, _) => todo!(), + Span::Emphasis(c) => format!("{}", span_to_html(c)), + Span::Strong(c) => format!("{}", span_to_html(c)), + } + .as_str() + } + out +} +fn blocks_to_html(blocks: Vec) -> String { + let mut out = String::new(); + for e in blocks { + out += match e { + markdown::Block::Header(text, level) => { + format!("{}", span_to_html(text)) + } + markdown::Block::Paragraph(p) => format!("

{}

", span_to_html(p)), + markdown::Block::Blockquote(q) => format!("{}", blocks_to_html(q)), + markdown::Block::CodeBlock(_syntax, content) => { + format!("
{}
", escape(&content)) // TODO syntax highlighting + } + markdown::Block::OrderedList(_, _) => todo!(), + markdown::Block::UnorderedList(_) => todo!(), + markdown::Block::Raw(r) => r, + markdown::Block::Hr => format!("
"), + } + .as_str(); + } + out +} + +fn escape(text: &str) -> String { + text.replace("&", "&") + .replace("<", "<") + .replace(">", ">") + .replace("'", "’") + .replace("\"", """) +} -- cgit v1.2.3-70-g09d2