aboutsummaryrefslogtreecommitdiff
path: root/tools/src
diff options
context:
space:
mode:
Diffstat (limited to 'tools/src')
-rw-r--r--tools/src/main.rs45
-rw-r--r--tools/src/markdown.rs54
2 files changed, 93 insertions, 6 deletions
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<String>,
+ #[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<String>, 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<MdElement>);
+
+pub enum MdElement {
+ Heading(u8, Vec<RichText>),
+ Paragraph(Vec<RichText>),
+ Code { syntax: String, content: String },
+}
+
+pub enum RichText {
+ Text(String),
+ Bold(Box<RichText>),
+ Strike(Box<RichText>),
+ Italic(Box<RichText>),
+ Code(Box<RichText>),
+ Link(String, Vec<RichText>),
+}
+
+impl Markdown {
+ pub fn parse(s: &str) -> anyhow::Result<Self> {
+ 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<Vec<Self>> {
+ let mut before = String::new();
+ let mut after = String::new();
+ let mut after_el = false;
+ for c in s.chars() {
+ match c {
+ _ => {}
+ }
+ }
+ Ok(segs)
+ }
+}