From 9ca6cc8c0acdd68d2c79f1f990c9dd81dd9fef4b Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 29 Aug 2022 15:43:02 +0200 Subject: a --- tools/src/markdown.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tools/src/markdown.rs (limited to 'tools/src/markdown.rs') 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