src and dist paths

This commit is contained in:
Logan 2024-09-07 14:33:15 -05:00
parent 445341451c
commit 8c8fbc91aa
5 changed files with 508 additions and 32 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target
/dist

383
Cargo.lock generated
View file

@ -2,6 +2,230 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "anstream"
version = "0.6.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
[[package]]
name = "anstyle-parse"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]]
name = "backtrace"
version = "0.3.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "bstr"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c"
dependencies = [
"memchr",
"serde",
]
[[package]]
name = "cc"
version = "1.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a93fe60e2fc87b6ba2c117f67ae14f66e3fc7d6a1e612a25adb238cc980eadb3"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.5.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_lex"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
[[package]]
name = "colorchoice"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "gimli"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
[[package]]
name = "globset"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57da3b9b5b85bd66f31093f8c408b90a74431672542466497dcbdfdc02034be1"
dependencies = [
"aho-corasick",
"bstr",
"log",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "ignore"
version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b46810df39e66e925525d6e38ce1e7f6e1d208f72dc39757880fcb66e2c58af1"
dependencies = [
"crossbeam-deque",
"globset",
"log",
"memchr",
"regex-automata",
"same-file",
"walkdir",
"winapi-util",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "libc"
version = "0.2.158"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
[[package]]
name = "log"
version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "markdown"
version = "1.0.0-alpha.20"
@ -15,10 +239,93 @@ dependencies = [
name = "mdconvert"
version = "0.1.0"
dependencies = [
"ignore",
"markdown",
"minifier",
"tokio",
"walkdir",
]
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "minifier"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aa3f302fe0f8de065d4a2d1ed64f60204623cac58b80cd3c2a83a25d5a7d437"
dependencies = [
"clap",
]
[[package]]
name = "miniz_oxide"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08"
dependencies = [
"adler",
]
[[package]]
name = "object"
version = "0.36.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "084f1a5821ac4c651660a94a7153d27ac9d8a53736203f58b31945ded098070a"
dependencies = [
"memchr",
]
[[package]]
name = "pin-project-lite"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]]
name = "proc-macro2"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex-automata"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
[[package]]
name = "rustc-demangle"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "same-file"
version = "1.0.6"
@ -28,12 +335,77 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "serde"
version = "1.0.210"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.210"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tokio"
version = "1.40.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998"
dependencies = [
"backtrace",
"pin-project-lite",
]
[[package]]
name = "unicode-id"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1b6def86329695390197b82c1e244a54a131ceb66c996f2088a3876e2ae083f"
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "walkdir"
version = "2.5.0"
@ -50,7 +422,16 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys",
"windows-sys 0.59.0",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]

View file

@ -4,5 +4,8 @@ version = "0.1.0"
edition = "2021"
[dependencies]
tokio = "1.4"
markdown = "1.0.0-alpha.20"
walkdir = "2.5"
ignore = "0.4"
minifier = "0.3.1"

View file

@ -1,40 +1,133 @@
use markdown::*;
use std::ffi::OsStr;
use std::{io::prelude::*, path::PathBuf};
use std::{
fs::File,
io::prelude::*,
path::{Path, PathBuf},
};
fn convert_file(
entry: PathBuf,
fn main() {
let mut src_path = String::from("./");
let mut dist_path = String::from("./dist");
for arg in std::env::args() {
if let Some(s) = arg.strip_prefix("--path=") {
src_path = String::from(s);
} else if let Some(s) = arg.strip_prefix("--target=") {
dist_path = String::from(s);
}
}
let src_path = PathBuf::from(src_path).canonicalize().unwrap();
assert!(src_path.is_dir());
let dist_path = PathBuf::from(dist_path);
let _ = std::fs::create_dir_all(&dist_path);
let dist_path = dist_path.canonicalize().unwrap();
convert_all(&src_path, &dist_path)
}
fn opts() -> Options {
Options {
parse: ParseOptions::gfm(),
compile: CompileOptions {
allow_dangerous_html: true,
allow_dangerous_protocol: true,
gfm_task_list_item_checkable: true,
gfm_tagfilter: false,
..Default::default()
},
}
}
fn convert_all(src_path: &Path, dist_path: &Path) {
let head = std::fs::read_to_string("./head").unwrap_or_default();
for entry in ignore::Walk::new(src_path) {
let entry = match entry {
Ok(p) => p,
Err(_) => continue,
};
let file = entry.path();
let extension = file
.extension()
.unwrap_or_default()
.to_str()
.unwrap_or_default();
match extension {
"md" => convert_md(file, src_path, dist_path, &head),
"css" => convert_css(file, src_path, dist_path),
"js" => convert_js(file, src_path, dist_path),
"json" => convert_json(file, src_path, dist_path),
_ => Ok(()),
}
.unwrap();
}
}
fn convert_md(
file: &Path,
src_path: &Path,
dist_path: &Path,
head: &str,
opts: &Options,
) -> Result<(), std::io::Error> {
let md_file = std::fs::read_to_string(&entry)?;
let html_str = to_html_with_options(&md_file, &opts)
let opts = &opts();
let (input, mut output) = read_and_open(file, src_path, dist_path, "html")?;
let inner_html = to_html_with_options(&input, &opts)
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, ""))?;
let html_str = format!(
let dom = format!(
"<!DOCTYPE html><html><head>{}</head><body><main>{}</main></body></html>",
head, html_str
head, inner_html
);
let mut html_path = entry.clone();
html_path.set_extension("html");
let mut html_file = std::fs::File::create(html_path)?;
html_file.write(html_str.as_bytes())?;
output.write(dom.as_bytes())?;
Ok(())
}
fn main() {
let mut opts = Options::default();
opts.compile.allow_dangerous_html = true;
opts.compile.allow_dangerous_protocol = true;
opts.compile.gfm_tagfilter = false;
let head = std::fs::read_to_string("./head").unwrap_or("".to_owned());
for entry in walkdir::WalkDir::new("./") {
let entry = match entry {
Ok(e) => e.into_path(),
Err(_) => continue,
};
if entry.extension() == Some(OsStr::new("md")) {
convert_file(entry, &head, &opts).unwrap();
}
}
fn convert_css(
file: &Path,
src_path: &Path,
dist_path: &Path,
) -> Result<(), std::io::Error> {
let (input, mut output) = read_and_open(file, src_path, dist_path, "css")?;
let minified = match minifier::css::minify(&input) {
Ok(m) => m.to_string(),
Err(_) => input,
};
output.write(minified.as_bytes())?;
Ok(())
}
fn convert_json(
file: &Path,
src_path: &Path,
dist_path: &Path,
) -> Result<(), std::io::Error> {
let (input, mut output) = read_and_open(file, src_path, dist_path, "json")?;
let minified = minifier::json::minify(&input).to_string();
output.write(minified.as_bytes())?;
Ok(())
}
fn convert_js(
file: &Path,
src_path: &Path,
dist_path: &Path,
) -> Result<(), std::io::Error> {
let (input, mut output) = read_and_open(file, src_path, dist_path, "js")?;
let minified = minifier::js::minify(&input).to_string();
output.write(minified.as_bytes())?;
Ok(())
}
fn read_and_open(
file: &Path,
src_path: &Path,
dist_path: &Path,
new_extension: &str,
) -> Result<(String, File), std::io::Error> {
let file_contents = std::fs::read_to_string(&file)?;
let dist_path = dist_path
.join(file.strip_prefix(src_path).unwrap_or(file))
.with_extension(new_extension);
if let Some(path) = dist_path.parent() {
let _ = std::fs::create_dir_all(path);
}
let output_file = std::fs::File::create(dist_path)?;
Ok((file_contents, output_file))
}

View file

@ -1,2 +0,0 @@
<!DOCTYPE html><html><head></head><body><main><h1>hello world</h1>
</main></body></html>