diff options
| author | Teddy Wing | 2021-06-27 23:47:50 +0200 |
|---|---|---|
| committer | Teddy Wing | 2021-06-27 23:47:50 +0200 |
| commit | 38b1c488601cfc2479f02df555b135ef01aa5618 (patch) | |
| tree | 02516c03e19be86efed57a4d11989cfb67ac6c94 /rst/src | |
| parent | c2eace26cd421ab773f325264eaae0c4e15e932c (diff) | |
| download | rust-rst-38b1c488601cfc2479f02df555b135ef01aa5618.tar.bz2 | |
rst: Allow reading from standard input
Enable the `rst` binary to read a document from standard input. If no
`file` command line argument is given, assume that the document comes
from standard input.
Diffstat (limited to 'rst/src')
| -rw-r--r-- | rst/src/main.rs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/rst/src/main.rs b/rst/src/main.rs index 318bcb6..1418a5e 100644 --- a/rst/src/main.rs +++ b/rst/src/main.rs @@ -12,6 +12,8 @@ use rst_renderer::{ render_html, }; +use std::io::{self, Read}; + arg_enum! { #[derive(Debug)] #[allow(non_camel_case_types)] @@ -26,7 +28,7 @@ struct Cli { raw(possible_values = "&Format::variants()", case_insensitive = "true"), )] format: Format, - file: String, + file: Option<String>, #[structopt(flatten)] verbosity: Verbosity, } @@ -34,9 +36,18 @@ struct Cli { fn main() -> CliResult { let args = Cli::from_args(); args.verbosity.setup_env_logger("rst")?; - + + let content = if let Some(file) = args.file { + read_file(file)? + } else { + let mut stdin = String::new(); + io::stdin().read_to_string(&mut stdin)?; + + stdin + }; + // TODO: somehow make it work without replacing tabs - let content = read_file(args.file)?.replace('\t', " ".repeat(8).as_ref()); + let content = content.replace('\t', " ".repeat(8).as_ref()); let document = parse(&content)?; let stdout = std::io::stdout(); match args.format { |
