First attempt at implementing drawers.
This commit is contained in:
parent
1f7c24545b
commit
9e4bf553d3
23
org_mode_samples/drawer/Makefile
Normal file
23
org_mode_samples/drawer/Makefile
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
SHELL := bash
|
||||||
|
.ONESHELL:
|
||||||
|
.SHELLFLAGS := -eu -o pipefail -c
|
||||||
|
.DELETE_ON_ERROR:
|
||||||
|
MAKEFLAGS += --warn-undefined-variables
|
||||||
|
MAKEFLAGS += --no-builtin-rules
|
||||||
|
SRCFILES := $(wildcard *.org)
|
||||||
|
OUTFILES := $(patsubst %.org,%.tree.txt,$(SRCFILES))
|
||||||
|
|
||||||
|
ifeq ($(origin .RECIPEPREFIX), undefined)
|
||||||
|
$(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later)
|
||||||
|
endif
|
||||||
|
.RECIPEPREFIX = >
|
||||||
|
|
||||||
|
.PHONY: all
|
||||||
|
all: $(OUTFILES)
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
> rm -rf $(OUTFILES)
|
||||||
|
|
||||||
|
%.tree.txt: %.org ../common.el ../dump_org_ast.bash
|
||||||
|
> ../dump_org_ast.bash $< $@
|
9
org_mode_samples/drawer/simple.org
Normal file
9
org_mode_samples/drawer/simple.org
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
* Headline
|
||||||
|
before
|
||||||
|
:candle:
|
||||||
|
inside
|
||||||
|
|
||||||
|
the drawer
|
||||||
|
:end:
|
||||||
|
|
||||||
|
after
|
@ -1,8 +1,76 @@
|
|||||||
|
use nom::branch::alt;
|
||||||
|
use nom::bytes::complete::tag;
|
||||||
|
use nom::bytes::complete::take_while;
|
||||||
|
use nom::character::complete::line_ending;
|
||||||
|
use nom::character::complete::space0;
|
||||||
|
use nom::combinator::eof;
|
||||||
|
use nom::combinator::recognize;
|
||||||
|
use nom::multi::many_till;
|
||||||
|
use nom::sequence::tuple;
|
||||||
|
|
||||||
use super::Context;
|
use super::Context;
|
||||||
|
use crate::parser::element::element;
|
||||||
use crate::parser::error::Res;
|
use crate::parser::error::Res;
|
||||||
|
use crate::parser::parser_context::ChainBehavior;
|
||||||
|
use crate::parser::parser_context::ContextElement;
|
||||||
|
use crate::parser::parser_context::ExitMatcherNode;
|
||||||
|
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::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
||||||
|
use crate::parser::util::start_of_line;
|
||||||
|
use crate::parser::util::WORD_CONSTITUENT_CHARACTERS;
|
||||||
use crate::parser::Drawer;
|
use crate::parser::Drawer;
|
||||||
|
|
||||||
#[tracing::instrument(ret, level = "debug")]
|
#[tracing::instrument(ret, level = "debug")]
|
||||||
pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Drawer<'s>> {
|
pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Drawer<'s>> {
|
||||||
todo!()
|
start_of_line(context, input)?;
|
||||||
|
let (remaining, _leading_whitespace) = space0(input)?;
|
||||||
|
let (remaining, (_open_colon, drawer_name, _close_colon, _new_line)) = tuple((
|
||||||
|
tag(":"),
|
||||||
|
name,
|
||||||
|
tag(":"),
|
||||||
|
recognize(tuple((space0, line_ending))),
|
||||||
|
))(remaining)?;
|
||||||
|
|
||||||
|
let parser_context = context
|
||||||
|
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||||
|
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||||
|
exit_matcher: ChainBehavior::AndParent(Some(&drawer_end)),
|
||||||
|
}));
|
||||||
|
|
||||||
|
let element_matcher = parser_with_context!(element)(&parser_context);
|
||||||
|
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||||
|
let (remaining, (children, _exit_contents)) =
|
||||||
|
many_till(element_matcher, exit_matcher)(remaining)?;
|
||||||
|
let (remaining, _end) = drawer_end(&parser_context, remaining)?;
|
||||||
|
|
||||||
|
let (remaining, _trailing_ws) =
|
||||||
|
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||||
|
let source = get_consumed(input, remaining);
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
remaining,
|
||||||
|
Drawer {
|
||||||
|
source,
|
||||||
|
name: drawer_name,
|
||||||
|
children,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(ret, level = "debug")]
|
||||||
|
fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> {
|
||||||
|
take_while(|c| WORD_CONSTITUENT_CHARACTERS.contains(c) || "-_".contains(c))(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(ret, level = "debug")]
|
||||||
|
fn drawer_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
|
||||||
|
start_of_line(context, input)?;
|
||||||
|
recognize(tuple((
|
||||||
|
space0,
|
||||||
|
tag(":end:"),
|
||||||
|
space0,
|
||||||
|
alt((line_ending, eof)),
|
||||||
|
)))(input)
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,6 @@ pub struct FootnoteDefinition<'s> {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Drawer<'s> {
|
pub struct Drawer<'s> {
|
||||||
pub source: &'s str,
|
pub source: &'s str,
|
||||||
pub label: &'s str,
|
pub name: &'s str,
|
||||||
pub children: Vec<Element<'s>>,
|
pub children: Vec<Element<'s>>,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user