diff options
Diffstat (limited to 'tools/src/markdown.rs')
-rw-r--r-- | tools/src/markdown.rs | 54 |
1 files changed, 54 insertions, 0 deletions
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) + } +} |