Skip to content

Commit

Permalink
Merge pull request #49 from kivikakk/default-info-string
Browse files Browse the repository at this point in the history
default fenced code block info strings
  • Loading branch information
Ashe Connor authored Feb 1, 2018
2 parents 23e978c + beebb8c commit dfe55ae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ FLAGS:
-V, --version Prints version information
OPTIONS:
-e, --extension <EXTENSION>... Specify an extension name to use [values: strikethrough, tagfilter, table, autolink, tasklist, superscript, footnotes]
-t, --to <FORMAT> Specify output format [default: html] [values: html, commonmark]
--header-ids <PREFIX> Use the Comrak header IDs extension, with the given ID prefix
--width <WIDTH> Specify wrap width (0 = nowrap) [default: 0]
--default-info-string <INFO> Default value for fenced code block's info strings if none is given
-e, --extension <EXTENSION>... Specify an extension name to use [values: strikethrough, tagfilter, table,
autolink, tasklist, superscript, footnotes]
-t, --to <FORMAT> Specify output format [default: html] [values: html, commonmark]
--header-ids <PREFIX> Use the Comrak header IDs extension, with the given ID prefix
--width <WIDTH> Specify wrap width (0 = nowrap) [default: 0]
ARGS:
<FILE>... The CommonMark file to parse; or standard input if none passed
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ fn main() {
.long("github-pre-lang")
.help("Use GitHub-style <pre lang> for code blocks"),
)
.arg(
clap::Arg::with_name("default-info-string")
.long("default-info-string")
.help("Default value for fenced code block's info strings if none is given")
.value_name("INFO")
.takes_value(true)
)
.arg(
clap::Arg::with_name("extension")
.short("e")
Expand Down Expand Up @@ -117,6 +124,9 @@ fn main() {
.unwrap_or("0")
.parse()
.unwrap_or(0),
default_info_string: matches
.value_of("default-info-string")
.map(|e| e.to_owned()),
ext_strikethrough: exts.remove("strikethrough"),
ext_tagfilter: exts.remove("tagfilter"),
ext_table: exts.remove("table"),
Expand Down
23 changes: 22 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ pub struct ComrakOptions {
/// ```
pub width: usize,

/// The default info string for fenced code blocks.
///
/// ```
/// # use comrak::{markdown_to_html, ComrakOptions};
/// let mut options = ComrakOptions::default();
/// assert_eq!(markdown_to_html("```\nfn hello();\n```\n", &options),
/// "<pre><code>fn hello();\n</code></pre>\n");
///
/// options.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");
/// ```
pub default_info_string: Option<String>,

/// Enables the
/// [strikethrough extension](https://github.github.com/gfm/#strikethrough-extension-)
/// from the GFM spec.
Expand Down Expand Up @@ -1062,7 +1076,14 @@ impl<'a, 'o> Parser<'a, 'o> {
let mut tmp = entity::unescape_html(&content[..pos]);
strings::trim(&mut tmp);
strings::unescape(&mut tmp);
ncb.info = tmp;
if tmp.is_empty() {
ncb.info = self.options
.default_info_string
.as_ref()
.map_or(vec![], |s| s.as_bytes().to_vec());
} else {
ncb.info = tmp;
}

if content[pos] == b'\r' {
pos += 1;
Expand Down

0 comments on commit dfe55ae

Please sign in to comment.