Add flag to print to stdout

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/17/head
trivernis 4 years ago
parent 673a7527d6
commit 661a6e5a85
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

2
Cargo.lock generated

@ -2315,7 +2315,7 @@ checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
[[package]] [[package]]
name = "snekdown" name = "snekdown"
version = "0.32.2" version = "0.32.3"
dependencies = [ dependencies = [
"asciimath-rs", "asciimath-rs",
"base64 0.12.3", "base64 0.12.3",

@ -1,6 +1,6 @@
[package] [package]
name = "snekdown" name = "snekdown"
version = "0.32.2" version = "0.32.3"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
license-file = "LICENSE" license-file = "LICENSE"

@ -10,7 +10,7 @@ use snekdown::settings::Settings;
use snekdown::utils::caching::CacheStorage; use snekdown::utils::caching::CacheStorage;
use snekdown::Parser; use snekdown::Parser;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write}; use std::io::{stdout, BufWriter, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
@ -48,7 +48,11 @@ struct RenderOptions {
/// Path for the output file /// Path for the output file
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]
output: PathBuf, output: Option<PathBuf>,
/// If the output should be written to stdout instead of the output file
#[structopt(long = "stdout")]
stdout: bool,
/// the output format /// the output format
#[structopt(short, long, default_value = "html")] #[structopt(short, long, default_value = "html")]
@ -165,6 +169,7 @@ fn render(opt: &RenderOptions) -> Parser {
exit(1) exit(1)
} }
let start = Instant::now(); let start = Instant::now();
let mut parser = Parser::with_defaults(ParserOptions::default().add_path(opt.input.clone())); let mut parser = Parser::with_defaults(ParserOptions::default().add_path(opt.input.clone()));
@ -173,16 +178,24 @@ fn render(opt: &RenderOptions) -> Parser {
log::info!("Parsing + Processing took: {:?}", start.elapsed()); log::info!("Parsing + Processing took: {:?}", start.elapsed());
let start_render = Instant::now(); let start_render = Instant::now();
let file = OpenOptions::new() if let Some(output) = &opt.output {
.read(true) let file = OpenOptions::new()
.write(true) .read(true)
.truncate(true) .write(true)
.create(true) .truncate(true)
.open(&opt.output) .create(true)
.unwrap(); .open(output)
let writer = BufWriter::new(file); .unwrap();
render_format(opt, document, BufWriter::new(file));
} else {
if !opt.stdout {
log::error!("No output file specified");
exit(1)
}
render_format(opt, document, BufWriter::new(stdout()));
}
render_format(opt, document, writer);
log::info!("Rendering took: {:?}", start_render.elapsed()); log::info!("Rendering took: {:?}", start_render.elapsed());
log::info!("Total: {:?}", start.elapsed()); log::info!("Total: {:?}", start.elapsed());
@ -190,7 +203,7 @@ fn render(opt: &RenderOptions) -> Parser {
} }
#[cfg(not(feature = "pdf"))] #[cfg(not(feature = "pdf"))]
fn render_format(opt: &RenderOptions, document: Document, writer: BufWriter<File>) { fn render_format<W: Write + 'static>(opt: &RenderOptions, document: Document, writer: W) {
match opt.format.as_str() { match opt.format.as_str() {
"html" => render_html(document, writer), "html" => render_html(document, writer),
_ => log::error!("Unknown format {}", opt.format), _ => log::error!("Unknown format {}", opt.format),
@ -198,7 +211,7 @@ fn render_format(opt: &RenderOptions, document: Document, writer: BufWriter<File
} }
#[cfg(feature = "pdf")] #[cfg(feature = "pdf")]
fn render_format(opt: &RenderOptions, document: Document, writer: BufWriter<File>) { fn render_format<W: Write + 'static>(opt: &RenderOptions, document: Document, writer: W) {
match opt.format.as_str() { match opt.format.as_str() {
"html" => render_html(document, writer), "html" => render_html(document, writer),
"pdf" => render_pdf(document, writer), "pdf" => render_pdf(document, writer),
@ -206,14 +219,14 @@ fn render_format(opt: &RenderOptions, document: Document, writer: BufWriter<File
} }
} }
fn render_html(document: Document, writer: BufWriter<File>) { fn render_html<W: Write + 'static>(document: Document, writer: W) {
let mut writer = HTMLWriter::new(Box::new(writer), document.config.lock().style.theme.clone()); let mut writer = HTMLWriter::new(Box::new(writer), document.config.lock().style.theme.clone());
document.to_html(&mut writer).unwrap(); document.to_html(&mut writer).unwrap();
writer.flush().unwrap(); writer.flush().unwrap();
} }
#[cfg(feature = "pdf")] #[cfg(feature = "pdf")]
fn render_pdf(document: Document, mut writer: BufWriter<File>) { fn render_pdf<W: Write + 'static>(document: Document, mut writer: W) {
use snekdown::format::chromium_pdf::render_to_pdf; use snekdown::format::chromium_pdf::render_to_pdf;
let result = render_to_pdf(document).expect("Failed to render pdf!"); let result = render_to_pdf(document).expect("Failed to render pdf!");

Loading…
Cancel
Save