2020-05-31 16:29:43 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use comrak::nodes::{AstNode, NodeValue};
|
|
|
|
use comrak::{format_commonmark, parse_document, Arena, ComrakOptions};
|
|
|
|
use std::fs::read_to_string;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
pub(crate) fn read(fname: PathBuf, tag: String) -> Result<String> {
|
|
|
|
let data = read_to_string(fname)?;
|
|
|
|
let arena = Arena::new();
|
|
|
|
let mut root = parse_document(&arena, &data, &ComrakOptions::default());
|
|
|
|
|
2020-06-01 02:17:13 +00:00
|
|
|
let mut collect = false;
|
|
|
|
let mut buf = Vec::<u8>::new();
|
2020-05-31 16:29:43 +00:00
|
|
|
|
2020-06-01 02:17:13 +00:00
|
|
|
iter_nodes(&mut root, &mut |node| {
|
2020-05-31 16:29:43 +00:00
|
|
|
let nd = node.data.borrow();
|
|
|
|
|
|
|
|
match nd.value {
|
|
|
|
NodeValue::Heading(ref hdr) => {
|
|
|
|
if hdr.level == 2 {
|
2020-06-01 02:17:13 +00:00
|
|
|
if collect {
|
|
|
|
collect = false;
|
2020-05-31 16:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let found_tag = String::from_utf8(nd.content.clone())?;
|
|
|
|
|
2020-07-09 14:06:47 +00:00
|
|
|
if found_tag == tag || found_tag == format!("[{}]", tag) {
|
2020-06-01 02:17:13 +00:00
|
|
|
collect = true;
|
2020-05-31 16:29:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-01 02:17:13 +00:00
|
|
|
if collect {
|
|
|
|
format_commonmark(&node, &ComrakOptions::default(), &mut buf)?;
|
2020-05-31 16:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
NodeValue::Item(_) => Ok(()),
|
|
|
|
_ => {
|
2020-06-01 02:17:13 +00:00
|
|
|
if collect {
|
|
|
|
format_commonmark(&node, &ComrakOptions::default(), &mut buf)?;
|
2020-05-31 16:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
2020-06-01 02:17:13 +00:00
|
|
|
Ok(String::from_utf8(buf)?)
|
2020-05-31 16:29:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 02:17:13 +00:00
|
|
|
fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &mut F) -> Result<()>
|
2020-05-31 16:29:43 +00:00
|
|
|
where
|
2020-06-01 02:17:13 +00:00
|
|
|
F: FnMut(&'a AstNode<'a>) -> Result<()>,
|
2020-05-31 16:29:43 +00:00
|
|
|
{
|
|
|
|
f(node)?;
|
|
|
|
for c in node.children() {
|
|
|
|
match c.data.borrow().value {
|
|
|
|
NodeValue::Text(_) => {}
|
|
|
|
NodeValue::Item(_) => {}
|
|
|
|
NodeValue::Code(_) => {}
|
|
|
|
_ => {
|
|
|
|
iter_nodes(c, f)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-31 16:53:34 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
2020-07-09 14:06:47 +00:00
|
|
|
fn basic() {
|
2020-05-31 16:57:58 +00:00
|
|
|
let res = super::read("testdata/basic.md".into(), "0.1.0".into());
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let delta = res.unwrap();
|
2020-06-12 11:18:13 +00:00
|
|
|
assert_eq!(
|
|
|
|
delta,
|
|
|
|
"Hi there this is a test\\!\n### ADDED\n - something\n"
|
|
|
|
)
|
2020-05-31 16:53:34 +00:00
|
|
|
}
|
2020-07-09 14:06:47 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn brackets() {
|
|
|
|
let res = super::read("testdata/brackets.md".into(), "0.1.0".into());
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let delta = res.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
delta,
|
|
|
|
"Hi there this is a test\\!\n### ADDED\n - something\n"
|
|
|
|
)
|
|
|
|
}
|
2020-05-31 16:53:34 +00:00
|
|
|
}
|