diff options
| author | Philipp A | 2023-12-28 15:43:24 +0100 | 
|---|---|---|
| committer | GitHub | 2023-12-28 15:43:24 +0100 | 
| commit | 0f4f1a420cbcf263a9118ec9b288c95f1b59ade8 (patch) | |
| tree | 030abd05e8894e4e9c6775794f890180cf6c5e91 /rst/src/main.rs | |
| parent | 38b1c488601cfc2479f02df555b135ef01aa5618 (diff) | |
| parent | 774dd4798aedc40b38c6480e9c47f34c482f12d0 (diff) | |
| download | rust-rst-0f4f1a420cbcf263a9118ec9b288c95f1b59ade8.tar.bz2 | |
Merge branch 'main' into allow-rst-to-read-from-stdin
Diffstat (limited to 'rst/src/main.rs')
| -rw-r--r-- | rst/src/main.rs | 82 | 
1 files changed, 41 insertions, 41 deletions
| diff --git a/rst/src/main.rs b/rst/src/main.rs index 1418a5e..6a7ed44 100644 --- a/rst/src/main.rs +++ b/rst/src/main.rs @@ -1,59 +1,59 @@ -use structopt::StructOpt;  use clap::arg_enum;  use quicli::{ -	fs::read_file, -	prelude::{CliResult,Verbosity}, +    fs::read_file, +    prelude::{CliResult, Verbosity},  }; +use structopt::StructOpt;  use rst_parser::parse; -use rst_renderer::{ -	render_json, -	render_xml, -	render_html, -}; +use rst_renderer::{render_html, render_json, render_xml};  use std::io::{self, Read};  arg_enum! { -	#[derive(Debug)] -	#[allow(non_camel_case_types)] -	enum Format { json, xml, html } +    #[derive(Debug)] +    #[allow(non_camel_case_types)] +    enum Format { json, xml, html }  }  #[derive(Debug, StructOpt)]  #[structopt(raw(setting = "structopt::clap::AppSettings::ColoredHelp"))]  struct Cli { -	#[structopt( -		long = "format", short = "f", default_value = "html",  // xml is pretty defunct… -		raw(possible_values = "&Format::variants()", case_insensitive = "true"), -	)] -	format: Format, -	file: Option<String>, -	#[structopt(flatten)] -	verbosity: Verbosity, +    #[structopt( +        long = "format", short = "f", default_value = "html",  // xml is pretty defunct… +        raw(possible_values = "&Format::variants()", case_insensitive = "true"), +    )] +    format: Format, +    file: Option<String>, +    #[structopt(flatten)] +    verbosity: Verbosity,  }  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 = content.replace('\t', " ".repeat(8).as_ref()); -	let document = parse(&content)?; -	let stdout = std::io::stdout(); -	match args.format { -		Format::json => render_json(&document, stdout)?, -		Format::xml  => render_xml (&document, stdout)?, -		Format::html => render_html(&document, stdout, true)?, -	} -	Ok(()) +    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 mut content = read_file(args.file)?.replace('\t', " ".repeat(8).as_ref()); +    // Allows for less complex grammar +    if !content.ends_with('\n') { +        content.push('\n'); +    } +    let document = parse(&content)?; +    let stdout = std::io::stdout(); +    match args.format { +        Format::json => render_json(&document, stdout)?, +        Format::xml  => render_xml (&document, stdout)?, +        Format::html => render_html(&document, stdout, true)?, +    } +    Ok(())  } | 
