Compare the properties of plain links.

This commit is contained in:
Tom Alexander
2023-10-07 02:22:36 -04:00
parent 6973d5a2c0
commit a55694176c
6 changed files with 150 additions and 9 deletions

View File

@@ -1,11 +1,15 @@
use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag;
use nom::bytes::complete::tag_no_case;
use nom::character::complete::anychar;
use nom::character::complete::none_of;
use nom::character::complete::one_of;
use nom::combinator::consumed;
use nom::combinator::eof;
use nom::combinator::map;
use nom::combinator::not;
use nom::combinator::opt;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::combinator::verify;
@@ -30,6 +34,7 @@ use crate::error::Res;
use crate::parser::util::exit_matcher_parser;
use crate::parser::util::get_consumed;
use crate::parser::util::WORD_CONSTITUENT_CHARACTERS;
use crate::types::LinkType;
use crate::types::PlainLink;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
@@ -38,9 +43,7 @@ pub(crate) fn plain_link<'b, 'g, 'r, 's>(
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, PlainLink<'s>> {
let (remaining, _) = pre(context, input)?;
let (remaining, proto) = protocol(context, remaining)?;
let (remaining, _separator) = tag(":")(remaining)?;
let (remaining, path) = path_plain(context, remaining)?;
let (remaining, path_plain) = parse_path_plain(context, remaining)?;
peek(parser_with_context!(post)(context))(remaining)?;
let (remaining, _trailing_whitespace) =
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
@@ -49,12 +52,22 @@ pub(crate) fn plain_link<'b, 'g, 'r, 's>(
remaining,
PlainLink {
source: source.into(),
link_type: proto.into(),
path: path.into(),
link_type: path_plain.link_type,
path: path_plain.path,
raw_link: path_plain.raw_link,
search_option: path_plain.search_option,
},
))
}
#[derive(Debug)]
struct PathPlain<'s> {
link_type: LinkType<'s>,
path: &'s str,
raw_link: &'s str,
search_option: Option<&'s str>,
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn pre<'b, 'g, 'r, 's>(
_context: RefContext<'b, 'g, 'r, 's>,
@@ -84,6 +97,59 @@ fn post<'b, 'g, 'r, 's>(
Ok((remaining, ()))
}
fn parse_path_plain<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, PathPlain<'s>> {
alt((
parser_with_context!(file_path_plain)(context),
parser_with_context!(protocol_path_plain)(context),
))(input)
}
fn file_path_plain<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, PathPlain<'s>> {
let (remaining, (raw_link, (_, path, search_option))) = consumed(tuple((
tag("file:"),
parser_with_context!(path_plain)(context),
opt(map(
tuple((tag("::"), is_not(" \t\r\n"))),
|(_, search_option)| search_option,
)),
)))(input)?;
Ok((
remaining,
PathPlain {
link_type: LinkType::File,
path: path.into(),
raw_link: raw_link.into(),
search_option: search_option.map(Into::<&str>::into),
},
))
}
fn protocol_path_plain<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, PathPlain<'s>> {
let (remaining, (raw_link, (protocol, _, path))) = consumed(tuple((
parser_with_context!(protocol)(context),
tag(":"),
parser_with_context!(path_plain)(context),
)))(input)?;
Ok((
remaining,
PathPlain {
link_type: LinkType::Protocol(protocol.into()),
path: path.into(),
raw_link: raw_link.into(),
search_option: None,
},
))
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn protocol<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,