1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
use crate::{convert::jupyter_from_json, error::Error::UnsupportedFormat, Result, ToNotedown}; use html_parser::Dom; use markdown::tokenize; use notedown_parser::{ParserConfig, AST}; use serde_json::Value; use std::{fs::read_to_string, path::Path}; pub fn parse_by_ext(file: impl AsRef<Path>) -> Result<AST> { let ext = file.as_ref().extension().unwrap_or_default().to_str().unwrap_or_default(); let input = &read_to_string(file.as_ref())?; match ext { "note" | "notedown" => parse_notedown(input), "md" | "markdown" => parse_markdown(input), "html" => parse_html(input), "ipynb" => parse_jupyter(input), _ => Err(UnsupportedFormat(String::from(ext))), } } pub fn parse_notedown(input: &str) -> Result<AST> { Ok(ParserConfig::default().parse(input)?) } pub fn parse_markdown(input: &str) -> Result<AST> { Ok(tokenize(input).to_notedown()) } pub fn parse_html(text: &str) -> Result<AST> { Ok(Dom::parse(text)?.to_notedown()) } pub fn parse_jupyter(text: &str) -> Result<AST> { let v: Value = serde_json::from_str(text)?; Ok(jupyter_from_json(&v)?) }