mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-09 09:49:10 +02:00
Merge pull request #95 from Plommonsorbet/render_as_markdown_option
Add -m / --markdown option for raw rendering
This commit is contained in:
commit
23d7301b53
7 changed files with 76 additions and 11 deletions
|
|
@ -71,6 +71,7 @@ These are the clients I tried but failed to compile or run:
|
|||
-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
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ _tealdeer()
|
|||
_init_completion || return
|
||||
|
||||
case $prev in
|
||||
-h|--help|-v|--version|-l|--list|-u|--update|-c|--clear-cache|--config-path|--seed-config|-q|--quiet)
|
||||
-h|--help|-v|--version|-l|--list|-u|--update|-c|--clear-cache|-p|--pager|-m|--markdown|--config-path|--seed-config|-q|--quiet)
|
||||
return
|
||||
;;
|
||||
-f|--render)
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ complete -c tldr -s f -l render -d 'Render a specific markdown file.' -r
|
|||
complete -c tldr -s o -l os -d 'Override the operating system.' -xa 'linux osx sunos windows other'
|
||||
complete -c tldr -s u -l update -d 'Update the local cache.' -f
|
||||
complete -c tldr -s c -l clear-cache -d 'Clear the local cache.' -f
|
||||
complete -c tldr -s p -l pager -d 'Use a pager to page output.' -f
|
||||
complete -c tldr -s m -l markdown -d 'Display the raw markdown instead of rendering it.' -f
|
||||
complete -c tldr -s q -l quiet -d 'Suppress informational messages.' -f
|
||||
complete -c tldr -l config-path -d 'Show config file path.' -f
|
||||
complete -c tldr -l seed-config -d 'Create a basic config.' -f
|
||||
|
|
|
|||
22
src/main.rs
22
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 {
|
||||
|
|
|
|||
22
tests/lib.rs
22
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()])
|
||||
|
|
|
|||
33
tests/tar-markdown.expected
Normal file
33
tests/tar-markdown.expected
Normal 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"}}`
|
||||
|
|
@ -8,7 +8,7 @@ _tealdeer() {
|
|||
local I="-h --help -v --version"
|
||||
integer ret=1
|
||||
local -a args
|
||||
|
||||
|
||||
args+=(
|
||||
"($I -l --list)"{-l,--list}"[List all commands in the cache]"
|
||||
"($I -f --render)"{-f,--render}"[Render a specific markdown file]:file:_files"
|
||||
|
|
@ -21,6 +21,7 @@ _tealdeer() {
|
|||
"($I -u --update)"{-u,--update}"[Update the local cache]"
|
||||
"($I -c --clear-cache)"{-c,--clear-cache}"[Clear the local cache]"
|
||||
"($I -p --pager)"{-p,--pager}"[Use a pager to page output]"
|
||||
"($I -m --markdown)"{-m,--markdown}"[Display the raw markdown instead of rendering it]"
|
||||
"($I -q --quiet)"{-q,--quiet}"[Suppress informational messages]"
|
||||
"($I)--config-path[Show config file path]"
|
||||
"($I)--seed-config[Create a basic config]"
|
||||
|
|
@ -29,7 +30,7 @@ _tealdeer() {
|
|||
'*:file:_files'
|
||||
'1: :_applications'
|
||||
)
|
||||
|
||||
|
||||
_arguments $args[@] && ret=0
|
||||
return ret
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue