Restrict protocol links to org-link-parameters.

This commit is contained in:
Tom Alexander 2023-10-06 19:18:58 -04:00
parent f220fd63e5
commit 89fcf6cb54
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 23 additions and 42 deletions

View File

@ -40,8 +40,8 @@ pub struct GlobalSettings<'g, 's> {
/// The allowed protocols for links (for example, the "https" in "https://foo.bar/").
///
/// COrresponds to org-link-parameters elisp variable.
pub org_link_parameters: &'g [&'g str],
/// Corresponds to org-link-parameters elisp variable.
pub link_parameters: &'g [&'g str],
}
pub const DEFAULT_TAB_WIDTH: IndentationLevel = 8;
@ -60,7 +60,7 @@ impl<'g, 's> GlobalSettings<'g, 's> {
odd_levels_only: HeadlineLevelFilter::default(),
footnote_section: "Footnotes",
coderef_label_format: "(ref:%s)",
org_link_parameters: &ORG_LINK_PARAMETERS,
link_parameters: &ORG_LINK_PARAMETERS,
}
}
}

View File

@ -32,33 +32,6 @@ use crate::parser::util::get_consumed;
use crate::parser::util::WORD_CONSTITUENT_CHARACTERS;
use crate::types::PlainLink;
// TODO: Make this a user-provided variable corresponding to elisp's org-link-parameters
const ORG_LINK_PARAMETERS: [&'static str; 23] = [
"id",
"eww",
"rmail",
"mhe",
"irc",
"info",
"gnus",
"docview",
"bibtex",
"bbdb",
"w3m",
"doi",
"file+sys",
"file+emacs",
"shell",
"news",
"mailto",
"https",
"http",
"ftp",
"help",
"file",
"elisp",
];
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn plain_link<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
@ -113,12 +86,11 @@ fn post<'b, 'g, 'r, 's>(
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn protocol<'b, 'g, 'r, 's>(
_context: RefContext<'b, 'g, 'r, 's>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
// TODO: This should be defined by org-link-parameters
for link_parameter in ORG_LINK_PARAMETERS {
let result = tag_no_case::<_, _, CustomError<_>>(link_parameter)(input);
for link_parameter in context.get_global_settings().link_parameters {
let result = tag_no_case::<_, _, CustomError<_>>(*link_parameter)(input);
match result {
Ok((remaining, ent)) => {
return Ok((remaining, ent));

View File

@ -2,7 +2,6 @@ use nom::branch::alt;
use nom::bytes::complete::escaped;
use nom::bytes::complete::tag;
use nom::bytes::complete::take_till1;
use nom::bytes::complete::take_until;
use nom::character::complete::anychar;
use nom::combinator::consumed;
use nom::combinator::eof;
@ -18,6 +17,7 @@ use nom::sequence::tuple;
use super::object_parser::regular_link_description_set_object;
use super::org_source::OrgSource;
use super::plain_link::protocol;
use super::util::exit_matcher_parser;
use super::util::get_consumed;
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
@ -99,7 +99,7 @@ struct PathReg<'s> {
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn pathreg<'b, 'g, 'r, 's>(
_context: RefContext<'b, 'g, 'r, 's>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, PathReg<'s>> {
let (remaining, path) = map_parser(
@ -111,18 +111,21 @@ fn pathreg<'b, 'g, 'r, 's>(
'\\',
anychar,
),
parse_path_reg,
parser_with_context!(parse_path_reg)(context),
)(input)?;
Ok((remaining, path))
}
fn parse_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
fn parse_path_reg<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, PathReg<'s>> {
alt((
file_path_reg,
id_path_reg,
custom_id_path_reg,
code_ref_path_reg,
protocol_path_reg,
parser_with_context!(protocol_path_reg)(context),
fuzzy_path_reg,
))(input)
}
@ -189,9 +192,15 @@ fn code_ref_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>
))
}
fn protocol_path_reg<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PathReg<'s>> {
let (remaining, (raw_link, (protocol, _, path))) =
consumed(tuple((take_until(":"), tag(":"), rest)))(input)?;
fn protocol_path_reg<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, PathReg<'s>> {
let (remaining, (raw_link, (protocol, _, path))) = consumed(tuple((
parser_with_context!(protocol)(context),
tag(":"),
rest,
)))(input)?;
Ok((
remaining,
PathReg {