Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract MdqNode states to separate structs #24

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/fmt_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Borrow;
use markdown::mdast::AlignKind;
use serde_json::{json, Map, Value};

use crate::tree::{CodeOpts, CodeVariant, Inline, Link, MdqNode};
use crate::tree::*;

const BODY_KEY: &str = "body";

Expand All @@ -17,21 +17,21 @@ where

pub fn node_to_json<R: InlineResolver>(node: &MdqNode) -> Value {
match node {
MdqNode::Root { body } => to_jsons::<R>(body),
MdqNode::Header {
MdqNode::Root(Root { body }) => to_jsons::<R>(body),
MdqNode::Header(Header {
depth,
title,
body: contents,
} => json!({
}) => json!({
"header": json!({
"title": R::inlines_to_value(title),
"depth": json!(depth),
BODY_KEY: to_jsons::<R>(contents),
})
}),
MdqNode::Paragraph { body } => json!({"paragraph": R::inlines_to_value(body)}),
MdqNode::BlockQuote { body } => json!({"block_quote": to_jsons::<R>(body)}),
MdqNode::List { starting_index, items } => {
MdqNode::Paragraph(Paragraph { body }) => json!({"paragraph": R::inlines_to_value(body)}),
MdqNode::BlockQuote(BlockQuote { body }) => json!({"block_quote": to_jsons::<R>(body)}),
MdqNode::List(List { starting_index, items }) => {
let mut as_map = Map::new();
match starting_index {
Some(start_idx) => {
Expand All @@ -57,10 +57,10 @@ pub fn node_to_json<R: InlineResolver>(node: &MdqNode) -> Value {

json!({"list": Value::Object(as_map)})
}
MdqNode::Table {
MdqNode::Table(Table {
alignments: align,
rows,
} => {
}) => {
let aligns: Vec<Option<&str>> = align
.iter()
.map(|a| match a {
Expand All @@ -84,7 +84,7 @@ pub fn node_to_json<R: InlineResolver>(node: &MdqNode) -> Value {
MdqNode::ThematicBreak => {
json!({"thematic_break": Value::Null})
}
MdqNode::CodeBlock { variant, value } => match variant {
MdqNode::CodeBlock(CodeBlock { variant, value }) => match variant {
CodeVariant::Code(opts) => {
let (lang, meta) = match opts {
None => (None, None),
Expand Down
16 changes: 8 additions & 8 deletions src/fmt_md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::Write;

use crate::output::{Block, Output};
use crate::str_utils::{pad_to, standard_align};
use crate::tree::{CodeVariant, Footnote, Inline, InlineVariant, Link, LinkReference, MdqNode, SpanVariant};
use crate::tree::*;

#[derive(Default)]
pub struct MdOptions {
Expand Down Expand Up @@ -129,8 +129,8 @@ impl<'a> MdWriterState<'a> {
W: Write,
{
match node {
MdqNode::Root { body } => self.write_md(out, body),
MdqNode::Header { depth, title, body } => {
MdqNode::Root(Root { body }) => self.write_md(out, body),
MdqNode::Header(Header { depth, title, body }) => {
out.with_block(Block::Plain, |out| {
for _ in 0..*depth {
out.write_str("#");
Expand All @@ -149,17 +149,17 @@ impl<'a> MdWriterState<'a> {
self.write_footnote_definitions(out);
}
}
MdqNode::Paragraph { body } => {
MdqNode::Paragraph(Paragraph { body }) => {
out.with_block(Block::Plain, |out| {
self.write_line(out, body);
});
}
MdqNode::BlockQuote { body } => {
MdqNode::BlockQuote(BlockQuote { body }) => {
out.with_block(Block::Quote, |out| {
self.write_md(out, body);
});
}
MdqNode::List { starting_index, items } => {
MdqNode::List(List { starting_index, items }) => {
out.with_block(Block::Plain, |out| {
let mut index = starting_index.clone();
let mut prefix = String::with_capacity(8); // enough for "12. [ ] "
Expand All @@ -184,7 +184,7 @@ impl<'a> MdWriterState<'a> {
}
});
}
MdqNode::Table { alignments, rows } => {
MdqNode::Table(Table { alignments, rows }) => {
let mut row_strs = Vec::with_capacity(rows.len());

let mut column_widths = [0].repeat(alignments.len());
Expand Down Expand Up @@ -277,7 +277,7 @@ impl<'a> MdWriterState<'a> {
MdqNode::ThematicBreak => {
// out.with_block(Block::Plain, |out| out.write_str("***"));
}
MdqNode::CodeBlock { variant, value } => {
MdqNode::CodeBlock(CodeBlock { variant, value }) => {
let (surround, meta) = match variant {
CodeVariant::Code(opts) => {
let meta = if let Some(opts) = opts {
Expand Down
16 changes: 8 additions & 8 deletions src/select.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::fmt_json;
use crate::tree::{CodeVariant, Inline, MdqNode};
use crate::tree::*;

#[allow(dead_code)]
pub enum Selector {
Expand Down Expand Up @@ -66,8 +66,8 @@ impl Selector {
}

let result = match node {
MdqNode::Root { body } => SelectResult::Recurse(body),
MdqNode::Header { title, body, .. } => {
MdqNode::Root(Root { body }) => SelectResult::Recurse(body),
MdqNode::Header(Header { title, body, .. }) => {
if let Selector::Heading(matcher) = self {
if matcher.matches(&Self::line_to_string(title)) {
SelectResult::Found(body.iter().map(|elem| elem).collect())
Expand All @@ -78,11 +78,11 @@ impl Selector {
SelectResult::Recurse(body)
}
}
MdqNode::Paragraph { .. } => {
MdqNode::Paragraph(Paragraph { .. }) => {
SelectResult::None // see TODO on Selector
}
MdqNode::BlockQuote { body } => SelectResult::Recurse(body),
MdqNode::List { starting_index, items } => {
MdqNode::BlockQuote(BlockQuote { body }) => SelectResult::Recurse(body),
MdqNode::List(List { starting_index, items }) => {
let _is_ordered = starting_index.is_some(); // TODO use in selected
SelectResult::RecurseOwned(
items
Expand All @@ -94,13 +94,13 @@ impl Selector {
.collect(),
)
}
MdqNode::Table { .. } => {
MdqNode::Table(Table { .. }) => {
SelectResult::None // TODO need to recurse
}
MdqNode::ThematicBreak => {
SelectResult::None // can't be selected, doesn't have children
}
MdqNode::CodeBlock { variant, value } => {
MdqNode::CodeBlock(CodeBlock { variant, value }) => {
let matched = match (self, variant) {
(Selector::CodeBlock(matcher), CodeVariant::Code(_)) => matcher.matches(value),
(_, _) => false,
Expand Down
Loading