diff --git a/README.md b/README.md index 5adffbe7..1e456b90 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,12 @@ FLAGS: -V, --version Prints version information OPTIONS: - -e, --extension ... Specify an extension name to use [values: strikethrough, tagfilter, table, autolink, tasklist, superscript, footnotes] - -t, --to Specify output format [default: html] [values: html, commonmark] - --header-ids Use the Comrak header IDs extension, with the given ID prefix - --width Specify wrap width (0 = nowrap) [default: 0] + --default-info-string Default value for fenced code block's info strings if none is given + -e, --extension ... Specify an extension name to use [values: strikethrough, tagfilter, table, + autolink, tasklist, superscript, footnotes] + -t, --to Specify output format [default: html] [values: html, commonmark] + --header-ids Use the Comrak header IDs extension, with the given ID prefix + --width Specify wrap width (0 = nowrap) [default: 0] ARGS: ... The CommonMark file to parse; or standard input if none passed diff --git a/src/main.rs b/src/main.rs index 3961d554..61d6feab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,6 +54,13 @@ fn main() { .long("github-pre-lang") .help("Use GitHub-style
 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")
@@ -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"),
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index f106dca9..6a09349f 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -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),
+    ///            "
fn hello();\n
\n"); + /// + /// options.default_info_string = Some("rust".into()); + /// assert_eq!(markdown_to_html("```\nfn hello();\n```\n", &options), + /// "
fn hello();\n
\n"); + /// ``` + pub default_info_string: Option, + /// Enables the /// [strikethrough extension](https://github.github.com/gfm/#strikethrough-extension-) /// from the GFM spec. @@ -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;