Skip to content

Commit

Permalink
🔮 Parse typed org mode document
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Jul 23, 2017
1 parent 606f70a commit dcb4a93
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
65 changes: 40 additions & 25 deletions projects/panduck-org-mode/src/from_org/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<NotedownRoot, NotedownError> {
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<RootItem, NotedownError> {
todo!()
}
}

impl<N: AstNode + NoteBlock> NoteBlockList for AstChildren<N> {
fn note_down_block(self, state: &mut ReadState) -> Vec<RootItem> {
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
}
}

Expand Down Expand Up @@ -78,9 +104,7 @@ impl NoteInlineList for SyntaxNodeChildren {
impl NoteBlock for SyntaxNode {
fn note_down_block(self, state: &mut ReadState) -> Result<RootItem, NotedownError> {
match self.kind() {
SyntaxKind::SECTION => {
Ok(RootItem::Placeholder)
}
SyntaxKind::SECTION => Ok(RootItem::Placeholder),
SyntaxKind::HEADLINE => {
let inline = self.children().note_down_inline(state);
todo!()
Expand All @@ -90,25 +114,16 @@ impl NoteBlock for SyntaxNode {
}
}


impl NoteInline for SyntaxNode {
fn note_down_inline(self, state: &mut ReadState) -> Result<ParagraphItem, NotedownError> {
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!")
Expand Down
16 changes: 16 additions & 0 deletions projects/panduck-org-mode/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ impl ReadState {
}
}
}
pub fn finish(mut self, result: Result<NotedownRoot, NotedownError>) -> Result<NotedownRoot, Vec<NotedownError>> {
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 {
Expand Down

0 comments on commit dcb4a93

Please sign in to comment.