From dcb4a93c35a9a1f13950a03f0b83626de6a5436f Mon Sep 17 00:00:00 2001 From: Aster Date: Sun, 23 Jul 2017 05:10:55 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=AE=20Parse=20typed=20org=20mode=20doc?= =?UTF-8?q?ument?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- projects/panduck-org-mode/src/from_org/mod.rs | 65 ++++++++++++------- projects/panduck-org-mode/src/utils/mod.rs | 16 +++++ 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/projects/panduck-org-mode/src/from_org/mod.rs b/projects/panduck-org-mode/src/from_org/mod.rs index 952085d..5ea3166 100644 --- a/projects/panduck-org-mode/src/from_org/mod.rs +++ b/projects/panduck-org-mode/src/from_org/mod.rs @@ -1,11 +1,13 @@ use crate::utils::{NoteBlock, NoteBlockList, NoteInline, NoteInlineList, NoteRoot, ReadState}; -use orgize::{rowan::ast::AstNode, ParseConfig, SyntaxKind, SyntaxNode, SyntaxNodeChildren}; -use orgize::ast::HeadlineTitle; +use orgize::{ + ast::{Document, Headline, HeadlineTitle}, + rowan::ast::{AstChildren, AstNode}, + ParseConfig, SyntaxKind, SyntaxNode, SyntaxNodeChildren, +}; use wasi_notedown::exports::notedown::core::{ - syntax_tree::{NotedownRoot, RootItem}, + syntax_tree::{NotedownRoot, ParagraphItem, RootItem}, types::{NotedownError, Object}, }; -use wasi_notedown::exports::notedown::core::syntax_tree::ParagraphItem; // mod blocks; // mod html; @@ -26,14 +28,38 @@ impl MarkdownParser { let config = ParseConfig { ..Default::default() }; let mut state = ReadState::default(); let org = config.parse(input); - let doc = org.document(); - let root = match doc.syntax().note_down_root(&mut state) { - Ok(o) => o, - Err(_) => { - todo!() + let root = org.document().note_down_root(&mut state); + state.finish(root) + } +} +impl NoteRoot for Document { + fn note_down_root(self, state: &mut ReadState) -> Result { + let heads = self.headlines().note_down_block(state); + Ok(NotedownRoot { + blocks: heads, + config: Object { map: vec![] }, + path: None, + }) + + + } +} +impl NoteBlock for Headline { + fn note_down_block(self, state: &mut ReadState) -> Result { + todo!() + } +} + +impl NoteBlockList for AstChildren { + fn note_down_block(self, state: &mut ReadState) -> Vec { + let mut out = Vec::with_capacity(self.size_hint().0); + for node in self { + match node.note_down_block(state) { + Ok(o) => out.push(o), + Err(e) => state.note_error(e), } - }; - Ok(root) + } + out } } @@ -78,9 +104,7 @@ impl NoteInlineList for SyntaxNodeChildren { impl NoteBlock for SyntaxNode { fn note_down_block(self, state: &mut ReadState) -> Result { match self.kind() { - SyntaxKind::SECTION => { - Ok(RootItem::Placeholder) - } + SyntaxKind::SECTION => Ok(RootItem::Placeholder), SyntaxKind::HEADLINE => { let inline = self.children().note_down_inline(state); todo!() @@ -90,25 +114,16 @@ impl NoteBlock for SyntaxNode { } } - impl NoteInline for SyntaxNode { fn note_down_inline(self, state: &mut ReadState) -> Result { match self.kind() { - SyntaxKind::HEADLINE => { - Ok(ParagraphItem::Placeholder) - } - SyntaxKind::HEADLINE_TITLE => { - Ok(ParagraphItem::Placeholder) - } + SyntaxKind::HEADLINE => Ok(ParagraphItem::Placeholder), + SyntaxKind::HEADLINE_TITLE => Ok(ParagraphItem::Placeholder), _ => unreachable!("SyntaxKind::{:?} => {{}}", self.kind()), } } } - - - - #[test] fn ready() { println!("it works!") diff --git a/projects/panduck-org-mode/src/utils/mod.rs b/projects/panduck-org-mode/src/utils/mod.rs index 6aae3b6..a0ff5ec 100644 --- a/projects/panduck-org-mode/src/utils/mod.rs +++ b/projects/panduck-org-mode/src/utils/mod.rs @@ -21,6 +21,22 @@ impl ReadState { } } } + pub fn finish(mut self, result: Result) -> Result> { + match result { + Ok(o) => { + if self.errors.is_empty() { + Ok(o) + } + else { + Err(self.errors) + } + } + Err(e) => { + self.note_error(e); + Err(self.errors) + } + } + } } pub trait NoteRoot {