Implement the export snippet parser.

This commit is contained in:
Tom Alexander 2023-07-19 00:37:51 -04:00
parent 1fb8ce9af6
commit 95e033a99b
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 64 additions and 7 deletions

View File

@ -1,6 +1,19 @@
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::anychar;
use nom::combinator::opt;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::combinator::verify;
use nom::multi::many1;
use nom::multi::many_till;
use nom::sequence::tuple;
use super::Context; use super::Context;
use crate::error::Res; use crate::error::Res;
use crate::parser::util::not_yet_implemented; use crate::parser::parser_with_context::parser_with_context;
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::ExportSnippet; use crate::parser::ExportSnippet;
#[tracing::instrument(ret, level = "debug")] #[tracing::instrument(ret, level = "debug")]
@ -8,6 +21,41 @@ pub fn export_snippet<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: &'s str,
) -> Res<&'s str, ExportSnippet<'s>> { ) -> Res<&'s str, ExportSnippet<'s>> {
not_yet_implemented()?; let (remaining, _) = tag("@@")(input)?;
todo!() let (remaining, backend_name) = backend(context, remaining)?;
let (remaining, backend_contents) =
opt(tuple((tag(":"), parser_with_context!(contents)(context))))(remaining)?;
let (remaining, _) = tag("@@")(remaining)?;
let source = get_consumed(input, remaining);
Ok((
remaining,
ExportSnippet {
source,
backend: backend_name,
contents: backend_contents.map(|(_colon, backend_contents)| backend_contents),
},
))
}
#[tracing::instrument(ret, level = "debug")]
fn backend<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let (remaining, backend_name) =
recognize(many1(verify(anychar, |c| c.is_alphanumeric() || *c == '-')))(input)?;
Ok((remaining, backend_name))
}
#[tracing::instrument(ret, level = "debug")]
fn contents<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let (remaining, source) = recognize(verify(
many_till(
anychar,
peek(alt((
parser_with_context!(exit_matcher_parser)(context),
tag("@@"),
))),
),
|(children, _exit_contents)| !children.is_empty(),
))(input)?;
Ok((remaining, source))
} }

View File

@ -6,10 +6,10 @@ use nom::character::complete::line_ending;
use nom::character::complete::none_of; use nom::character::complete::none_of;
use nom::character::complete::one_of; use nom::character::complete::one_of;
use nom::character::complete::space0; use nom::character::complete::space0;
use nom::combinator::opt;
use nom::combinator::peek; use nom::combinator::peek;
use nom::combinator::recognize; use nom::combinator::recognize;
use nom::combinator::verify; use nom::combinator::verify;
use nom::multi::many0;
use nom::multi::many_till; use nom::multi::many_till;
use nom::sequence::tuple; use nom::sequence::tuple;
@ -45,7 +45,7 @@ pub fn latex_fragment<'r, 's>(
fn raw_latex_fragment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn raw_latex_fragment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let (remaining, _) = tag("\\")(input)?; let (remaining, _) = tag("\\")(input)?;
let (remaining, _) = name(context, remaining)?; let (remaining, _) = name(context, remaining)?;
let (remaining, _) = opt(parser_with_context!(brackets)(context))(remaining)?; let (remaining, _) = many0(parser_with_context!(brackets)(context))(remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok((remaining, source)) Ok((remaining, source))

View File

@ -114,7 +114,7 @@ pub struct LatexFragment<'s> {
pub struct ExportSnippet<'s> { pub struct ExportSnippet<'s> {
pub source: &'s str, pub source: &'s str,
pub backend: &'s str, pub backend: &'s str,
pub contents: &'s str, pub contents: Option<&'s str>,
} }
impl<'s> Source<'s> for Object<'s> { impl<'s> Source<'s> for Object<'s> {

View File

@ -9,6 +9,7 @@ use super::Context;
use crate::error::Res; use crate::error::Res;
use crate::parser::angle_link::angle_link; use crate::parser::angle_link::angle_link;
use crate::parser::entity::entity; use crate::parser::entity::entity;
use crate::parser::export_snippet::export_snippet;
use crate::parser::latex_fragment::latex_fragment; use crate::parser::latex_fragment::latex_fragment;
use crate::parser::object::Object; use crate::parser::object::Object;
use crate::parser::org_macro::org_macro; use crate::parser::org_macro::org_macro;
@ -22,10 +23,14 @@ pub fn standard_set_object<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: &'s str,
) -> Res<&'s str, Object<'s>> { ) -> Res<&'s str, Object<'s>> {
// TODO: export snippets, footnote references, citations (NOT citation references), inline babel calls, inline source blocks, line breaks, links, macros, targets and radio targets, statistics cookies, subscript and superscript, timestamps, and text markup. // TODO: footnote references, citations (NOT citation references), inline babel calls, inline source blocks, line breaks, links, macros, targets and radio targets, statistics cookies, subscript and superscript, timestamps, and text markup.
not(|i| context.check_exit_matcher(i))(input)?; not(|i| context.check_exit_matcher(i))(input)?;
alt(( alt((
map(
parser_with_context!(export_snippet)(context),
Object::ExportSnippet,
),
map(parser_with_context!(entity)(context), Object::Entity), map(parser_with_context!(entity)(context), Object::Entity),
map( map(
parser_with_context!(latex_fragment)(context), parser_with_context!(latex_fragment)(context),
@ -74,6 +79,10 @@ pub fn any_object_except_plain_text<'r, 's>(
) -> Res<&'s str, Object<'s>> { ) -> Res<&'s str, Object<'s>> {
// Used for exit matchers so this does not check exit matcher condition. // Used for exit matchers so this does not check exit matcher condition.
alt(( alt((
map(
parser_with_context!(export_snippet)(context),
Object::ExportSnippet,
),
map(parser_with_context!(entity)(context), Object::Entity), map(parser_with_context!(entity)(context), Object::Entity),
map( map(
parser_with_context!(latex_fragment)(context), parser_with_context!(latex_fragment)(context),