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.
This commit is contained in:
Isak Johansson 2019-12-17 08:56:19 +01:00 committed by Danilo Bargen
commit 6719acb4f2
3 changed files with 69 additions and 8 deletions

View file

@ -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 {

View file

@ -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()])

View file

@ -0,0 +1,33 @@
# tar
> Archiving utility.
> Often combined with a compression method, such as gzip or bzip.
> More information: <https://www.gnu.org/software/tar>.
- 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"}}`