Skip to content

Commit

Permalink
Merge branch 'options-split'
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Feb 6, 2020
2 parents 644e367 + 7fb7cf5 commit 660ec92
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 260 deletions.
23 changes: 13 additions & 10 deletions examples/s-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const INDENT: usize = 4;
const CLOSE_NEWLINE: bool = false;

use comrak::nodes::{AstNode, NodeValue};
use comrak::{parse_document, Arena, ComrakOptions};
use comrak::{parse_document, Arena, ComrakOptions, ComrakExtensionOptions};
use std::env;
use std::error::Error;
use std::fs::File;
Expand Down Expand Up @@ -78,14 +78,17 @@ fn dump(source: &str) -> io::Result<()> {
let arena = Arena::new();

let opts = ComrakOptions {
ext_strikethrough: true,
ext_tagfilter: true,
ext_table: true,
ext_autolink: true,
ext_tasklist: true,
ext_superscript: true,
ext_footnotes: true,
ext_description_lists: true,
extension: ComrakExtensionOptions {
strikethrough: true,
tagfilter: true,
table: true,
autolink: true,
tasklist: true,
superscript: true,
footnotes: true,
description_lists: true,
..ComrakExtensionOptions::default()
},
..ComrakOptions::default()
};

Expand All @@ -95,7 +98,7 @@ fn dump(source: &str) -> io::Result<()> {
iter_nodes(doc, &mut output, 0)
}

fn main() -> Result<(), Box<Error>> {
fn main() -> Result<(), Box<dyn Error>> {
let mut args = env::args_os().skip(1).peekable();
let mut body = String::new();

Expand Down
10 changes: 5 additions & 5 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
self.begin_content = self.begin_content && isdigit(buf[i]);
}

if self.options.width > 0
&& self.column > self.options.width
if self.options.render.width > 0
&& self.column > self.options.render.width
&& !self.begin_line
&& self.last_breakable > 0
{
Expand Down Expand Up @@ -282,7 +282,7 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {

fn format_node(&mut self, node: &'a AstNode<'a>, entering: bool) -> bool {
self.node = node;
let allow_wrap = self.options.width > 0 && !self.options.hardbreaks;
let allow_wrap = self.options.render.width > 0 && !self.options.render.hardbreaks;

if !(match node.data.borrow().value {
NodeValue::Item(..) => true,
Expand Down Expand Up @@ -444,13 +444,13 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
self.output(literal, allow_wrap, Escaping::Normal);
},
NodeValue::LineBreak => if entering {
if !self.options.hardbreaks {
if !self.options.render.hardbreaks {
write!(self, " ").unwrap();
}
self.cr();
},
NodeValue::SoftBreak => if entering {
if !self.no_linebreaks && self.options.width == 0 && !self.options.hardbreaks {
if !self.no_linebreaks && self.options.render.width == 0 && !self.options.render.hardbreaks {
self.cr();
} else {
self.output(&[b' '], allow_wrap, Escaping::Literal);
Expand Down
18 changes: 9 additions & 9 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ impl<'o> HtmlFormatter<'o> {
self.cr()?;
write!(self.output, "<h{}>", nch.level)?;

if let Some(ref prefix) = self.options.ext_header_ids {
if let Some(ref prefix) = self.options.extension.header_ids {
let mut text_content = Vec::with_capacity(20);
self.collect_text(node, &mut text_content);

Expand Down Expand Up @@ -481,7 +481,7 @@ impl<'o> HtmlFormatter<'o> {
first_tag += 1;
}

if self.options.github_pre_lang {
if self.options.render.github_pre_lang {
self.output.write_all(b"<pre lang=\"")?;
self.escape(&ncb.info[..first_tag])?;
self.output.write_all(b"\"><code>")?;
Expand All @@ -496,9 +496,9 @@ impl<'o> HtmlFormatter<'o> {
},
NodeValue::HtmlBlock(ref nhb) => if entering {
self.cr()?;
if !self.options.unsafe_ {
if !self.options.render.unsafe_ {
self.output.write_all(b"<!-- raw HTML omitted -->")?;
} else if self.options.ext_tagfilter {
} else if self.options.extension.tagfilter {
tagfilter_block(&nhb.literal, &mut self.output)?;
} else {
self.output.write_all(&nhb.literal)?;
Expand Down Expand Up @@ -543,7 +543,7 @@ impl<'o> HtmlFormatter<'o> {
self.output.write_all(b"<br />\n")?;
},
NodeValue::SoftBreak => if entering {
if self.options.hardbreaks {
if self.options.render.hardbreaks {
self.output.write_all(b"<br />\n")?;
} else {
self.output.write_all(b"\n")?;
Expand All @@ -555,9 +555,9 @@ impl<'o> HtmlFormatter<'o> {
self.output.write_all(b"</code>")?;
},
NodeValue::HtmlInline(ref literal) => if entering {
if !self.options.unsafe_ {
if !self.options.render.unsafe_ {
self.output.write_all(b"<!-- raw HTML omitted -->")?;
} else if self.options.ext_tagfilter && tagfilter(literal) {
} else if self.options.extension.tagfilter && tagfilter(literal) {
self.output.write_all(b"&lt;")?;
self.output.write_all(&literal[1..])?;
} else {
Expand Down Expand Up @@ -586,7 +586,7 @@ impl<'o> HtmlFormatter<'o> {
},
NodeValue::Link(ref nl) => if entering {
self.output.write_all(b"<a href=\"")?;
if self.options.unsafe_ || !dangerous_url(&nl.url) {
if self.options.render.unsafe_ || !dangerous_url(&nl.url) {
self.escape_href(&nl.url)?;
}
if !nl.title.is_empty() {
Expand All @@ -599,7 +599,7 @@ impl<'o> HtmlFormatter<'o> {
},
NodeValue::Image(ref nl) => if entering {
self.output.write_all(b"<img src=\"")?;
if self.options.unsafe_ || !dangerous_url(&nl.url) {
if self.options.render.unsafe_ || !dangerous_url(&nl.url) {
self.escape_href(&nl.url)?;
}
self.output.write_all(b"\" alt=\"")?;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ mod tests;

pub use cm::format_document as format_commonmark;
pub use html::format_document as format_html;
pub use parser::{parse_document, parse_document_with_broken_link_callback, ComrakOptions};
pub use parser::{parse_document, parse_document_with_broken_link_callback, ComrakOptions, ComrakExtensionOptions, ComrakParseOptions, ComrakRenderOptions};
pub use typed_arena::Arena;
pub use html::Anchorizer;

Expand Down
50 changes: 28 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate comrak;
#[macro_use]
extern crate clap;

use comrak::{Arena, ComrakOptions};
use comrak::{Arena, ComrakOptions, ComrakExtensionOptions, ComrakParseOptions, ComrakRenderOptions};

use std::boxed::Box;
use std::collections::BTreeSet;
Expand Down Expand Up @@ -108,27 +108,33 @@ fn main() -> Result<(), Box<dyn Error>> {
.map_or(BTreeSet::new(), |vals| vals.collect());

let options = ComrakOptions {
hardbreaks: matches.is_present("hardbreaks"),
smart: matches.is_present("smart"),
github_pre_lang: matches.is_present("github-pre-lang") || matches.is_present("gfm"),
width: matches
.value_of("width")
.unwrap_or("0")
.parse()
.unwrap_or(0),
default_info_string: matches
.value_of("default-info-string")
.map(|e| e.to_owned()),
unsafe_: matches.is_present("unsafe"),
ext_strikethrough: exts.remove("strikethrough") || matches.is_present("gfm"),
ext_tagfilter: exts.remove("tagfilter") || matches.is_present("gfm"),
ext_table: exts.remove("table") || matches.is_present("gfm"),
ext_autolink: exts.remove("autolink") || matches.is_present("gfm"),
ext_tasklist: exts.remove("tasklist") || matches.is_present("gfm"),
ext_superscript: exts.remove("superscript"),
ext_header_ids: matches.value_of("header-ids").map(|s| s.to_string()),
ext_footnotes: exts.remove("footnotes"),
ext_description_lists: exts.remove("description-lists"),
extension: ComrakExtensionOptions {
strikethrough: exts.remove("strikethrough") || matches.is_present("gfm"),
tagfilter: exts.remove("tagfilter") || matches.is_present("gfm"),
table: exts.remove("table") || matches.is_present("gfm"),
autolink: exts.remove("autolink") || matches.is_present("gfm"),
tasklist: exts.remove("tasklist") || matches.is_present("gfm"),
superscript: exts.remove("superscript"),
header_ids: matches.value_of("header-ids").map(|s| s.to_string()),
footnotes: exts.remove("footnotes"),
description_lists: exts.remove("description-lists"),
},
parse: ComrakParseOptions {
smart: matches.is_present("smart"),
default_info_string: matches
.value_of("default-info-string")
.map(|e| e.to_owned()),
},
render: ComrakRenderOptions {
hardbreaks: matches.is_present("hardbreaks"),
github_pre_lang: matches.is_present("github-pre-lang") || matches.is_present("gfm"),
width: matches
.value_of("width")
.unwrap_or("0")
.parse()
.unwrap_or(0),
unsafe_: matches.is_present("unsafe"),
},
};

if !exts.is_empty() {
Expand Down
38 changes: 19 additions & 19 deletions src/parser/inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
] {
s.special_chars[c as usize] = true;
}
if options.ext_strikethrough {
if options.extension.strikethrough {
s.special_chars[b'~' as usize] = true;
s.skip_chars[b'~' as usize] = true;
}
if options.ext_superscript {
if options.extension.superscript {
s.special_chars[b'^' as usize] = true;
}
for &c in &[b'"', b'\'', b'.', b'-'] {
Expand Down Expand Up @@ -136,9 +136,9 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
new_inl = Some(make_inline(self.arena, NodeValue::Text(b"!".to_vec())));
}
}
_ => if self.options.ext_strikethrough && c == '~' {
_ => if self.options.extension.strikethrough && c == '~' {
new_inl = Some(self.handle_delim(b'~'));
} else if self.options.ext_superscript && c == '^' {
} else if self.options.extension.superscript && c == '^' {
new_inl = Some(self.handle_delim(b'^'));
} else {
let endpos = self.find_special_char();
Expand Down Expand Up @@ -232,10 +232,10 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
i['_' as usize] = stack_bottom;
i['\'' as usize] = stack_bottom;
i['"' as usize] = stack_bottom;
if self.options.ext_strikethrough {
if self.options.extension.strikethrough {
i['~' as usize] = stack_bottom;
}
if self.options.ext_superscript {
if self.options.extension.superscript {
i['^' as usize] = stack_bottom;
}
}
Expand Down Expand Up @@ -305,8 +305,8 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
// both get passed.
if closer.unwrap().delim_char == b'*'
|| closer.unwrap().delim_char == b'_'
|| (self.options.ext_strikethrough && closer.unwrap().delim_char == b'~')
|| (self.options.ext_superscript && closer.unwrap().delim_char == b'^')
|| (self.options.extension.strikethrough && closer.unwrap().delim_char == b'~')
|| (self.options.extension.superscript && closer.unwrap().delim_char == b'^')
{
if opener_found {
// Finally, here's the happy case where the delimiters
Expand Down Expand Up @@ -443,7 +443,7 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
if self.special_chars[self.input[n] as usize] {
return n;
}
if self.options.smart && self.smart_chars[self.input[n] as usize] {
if self.options.parse.smart && self.smart_chars[self.input[n] as usize] {
return n;
}
}
Expand Down Expand Up @@ -532,9 +532,9 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
pub fn handle_delim(&mut self, c: u8) -> &'a AstNode<'a> {
let (numdelims, can_open, can_close) = self.scan_delims(c);

let contents = if c == b'\'' && self.options.smart {
let contents = if c == b'\'' && self.options.parse.smart {
b"\xE2\x80\x99".to_vec()
} else if c == b'"' && self.options.smart {
} else if c == b'"' && self.options.parse.smart {
if can_close {
b"\xE2\x90\x9D".to_vec()
} else {
Expand All @@ -545,7 +545,7 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
};
let inl = make_inline(self.arena, NodeValue::Text(contents));

if (can_open || can_close) && (!(c == b'\'' || c == b'"') || self.options.smart) {
if (can_open || can_close) && (!(c == b'\'' || c == b'"') || self.options.parse.smart) {
self.push_delimiter(c, can_open, can_close, inl);
}

Expand All @@ -556,11 +556,11 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
let start = self.pos;
self.pos += 1;

if !self.options.smart || self.peek_char().map_or(false, |&c| c != b'-') {
if !self.options.parse.smart || self.peek_char().map_or(false, |&c| c != b'-') {
return make_inline(self.arena, NodeValue::Text(vec![b'-']));
}

while self.options.smart && self.peek_char().map_or(false, |&c| c == b'-') {
while self.options.parse.smart && self.peek_char().map_or(false, |&c| c == b'-') {
self.pos += 1;
}

Expand Down Expand Up @@ -589,7 +589,7 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {

pub fn handle_period(&mut self) -> &'a AstNode<'a> {
self.pos += 1;
if self.options.smart && self.peek_char().map_or(false, |&c| c == b'.') {
if self.options.parse.smart && self.peek_char().map_or(false, |&c| c == b'.') {
self.pos += 1;
if self.peek_char().map_or(false, |&c| c == b'.') {
self.pos += 1;
Expand Down Expand Up @@ -723,7 +723,7 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
opener_num_chars -= use_delims;
closer_num_chars -= use_delims;

if self.options.ext_strikethrough && opener_char == b'~' {
if self.options.extension.strikethrough && opener_char == b'~' {
if opener_num_chars != closer_num_chars ||
opener_num_chars > 0 {
return None
Expand Down Expand Up @@ -757,9 +757,9 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {

let emph = make_inline(
self.arena,
if self.options.ext_strikethrough && opener_char == b'~' {
if self.options.extension.strikethrough && opener_char == b'~' {
NodeValue::Strikethrough
} else if self.options.ext_superscript && opener_char == b'^' {
} else if self.options.extension.superscript && opener_char == b'^' {
NodeValue::Superscript
} else if use_delims == 1 {
NodeValue::Emph
Expand Down Expand Up @@ -970,7 +970,7 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
}

let mut text: Option<Vec<u8>> = None;
if self.options.ext_footnotes
if self.options.extension.footnotes
&& match self.brackets[brackets_len - 1].inl_text.next_sibling() {
Some(n) => {
text = n.data.borrow().value.text().cloned();
Expand Down
Loading

0 comments on commit 660ec92

Please sign in to comment.