Merge branch 'inline_source_block'
This commit is contained in:
commit
fb83e8d453
2
org_mode_samples/inline_source_block/simple.org
Normal file
2
org_mode_samples/inline_source_block/simple.org
Normal file
@ -0,0 +1,2 @@
|
||||
before src_foo{ipsum} after
|
||||
src_bar[lorem]{ipsum}
|
@ -26,6 +26,7 @@ use crate::parser::GreaterBlock;
|
||||
use crate::parser::Heading;
|
||||
use crate::parser::HorizontalRule;
|
||||
use crate::parser::InlineBabelCall;
|
||||
use crate::parser::InlineSourceBlock;
|
||||
use crate::parser::Italic;
|
||||
use crate::parser::Keyword;
|
||||
use crate::parser::LatexEnvironment;
|
||||
@ -168,6 +169,7 @@ fn compare_object<'s>(
|
||||
Object::Citation(obj) => compare_citation(source, emacs, obj),
|
||||
Object::CitationReference(obj) => compare_citation_reference(source, emacs, obj),
|
||||
Object::InlineBabelCall(obj) => compare_inline_babel_call(source, emacs, obj),
|
||||
Object::InlineSourceBlock(obj) => compare_inline_source_block(source, emacs, obj),
|
||||
}
|
||||
}
|
||||
|
||||
@ -1413,3 +1415,26 @@ fn compare_inline_babel_call<'s>(
|
||||
children: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn compare_inline_source_block<'s>(
|
||||
source: &'s str,
|
||||
emacs: &'s Token<'s>,
|
||||
rust: &'s InlineSourceBlock<'s>,
|
||||
) -> Result<DiffResult, Box<dyn std::error::Error>> {
|
||||
let mut this_status = DiffStatus::Good;
|
||||
let emacs_name = "inline-src-block";
|
||||
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(),
|
||||
})
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ fn header<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s
|
||||
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::BabelHeaderBracket(BabelHeaderBracket {
|
||||
position: input,
|
||||
position: remaining,
|
||||
depth: 0,
|
||||
}))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
@ -80,7 +80,7 @@ fn header<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn header_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let context_depth = get_bracket_depth(context)
|
||||
.expect("This function should only be called from inside a inline babel call header.");
|
||||
.expect("This function should only be called from inside an inline babel call header.");
|
||||
let text_since_context_entry = get_consumed(context_depth.position, input);
|
||||
let mut current_depth = context_depth.depth;
|
||||
for c in text_since_context_entry.chars() {
|
||||
@ -106,7 +106,7 @@ fn argument<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'
|
||||
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::BabelHeaderBracket(BabelHeaderBracket {
|
||||
position: input,
|
||||
position: remaining,
|
||||
depth: 0,
|
||||
}))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
@ -125,7 +125,7 @@ fn argument<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn argument_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let context_depth = get_bracket_depth(context)
|
||||
.expect("This function should only be called from inside a inline babel call argument.");
|
||||
.expect("This function should only be called from inside an inline babel call argument.");
|
||||
let text_since_context_entry = get_consumed(context_depth.position, input);
|
||||
let mut current_depth = context_depth.depth;
|
||||
for c in text_since_context_entry.chars() {
|
||||
|
195
src/parser/inline_source_block.rs
Normal file
195
src/parser/inline_source_block.rs
Normal file
@ -0,0 +1,195 @@
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::bytes::complete::tag_no_case;
|
||||
use nom::character::complete::anychar;
|
||||
use nom::character::complete::line_ending;
|
||||
use nom::character::complete::one_of;
|
||||
use nom::character::complete::space0;
|
||||
use nom::combinator::opt;
|
||||
use nom::combinator::recognize;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many_till;
|
||||
use tracing::span;
|
||||
|
||||
use super::Context;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_context::InlineSourceBlockBracket;
|
||||
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::InlineSourceBlock;
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
pub fn inline_source_block<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, InlineSourceBlock<'s>> {
|
||||
let (remaining, _) = tag_no_case("src_")(input)?;
|
||||
let (remaining, _) = lang(context, remaining)?;
|
||||
let (remaining, _header1) = opt(parser_with_context!(header)(context))(remaining)?;
|
||||
let (remaining, _body) = body(context, remaining)?;
|
||||
let (remaining, _) = space0(remaining)?;
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((remaining, InlineSourceBlock { source }))
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn lang<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &lang_end,
|
||||
}));
|
||||
let (remaining, lang) = recognize(many_till(
|
||||
verify(anychar, |c| !(c.is_whitespace() || "[{".contains(*c))),
|
||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
||||
))(input)?;
|
||||
Ok((remaining, lang))
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn lang_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
recognize(one_of("[{"))(input)
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn header<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let (remaining, _) = tag("[")(input)?;
|
||||
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::InlineSourceBlockBracket(
|
||||
InlineSourceBlockBracket {
|
||||
position: remaining,
|
||||
depth: 0,
|
||||
},
|
||||
))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &header_end,
|
||||
}));
|
||||
|
||||
let (remaining, header_contents) = recognize(many_till(
|
||||
anychar,
|
||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
||||
))(remaining)?;
|
||||
let (remaining, _) = tag("]")(remaining)?;
|
||||
Ok((remaining, header_contents))
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn header_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let context_depth = get_bracket_depth(context)
|
||||
.expect("This function should only be called from inside an inline source block header.");
|
||||
let text_since_context_entry = get_consumed(context_depth.position, input);
|
||||
let mut current_depth = context_depth.depth;
|
||||
for c in text_since_context_entry.chars() {
|
||||
match c {
|
||||
'[' => {
|
||||
current_depth += 1;
|
||||
}
|
||||
']' if current_depth == 0 => {
|
||||
panic!("Exceeded inline source block header bracket depth.")
|
||||
}
|
||||
']' if current_depth > 0 => {
|
||||
current_depth -= 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if current_depth == 0 {
|
||||
let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input);
|
||||
if close_bracket.is_ok() {
|
||||
return close_bracket;
|
||||
}
|
||||
}
|
||||
|
||||
line_ending(input)
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn body<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let (remaining, _) = tag("{")(input)?;
|
||||
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::InlineSourceBlockBracket(
|
||||
InlineSourceBlockBracket {
|
||||
position: remaining,
|
||||
depth: 0,
|
||||
},
|
||||
))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &body_end,
|
||||
}));
|
||||
|
||||
let (remaining, body_contents) = recognize(many_till(
|
||||
anychar,
|
||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
||||
))(remaining)?;
|
||||
let (remaining, _) = {
|
||||
let span = span!(
|
||||
tracing::Level::DEBUG,
|
||||
"outside end body",
|
||||
remaining = remaining
|
||||
);
|
||||
let _enter = span.enter();
|
||||
tag("}")(remaining)?
|
||||
};
|
||||
Ok((remaining, body_contents))
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
fn body_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||
let context_depth = get_bracket_depth(context)
|
||||
.expect("This function should only be called from inside an inline source block body.");
|
||||
let text_since_context_entry = get_consumed(context_depth.position, input);
|
||||
let mut current_depth = context_depth.depth;
|
||||
for c in text_since_context_entry.chars() {
|
||||
match c {
|
||||
'{' => {
|
||||
current_depth += 1;
|
||||
}
|
||||
'}' if current_depth == 0 => {
|
||||
panic!("Exceeded inline source block body bracket depth.")
|
||||
}
|
||||
'}' if current_depth > 0 => {
|
||||
current_depth -= 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
{
|
||||
let span = span!(
|
||||
tracing::Level::DEBUG,
|
||||
"inside end body",
|
||||
remaining = input,
|
||||
current_depth = current_depth
|
||||
);
|
||||
let _enter = span.enter();
|
||||
|
||||
if current_depth == 0 {
|
||||
let close_bracket = tag::<&str, &str, CustomError<&str>>("}")(input);
|
||||
if close_bracket.is_ok() {
|
||||
return close_bracket;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
line_ending(input)
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
pub fn get_bracket_depth<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
) -> Option<&'r InlineSourceBlockBracket<'s>> {
|
||||
for node in context.iter() {
|
||||
match node.get_data() {
|
||||
ContextElement::InlineSourceBlockBracket(depth) => return Some(depth),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
@ -19,6 +19,7 @@ mod greater_block;
|
||||
mod greater_element;
|
||||
mod horizontal_rule;
|
||||
mod inline_babel_call;
|
||||
mod inline_source_block;
|
||||
mod keyword;
|
||||
mod latex_environment;
|
||||
mod latex_fragment;
|
||||
@ -83,6 +84,7 @@ pub use object::Entity;
|
||||
pub use object::ExportSnippet;
|
||||
pub use object::FootnoteReference;
|
||||
pub use object::InlineBabelCall;
|
||||
pub use object::InlineSourceBlock;
|
||||
pub use object::Italic;
|
||||
pub use object::LatexFragment;
|
||||
pub use object::Object;
|
||||
|
@ -22,6 +22,7 @@ pub enum Object<'s> {
|
||||
Citation(Citation<'s>),
|
||||
CitationReference(CitationReference<'s>),
|
||||
InlineBabelCall(InlineBabelCall<'s>),
|
||||
InlineSourceBlock(InlineSourceBlock<'s>),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@ -143,6 +144,11 @@ pub struct InlineBabelCall<'s> {
|
||||
pub source: &'s str,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct InlineSourceBlock<'s> {
|
||||
pub source: &'s str,
|
||||
}
|
||||
|
||||
impl<'s> Source<'s> for Object<'s> {
|
||||
fn get_source(&'s self) -> &'s str {
|
||||
match self {
|
||||
@ -166,6 +172,7 @@ impl<'s> Source<'s> for Object<'s> {
|
||||
Object::Citation(obj) => obj.source,
|
||||
Object::CitationReference(obj) => obj.source,
|
||||
Object::InlineBabelCall(obj) => obj.source,
|
||||
Object::InlineSourceBlock(obj) => obj.source,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -283,3 +290,9 @@ impl<'s> Source<'s> for InlineBabelCall<'s> {
|
||||
self.source
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> Source<'s> for InlineSourceBlock<'s> {
|
||||
fn get_source(&'s self) -> &'s str {
|
||||
self.source
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ use crate::parser::entity::entity;
|
||||
use crate::parser::export_snippet::export_snippet;
|
||||
use crate::parser::footnote_reference::footnote_reference;
|
||||
use crate::parser::inline_babel_call::inline_babel_call;
|
||||
use crate::parser::inline_source_block::inline_source_block;
|
||||
use crate::parser::latex_fragment::latex_fragment;
|
||||
use crate::parser::object::Object;
|
||||
use crate::parser::org_macro::org_macro;
|
||||
@ -26,10 +27,14 @@ pub fn standard_set_object<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, Object<'s>> {
|
||||
// TODO: inline source blocks, line breaks, targets (different from radio targets), statistics cookies, subscript and superscript, timestamps.
|
||||
// TODO: line breaks, targets (different from radio targets), statistics cookies, subscript and superscript, timestamps.
|
||||
not(|i| context.check_exit_matcher(i))(input)?;
|
||||
|
||||
alt((
|
||||
map(
|
||||
parser_with_context!(inline_source_block)(context),
|
||||
Object::InlineSourceBlock,
|
||||
),
|
||||
map(
|
||||
parser_with_context!(inline_babel_call)(context),
|
||||
Object::InlineBabelCall,
|
||||
@ -91,6 +96,10 @@ pub fn any_object_except_plain_text<'r, 's>(
|
||||
) -> Res<&'s str, Object<'s>> {
|
||||
// Used for exit matchers so this does not check exit matcher condition.
|
||||
alt((
|
||||
map(
|
||||
parser_with_context!(inline_source_block)(context),
|
||||
Object::InlineSourceBlock,
|
||||
),
|
||||
map(
|
||||
parser_with_context!(inline_babel_call)(context),
|
||||
Object::InlineBabelCall,
|
||||
@ -130,8 +139,12 @@ pub fn regular_link_description_object_set<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, Object<'s>> {
|
||||
// TODO: minimal set of objects as well as export snippets, inline source blocks, and statistics cookies. It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]]
|
||||
// TODO: minimal set of objects as well as export snippets, and statistics cookies. It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]]
|
||||
alt((
|
||||
map(
|
||||
parser_with_context!(inline_source_block)(context),
|
||||
Object::InlineSourceBlock,
|
||||
),
|
||||
map(
|
||||
parser_with_context!(inline_babel_call)(context),
|
||||
Object::InlineBabelCall,
|
||||
|
@ -181,6 +181,19 @@ pub enum ContextElement<'r, 's> {
|
||||
/// unbalanced brackets can be detected in the middle of an
|
||||
/// object.
|
||||
BabelHeaderBracket(BabelHeaderBracket<'s>),
|
||||
|
||||
/// Stores the current bracket or parenthesis depth inside an inline babel call.
|
||||
///
|
||||
/// Inside an inline babel call the headers must have balanced
|
||||
/// parentheses () and the arguments must have balanced brackets
|
||||
/// [], so this stores the amount of opening brackets subtracted
|
||||
/// by the amount of closing brackets within the definition must
|
||||
/// equal zero.
|
||||
///
|
||||
/// A reference to the position in the string is also included so
|
||||
/// unbalanced brackets can be detected in the middle of an
|
||||
/// object.
|
||||
InlineSourceBlockBracket(InlineSourceBlockBracket<'s>),
|
||||
}
|
||||
|
||||
pub struct ExitMatcherNode<'r> {
|
||||
@ -206,6 +219,12 @@ pub struct BabelHeaderBracket<'s> {
|
||||
pub depth: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct InlineSourceBlockBracket<'s> {
|
||||
pub position: &'s str,
|
||||
pub depth: usize,
|
||||
}
|
||||
|
||||
impl<'r> std::fmt::Debug for ExitMatcherNode<'r> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut formatter = f.debug_struct("ExitMatcherNode");
|
||||
|
@ -61,6 +61,7 @@ impl<'r, 's> Token<'r, 's> {
|
||||
Object::Citation(_) => Box::new(std::iter::empty()), // TODO: Iterate over children
|
||||
Object::CitationReference(_) => Box::new(std::iter::empty()), // TODO: Iterate over children
|
||||
Object::InlineBabelCall(_) => Box::new(std::iter::empty()),
|
||||
Object::InlineSourceBlock(_) => Box::new(std::iter::empty()),
|
||||
},
|
||||
Token::Element(elem) => match elem {
|
||||
Element::Paragraph(inner) => Box::new(inner.children.iter().map(Token::Object)),
|
||||
|
@ -1 +1,2 @@
|
||||
[cite/a/b-_/foo:globalprefix;keyprefix @foo keysuffix;globalsuffix]
|
||||
before src_foo{ipsum} after
|
||||
src_bar[lorem]{ipsum}
|
||||
|
Loading…
Reference in New Issue
Block a user