Create structure for LaTeX fragments.

This commit is contained in:
Tom Alexander 2023-07-18 20:51:06 -04:00
parent 11e76814f4
commit 3ab0dd4531
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 96 additions and 2 deletions

View File

@ -24,6 +24,7 @@ use crate::parser::HorizontalRule;
use crate::parser::Italic;
use crate::parser::Keyword;
use crate::parser::LatexEnvironment;
use crate::parser::LatexFragment;
use crate::parser::Object;
use crate::parser::OrgMacro;
use crate::parser::Paragraph;
@ -156,6 +157,7 @@ fn compare_object<'s>(
Object::AngleLink(obj) => compare_angle_link(source, emacs, obj),
Object::OrgMacro(obj) => compare_org_macro(source, emacs, obj),
Object::Entity(obj) => compare_entity(source, emacs, obj),
Object::LatexFragment(obj) => compare_latex_fragment(source, emacs, obj),
}
}
@ -1263,3 +1265,26 @@ fn compare_entity<'s>(
children: Vec::new(),
})
}
fn compare_latex_fragment<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s LatexFragment<'s>,
) -> Result<DiffResult, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good;
let emacs_name = "latex-fragment";
if assert_name(emacs, emacs_name).is_err() {
this_status = DiffStatus::Bad;
}
if assert_bounds(source, emacs, rust).is_err() {
this_status = DiffStatus::Bad;
}
Ok(DiffResult {
status: this_status,
name: emacs_name.to_owned(),
message: None,
children: Vec::new(),
})
}

View File

@ -0,0 +1,39 @@
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::tag_no_case;
use nom::character::complete::satisfy;
use nom::character::complete::space0;
use nom::combinator::eof;
use nom::combinator::peek;
use nom::combinator::recognize;
use super::Context;
use crate::error::Res;
use crate::parser::object::Entity;
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::util::get_consumed;
use crate::parser::LatexFragment;
#[tracing::instrument(ret, level = "debug")]
pub fn latex_fragment<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, LatexFragment<'s>> {
let (remaining, _) = tag("\\")(input)?;
todo!()
// let (remaining, entity_name) = name(context, remaining)?;
// let (remaining, _) = alt((
// tag("{}"),
// peek(recognize(parser_with_context!(entity_end)(context))),
// ))(remaining)?;
// let (remaining, _) = space0(remaining)?;
// let source = get_consumed(input, remaining);
// Ok((
// remaining,
// Entity {
// source,
// entity_name,
// },
// ))
}

View File

@ -16,6 +16,7 @@ mod greater_element;
mod horizontal_rule;
mod keyword;
mod latex_environment;
mod latex_fragment;
mod lesser_block;
mod lesser_element;
mod list;
@ -73,6 +74,7 @@ pub use object::Bold;
pub use object::Code;
pub use object::Entity;
pub use object::Italic;
pub use object::LatexFragment;
pub use object::Object;
pub use object::OrgMacro;
pub use object::PlainLink;

View File

@ -16,6 +16,7 @@ pub enum Object<'s> {
AngleLink(AngleLink<'s>),
OrgMacro(OrgMacro<'s>),
Entity(Entity<'s>),
LatexFragment(LatexFragment<'s>),
}
#[derive(Debug, PartialEq)]
@ -103,6 +104,12 @@ pub struct Entity<'s> {
pub entity_name: &'s str,
}
#[derive(Debug, PartialEq)]
pub struct LatexFragment<'s> {
pub source: &'s str,
pub entity_name: &'s str,
}
impl<'s> Source<'s> for Object<'s> {
fn get_source(&'s self) -> &'s str {
match self {
@ -120,6 +127,7 @@ impl<'s> Source<'s> for Object<'s> {
Object::AngleLink(obj) => obj.source,
Object::OrgMacro(obj) => obj.source,
Object::Entity(obj) => obj.source,
Object::LatexFragment(obj) => obj.source,
}
}
}
@ -201,3 +209,9 @@ impl<'s> Source<'s> for Entity<'s> {
self.source
}
}
impl<'s> Source<'s> for LatexFragment<'s> {
fn get_source(&'s self) -> &'s str {
self.source
}
}

View File

@ -9,6 +9,7 @@ use super::Context;
use crate::error::Res;
use crate::parser::angle_link::angle_link;
use crate::parser::entity::entity;
use crate::parser::latex_fragment::latex_fragment;
use crate::parser::object::Object;
use crate::parser::org_macro::org_macro;
use crate::parser::plain_link::plain_link;
@ -21,11 +22,15 @@ pub fn standard_set_object<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, Object<'s>> {
// TODO: LaTeX fragments, 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: 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.
not(|i| context.check_exit_matcher(i))(input)?;
alt((
map(parser_with_context!(entity)(context), Object::Entity),
map(
parser_with_context!(latex_fragment)(context),
Object::LatexFragment,
),
map(parser_with_context!(radio_link)(context), Object::RadioLink),
map(
parser_with_context!(radio_target)(context),
@ -48,11 +53,15 @@ pub fn minimal_set_object<'r, 's>(
context: Context<'r, 's>,
input: &'s str,
) -> Res<&'s str, Object<'s>> {
// TODO: LaTeX fragments, superscripts and subscripts
// TODO: superscripts and subscripts
not(|i| context.check_exit_matcher(i))(input)?;
alt((
map(parser_with_context!(entity)(context), Object::Entity),
map(
parser_with_context!(latex_fragment)(context),
Object::LatexFragment,
),
parser_with_context!(text_markup)(context),
map(parser_with_context!(plain_text)(context), Object::PlainText),
))(input)
@ -66,6 +75,10 @@ pub fn any_object_except_plain_text<'r, 's>(
// Used for exit matchers so this does not check exit matcher condition.
alt((
map(parser_with_context!(entity)(context), Object::Entity),
map(
parser_with_context!(latex_fragment)(context),
Object::LatexFragment,
),
map(parser_with_context!(radio_link)(context), Object::RadioLink),
map(
parser_with_context!(radio_target)(context),

View File

@ -53,6 +53,7 @@ impl<'r, 's> Token<'r, 's> {
Object::AngleLink(_) => Box::new(std::iter::empty()),
Object::OrgMacro(_) => Box::new(std::iter::empty()),
Object::Entity(_) => Box::new(std::iter::empty()),
Object::LatexFragment(_) => Box::new(std::iter::empty()),
},
Token::Element(elem) => match elem {
Element::Paragraph(inner) => Box::new(inner.children.iter().map(Token::Object)),