use crate::md_elem::elem::FrontMatterVariant;
use crate::md_elem::{MdContext, MdDoc, MdElem};
use crate::query::ParseError;
use crate::select::{MatchReplace, Result, SelectorAdapter};
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ListItemTask {
Selected,
Unselected,
Either,
None,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ListItemMatcher {
pub ordered: bool,
pub task: ListItemTask,
pub matcher: MatchReplace,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SectionMatcher {
pub title: MatchReplace,
pub level_min: Option<u8>,
pub level_max: Option<u8>,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LinklikeMatcher {
pub display_matcher: MatchReplace,
pub url_matcher: MatchReplace,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BlockQuoteMatcher {
pub text: MatchReplace,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct HtmlMatcher {
pub html: MatchReplace,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ParagraphMatcher {
pub text: MatchReplace,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CodeBlockMatcher {
pub language: MatchReplace,
pub contents: MatchReplace,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FrontMatterMatcher {
pub variant: Option<FrontMatterVariant>,
pub text: MatchReplace,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TableMatcher {
pub headers: MatchReplace,
pub rows: MatchReplace,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Selector {
Chain(Vec<Self>),
Section(SectionMatcher),
ListItem(ListItemMatcher),
Link(LinklikeMatcher),
Image(LinklikeMatcher),
BlockQuote(BlockQuoteMatcher),
CodeBlock(CodeBlockMatcher),
FrontMatter(FrontMatterMatcher),
Html(HtmlMatcher),
Paragraph(ParagraphMatcher),
Table(TableMatcher),
}
impl Selector {
pub fn find_nodes(self, doc: MdDoc) -> Result<(Vec<MdElem>, MdContext)> {
let MdDoc { ctx, roots } = doc;
let result_elems = SelectorAdapter::from(self).find_nodes(&ctx, vec![MdElem::Doc(roots)])?;
Ok((result_elems, ctx))
}
}
impl TryFrom<&'_ str> for Selector {
type Error = ParseError;
fn try_from(value: &'_ str) -> std::result::Result<Self, Self::Error> {
Selector::try_parse(value).map_err(ParseError::new)
}
}
impl TryFrom<&'_ String> for Selector {
type Error = ParseError;
fn try_from(value: &'_ String) -> std::result::Result<Self, Self::Error> {
Selector::try_from(value.as_str())
}
}