about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-01-19T23·46+0000
committerVincent Ambo <tazjin@google.com>2020-01-19T23·54+0000
commitd752cbecc0b36264e36cd870c6651ea8303d16ba (patch)
treecd8ba74cf1cd351820f89061786d1782cab8c631
parent47541340c0ac7226263f9151f1bf0f133144ae7e (diff)
feat(cheddar): Add toggle flag for about-filter behaviour r/426
Cheddar now needs to be passed the --about-filter flag to toggle the
behaviour for rendering Markdown into HTML.

By default Markdown will be highlighted like normal source code (i.e.
cgit source-filtering is the default behaviour).
-rw-r--r--tools/cheddar/src/main.rs51
1 files changed, 38 insertions, 13 deletions
diff --git a/tools/cheddar/src/main.rs b/tools/cheddar/src/main.rs
index d0115d391c9d..8968e4382310 100644
--- a/tools/cheddar/src/main.rs
+++ b/tools/cheddar/src/main.rs
@@ -46,17 +46,41 @@ lazy_static! {
 // Emulates the GitHub style (subtle background hue and padding).
 const BLOCK_PRE: &str = "<pre style=\"background-color:#f6f8fa;padding:16px;\">\n";
 
-fn args_extension() -> Option<String> {
-    // The name of the file to be formatted is usually passed in as
-    // the first argument and can be used to determine a syntax set.
-    let args = env::args().collect::<Vec<String>>();
-    if args.len() != 2 {
-        return None
+struct Args {
+    /// Should Cheddar run as an about filter? (i.e. give special
+    /// rendering treatment to Markdown documents)
+    about_filter: bool,
+
+    /// What file extension has been supplied (if any)?
+    extension: Option<String>,
+}
+
+/// Parse the command-line flags passed to cheddar to determine
+/// whether it is running in about-filter mode (`--about-filter`) and
+/// what file extension has been supplied.
+fn parse_args() -> Args {
+    let mut args = Args {
+        about_filter: false,
+        extension: None,
+    };
+
+    for (i, arg) in env::args().enumerate() {
+        if i == 0 {
+            continue;
+        }
+
+        if arg == "--about-filter" {
+            args.about_filter = true;
+            continue;
+        }
+
+        args.extension = Path::new(&arg)
+            .extension()
+            .and_then(OsStr::to_str)
+            .map(|s| s.to_string());
     }
 
-    Path::new(&args[1]).extension()
-        .and_then(OsStr::to_str)
-        .map(|s| s.to_string())
+    return args
 }
 
 fn should_continue(res: &io::Result<usize>) -> bool {
@@ -190,9 +214,10 @@ fn format_code(extension: Option<&str>) {
 }
 
 fn main() {
-    let extension = args_extension();
-    match extension.as_ref().map(String::as_str) {
-        Some("md") => format_markdown(),
-        extension => format_code(extension),
+    let args = parse_args();
+
+    match args.extension.as_ref().map(String::as_str) {
+        Some("md") if args.about_filter => format_markdown(),
+        extension  => format_code(extension),
     }
 }