ParseBuilder

Struct ParseBuilder 

Source
pub struct ParseBuilder<'c, S: State = Empty> { /* private fields */ }
Expand description

Use builder syntax to set the inputs and finish with build().

Implementations§

Source§

impl<'c, S: State> ParseBuilder<'c, S>

Source

pub fn build(self) -> Parse<'c>
where S: IsComplete,

Finish building and return the requested object

Source

pub fn smart(self, value: bool) -> ParseBuilder<'c, SetSmart<S>>
where S::Smart: IsUnset,

Optional (Some / Option setters). Default: false.

Punctuation (quotes, full-stops and hyphens) are converted into ‘smart’ punctuation.

let mut options = Options::default();
assert_eq!(markdown_to_html("'Hello,' \"world\" ...", &options),
           "<p>'Hello,' &quot;world&quot; ...</p>\n");

options.parse.smart = true;
assert_eq!(markdown_to_html("'Hello,' \"world\" ...", &options),
           "<p>‘Hello,’ “world” …</p>\n");
Source

pub fn maybe_smart(self, value: Option<bool>) -> ParseBuilder<'c, SetSmart<S>>
where S::Smart: IsUnset,

Optional (Some / Option setters). Default: false.

Punctuation (quotes, full-stops and hyphens) are converted into ‘smart’ punctuation.

let mut options = Options::default();
assert_eq!(markdown_to_html("'Hello,' \"world\" ...", &options),
           "<p>'Hello,' &quot;world&quot; ...</p>\n");

options.parse.smart = true;
assert_eq!(markdown_to_html("'Hello,' \"world\" ...", &options),
           "<p>‘Hello,’ “world” …</p>\n");
Source

pub fn default_info_string( self, value: String, ) -> ParseBuilder<'c, SetDefaultInfoString<S>>
where S::DefaultInfoString: IsUnset,

Optional (Some / Option setters).

The default info string for fenced code blocks.

let mut options = Options::default();
assert_eq!(markdown_to_html("```\nfn hello();\n```\n", &options),
           "<pre><code>fn hello();\n</code></pre>\n");

options.parse.default_info_string = Some("rust".into());
assert_eq!(markdown_to_html("```\nfn hello();\n```\n", &options),
           "<pre><code class=\"language-rust\">fn hello();\n</code></pre>\n");
Source

pub fn maybe_default_info_string( self, value: Option<String>, ) -> ParseBuilder<'c, SetDefaultInfoString<S>>
where S::DefaultInfoString: IsUnset,

Optional (Some / Option setters).

The default info string for fenced code blocks.

let mut options = Options::default();
assert_eq!(markdown_to_html("```\nfn hello();\n```\n", &options),
           "<pre><code>fn hello();\n</code></pre>\n");

options.parse.default_info_string = Some("rust".into());
assert_eq!(markdown_to_html("```\nfn hello();\n```\n", &options),
           "<pre><code class=\"language-rust\">fn hello();\n</code></pre>\n");
Source

pub fn relaxed_tasklist_matching( self, value: bool, ) -> ParseBuilder<'c, SetRelaxedTasklistMatching<S>>
where S::RelaxedTasklistMatching: IsUnset,

Optional (Some / Option setters). Default: false.

Whether or not a simple x or X is used for tasklist or any other symbol is allowed.

Source

pub fn maybe_relaxed_tasklist_matching( self, value: Option<bool>, ) -> ParseBuilder<'c, SetRelaxedTasklistMatching<S>>
where S::RelaxedTasklistMatching: IsUnset,

Optional (Some / Option setters). Default: false.

Whether or not a simple x or X is used for tasklist or any other symbol is allowed.

Source

pub fn tasklist_in_table( self, value: bool, ) -> ParseBuilder<'c, SetTasklistInTable<S>>
where S::TasklistInTable: IsUnset,

Optional (Some / Option setters). Default: false.

Whether tasklist items can be parsed in table cells. At present, the tasklist item must be the only content in the cell. Both tables and tasklists much be enabled for this to work.

let mut options = Options::default();
options.extension.table = true;
options.extension.tasklist = true;
assert_eq!(markdown_to_html("| val |\n| - |\n| [ ] |\n", &options),
           "<table>\n<thead>\n<tr>\n<th>val</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>[ ]</td>\n</tr>\n</tbody>\n</table>\n");

options.parse.tasklist_in_table = true;
assert_eq!(markdown_to_html("| val |\n| - |\n| [ ] |\n", &options),
           "<table>\n<thead>\n<tr>\n<th>val</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>\n<input type=\"checkbox\" disabled=\"\" /> </td>\n</tr>\n</tbody>\n</table>\n");
Source

pub fn maybe_tasklist_in_table( self, value: Option<bool>, ) -> ParseBuilder<'c, SetTasklistInTable<S>>
where S::TasklistInTable: IsUnset,

Optional (Some / Option setters). Default: false.

Whether tasklist items can be parsed in table cells. At present, the tasklist item must be the only content in the cell. Both tables and tasklists much be enabled for this to work.

let mut options = Options::default();
options.extension.table = true;
options.extension.tasklist = true;
assert_eq!(markdown_to_html("| val |\n| - |\n| [ ] |\n", &options),
           "<table>\n<thead>\n<tr>\n<th>val</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>[ ]</td>\n</tr>\n</tbody>\n</table>\n");

options.parse.tasklist_in_table = true;
assert_eq!(markdown_to_html("| val |\n| - |\n| [ ] |\n", &options),
           "<table>\n<thead>\n<tr>\n<th>val</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>\n<input type=\"checkbox\" disabled=\"\" /> </td>\n</tr>\n</tbody>\n</table>\n");

Optional (Some / Option setters). Default: false.

Relax parsing of autolinks, allow links to be detected inside brackets and allow all url schemes. It is intended to allow a very specific type of autolink detection, such as [this http://and.com that] or {http://foo.com}, on a best can basis.

let mut options = Options::default();
options.extension.autolink = true;
assert_eq!(markdown_to_html("[https://foo.com]", &options),
           "<p>[https://foo.com]</p>\n");

options.parse.relaxed_autolinks = true;
assert_eq!(markdown_to_html("[https://foo.com]", &options),
           "<p>[<a href=\"https://foo.com\">https://foo.com</a>]</p>\n");

Optional (Some / Option setters). Default: false.

Relax parsing of autolinks, allow links to be detected inside brackets and allow all url schemes. It is intended to allow a very specific type of autolink detection, such as [this http://and.com that] or {http://foo.com}, on a best can basis.

let mut options = Options::default();
options.extension.autolink = true;
assert_eq!(markdown_to_html("[https://foo.com]", &options),
           "<p>[https://foo.com]</p>\n");

options.parse.relaxed_autolinks = true;
assert_eq!(markdown_to_html("[https://foo.com]", &options),
           "<p>[<a href=\"https://foo.com\">https://foo.com</a>]</p>\n");
Source

pub fn ignore_setext(self, value: bool) -> ParseBuilder<'c, SetIgnoreSetext<S>>
where S::IgnoreSetext: IsUnset,

Optional (Some / Option setters). Default: false.

Ignore setext headings in input.

let mut options = Options::default();
let input = "setext heading\n---";

assert_eq!(markdown_to_html(input, &options),
           "<h2>setext heading</h2>\n");

options.parse.ignore_setext = true;
assert_eq!(markdown_to_html(input, &options),
           "<p>setext heading</p>\n<hr />\n");
Source

pub fn maybe_ignore_setext( self, value: Option<bool>, ) -> ParseBuilder<'c, SetIgnoreSetext<S>>
where S::IgnoreSetext: IsUnset,

Optional (Some / Option setters). Default: false.

Ignore setext headings in input.

let mut options = Options::default();
let input = "setext heading\n---";

assert_eq!(markdown_to_html(input, &options),
           "<h2>setext heading</h2>\n");

options.parse.ignore_setext = true;
assert_eq!(markdown_to_html(input, &options),
           "<p>setext heading</p>\n<hr />\n");

Optional (Some / Option setters).

In case the parser encounters any potential links that have a broken reference (e.g [foo] when there is no [foo]: url entry at the bottom) the provided callback will be called with the reference name, both in normalized form and unmodified, and the returned pair will be used as the link destination and title if not None.

let cb = |link_ref: BrokenLinkReference| match link_ref.normalized {
    "foo" => Some(ResolvedReference {
        url: "https://www.rust-lang.org/".to_string(),
        title: "The Rust Language".to_string(),
    }),
    _ => None,
};

let mut options = Options::default();
options.parse.broken_link_callback = Some(Arc::new(cb));

let output = markdown_to_html(
    "# Cool input!\nWow look at this cool [link][foo]. A [broken link] renders as text.",
    &options,
);

assert_eq!(output,
           "<h1>Cool input!</h1>\n<p>Wow look at this cool \
           <a href=\"https://www.rust-lang.org/\" title=\"The Rust Language\">link</a>. \
           A [broken link] renders as text.</p>\n");

Optional (Some / Option setters).

In case the parser encounters any potential links that have a broken reference (e.g [foo] when there is no [foo]: url entry at the bottom) the provided callback will be called with the reference name, both in normalized form and unmodified, and the returned pair will be used as the link destination and title if not None.

let cb = |link_ref: BrokenLinkReference| match link_ref.normalized {
    "foo" => Some(ResolvedReference {
        url: "https://www.rust-lang.org/".to_string(),
        title: "The Rust Language".to_string(),
    }),
    _ => None,
};

let mut options = Options::default();
options.parse.broken_link_callback = Some(Arc::new(cb));

let output = markdown_to_html(
    "# Cool input!\nWow look at this cool [link][foo]. A [broken link] renders as text.",
    &options,
);

assert_eq!(output,
           "<h1>Cool input!</h1>\n<p>Wow look at this cool \
           <a href=\"https://www.rust-lang.org/\" title=\"The Rust Language\">link</a>. \
           A [broken link] renders as text.</p>\n");
Source

pub fn leave_footnote_definitions( self, value: bool, ) -> ParseBuilder<'c, SetLeaveFootnoteDefinitions<S>>
where S::LeaveFootnoteDefinitions: IsUnset,

Optional (Some / Option setters). Default: false.

Leave footnote definitions in place in the document tree, rather than reordering them to the end. This will also cause unreferenced footnote definitions to remain in the tree, rather than being removed.

Comrak’s default formatters expect this option to be turned off, so use with care if you use the default formatters.

let mut options = Options::default();
options.extension.footnotes = true;
let arena = Arena::new();
let input = concat!(
  "Remember burning a CD?[^cd]\n",
  "\n",
  "[^cd]: In the Old Days, a 4x burner was considered good.\n",
  "\n",
  "[^dvd]: And DVD-RWs? Those were something else.\n",
  "\n",
  "Me neither.",
);

fn node_kinds<'a>(doc: Node<'a>) -> Vec<&'static str> {
  doc.descendants().map(|n| n.data().value.xml_node_name()).collect()
}

let root = parse_document(&arena, input, &options);
assert_eq!(
  node_kinds(root),
  &["document", "paragraph", "text", "footnote_reference", "paragraph", "text",
    "footnote_definition", "paragraph", "text"],
);

options.parse.leave_footnote_definitions = true;

let root = parse_document(&arena, input, &options);
assert_eq!(
  node_kinds(root),
  &["document", "paragraph", "text", "footnote_reference", "footnote_definition",
    "paragraph", "text", "footnote_definition", "paragraph", "text", "paragraph", "text"],
);
Source

pub fn maybe_leave_footnote_definitions( self, value: Option<bool>, ) -> ParseBuilder<'c, SetLeaveFootnoteDefinitions<S>>
where S::LeaveFootnoteDefinitions: IsUnset,

Optional (Some / Option setters). Default: false.

Leave footnote definitions in place in the document tree, rather than reordering them to the end. This will also cause unreferenced footnote definitions to remain in the tree, rather than being removed.

Comrak’s default formatters expect this option to be turned off, so use with care if you use the default formatters.

let mut options = Options::default();
options.extension.footnotes = true;
let arena = Arena::new();
let input = concat!(
  "Remember burning a CD?[^cd]\n",
  "\n",
  "[^cd]: In the Old Days, a 4x burner was considered good.\n",
  "\n",
  "[^dvd]: And DVD-RWs? Those were something else.\n",
  "\n",
  "Me neither.",
);

fn node_kinds<'a>(doc: Node<'a>) -> Vec<&'static str> {
  doc.descendants().map(|n| n.data().value.xml_node_name()).collect()
}

let root = parse_document(&arena, input, &options);
assert_eq!(
  node_kinds(root),
  &["document", "paragraph", "text", "footnote_reference", "paragraph", "text",
    "footnote_definition", "paragraph", "text"],
);

options.parse.leave_footnote_definitions = true;

let root = parse_document(&arena, input, &options);
assert_eq!(
  node_kinds(root),
  &["document", "paragraph", "text", "footnote_reference", "footnote_definition",
    "paragraph", "text", "footnote_definition", "paragraph", "text", "paragraph", "text"],
);
Source

pub fn escaped_char_spans( self, value: bool, ) -> ParseBuilder<'c, SetEscapedCharSpans<S>>
where S::EscapedCharSpans: IsUnset,

Optional (Some / Option setters). Default: false.

Leave escaped characters in an Escaped node in the document tree.

let mut options = Options::default();
let arena = Arena::new();
let input = "Notify user \\@example";

fn node_kinds<'a>(doc: Node<'a>) -> Vec<&'static str> {
  doc.descendants().map(|n| n.data().value.xml_node_name()).collect()
}

let root = parse_document(&arena, input, &options);
assert_eq!(
  node_kinds(root),
  &["document", "paragraph", "text"],
);

options.parse.escaped_char_spans = true;
let root = parse_document(&arena, input, &options);
assert_eq!(
  node_kinds(root),
  &["document", "paragraph", "text", "escaped", "text", "text"],
);

Note that enabling the escaped_char_spans render option will cause this option to be enabled.

Source

pub fn maybe_escaped_char_spans( self, value: Option<bool>, ) -> ParseBuilder<'c, SetEscapedCharSpans<S>>
where S::EscapedCharSpans: IsUnset,

Optional (Some / Option setters). Default: false.

Leave escaped characters in an Escaped node in the document tree.

let mut options = Options::default();
let arena = Arena::new();
let input = "Notify user \\@example";

fn node_kinds<'a>(doc: Node<'a>) -> Vec<&'static str> {
  doc.descendants().map(|n| n.data().value.xml_node_name()).collect()
}

let root = parse_document(&arena, input, &options);
assert_eq!(
  node_kinds(root),
  &["document", "paragraph", "text"],
);

options.parse.escaped_char_spans = true;
let root = parse_document(&arena, input, &options);
assert_eq!(
  node_kinds(root),
  &["document", "paragraph", "text", "escaped", "text", "text"],
);

Note that enabling the escaped_char_spans render option will cause this option to be enabled.

Auto Trait Implementations§

§

impl<'c, S> Freeze for ParseBuilder<'c, S>

§

impl<'c, S> RefUnwindSafe for ParseBuilder<'c, S>

§

impl<'c, S> Send for ParseBuilder<'c, S>

§

impl<'c, S> Sync for ParseBuilder<'c, S>

§

impl<'c, S> Unpin for ParseBuilder<'c, S>

§

impl<'c, S> UnwindSafe for ParseBuilder<'c, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.