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

various renames #59

Merged
merged 2 commits into from
Jun 23, 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
210 changes: 105 additions & 105 deletions src/fmt_md.rs

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/fmt_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod tests {
use super::*;
use indoc::indoc;

use crate::tree::{Block, Inline, LeafBlock, MdqNode, ReadOptions, SpanVariant, TextVariant};
use crate::tree::{Block, Inline, LeafBlock, MdElem, ReadOptions, SpanVariant, TextVariant};
use crate::unwrap;
use markdown::ParseOptions;

Expand Down Expand Up @@ -71,8 +71,8 @@ mod tests {
#[test]
fn text_html() {
let node = markdown::to_mdast("<foo>", &ParseOptions::gfm()).unwrap();
let mdq_nodes = MdqNode::read(node, &ReadOptions::default()).unwrap();
unwrap!(&mdq_nodes[0], MdqNode::Inline(inline));
let md_elems = MdElem::read(node, &ReadOptions::default()).unwrap();
unwrap!(&md_elems[0], MdElem::Inline(inline));
VARIANTS_CHECKER.see(inline);
let actual = inlines_to_plain_string(&[inline]);
assert_eq!(&actual, "");
Expand Down Expand Up @@ -121,8 +121,8 @@ mod tests {
let mut options = ParseOptions::gfm();
options.constructs.math_text = true;
let node = markdown::to_mdast(md, &options).unwrap();
let mdq_nodes = MdqNode::read(node, &ReadOptions::default()).unwrap();
unwrap!(&mdq_nodes[0], MdqNode::Block(Block::LeafBlock(LeafBlock::Paragraph(p)))); // TODO can I use m_node here?
let md_elems = MdElem::read(node, &ReadOptions::default()).unwrap();
unwrap!(&md_elems[0], MdElem::Block(Block::LeafBlock(LeafBlock::Paragraph(p)))); // TODO can I use m_node here?
p.body.iter().for_each(|inline| VARIANTS_CHECKER.see(inline));
let actual = inlines_to_plain_string(&p.body);
assert_eq!(&actual, expect);
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::{env, io};
use crate::fmt_md::MdOptions;
use crate::output::Stream;
use crate::select::selector::MdqRefSelector;
use crate::tree::{MdqNode, ReadOptions};
use crate::tree_ref::MdqNodeRef;
use crate::tree::{MdElem, ReadOptions};
use crate::tree_ref::MdElemRef;

mod fmt_md;
mod fmt_str;
Expand All @@ -24,13 +24,13 @@ fn main() {
let mut contents = String::new();
stdin().read_to_string(&mut contents).expect("invalid input (not utf8)");
let ast = markdown::to_mdast(&mut contents, &markdown::ParseOptions::gfm()).unwrap();
let mdqs = MdqNode::read(ast, &ReadOptions::default()).unwrap();
let mdqs = MdElem::read(ast, &ReadOptions::default()).unwrap();
let mut out = output::Output::new(Stream(io::stdout()));

let selectors_str = env::args().nth(1).unwrap_or("".to_string());
let selectors = MdqRefSelector::parse(&selectors_str).expect("failed to parse selector");

let mut pipeline_nodes = MdqNodeRef::wrap_vec(&mdqs);
let mut pipeline_nodes = MdElemRef::wrap_vec(&mdqs);
for selector in selectors {
let new_pipeline = selector.find_nodes(pipeline_nodes);
pipeline_nodes = new_pipeline;
Expand Down
10 changes: 5 additions & 5 deletions src/matcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::fmt_str::inlines_to_plain_string;
use crate::parsing_iter::ParsingIterator;
use crate::select::{ParseErrorReason, ParseResult, SELECTOR_SEPARATOR};
use crate::tree::{Block, Container, Inline, LeafBlock, MdqNode};
use crate::tree::{Block, Container, Inline, LeafBlock, MdElem};
use regex::Regex;
use std::borrow::Borrow;

Expand Down Expand Up @@ -36,7 +36,7 @@ impl StringMatcher {
self.matches(&inlines_to_plain_string(haystack))
}

pub fn matches_any<N: Borrow<MdqNode>>(&self, haystacks: &[N]) -> bool {
pub fn matches_any<N: Borrow<MdElem>>(&self, haystacks: &[N]) -> bool {
if matches!(self, StringMatcher::Any) {
return true;
}
Expand Down Expand Up @@ -68,10 +68,10 @@ impl StringMatcher {
}
}

fn matches_node(&self, node: &MdqNode) -> bool {
fn matches_node(&self, node: &MdElem) -> bool {
match node {
MdqNode::Block(block) => self.matches_block(block),
MdqNode::Inline(inline) => self.matches_inlines(&[inline]),
MdElem::Block(block) => self.matches_block(block),
MdElem::Inline(inline) => self.matches_inlines(&[inline]),
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/select/interface.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::parse_common::Position;
use crate::tree::MdqNode;
use crate::tree_ref::MdqNodeRef;
use crate::tree::MdElem;
use crate::tree_ref::MdElemRef;

pub enum SelectResult<'a> {
One(MdqNodeRef<'a>),
Multi(&'a Vec<MdqNode>),
One(MdElemRef<'a>),
Multi(&'a Vec<MdElem>),
}

pub type ParseResult<T> = Result<T, ParseErrorReason>;
Expand Down
4 changes: 2 additions & 2 deletions src/select/list_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::matcher::StringMatcher;
use crate::parsing_iter::ParsingIterator;
use crate::select::base::Selector;
use crate::select::{ParseErrorReason, ParseResult, SelectResult};
use crate::tree_ref::{ListItemRef, MdqNodeRef};
use crate::tree_ref::{ListItemRef, MdElemRef};

#[derive(Debug, PartialEq)]
pub struct ListItemSelector {
Expand Down Expand Up @@ -78,7 +78,7 @@ impl<'a> Selector<'a, ListItemRef<'a>> for ListItemSelector {
}

fn pick(&self, item: ListItemRef<'a>) -> SelectResult<'a> {
SelectResult::One(MdqNodeRef::ListItem(item))
SelectResult::One(MdElemRef::ListItem(item))
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/select/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::select::interface::{ParseError, ParseErrorReason, ParseResult, Select
use crate::select::list_item::{ListItemSelector, ListItemType};
use crate::select::section::SectionSelector;
use crate::tree::Inline;
use crate::tree_ref::{ListItemRef, MdqNodeRef, NonSelectable};
use crate::tree_ref::{ListItemRef, MdElemRef, NonSelectable};
use crate::wrap_mdq_refs;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -56,18 +56,18 @@ impl MdqRefSelector {
Ok(selectors)
}

pub fn find_nodes<'a>(&self, nodes: Vec<MdqNodeRef<'a>>) -> Vec<MdqNodeRef<'a>> {
pub fn find_nodes<'a>(&self, nodes: Vec<MdElemRef<'a>>) -> Vec<MdElemRef<'a>> {
let mut result = Vec::with_capacity(8); // arbitrary guess
for node in nodes {
self.build_output(&mut result, node);
}
result
}

fn build_output<'a>(&self, out: &mut Vec<MdqNodeRef<'a>>, node: MdqNodeRef<'a>) {
fn build_output<'a>(&self, out: &mut Vec<MdElemRef<'a>>, node: MdElemRef<'a>) {
let result = match (self, node.clone()) {
(MdqRefSelector::Section(selector), MdqNodeRef::Section(header)) => selector.try_select(&header),
(MdqRefSelector::ListItem(selector), MdqNodeRef::ListItem(item)) => selector.try_select(item),
(MdqRefSelector::Section(selector), MdElemRef::Section(header)) => selector.try_select(&header),
(MdqRefSelector::ListItem(selector), MdElemRef::ListItem(item)) => selector.try_select(item),
_ => None,
};
match result {
Expand All @@ -87,27 +87,27 @@ impl MdqRefSelector {
/// selector-specific. For example, an [MdqNode::Section] has child nodes both in its title and in its body, but
/// only the body nodes are relevant for select recursion. `MdqNode` shouldn't need to know about that oddity; it
/// belongs here.
fn find_children<'a>(node: MdqNodeRef) -> Vec<MdqNodeRef> {
fn find_children<'a>(node: MdElemRef) -> Vec<MdElemRef> {
match node {
MdqNodeRef::Section(s) => MdqNodeRef::wrap_vec(&s.body),
MdqNodeRef::ListItem(ListItemRef(_, item)) => MdqNodeRef::wrap_vec(&item.item),
MdqNodeRef::Inline(inline) => match inline {
Inline::Span { children, .. } => children.iter().map(|child| MdqNodeRef::Inline(child)).collect(),
Inline::Footnote(footnote) => MdqNodeRef::wrap_vec(&footnote.text),
MdElemRef::Section(s) => MdElemRef::wrap_vec(&s.body),
MdElemRef::ListItem(ListItemRef(_, item)) => MdElemRef::wrap_vec(&item.item),
MdElemRef::Inline(inline) => match inline {
Inline::Span { children, .. } => children.iter().map(|child| MdElemRef::Inline(child)).collect(),
Inline::Footnote(footnote) => MdElemRef::wrap_vec(&footnote.text),
Inline::Link { .. } => {
// TODO need to return an MdqNodeRef::Link
Vec::new()
}
Inline::Text { .. } | Inline::Image { .. } => Vec::new(),
},
MdqNodeRef::NonSelectable(elem) => match elem {
MdElemRef::NonSelectable(elem) => match elem {
NonSelectable::Paragraph(p) => wrap_mdq_refs!(Inline: &p.body),
NonSelectable::BlockQuote(b) => MdqNodeRef::wrap_vec(&b.body),
NonSelectable::BlockQuote(b) => MdElemRef::wrap_vec(&b.body),
NonSelectable::List(list) => {
let mut idx = list.starting_index;
let mut result = Vec::with_capacity(list.items.len());
for item in &list.items {
result.push(MdqNodeRef::ListItem(ListItemRef(idx.clone(), item)));
result.push(MdElemRef::ListItem(ListItemRef(idx.clone(), item)));
if let Some(idx) = idx.as_mut() {
*idx += 1;
}
Expand All @@ -120,7 +120,7 @@ impl MdqRefSelector {
for row in &table.rows {
for col in row {
for cell in col {
result.push(MdqNodeRef::Inline(cell));
result.push(MdElemRef::Inline(cell));
}
}
}
Expand Down
Loading