From 3810f4969141f4ce0b177f33ec033682d9a6dceb Mon Sep 17 00:00:00 2001 From: Aster Date: Tue, 18 Jun 2024 10:34:11 +0800 Subject: [PATCH] Define ast in wasi --- .github/workflows/rust.yml | 4 +- Cargo.toml | 6 +- projects/panduck-html/Cargo.toml | 8 +-- projects/panduck-jupyter/Cargo.toml | 6 +- projects/panduck-latex/Cargo.toml | 2 +- projects/panduck-markdown/Cargo.toml | 5 +- .../panduck-markdown/src/from_cmd/blocks.rs | 9 ++- .../panduck-markdown/src/from_cmd/inline.rs | 35 +++++------ .../panduck-markdown/src/from_cmd/list.rs | 2 +- projects/panduck-markdown/src/from_cmd/mod.rs | 61 +++++++------------ .../panduck-markdown/src/from_pandoc/mod.rs | 42 +++++++------ projects/panduck-markdown/src/utils/mod.rs | 37 ++++++++--- projects/panduck-org-mode/Cargo.toml | 6 +- projects/pretty-print/Cargo.toml | 2 +- 14 files changed, 110 insertions(+), 115 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index ec3048c..c81a2fa 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -11,8 +11,8 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [macos-latest, windows-latest] -# os: [ubuntu-latest, macos-latest, windows-latest] +# os: [macos-latest, windows-latest] + os: [ubuntu-latest, macos-latest, windows-latest] steps: - run: git config --global core.autocrlf false - uses: actions/checkout@v2 diff --git a/Cargo.toml b/Cargo.toml index 8fd42b2..1339e1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,8 @@ opt-level = "s" [workspace.metadata.release] push = false -tag = false \ No newline at end of file +tag = false + +[workspace.dependencies.notedown-wasi] +version = "0.0.*" +path = 'C:\Users\Dell\CLionProjects\notedown-rs\projects\notedown-wasi' \ No newline at end of file diff --git a/projects/panduck-html/Cargo.toml b/projects/panduck-html/Cargo.toml index 287be0a..bef4be8 100644 --- a/projects/panduck-html/Cargo.toml +++ b/projects/panduck-html/Cargo.toml @@ -9,8 +9,7 @@ license = "MPL-2.0" edition = "2018" [dependencies] -notedown_ast = "0.16.3" -notedown-error = "1.1.10" +notedown-wasi = { workspace = true } html-escape = "0.2.11" serde = { version = "1.0.144", features = ["derive"] } merge = "0.1.0" @@ -32,10 +31,5 @@ version = "0" path = "../pretty-print" features = [] -[dev-dependencies.panduck-core] -version = "0.1.3" -path = "../panduck-core" -features = ["markdown"] - [features] default = ["katex", "syntect"] diff --git a/projects/panduck-jupyter/Cargo.toml b/projects/panduck-jupyter/Cargo.toml index 7e90dd8..b1d0cc5 100644 --- a/projects/panduck-jupyter/Cargo.toml +++ b/projects/panduck-jupyter/Cargo.toml @@ -9,13 +9,9 @@ license = "MPL-2.0" edition = "2018" [dependencies] +notedown-wasi = { workspace = true } serde_json = "1.0.115" -[dependencies.notedown-wasi] -version = "0.0.*" -#path = 'C:\Users\Dell\CLionProjects\notedown-rs\projects\notedown-wasi' -features = [] - [features] default = [] server = [] diff --git a/projects/panduck-latex/Cargo.toml b/projects/panduck-latex/Cargo.toml index 494c3b3..88e340f 100644 --- a/projects/panduck-latex/Cargo.toml +++ b/projects/panduck-latex/Cargo.toml @@ -9,7 +9,7 @@ license = "MPL-2.0" edition = "2021" [dependencies] -notedown_ast = "0.16.3" +notedown-wasi = { workspace = true} notedown-error = "1.1.10" notedown-rt = "0.3.2" serde = { version = "1.0.144", features = ["derive"] } diff --git a/projects/panduck-markdown/Cargo.toml b/projects/panduck-markdown/Cargo.toml index 5d142dc..06d4fc0 100644 --- a/projects/panduck-markdown/Cargo.toml +++ b/projects/panduck-markdown/Cargo.toml @@ -9,13 +9,10 @@ license = "MPL-2.0" edition = "2018" [dependencies] -toml = "0.8.12" +notedown-wasi = { workspace = true, features = ["toml"]} markdown = "1.0.0-alpha.16" pandoc_ast = "0.8.6" -[dependencies.notedown-wasi] -version = "0.0.*" -#path = 'C:\Users\Dell\CLionProjects\notedown-rs\projects\notedown-wasi' [features] diff --git a/projects/panduck-markdown/src/from_cmd/blocks.rs b/projects/panduck-markdown/src/from_cmd/blocks.rs index dc9daac..5799922 100644 --- a/projects/panduck-markdown/src/from_cmd/blocks.rs +++ b/projects/panduck-markdown/src/from_cmd/blocks.rs @@ -1,6 +1,10 @@ use super::*; -use markdown::mdast::{Definition, FootnoteDefinition, Heading, Toml, Yaml}; -use wasi_notedown::exports::notedown::core::syntax_tree::{CommandAction, HeadingBlock}; + +impl NoteBlockList for Vec { + fn note_down_block(self, state: &mut ReadState) -> Vec { + group_block(self, state) + } +} impl NoteBlock for Node { fn note_down_block(self, state: &mut ReadState) -> Result { @@ -48,6 +52,7 @@ impl NoteBlock for Node { impl NoteBlock for Toml { fn note_down_block(self, state: &mut ReadState) -> Result { + Ok(RootItem::Placeholder) } } diff --git a/projects/panduck-markdown/src/from_cmd/inline.rs b/projects/panduck-markdown/src/from_cmd/inline.rs index 13a67d2..7940a72 100644 --- a/projects/panduck-markdown/src/from_cmd/inline.rs +++ b/projects/panduck-markdown/src/from_cmd/inline.rs @@ -1,14 +1,9 @@ use super::*; -use markdown::mdast::{FootnoteReference, Image, Link, MdxTextExpression}; -use std::str::FromStr; -use wasi_notedown::{ - exports::notedown::core::{ - syntax_tree::{ImageReference, LinkReference}, - types::Url, - }, - UrlNative, -}; -use wasi_notedown::exports::notedown::core::syntax_tree::CommandAction; +impl NoteInlineList for Vec { + fn note_down_inline(self, state: &mut ReadState) -> Vec { + group_inline(self, state) + } +} impl NoteInline for Node { fn note_down_inline(self, state: &mut ReadState) -> Result { @@ -58,26 +53,26 @@ impl NoteInline for Text { } } -impl NoteInline for Strong { +impl NoteInline for Emphasis { fn note_down_inline(self, state: &mut ReadState) -> Result { - // let mut blocks = paragraph_items(self.children, state)?; - let text = StyledText { type_: StyleType::BOLD, range: self.position.as_range() }; + let _ = self.children.note_down_inline(state); + let text = StyledText { type_: StyleType::ITALIC, range: self.position.as_range() }; Ok(ParagraphItem::Style(text)) } } -impl NoteInline for Delete { +impl NoteInline for Strong { fn note_down_inline(self, state: &mut ReadState) -> Result { - // let mut blocks = paragraph_items(self.children, state)?; - let text = StyledText { type_: StyleType::ITALIC, range: self.position.as_range() }; + let _ = self.children.note_down_inline(state); + let text = StyledText { type_: StyleType::BOLD, range: self.position.as_range() }; Ok(ParagraphItem::Style(text)) } } -impl NoteInline for Emphasis { - fn note_down_inline(self, _: &mut ReadState) -> Result { - // let mut blocks = paragraph_items(self.children, state)?; - let text = StyledText { type_: StyleType::ITALIC, range: self.position.as_range() }; +impl NoteInline for Delete { + fn note_down_inline(self, state: &mut ReadState) -> Result { + let _ = self.children.note_down_inline(state); + let text = StyledText { type_: StyleType::STRIKETHROUGH, range: self.position.as_range() }; Ok(ParagraphItem::Style(text)) } } diff --git a/projects/panduck-markdown/src/from_cmd/list.rs b/projects/panduck-markdown/src/from_cmd/list.rs index 4c7ac66..daf2535 100644 --- a/projects/panduck-markdown/src/from_cmd/list.rs +++ b/projects/panduck-markdown/src/from_cmd/list.rs @@ -23,6 +23,6 @@ impl NoteBlock for List { } fn list_item(item: markdown::mdast::ListItem, state: &mut ReadState) -> Option { - let items = root_items(item.children, state).unwrap(); + let items = item.children.note_down_block(state); Some(ListItem { level: 0, checked: item.checked, range: item.position.as_range() }) } diff --git a/projects/panduck-markdown/src/from_cmd/mod.rs b/projects/panduck-markdown/src/from_cmd/mod.rs index 4d24c95..c147229 100644 --- a/projects/panduck-markdown/src/from_cmd/mod.rs +++ b/projects/panduck-markdown/src/from_cmd/mod.rs @@ -1,16 +1,22 @@ -use crate::utils::{ root_items, GetTextRange, NoteBlock, NoteInline, NoteInlineList, NoteRoot, ReadState}; +use crate::utils::{GetTextRange, group_block, group_inline, NoteBlock, NoteBlockList, NoteInline, NoteInlineList, NoteRoot, ReadState}; use markdown::{ mdast::{ - BlockQuote, Code, Delete, Emphasis, InlineCode, InlineMath, List, Math, Node, Paragraph, Root, Strong, Table, Text, + BlockQuote, Code, Definition, Delete, Emphasis, FootnoteDefinition, FootnoteReference, Heading, Image, InlineCode, + InlineMath, Link, List, Math, MdxTextExpression, Node, Paragraph, Strong, Table, Text, Toml, Yaml, }, to_mdast, Constructs, ParseOptions, }; -use wasi_notedown::exports::notedown::core::{ - syntax_tree::{ - CodeEnvironment, CodeHighlight, ListEnvironment, ListItem, MathContent, MathDisplay, MathEnvironment, NormalText, - NotedownRoot, ParagraphBlock, ParagraphItem, RootItem, StyleType, StyledText, TableCell, TableEnvironment, TableRow, +use std::str::FromStr; +use wasi_notedown::{ + exports::notedown::core::{ + syntax_tree::{ + CodeEnvironment, CodeHighlight, CommandAction, HeadingBlock, ImageReference, LinkReference, ListEnvironment, + ListItem, MathContent, MathDisplay, MathEnvironment, NormalText, NotedownRoot, ParagraphBlock, ParagraphItem, + RootItem, StyleType, StyledText, TableCell, TableEnvironment, TableRow, + }, + types::{NotedownError, TextRange, Url}, }, - types::{NotedownError, Object, TextRange}, + UrlNative, }; mod blocks; @@ -66,21 +72,16 @@ impl MarkdownParser { mdx_jsx_text: true, thematic_break: true, }, - gfm_strikethrough_single_tilde: false, - math_text_single_dollar: false, + gfm_strikethrough_single_tilde: true, + math_text_single_dollar: true, mdx_expression_parse: None, mdx_esm_parse: None, }; let mut state = ReadState::default(); let root = match to_mdast(input, &config) { - Ok(to_mdast) => match to_mdast.note_down_root(&mut state) { - Ok(o) => o, - Err(e) => { - todo!() - } - }, + Ok(to_mdast) => to_mdast.note_down_root(&mut state), Err(e) => { - todo!() + todo!("{}", e) } }; Ok(root) @@ -88,33 +89,17 @@ impl MarkdownParser { } impl NoteRoot for Node { - fn note_down_root(self, state: &mut ReadState) -> Result { - match self { - Self::Root(node) => node.note_down_root(state), + fn note_down_root(self, state: &mut ReadState) -> NotedownRoot { + let blocks = match self { + Self::Root(node) => node.children.note_down_block(state), _ => unreachable!(), - } + }; + NotedownRoot { blocks, config: state.get_object(), path: state.get_path() } } } -impl NoteRoot for Root { - fn note_down_root(self, state: &mut ReadState) -> Result { - let blocks = root_items(self.children, state)?; - Ok(NotedownRoot { blocks, config: Object { map: vec![] }, path: None }) - } -} -impl NoteInlineList for Vec { - fn note_down_inline(self, state: &mut ReadState) -> Vec { - let mut items = Vec::with_capacity(self.len()); - for x in self { - match x.note_down_inline(state) { - Ok(o) => items.push(o), - Err(e) => state.note_error(e), - } - } - items - } -} + #[test] fn ready() { diff --git a/projects/panduck-markdown/src/from_pandoc/mod.rs b/projects/panduck-markdown/src/from_pandoc/mod.rs index ddea058..60a92c4 100644 --- a/projects/panduck-markdown/src/from_pandoc/mod.rs +++ b/projects/panduck-markdown/src/from_pandoc/mod.rs @@ -1,22 +1,24 @@ -use crate::utils::{NoteBlock, NoteInline, NoteInlineList, ReadState}; +use crate::utils::{group_block, group_inline, NoteBlock, NoteBlockList, NoteInline, NoteInlineList, ReadState}; use pandoc_ast::{Block, Inline, MathType}; use std::str::FromStr; use wasi_notedown::{ exports::notedown::core::{ syntax_tree::{ - HeadingBlock, ImageReference, LinkReference, ParagraphBlock, ParagraphItem, RootItem, StyleType, StyledText, + CodeEnvironment, CommandAction, HeadingBlock, ImageReference, LinkReference, ParagraphBlock, ParagraphItem, + RootItem, StyleType, StyledText, }, types::{NotedownError, TextRange, Url}, }, UrlNative, }; -impl NoteBlock for Vec { - fn note_down_block(self, state: &mut ReadState) -> Result { - todo!() +impl NoteBlockList for Vec { + fn note_down_block(self, state: &mut ReadState) -> Vec { + group_block(self, state) } } +#[allow(unused)] impl NoteBlock for Block { fn note_down_block(self, state: &mut ReadState) -> Result { let item = match self { @@ -24,11 +26,20 @@ impl NoteBlock for Block { unimplemented!() } Self::Para(v) => unimplemented!(), - Self::Div(_, children) => unimplemented!(), + Self::Div(attr, children) => { + unimplemented!() + }, Self::LineBlock(_) => { unimplemented!() } - Self::CodeBlock(attr, code) => unimplemented!(), + Self::CodeBlock(attr, code) => { + let code = CodeEnvironment { + action: CommandAction::Anonymous, + lines: code, + range: TextRange { head_offset: 0, tail_offset: 0 }, + }; + RootItem::Code(code) + } Self::RawBlock(_, _) => { unimplemented!() } @@ -56,14 +67,14 @@ impl NoteBlock for Block { RootItem::Heading(heading) } Self::HorizontalRule => unimplemented!(), - Self::Table(_, _, _, _, _, _) => { + Self::Table(attr, _, _, _, _, _) => { unimplemented!() } - Self::Figure(_, _, _) => { + Self::Figure(attr, cap, children) => { unimplemented!() } Self::Null => { - unimplemented!() + RootItem::Placeholder } }; Ok(item) @@ -72,14 +83,7 @@ impl NoteBlock for Block { impl NoteInlineList for Vec { fn note_down_inline(self, state: &mut ReadState) -> Vec { - let mut list = Vec::with_capacity(self.len()); - for item in self { - match item.note_down_inline(state) { - Ok(o) => list.push(o), - Err(e) => state.note_error(e), - } - } - list + group_inline(self, state) } } @@ -157,4 +161,4 @@ fn make_styled(children: Vec, state: &mut ReadState) -> ParagraphItem { let items = children.note_down_inline(state); let style = StyledText { type_: StyleType::UNDERLINE, range: TextRange { head_offset: 0, tail_offset: 0 } }; ParagraphItem::Style(style) -} \ No newline at end of file +} diff --git a/projects/panduck-markdown/src/utils/mod.rs b/projects/panduck-markdown/src/utils/mod.rs index 49038cb..a36eb4b 100644 --- a/projects/panduck-markdown/src/utils/mod.rs +++ b/projects/panduck-markdown/src/utils/mod.rs @@ -1,7 +1,7 @@ use markdown::{mdast::Node, unist::Position}; use wasi_notedown::exports::notedown::core::{ syntax_tree::{NotedownRoot, ParagraphItem, RootItem}, - types::{NotedownError, TextRange}, + types::{NotedownError, Object, TextRange, Url}, }; #[derive(Default)] @@ -22,16 +22,24 @@ impl ReadState { } } } + pub fn get_object(&mut self) -> Object { + Object { map: vec![] } + } + pub fn get_path(&self) -> Option { + None + } } pub trait NoteRoot { - fn note_down_root(self, state: &mut ReadState) -> Result; + fn note_down_root(self, state: &mut ReadState) -> NotedownRoot; } pub trait NoteBlock { fn note_down_block(self, state: &mut ReadState) -> Result; } - +pub trait NoteBlockList { + fn note_down_block(self, state: &mut ReadState) -> Vec; +} pub trait NoteInline { fn note_down_inline(self, state: &mut ReadState) -> Result; } @@ -51,17 +59,28 @@ impl GetTextRange for Option { } } } - -pub fn root_items(children: Vec, state: &mut ReadState) -> Result, NotedownError> { - let mut blocks = Vec::with_capacity(children.len()); - for x in children { +pub fn group_block(list: Vec, state: &mut ReadState) -> Vec { + let mut blocks = Vec::with_capacity(list.len()); + for x in list { match x.note_down_block(state) { Ok(o) => blocks.push(o), Err(e) => { - state.errors.push(e); + state.note_error(e); } } } - Ok(blocks) + blocks } +pub fn group_inline(list: Vec, state: &mut ReadState) -> Vec { + let mut blocks = Vec::with_capacity(list.len()); + for x in list { + match x.note_down_inline(state) { + Ok(o) => blocks.push(o), + Err(e) => { + state.note_error(e); + } + } + } + blocks +} diff --git a/projects/panduck-org-mode/Cargo.toml b/projects/panduck-org-mode/Cargo.toml index 26acafe..198d7e7 100644 --- a/projects/panduck-org-mode/Cargo.toml +++ b/projects/panduck-org-mode/Cargo.toml @@ -9,13 +9,9 @@ license = "MPL-2.0" edition = "2018" [dependencies] -toml = "0.8.12" +notedown-wasi = { workspace = true } orgize = "0.10.0-alpha.7" -[dependencies.notedown-wasi] -version = "0.0.*" -#path = 'C:\Users\Dell\CLionProjects\notedown-rs\projects\notedown-wasi' - [features] default = [] diff --git a/projects/pretty-print/Cargo.toml b/projects/pretty-print/Cargo.toml index 29614d9..117655c 100644 --- a/projects/pretty-print/Cargo.toml +++ b/projects/pretty-print/Cargo.toml @@ -10,7 +10,7 @@ license = "MPL-2.0" edition = "2021" [dependencies] -pretty = "0.11.3" +pretty = "0.12.3" [features] default = []