From 6719acb4f23dd15d53bce607f7b1e684bd098f31 Mon Sep 17 00:00:00 2001 From: Isak Johansson Date: Tue, 17 Dec 2019 08:56:19 +0100 Subject: [PATCH] Add -m / --markdown option for raw rendering New option to render as markdown, not with ANSI escape codes. This is useful for when you want to open it up in a pager which doesn't support ANSI escape codes by default. In my use case it allows me to open it inside vim without having to use an addon to convert ANSI escape codes to colours. --- src/main.rs | 22 ++++++++++++++++------ tests/lib.rs | 22 ++++++++++++++++++++-- tests/tar-markdown.expected | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 tests/tar-markdown.expected diff --git a/src/main.rs b/src/main.rs index 8de5ef0..b56ab4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ extern crate env_logger; use std::fs::File; use std::io::BufReader; +use std::io::BufRead; use std::path::{Path, PathBuf}; use std::process; use std::time::Duration; @@ -64,6 +65,7 @@ Options: -u --update Update the local cache -c --clear-cache Clear the local cache -p --pager Use a pager to page output + -m --markdown Display the raw markdown instead of rendering it -q --quiet Suppress informational messages --config-path Show config file path --seed-config Create a basic config @@ -100,10 +102,11 @@ struct Args { flag_quiet: bool, flag_config_path: bool, flag_seed_config: bool, + flag_markdown: bool, } /// Print page by path -fn print_page(path: &Path, enable_styles: bool) -> Result<(), String> { +fn print_page(path: &Path, enable_markdown: bool, enable_styles: bool) -> Result<(), String> { // Open file let file = File::open(path).map_err(|msg| format!("Could not open file: {}", msg))?; let reader = BufReader::new(file); @@ -121,9 +124,16 @@ fn print_page(path: &Path, enable_styles: bool) -> Result<(), String> { } }; - // Create tokenizer and print output - let mut tokenizer = Tokenizer::new(reader); - print_lines(&mut tokenizer, &config); + if enable_markdown { + // Print the raw markdown of the file. + for line in reader.lines() { + println!("{}", line.unwrap()); + } + } else { + // Create tokenizer and print output + let mut tokenizer = Tokenizer::new(reader); + print_lines(&mut tokenizer, &config); + }; Ok(()) } @@ -324,7 +334,7 @@ fn main() { // Render local file and exit if let Some(ref file) = args.flag_render { let path = PathBuf::from(file); - if let Err(msg) = print_page(&path, enable_styles) { + if let Err(msg) = print_page(&path, args.flag_markdown, enable_styles) { eprintln!("{}", msg); process::exit(1); } else { @@ -360,7 +370,7 @@ fn main() { // Search for command in cache if let Some(path) = cache.find_page(&command) { - if let Err(msg) = print_page(&path, enable_styles) { + if let Err(msg) = print_page(&path, args.flag_markdown, enable_styles) { eprintln!("{}", msg); process::exit(1); } else { diff --git a/tests/lib.rs b/tests/lib.rs index bb1e2d3..74f4a1e 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -193,6 +193,26 @@ fn test_show_config_path() { ))); } +#[test] +fn test_markdown_rendering() { + let testenv = TestEnv::new(); + + testenv + .command() + .args(&["--update"]) + .assert() + .success() + .stdout(contains("Successfully updated cache.")); + + let expected = include_str!("tar-markdown.expected"); + testenv + .command() + .args(&["-m", "tar"]) + .assert() + .success() + .stdout(similar(expected)); +} + fn _test_correct_rendering(input_file: &str, filename: &str) { let testenv = TestEnv::new(); @@ -202,9 +222,7 @@ fn _test_correct_rendering(input_file: &str, filename: &str) { let mut file = File::create(&file_path).unwrap(); file.write_all(input_file.as_bytes()).unwrap(); - // Load expected output let expected = include_str!("inkscape-default.expected"); - testenv .command() .args(&["-f", &file_path.to_str().unwrap()]) diff --git a/tests/tar-markdown.expected b/tests/tar-markdown.expected new file mode 100644 index 0000000..d141430 --- /dev/null +++ b/tests/tar-markdown.expected @@ -0,0 +1,33 @@ +# tar + +> Archiving utility. +> Often combined with a compression method, such as gzip or bzip. +> More information: . + +- Create an archive from files: + +`tar cf {{target.tar}} {{file1}} {{file2}} {{file3}}` + +- Create a gzipped archive: + +`tar czf {{target.tar.gz}} {{file1}} {{file2}} {{file3}}` + +- Extract a (compressed) archive into the current directory: + +`tar xf {{source.tar[.gz|.bz2|.xz]}}` + +- Extract an archive into a target directory: + +`tar xf {{source.tar}} -C {{directory}}` + +- Create a compressed archive, using archive suffix to determine the compression program: + +`tar caf {{target.tar.xz}} {{file1}} {{file2}} {{file3}}` + +- List the contents of a tar file: + +`tar tvf {{source.tar}}` + +- Extract files matching a pattern: + +`tar xf {{source.tar}} --wildcards {{"*.html"}}`