Add an initial implementation of PlainLink.

This commit is contained in:
Tom Alexander 2023-07-13 19:09:44 -04:00
parent 59f061ecca
commit 5f4e240af0
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 83 additions and 4 deletions

View File

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

View File

@ -76,7 +76,8 @@ pub struct RadioLink<'s> {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct PlainLink<'s> { pub struct PlainLink<'s> {
pub source: &'s str, pub source: &'s str,
pub children: Vec<Object<'s>>, pub link_type: &'s str,
pub path: &'s str,
} }
impl<'s> Source<'s> for Object<'s> { impl<'s> Source<'s> for Object<'s> {

View File

@ -68,6 +68,7 @@ pub fn any_object_except_plain_text<'r, 's>(
parser_with_context!(regular_link)(context), parser_with_context!(regular_link)(context),
Object::RegularLink, Object::RegularLink,
), ),
map(parser_with_context!(plain_link)(context), Object::PlainLink),
))(input) ))(input)
} }

View File

@ -1,6 +1,10 @@
use nom::branch::alt; use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::tag_no_case;
use nom::bytes::complete::take_while;
use nom::character::complete::none_of; use nom::character::complete::none_of;
use nom::combinator::eof; use nom::combinator::eof;
use nom::combinator::peek;
use nom::combinator::recognize; use nom::combinator::recognize;
use super::Context; use super::Context;
@ -8,14 +12,20 @@ use crate::error::CustomError;
use crate::error::MyError; use crate::error::MyError;
use crate::error::Res; use crate::error::Res;
use crate::parser::object::PlainLink; use crate::parser::object::PlainLink;
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::util::get_consumed;
use crate::parser::util::get_one_before; use crate::parser::util::get_one_before;
use crate::parser::util::not_yet_implemented;
use crate::parser::util::WORD_CONSTITUENT_CHARACTERS; use crate::parser::util::WORD_CONSTITUENT_CHARACTERS;
#[tracing::instrument(ret, level = "debug")] #[tracing::instrument(ret, level = "debug")]
pub fn plain_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainLink<'s>> { pub fn plain_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, PlainLink<'s>> {
not_yet_implemented()?; let (remaining, _) = pre(context, input)?;
todo!(); let (remaining, proto) = protocol(context, remaining)?;
let (remaining, _separator) = tag(":")(remaining)?;
let (remaining, path) = path_plain(context, remaining)?;
peek(parser_with_context!(post)(context))(remaining)?;
let source = get_consumed(input, remaining);
Ok((remaining, PlainLink { source, link_type: proto, path }))
} }
#[tracing::instrument(ret, level = "debug")] #[tracing::instrument(ret, level = "debug")]
@ -43,3 +53,44 @@ pub fn post<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, ()
let (remaining, _) = alt((eof, recognize(none_of(WORD_CONSTITUENT_CHARACTERS))))(input)?; let (remaining, _) = alt((eof, recognize(none_of(WORD_CONSTITUENT_CHARACTERS))))(input)?;
Ok((remaining, ())) Ok((remaining, ()))
} }
#[tracing::instrument(ret, level = "debug")]
pub fn protocol<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
let (remaining, proto) = alt((
alt((
tag_no_case("id"),
tag_no_case("eww"),
tag_no_case("rmail"),
tag_no_case("mhe"),
tag_no_case("irc"),
tag_no_case("info"),
tag_no_case("gnus"),
tag_no_case("docview"),
tag_no_case("bibtex"),
tag_no_case("bbdb"),
tag_no_case("w3m"),
)),
alt((
tag_no_case("doi"),
tag_no_case("file+sys"),
tag_no_case("file+emacs"),
tag_no_case("shell"),
tag_no_case("news"),
tag_no_case("mailto"),
tag_no_case("https"),
tag_no_case("http"),
tag_no_case("ftp"),
tag_no_case("help"),
tag_no_case("file"),
tag_no_case("elisp"),
)),
))(input)?;
Ok((remaining, proto))
}
#[tracing::instrument(ret, level = "debug")]
pub fn path_plain<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
// TODO: "optionally containing parenthesis-wrapped non-whitespace non-bracket substrings up to a depth of two. The string must end with either a non-punctation non-whitespace character, a forwards slash, or a parenthesis-wrapped substring"
take_while(|c| !" \t\r\n()[]<>".contains(c))(input)
// recognize(many1(none_of(" \t\r\n()[]<>")))(input)
}