Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write a sourcepos test for each NodeValue variant #498

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ caseless = "0.2.1"

[dev-dependencies]
ntest = "0.9"
strum = { version = "0.26.3", features = ["derive"] }
toml = "0.7.3"

[features]
Expand Down Expand Up @@ -78,3 +79,7 @@ clap = { version = "4.0", optional = true, features = [
"string",
"wrap_help",
] }

[[example]]
name = "syntect"
required-features = [ "syntect" ]
5 changes: 2 additions & 3 deletions examples/s-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const INDENT: usize = 4;
const CLOSE_NEWLINE: bool = false;

use comrak::nodes::{AstNode, NodeValue};
use comrak::{parse_document, Arena, ExtensionOptions, Options};
use comrak::{parse_document, Arena, ExtensionOptions, Options, WikiLinksMode};
use std::env;
use std::error::Error;
use std::fs::File;
Expand Down Expand Up @@ -86,8 +86,7 @@ fn dump(source: &str) -> io::Result<()> {
.multiline_block_quotes(true)
.math_dollars(true)
.math_code(true)
.wikilinks_title_after_pipe(true)
.wikilinks_title_before_pipe(true)
.wikilinks(WikiLinksMode::TitleFirst)
.build();

let opts = Options {
Expand Down
6 changes: 3 additions & 3 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::nodes::{
use crate::nodes::{NodeList, TableAlignment};
#[cfg(feature = "shortcodes")]
use crate::parser::shortcodes::NodeShortCode;
use crate::parser::Options;
use crate::parser::{Options, WikiLinksMode};
use crate::scanners;
use crate::strings::trim_start_match;
use crate::{nodes, Plugins};
Expand Down Expand Up @@ -761,12 +761,12 @@ impl<'a, 'o, 'c> CommonMarkFormatter<'a, 'o, 'c> {
fn format_wikilink(&mut self, nl: &NodeWikiLink, entering: bool) -> bool {
if entering {
write!(self, "[[").unwrap();
if self.options.extension.wikilinks_title_after_pipe {
if self.options.extension.wikilinks == Some(WikiLinksMode::UrlFirst) {
self.output(nl.url.as_bytes(), false, Escaping::Url);
write!(self, "|").unwrap();
}
} else {
if self.options.extension.wikilinks_title_before_pipe {
if self.options.extension.wikilinks == Some(WikiLinksMode::TitleFirst) {
write!(self, "|").unwrap();
self.output(nl.url.as_bytes(), false, Escaping::Url);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub use parser::{
parse_document, BrokenLinkCallback, BrokenLinkReference, ExtensionOptions,
ExtensionOptionsBuilder, ListStyleType, Options, ParseOptions, ParseOptionsBuilder, Plugins,
PluginsBuilder, RenderOptions, RenderOptionsBuilder, RenderPlugins, RenderPluginsBuilder,
ResolvedReference, URLRewriter,
ResolvedReference, URLRewriter, WikiLinksMode,
};
pub use typed_arena::Arena;
pub use xml::format_document as format_xml;
Expand Down
20 changes: 17 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::path::PathBuf;
use std::process;

use clap::{Parser, ValueEnum};
use comrak::{ExtensionOptions, ParseOptions, RenderOptions};
use comrak::{ExtensionOptions, ParseOptions, RenderOptions, WikiLinksMode};

const EXIT_SUCCESS: i32 = 0;
const EXIT_PARSE_CONFIG: i32 = 2;
Expand Down Expand Up @@ -252,6 +252,21 @@ fn main() -> Result<(), Box<dyn Error>> {

let exts = &cli.extensions;

let wikilinks_title_after_pipe = exts.contains(&Extension::WikilinksTitleAfterPipe);
let wikilinks_title_before_pipe = exts.contains(&Extension::WikilinksTitleBeforePipe);
let wikilinks_mode = match (wikilinks_title_after_pipe, wikilinks_title_before_pipe) {
(false, false) => None,
(true, false) => Some(WikiLinksMode::UrlFirst),
(false, true) => Some(WikiLinksMode::TitleFirst),
(true, true) => {
eprintln!(concat!(
"cannot enable both wikilinks-title-after-pipe ",
"and wikilinks-title-before-pipe at the same time"
));
process::exit(EXIT_PARSE_CONFIG);
}
};

let extension = ExtensionOptions::builder()
.strikethrough(exts.contains(&Extension::Strikethrough) || cli.gfm)
.tagfilter(exts.contains(&Extension::Tagfilter) || cli.gfm)
Expand All @@ -265,8 +280,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.multiline_block_quotes(exts.contains(&Extension::MultilineBlockQuotes))
.math_dollars(exts.contains(&Extension::MathDollars))
.math_code(exts.contains(&Extension::MathCode))
.wikilinks_title_after_pipe(exts.contains(&Extension::WikilinksTitleAfterPipe))
.wikilinks_title_before_pipe(exts.contains(&Extension::WikilinksTitleBeforePipe))
.maybe_wikilinks(wikilinks_mode)
.underline(exts.contains(&Extension::Underline))
.subscript(exts.contains(&Extension::Subscript))
.spoiler(exts.contains(&Extension::Spoiler))
Expand Down
13 changes: 10 additions & 3 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ pub use crate::parser::multiline_block_quote::NodeMultilineBlockQuote;

/// The core AST node enum.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(strum::EnumDiscriminants))]
#[cfg_attr(
test,
strum_discriminants(vis(pub(crate)), derive(strum::VariantArray, Hash))
)]
pub enum NodeValue {
/// The root of every CommonMark document. Contains **blocks**.
Document,
Expand Down Expand Up @@ -181,6 +186,8 @@ pub enum NodeValue {
MultilineBlockQuote(NodeMultilineBlockQuote),

/// **Inline**. A character that has been [escaped](https://github.github.com/gfm/#backslash-escapes)
///
/// Enabled with [`escaped_char_spans`](crate::RenderOptionsBuilder::escaped_char_spans).
Escaped,

/// **Inline**. A wikilink to some URL.
Expand Down Expand Up @@ -244,7 +251,7 @@ pub struct NodeTable {
}

/// An inline [code span](https://github.github.com/gfm/#code-spans).
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct NodeCode {
/// The number of backticks
pub num_backticks: usize,
Expand All @@ -257,7 +264,7 @@ pub struct NodeCode {
}

/// The details of a link's destination, or an image's source.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct NodeLink {
/// The URL for the link destination or image source.
pub url: String,
Expand All @@ -270,7 +277,7 @@ pub struct NodeLink {
}

/// The details of a wikilink's destination.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct NodeWikiLink {
/// The URL for the link destination.
pub url: String,
Expand Down
17 changes: 9 additions & 8 deletions src/parser/inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use std::str;
use typed_arena::Arena;
use unicode_categories::UnicodeCategories;

use super::WikiLinksMode;

const MAXBACKTICKS: usize = 80;
const MAX_LINK_LABEL_LENGTH: usize = 1000;
const MAX_MATH_DOLLARS: usize = 2;
Expand Down Expand Up @@ -235,8 +237,7 @@ impl<'a, 'r, 'o, 'd, 'i, 'c> Subject<'a, 'r, 'o, 'd, 'i, 'c> {

let mut wikilink_inl = None;

if (self.options.extension.wikilinks_title_after_pipe
|| self.options.extension.wikilinks_title_before_pipe)
if self.options.extension.wikilinks.is_some()
&& !self.within_brackets
&& self.peek_char() == Some(&(b'['))
{
Expand Down Expand Up @@ -1804,16 +1805,16 @@ impl<'a, 'r, 'o, 'd, 'i, 'c> Subject<'a, 'r, 'o, 'd, 'i, 'c> {
if self.peek_char() == Some(&(b']')) && self.peek_char_n(1) == Some(&(b']')) {
self.pos += 2;

if self.options.extension.wikilinks_title_after_pipe {
Some(WikilinkComponents {
match self.options.extension.wikilinks {
Some(WikiLinksMode::UrlFirst) => Some(WikilinkComponents {
url: left,
link_label: Some((right, right_startpos + 1, self.pos - 3)),
})
} else {
Some(WikilinkComponents {
}),
Some(WikiLinksMode::TitleFirst) => Some(WikilinkComponents {
url: right,
link_label: Some((left, left_startpos + 1, right_startpos - 1)),
})
}),
None => unreachable!(),
}
} else {
self.pos = left_startpos;
Expand Down
4 changes: 2 additions & 2 deletions src/parser/math.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// An inline math span
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct NodeMath {
/// Whether this is dollar math (`$` or `$$`).
/// `false` indicates it is code math
Expand All @@ -8,7 +8,7 @@ pub struct NodeMath {
/// Whether this is display math (using `$$`)
pub display_math: bool,

/// The literal contents of the math span.
/// The literal contents of the math span.
/// As the contents are not interpreted as Markdown at all,
/// they are contained within this structure,
/// rather than inserted into a child inline of any kind.
Expand Down
44 changes: 26 additions & 18 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ where
}
}

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
/// Selects between wikilinks with the title first or the URL first.
///
/// See [`ExtensionOptions::wikilinks`].
pub enum WikiLinksMode {
/// Indicates that the URL precedes the title. For example: `[[http://example.com|link
/// title]]`.
UrlFirst,

/// Indicates that the title precedes the URL. For example: `[[link title|http://example.com]]`.
TitleFirst,
}

#[non_exhaustive]
#[derive(Default, Debug, Clone, Builder)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
Expand Down Expand Up @@ -466,37 +481,28 @@ pub struct ExtensionOptions<'c> {
#[builder(default)]
pub shortcodes: bool,

/// Enables wikilinks using title after pipe syntax
/// Enables wikilinks
///
/// With [`WikiLinksMode::TitleFirst`]:
///
/// ```` md
/// [[url|link label]]
/// [[link label|url]]
/// ````
///
/// ```
/// # use comrak::{markdown_to_html, Options};
/// let mut options = Options::default();
/// options.extension.wikilinks_title_after_pipe = true;
/// assert_eq!(markdown_to_html("[[url|link label]]", &options),
/// "<p><a href=\"url\" data-wikilink=\"true\">link label</a></p>\n");
/// ```
#[builder(default)]
pub wikilinks_title_after_pipe: bool,

/// Enables wikilinks using title before pipe syntax
/// With [`WikiLinksMode::UrlFirst`]:
///
/// ```` md
/// [[link label|url]]
/// [[url|link label]]
/// ````
///
/// ```
/// # use comrak::{markdown_to_html, Options};
/// # use comrak::{markdown_to_html, Options, WikiLinksMode};
/// let mut options = Options::default();
/// options.extension.wikilinks_title_before_pipe = true;
/// options.extension.wikilinks = Some(WikiLinksMode::TitleFirst);
/// assert_eq!(markdown_to_html("[[link label|url]]", &options),
/// "<p><a href=\"url\" data-wikilink=\"true\">link label</a></p>\n");
/// ```
#[builder(default)]
pub wikilinks_title_before_pipe: bool,
pub wikilinks: Option<WikiLinksMode>,

/// Enables underlines using double underscores
///
Expand Down Expand Up @@ -865,6 +871,8 @@ pub struct RenderOptions {
/// let xml = markdown_to_commonmark_xml(input, &options);
/// assert!(xml.contains("<text sourcepos=\"1:4-1:15\" xml:space=\"preserve\">"));
/// ```
///
/// [`experimental_inline_sourcepos`]: crate::RenderOptionsBuilder::experimental_inline_sourcepos
#[builder(default)]
pub sourcepos: bool,

Expand Down
2 changes: 1 addition & 1 deletion src/parser/multiline_block_quote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// The metadata of a multiline blockquote.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct NodeMultilineBlockQuote {
/// The length of the fence.
pub fence_length: usize,
Expand Down
Loading
Loading