Skip to content

Commit

Permalink
Supports recursive parsing of markdown tables
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Apr 2, 2024
1 parent a1d5c4e commit 706eb5f
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ impl NoteBlock for Node {
Node::Break(_) => {
todo!()
}
Node::InlineCode(_) => {
todo!()
}
Node::InlineMath(_) => {
todo!()
}
Node::Delete(_) => {
todo!()
}
Expand Down Expand Up @@ -59,9 +53,6 @@ impl NoteBlock for Node {
Node::LinkReference(_) => {
todo!()
}
Node::Strong(_) => {
todo!()
}
Node::Text(_) => {
todo!()
}
Expand All @@ -75,16 +66,10 @@ impl NoteBlock for Node {
Node::Heading(_) => {
todo!()
}

Node::ThematicBreak(_) => {
todo!()
}
Node::Table(table) => table.note_down_block(state),
Node::TableRow(table) => table.note_down_block(state),
Node::TableCell(table) => table.note_down_block(state),
Node::ListItem(_) => {
todo!()
}
Node::Definition(_) => {
todo!()
}
Expand All @@ -100,7 +85,7 @@ impl NoteBlock for Paragraph {
match x.note_down_inline(state) {
Ok(o) => blocks.push(o),
Err(e) => {
state.errors.push(e);
state.note_error(e);
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions projects/panduck-markdown/src/reader/html.rs
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
use super::*;
use comrak::nodes::NodeHtmlBlock;

impl ToNotedown for NodeHtmlBlock {
fn into_notedown(self) -> ASTNode {
let html = String::from_utf8_lossy(&self.literal);
ASTKind::code_block(html, "html", None)
}
}
15 changes: 3 additions & 12 deletions projects/panduck-markdown/src/reader/inline.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use wasi_notedown::exports::notedown::core::syntax_tree::TableRow;
use super::*;

impl NoteInline for Node {
Expand Down Expand Up @@ -135,6 +136,8 @@ impl NoteInline for InlineCode {
}
}



impl NoteInline for InlineMath {
fn note_down_inline(self, _: &mut ReadState) -> Result<ParagraphItem, NotedownError> {
let content = MathContent::Tex(self.value);
Expand All @@ -143,15 +146,3 @@ impl NoteInline for InlineMath {
}
}

fn paragraph_items(children: Vec<Node>, state: &mut ReadState) -> Result<Vec<ParagraphItem>, NotedownError> {
let mut items = Vec::with_capacity(children.len());
for x in children {
match x.note_down_inline(state) {
Ok(o) => items.push(o),
Err(e) => {
state.errors.push(e);
}
}
}
Ok(items)
}
26 changes: 13 additions & 13 deletions projects/panduck-markdown/src/reader/list.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use super::*;
use markdown::mdast::BlockQuote;
use wasi_notedown::exports::notedown::core::syntax_tree::{ListEnvironment, ListItem};

impl NoteBlock for List {
impl NoteBlock for BlockQuote {
fn note_down_block(self, state: &mut ReadState) -> Result<RootItem, NotedownError> {
let list = ListEnvironment { items: vec![], range: self.position.as_range() };

Ok(RootItem::List(list))
}
}

impl NoteBlock for List {
fn note_down_block(self, state: &mut ReadState) -> Result<RootItem, NotedownError> {
let mut items = Vec::with_capacity(self.children.len());
for x in self.children {
match x {
Node::ListItem(v) => {
let items = root_items(v.children, state)?;
ListItem { level: 0, checked: v.checked, range: v.position.as_range() };
}
Node::ListItem(v) => items.extend(list_item(v, state)),
_ => unreachable!(),
};
}
let list = ListEnvironment { items, range: self.position.as_range() };
Ok(RootItem::List(list))
}
}

impl NoteBlock for BlockQuote {
fn note_down_block(self, state: &mut ReadState) -> Result<RootItem, NotedownError> {
let list = ListEnvironment { items: vec![], range: self.position.as_range() };

Ok(RootItem::List(list))
}
fn list_item(item: markdown::mdast::ListItem, state: &mut ReadState) -> Option<ListItem> {
let items = root_items(item.children, state).unwrap();
Some(ListItem { level: 0, checked: item.checked, range: item.position.as_range() })
}
27 changes: 9 additions & 18 deletions projects/panduck-markdown/src/reader/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use crate::utils::{GetTextRange, NoteBlock, NoteInline, NoteRoot, ReadState};
use crate::utils::{paragraph_items, root_items, GetTextRange, NoteBlock, NoteInline, NoteRoot, ReadState};
use markdown::{
mdast::{Code, Delete, Emphasis, InlineCode, InlineMath, List, Math, Node, Paragraph, Root, Strong, Table, Text},
mdast::{
BlockQuote, Code, Delete, Emphasis, InlineCode, InlineMath, List, Math, Node, Paragraph, Root, Strong, Table, Text,
},
to_mdast, Constructs, ParseOptions,
};
use wasi_notedown::exports::notedown::core::{
syntax_tree::{
CodeAction, CodeEnvironment, CodeHighlight, MathContent, MathDisplay, MathEnvironment, NormalText, NotedownRoot,
ParagraphBlock, ParagraphItem, RootItem, StyleType, StyledText,
CodeAction, CodeEnvironment, CodeHighlight, ListEnvironment, ListItem, MathContent, MathDisplay, MathEnvironment,
NormalText, NotedownRoot, ParagraphBlock, ParagraphItem, RootItem, StyleType, StyledText, TableCell, TableEnvironment,
TableRow,
},
types::{NotedownError, TextRange},
};

mod code;
mod blocks;
mod html;
mod inline;
mod list;
mod table;
Expand Down Expand Up @@ -100,19 +104,6 @@ impl NoteRoot for Root {
}
}

fn root_items(children: Vec<Node>, state: &mut ReadState) -> Result<Vec<RootItem>, NotedownError> {
let mut blocks = Vec::with_capacity(children.len());
for x in children {
match x.note_down_block(state) {
Ok(o) => blocks.push(o),
Err(e) => {
state.errors.push(e);
}
}
}
Ok(blocks)
}

#[test]
fn ready() {
println!("it works!")
Expand Down
36 changes: 19 additions & 17 deletions projects/panduck-markdown/src/reader/table.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
use markdown::mdast::{TableCell, TableRow};
use super::*;
use wasi_notedown::exports::notedown::core::syntax_tree::TableEnvironment;

impl NoteBlock for Table {
fn note_down_block(self, state: &mut ReadState) -> Result<RootItem, NotedownError> {
let items = root_items(self.children, state)?;


let table = TableEnvironment { rows: vec![], range: self.position.as_range() };
let mut rows = Vec::with_capacity(self.children.len());
for x in self.children {
match x {
Node::TableRow(x) => rows.extend(table_row(x, state)),
_ => unreachable!(),
}
}
let table = TableEnvironment { rows, range: self.position.as_range() };
Ok(RootItem::Table(table))
}
}

impl NoteBlock for TableRow {
fn note_down_block(self, state: &mut ReadState) -> Result<RootItem, NotedownError> {
let items = root_items(self.children, state)?;
let table = TableEnvironment { rows: vec![], range: self.position.as_range() };
Ok(RootItem::Table(table))
fn table_row(row: markdown::mdast::TableRow, state: &mut ReadState) -> Option<TableRow> {
let mut cells = Vec::with_capacity(row.children.len());
for x in row.children {
match x {
Node::TableCell(x) => cells.extend(table_cell(x, state)),
_ => unreachable!(),
}
}
Some(TableRow { cells, range: row.position.as_range() })
}

impl NoteBlock for TableCell {
fn note_down_block(self, state: &mut ReadState) -> Result<RootItem, NotedownError> {
let items = root_items(self.children, state)?;
let table = TableEnvironment { rows: vec![], range: self.position.as_range() };
Ok(RootItem::Table(table))
}
fn table_cell(cell: markdown::mdast::TableCell, state: &mut ReadState) -> Option<TableCell> {
let items = paragraph_items(cell.children, state).unwrap();
Some(TableCell { range: cell.position.as_range() })
}
62 changes: 47 additions & 15 deletions projects/panduck-markdown/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
use markdown::unist::Position;
use wasi_notedown::exports::notedown::core::syntax_tree::{NotedownRoot, ParagraphItem, RootItem};
use wasi_notedown::exports::notedown::core::types::{NotedownError, TextRange};
use markdown::{mdast::Node, unist::Position};
use wasi_notedown::exports::notedown::core::{
syntax_tree::{NotedownRoot, ParagraphItem, RootItem},
types::{NotedownError, TextRange},
};

#[derive(Default)]
pub struct ReadState {
pub errors: Vec<NotedownError>,
errors: Vec<NotedownError>,
}

impl ReadState {
pub fn note_error(&mut self, e: NotedownError) {
self.errors.push(e)
}
pub fn take_errors<T>(&mut self, r: Result<T, NotedownError>) -> Option<T> {
match r {
Ok(o) => Some(o),
Err(e) => {
self.note_error(e);
None
}
}
}
}

pub trait NoteRoot {
Expand All @@ -26,18 +43,33 @@ pub trait GetTextRange {
impl GetTextRange for Option<Position> {
fn as_range(&self) -> TextRange {
match self {
Some(s) => {
TextRange {
head_offset: s.start.offset as u32,
tail_offset: s.end.offset as u32,
}
Some(s) => TextRange { head_offset: s.start.offset as u32, tail_offset: s.end.offset as u32 },
None => TextRange { head_offset: 0, tail_offset: 0 },
}
}
}

pub fn root_items(children: Vec<Node>, state: &mut ReadState) -> Result<Vec<RootItem>, NotedownError> {
let mut blocks = Vec::with_capacity(children.len());
for x in children {
match x.note_down_block(state) {
Ok(o) => blocks.push(o),
Err(e) => {
state.errors.push(e);
}
None => {
TextRange {
head_offset: 0,
tail_offset: 0,
}
}
}
Ok(blocks)
}
pub fn paragraph_items(children: Vec<Node>, state: &mut ReadState) -> Result<Vec<ParagraphItem>, NotedownError> {
let mut items = Vec::with_capacity(children.len());
for x in children {
match x.note_down_inline(state) {
Ok(o) => items.push(o),
Err(e) => {
state.errors.push(e);
}
}
}
}
Ok(items)
}

0 comments on commit 706eb5f

Please sign in to comment.