Parse out the pathreg on regular links.

This commit is contained in:
Tom Alexander 2023-10-06 17:21:43 -04:00
parent 448902bb05
commit e84135985e
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 127 additions and 17 deletions

View File

@ -1 +1,9 @@
[[(foo)]] [[(foo)]]
[[((bar))]]
[[((baz)]]
# These become fuzzy
[[(foo) ]]
[[ (foo)]]

View File

@ -3,8 +3,15 @@ use nom::bytes::complete::escaped;
use nom::bytes::complete::tag; use nom::bytes::complete::tag;
use nom::bytes::complete::take_till1; use nom::bytes::complete::take_till1;
use nom::character::complete::anychar; use nom::character::complete::anychar;
use nom::combinator::consumed;
use nom::combinator::eof;
use nom::combinator::map_parser;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::combinator::rest;
use nom::combinator::verify; use nom::combinator::verify;
use nom::multi::many_till; use nom::multi::many_till;
use nom::sequence::tuple;
use super::object_parser::regular_link_description_set_object; use super::object_parser::regular_link_description_set_object;
use super::org_source::OrgSource; use super::org_source::OrgSource;
@ -17,6 +24,7 @@ use crate::context::ExitClass;
use crate::context::ExitMatcherNode; use crate::context::ExitMatcherNode;
use crate::context::RefContext; use crate::context::RefContext;
use crate::error::Res; use crate::error::Res;
use crate::types::LinkType;
use crate::types::Object; use crate::types::Object;
use crate::types::RegularLink; use crate::types::RegularLink;
@ -37,7 +45,7 @@ fn regular_link_without_description<'b, 'g, 'r, 's>(
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, RegularLink<'s>> { ) -> Res<OrgSource<'s>, RegularLink<'s>> {
let (remaining, _opening_bracket) = tag("[[")(input)?; let (remaining, _opening_bracket) = tag("[[")(input)?;
let (remaining, _path) = pathreg(context, remaining)?; let (remaining, path) = pathreg(context, remaining)?;
let (remaining, _closing_bracket) = tag("]]")(remaining)?; let (remaining, _closing_bracket) = tag("]]")(remaining)?;
let (remaining, _trailing_whitespace) = let (remaining, _trailing_whitespace) =
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?; maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
@ -46,9 +54,9 @@ fn regular_link_without_description<'b, 'g, 'r, 's>(
remaining, remaining,
RegularLink { RegularLink {
source: source.into(), source: source.into(),
link_type: todo!(), link_type: path.link_type,
path: todo!(), path: path.path,
raw_link: todo!(), raw_link: path.raw_link,
}, },
)) ))
} }
@ -59,7 +67,7 @@ fn regular_link_with_description<'b, 'g, 'r, 's>(
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, RegularLink<'s>> { ) -> Res<OrgSource<'s>, RegularLink<'s>> {
let (remaining, _opening_bracket) = tag("[[")(input)?; let (remaining, _opening_bracket) = tag("[[")(input)?;
let (remaining, _path) = pathreg(context, remaining)?; let (remaining, path) = pathreg(context, remaining)?;
let (remaining, _closing_bracket) = tag("][")(remaining)?; let (remaining, _closing_bracket) = tag("][")(remaining)?;
let (remaining, _description) = description(context, remaining)?; let (remaining, _description) = description(context, remaining)?;
let (remaining, _closing_bracket) = tag("]]")(remaining)?; let (remaining, _closing_bracket) = tag("]]")(remaining)?;
@ -70,29 +78,112 @@ fn regular_link_with_description<'b, 'g, 'r, 's>(
remaining, remaining,
RegularLink { RegularLink {
source: source.into(), source: source.into(),
link_type: todo!(), link_type: path.link_type,
path: todo!(), path: path.path,
raw_link: todo!(), raw_link: path.raw_link,
}, },
)) ))
} }
struct PathReg<'s> {
link_type: LinkType<'s>,
path: &'s str,
raw_link: &'s str,
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn pathreg<'b, 'g, 'r, 's>( fn pathreg<'b, 'g, 'r, 's>(
_context: RefContext<'b, 'g, 'r, 's>, _context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> { ) -> Res<OrgSource<'s>, PathReg<'s>> {
let (remaining, path) = escaped( let (remaining, path) = map_parser(
escaped(
take_till1(|c| match c { take_till1(|c| match c {
'\\' | '[' | ']' => true, '\\' | '[' | ']' => true,
_ => false, _ => false,
}), }),
'\\', '\\',
anychar, anychar,
),
parse_path_reg,
)(input)?; )(input)?;
Ok((remaining, path)) Ok((remaining, path))
} }
fn parse_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
alt((
file_path_reg,
id_path_reg,
custom_id_path_reg,
code_ref_path_reg,
fuzzy_path_reg,
))(input)
}
fn file_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
let (remaining, (raw_link, (_, path))) = consumed(tuple((tag("file:"), rest)))(input)?;
Ok((
remaining,
PathReg {
link_type: LinkType::File,
path: path.into(),
raw_link: raw_link.into(),
},
))
}
fn id_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
let (remaining, (raw_link, (_, path))) = consumed(tuple((tag("id:"), rest)))(input)?;
Ok((
remaining,
PathReg {
link_type: LinkType::Id,
path: path.into(),
raw_link: raw_link.into(),
},
))
}
fn custom_id_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
let (remaining, (raw_link, (_, path))) = consumed(tuple((tag("#"), rest)))(input)?;
Ok((
remaining,
PathReg {
link_type: LinkType::CustomId,
path: path.into(),
raw_link: raw_link.into(),
},
))
}
fn code_ref_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
let (remaining, (raw_link, (_, path, _))) = consumed(tuple((
tag("("),
recognize(many_till(anychar, peek(tuple((tag(")"), eof))))),
tag(")"),
)))(input)?;
Ok((
remaining,
PathReg {
link_type: LinkType::CodeRef,
path: path.into(),
raw_link: raw_link.into(),
},
))
}
fn fuzzy_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
let (remaining, body) = rest(input)?;
Ok((
remaining,
PathReg {
link_type: LinkType::Fuzzy,
path: body.into(),
raw_link: body.into(),
},
))
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn description<'b, 'g, 'r, 's>( fn description<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,

View File

@ -75,6 +75,7 @@ pub use object::InlineSourceBlock;
pub use object::Italic; pub use object::Italic;
pub use object::LatexFragment; pub use object::LatexFragment;
pub use object::LineBreak; pub use object::LineBreak;
pub use object::LinkType;
pub use object::Minute; pub use object::Minute;
pub use object::MinuteInner; pub use object::MinuteInner;
pub use object::Month; pub use object::Month;

View File

@ -77,7 +77,7 @@ pub struct PlainText<'s> {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct RegularLink<'s> { pub struct RegularLink<'s> {
pub source: &'s str, pub source: &'s str,
pub link_type: &'s str, pub link_type: LinkType<'s>,
pub path: &'s str, pub path: &'s str,
pub raw_link: &'s str, pub raw_link: &'s str,
} }
@ -638,3 +638,13 @@ impl<'s> Timestamp<'s> {
self.source.trim_end() self.source.trim_end()
} }
} }
#[derive(Debug, PartialEq)]
pub enum LinkType<'s> {
File,
Protocol(&'s str),
Id,
CustomId,
CodeRef,
Fuzzy,
}