Compare commits

...

10 Commits

Author SHA1 Message Date
Tom Alexander
20a8683894
Handle puncuation-only search options.
Some checks failed
rust-build Build rust-build has succeeded
rust-test Build rust-test has failed
rust-foreign-document-test Build rust-foreign-document-test has failed
2023-10-07 03:25:37 -04:00
Tom Alexander
3aa84c1743
Add test showing we are not handling puncutation-only search options properly. 2023-10-07 03:23:45 -04:00
Tom Alexander
7196e10b69
Set up regular links for adding application. 2023-10-07 03:14:16 -04:00
Tom Alexander
0c34df159f
Add test showing we are not handling application in regular links properly. 2023-10-07 03:03:57 -04:00
Tom Alexander
ddb09a1805
Add support for application in plain links. 2023-10-07 03:00:40 -04:00
Tom Alexander
c58b850570
Add support for search options. 2023-10-07 02:42:07 -04:00
Tom Alexander
a55694176c
Compare the properties of plain links. 2023-10-07 02:22:36 -04:00
Tom Alexander
6973d5a2c0
Since value and path are always the same for radio links, I removed the extra value. 2023-10-07 01:48:16 -04:00
Tom Alexander
592e773920
Add tests for plain link search options and relative paths. 2023-10-07 01:44:31 -04:00
Tom Alexander
be553aefb1
Add test showing plain links cannot be templates. 2023-10-07 01:44:31 -04:00
12 changed files with 268 additions and 31 deletions

View File

@ -0,0 +1,25 @@
non-link text::foo
eww://foo::foo
rmail://foo::foo
mhe://foo::foo
irc://foo::foo
info://foo::foo
gnus://foo::foo
docview://foo::foo
bibtex://foo::foo
bbdb://foo::foo
w3m://foo::foo
doi://foo::foo
file+sys://foo::foo
file+emacs://foo::foo
shell://foo::foo
news://foo::foo
mailto://foo::foo
https://foo::foo
http://foo::foo
ftp://foo::foo
help://foo::foo
file://foo::foo
elisp://foo::foo
randomfakeprotocl://foo::foo
non-link text::foo

View File

@ -0,0 +1,4 @@
# These do not become links
./simple.org::foo
../simple.org::foo
/simple.org::foo

View File

@ -0,0 +1,16 @@
file:simple.org::foo
file:simple.org::#foo
file:simple.org::foo bar
file:simple.org::foo
bar
file:simple.org::foo::bar
file:simple.org::/foo/
# Does not become a search option because it is inside parenthesis.
https://en.wikipedia.org/wiki/Shebang_(Uni::x)
file:simple.org::* foo
file:simple.org::*bar
file:simple.org::b*az

View File

@ -0,0 +1,8 @@
# None of these end up as links.
#+LINK: foo https://foo.bar/baz#%s
foo::lorem
cat::bat
#+LINK: cat https://dog%s
cat:bat

View File

@ -0,0 +1,2 @@
[[file+sys://foo]]
[[file+emacs://foo]]

View File

@ -0,0 +1,2 @@
# Does not become a search option because it is inside parenthesis.
[[https://en.wikipedia.org/wiki/Shebang_(Uni::x)]]

View File

@ -2802,8 +2802,8 @@ fn compare_regular_link<'b, 's>(
),
(
EmacsField::Required(":application"),
compare_identity,
compare_property_always_nil
|r| r.application.as_ref(),
compare_property_quoted_string
),
(
EmacsField::Required(":search-option"),
@ -2860,7 +2860,7 @@ fn compare_radio_link<'b, 's>(
),
(
EmacsField::Required(":raw-link"),
|r| Some(&r.raw_link),
|r| Some(r.get_raw_link()),
compare_property_quoted_string
),
(
@ -2936,10 +2936,55 @@ fn compare_plain_link<'b, 's>(
emacs: &'b Token<'s>,
rust: &'b PlainLink<'s>,
) -> Result<DiffEntry<'b, 's>, Box<dyn std::error::Error>> {
let this_status = DiffStatus::Good;
let message = None;
let mut this_status = DiffStatus::Good;
let mut message = None;
// TODO: Compare :type :path :format :raw-link :application :search-option
if let Some((new_status, new_message)) = compare_properties!(
emacs,
rust,
(
EmacsField::Required(":type"),
|r| {
match &r.link_type {
LinkType::File => Some(Cow::Borrowed("file")),
LinkType::Protocol(protocol) => Some(protocol.clone()),
LinkType::Id => Some(Cow::Borrowed("id")),
LinkType::CustomId => Some(Cow::Borrowed("custom-id")),
LinkType::CodeRef => Some(Cow::Borrowed("coderef")),
LinkType::Fuzzy => Some(Cow::Borrowed("fuzzy")),
}
},
compare_property_quoted_string
),
(
EmacsField::Required(":path"),
|r| Some(r.path),
compare_property_quoted_string
),
(
EmacsField::Required(":format"),
|_| Some("plain"),
compare_property_unquoted_atom
),
(
EmacsField::Required(":raw-link"),
|r| Some(r.raw_link),
compare_property_quoted_string
),
(
EmacsField::Required(":application"),
|r| r.application,
compare_property_quoted_string
),
(
EmacsField::Required(":search-option"),
|r| r.search_option,
compare_property_quoted_string
)
)? {
this_status = new_status;
message = new_message;
}
Ok(DiffResult {
status: this_status,

View File

@ -1,13 +1,19 @@
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::map_parser;
use nom::combinator::not;
use nom::combinator::opt;
use nom::combinator::peek;
use nom::combinator::recognize;
use nom::combinator::rest;
use nom::combinator::verify;
use nom::multi::many0;
use nom::multi::many1;
@ -30,6 +36,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 +45,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 +54,24 @@ 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,
application: path_plain.application,
},
))
}
#[derive(Debug)]
struct PathPlain<'s> {
link_type: LinkType<'s>,
path: &'s str,
raw_link: &'s str,
search_option: Option<&'s str>,
application: 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 +101,95 @@ 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)
}
pub(crate) fn parse_file_and_application<'s>(
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Option<OrgSource<'s>>> {
let (remaining, _) = tag("file")(input)?;
let (remaining, application) =
opt(map(tuple((tag("+"), rest)), |(_, application)| application))(remaining)?;
// Assert we consumed the entire protocol.
not(anychar)(remaining)?;
Ok((remaining, application))
}
fn file_path_plain<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, PathPlain<'s>> {
let path_plain_end = path_plain_end(input.get_parenthesis_depth(), true);
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
exit_matcher: &path_plain_end,
});
let parser_context = context.with_additional_node(&parser_context);
let (remaining, (raw_link, (application, _, path, search_option))) = consumed(tuple((
map_parser(
parser_with_context!(protocol)(&parser_context),
parse_file_and_application,
),
tag(":"),
parser_with_context!(path_plain)(&parser_context),
opt(map(
tuple((
tag("::"),
verify(is_not(" \t\r\n"), |search_option| {
Into::<&str>::into(search_option)
.chars()
.any(char::is_alphanumeric)
}),
)),
|(_, 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),
application: application.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 path_plain_end = path_plain_end(input.get_parenthesis_depth(), false);
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
exit_matcher: &path_plain_end,
});
let parser_context = context.with_additional_node(&parser_context);
let (remaining, (raw_link, (protocol, _, path))) = consumed(tuple((
parser_with_context!(protocol)(&parser_context),
tag(":"),
parser_with_context!(path_plain)(&parser_context),
)))(input)?;
Ok((
remaining,
PathPlain {
link_type: LinkType::Protocol(protocol.into()),
path: path.into(),
raw_link: raw_link.into(),
search_option: None,
application: None,
},
))
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn protocol<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
@ -109,24 +215,28 @@ fn path_plain<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
let path_plain_end = path_plain_end(input.get_parenthesis_depth());
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma,
exit_matcher: &path_plain_end,
});
let parser_context = context.with_additional_node(&parser_context);
// The caller needs to put an instance of path_plain_end on the context before calling this.
let (remaining, _components) = many1(alt((
parser_with_context!(path_plain_no_parenthesis)(&parser_context),
parser_with_context!(path_plain_parenthesis)(&parser_context),
parser_with_context!(path_plain_no_parenthesis)(context),
parser_with_context!(path_plain_parenthesis)(context),
)))(input)?;
let source = get_consumed(input, remaining);
Ok((remaining, source))
}
fn path_plain_end(starting_parenthesis_depth: BracketDepth) -> impl ContextMatcher {
move |context, input: OrgSource<'_>| _path_plain_end(context, input, starting_parenthesis_depth)
fn path_plain_end(
starting_parenthesis_depth: BracketDepth,
enable_search_option: bool,
) -> impl ContextMatcher {
move |context, input: OrgSource<'_>| {
_path_plain_end(
context,
input,
starting_parenthesis_depth,
enable_search_option,
)
}
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
@ -134,7 +244,16 @@ fn _path_plain_end<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
starting_parenthesis_depth: BracketDepth,
enable_search_option: bool,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
let current_depth = input.get_parenthesis_depth() - starting_parenthesis_depth;
if enable_search_option && current_depth == 0 {
let search_option = peek(tag("::"))(input);
if search_option.is_ok() {
return search_option;
}
}
let (remaining, _leading_punctuation) = many0(verify(anychar, |c| {
!" \t\r\n[]<>()/".contains(*c) && c.is_ascii_punctuation()
}))(input)?;
@ -144,7 +263,6 @@ fn _path_plain_end<'b, 'g, 'r, 's>(
return disallowed_character;
}
let current_depth = remaining.get_parenthesis_depth() - starting_parenthesis_depth;
if current_depth == 0 {
let close_parenthesis =
tag::<&str, OrgSource<'_>, CustomError<OrgSource<'_>>>(")")(remaining);
@ -161,7 +279,6 @@ fn _path_plain_end<'b, 'g, 'r, 's>(
}
}
// many0 punctuation
Err(nom::Err::Error(CustomError::MyError(MyError(
"No path plain end".into(),
))))

View File

@ -41,7 +41,6 @@ pub(crate) fn radio_link<'b, 'g, 'r, 's>(
source: source.into(),
children: rematched_target,
path: path.into(),
raw_link: path.into(),
},
));
}
@ -194,8 +193,7 @@ mod tests {
&Object::RadioLink(RadioLink {
source: "bar ",
children: vec![Object::PlainText(PlainText { source: "bar" })],
path: "bar".into(),
raw_link: "bar".into()
path: "bar".into()
})
);
}
@ -238,8 +236,7 @@ mod tests {
source: "*bar* ",
children: vec![Object::PlainText(PlainText { source: "bar" })]
})],
path: "*bar* ".into(),
raw_link: "*bar* ".into()
path: "*bar* ".into()
})
);
}

View File

@ -69,6 +69,7 @@ fn regular_link_without_description<'b, 'g, 'r, 's>(
raw_link: path.raw_link,
search_option: path.search_option,
children: Vec::new(),
application: path.application,
},
))
}
@ -95,6 +96,7 @@ fn regular_link_with_description<'b, 'g, 'r, 's>(
raw_link: path.raw_link,
search_option: path.search_option,
children: description,
application: path.application,
},
))
}
@ -105,6 +107,7 @@ struct PathReg<'s> {
path: Cow<'s, str>,
raw_link: Cow<'s, str>,
search_option: Option<Cow<'s, str>>,
application: Option<Cow<'s, str>>,
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
@ -161,6 +164,7 @@ fn parse_path_reg<'b, 'g, 'r, 's>(
path: link.path.into_owned().into(),
raw_link: link.raw_link.into_owned().into(),
search_option: link.search_option.map(|s| s.into_owned().into()),
application: link.application.map(|s| s.into_owned().into()),
},
))
} else {
@ -240,6 +244,8 @@ fn apply_link_templates<'b, 'g, 'r, 's>(
}
fn file_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
// TODO: Update this to parse out the application like plain link does.
// TODO: Handle parenthesis like plain link does.
let (remaining, (raw_link, (_, path, search_option))) = consumed(tuple((
alt((tag("file:"), peek(tag(".")), peek(tag("/")))),
recognize(many_till(anychar, alt((peek(tag("::")), eof)))),
@ -256,6 +262,7 @@ fn file_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
search_option: search_option
.map(Into::<&str>::into)
.map(Into::<Cow<str>>::into),
application: None, // TODO
},
))
}
@ -269,6 +276,7 @@ fn id_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
path: path.into(),
raw_link: raw_link.into(),
search_option: None,
application: None,
},
))
}
@ -282,6 +290,7 @@ fn custom_id_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s
path: path.into(),
raw_link: raw_link.into(),
search_option: None,
application: None,
},
))
}
@ -299,6 +308,7 @@ fn code_ref_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>
path: path.into(),
raw_link: raw_link.into(),
search_option: None,
application: None,
},
))
}
@ -319,6 +329,7 @@ fn protocol_path_reg<'b, 'g, 'r, 's>(
path: path.into(),
raw_link: raw_link.into(),
search_option: None,
application: None,
},
))
}
@ -332,6 +343,7 @@ fn fuzzy_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
path: body.into(),
raw_link: body.into(),
search_option: None,
application: None,
},
))
}

View File

@ -85,6 +85,7 @@ pub struct RegularLink<'s> {
pub raw_link: Cow<'s, str>,
pub search_option: Option<Cow<'s, str>>,
pub children: Vec<Object<'s>>,
pub application: Option<Cow<'s, str>>,
}
#[derive(Debug, PartialEq)]
@ -98,15 +99,17 @@ pub struct RadioTarget<'s> {
pub struct RadioLink<'s> {
pub source: &'s str,
pub path: &'s str,
pub raw_link: &'s str, // TODO: Is this always the same as path? If so, this could be a function instead of a field.
pub children: Vec<Object<'s>>,
}
#[derive(Debug, PartialEq)]
pub struct PlainLink<'s> {
pub source: &'s str,
pub link_type: &'s str,
pub link_type: LinkType<'s>,
pub path: &'s str,
pub raw_link: &'s str,
pub search_option: Option<&'s str>,
pub application: Option<&'s str>,
}
#[derive(Debug, PartialEq)]
@ -720,3 +723,9 @@ impl<'s> RegularLink<'s> {
})
}
}
impl<'s> RadioLink<'s> {
pub fn get_raw_link(&self) -> &'s str {
self.path
}
}