refactor(cheddar): Pass references readers & writers into renderers

This paves the way for using other things than stdin/stdout as
sources/sinks, which is required for example for implementing a
syntect_server replacement based on cheddar.

Change-Id: I5779db8dbf7b7ced109c26b940f721d237d60785
Reviewed-on: https://cl.tvl.fyi/c/depot/+/491
Reviewed-by: glittershark <grfn@gws.fyi>
Reviewed-by: lukegb <lukegb@tvl.fyi>
This commit is contained in:
Vincent Ambo 2020-06-19 03:35:05 +01:00 committed by tazjin
parent 9b5b88ae6c
commit d1524a5a6f

View file

@ -8,7 +8,7 @@ use std::collections::HashMap;
use std::env; use std::env;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::io::BufRead; use std::io::BufRead;
use std::io::Read; use std::io::Write;
use std::io; use std::io;
use std::path::Path; use std::path::Path;
use syntect::dumps::from_binary; use syntect::dumps::from_binary;
@ -182,12 +182,11 @@ fn format_callout_paragraph(callout: Callout) -> NodeValue {
}) })
} }
fn format_markdown() { fn format_markdown<R: BufRead, W: Write>(reader: &mut R, writer: &mut W) {
let document = { let document = {
let mut buffer = String::new(); let mut buffer = String::new();
let stdin = io::stdin(); reader.read_to_string(&mut buffer).expect("reading should work");
let mut stdin = stdin.lock(); drop(reader);
stdin.read_to_string(&mut buffer).expect("failed to read stdin");
buffer buffer
}; };
@ -228,17 +227,15 @@ fn format_markdown() {
} }
}); });
format_html(root, &MD_OPTS, &mut io::stdout()) format_html(root, &MD_OPTS, writer)
.expect("Markdown rendering failed"); .expect("Markdown rendering failed");
} }
fn format_code(args: &Args) { fn format_code<R: BufRead, W: Write>(reader: &mut R, writer: &mut W, args: &Args) {
let stdin = io::stdin();
let mut stdin = stdin.lock();
let mut linebuf = String::new(); let mut linebuf = String::new();
// Get the first line, we might need it for syntax identification. // Get the first line, we might need it for syntax identification.
let mut read_result = stdin.read_line(&mut linebuf); let mut read_result = reader.read_line(&mut linebuf);
// Set up the highlighter // Set up the highlighter
let theme = &THEMES.themes["InspiredGitHub"]; let theme = &THEMES.themes["InspiredGitHub"];
@ -272,15 +269,15 @@ fn format_code(args: &Args) {
// immediately output the current state to avoid keeping // immediately output the current state to avoid keeping
// things in memory // things in memory
print!("{}", outbuf); write!(writer, "{}", outbuf).expect("write should not fail");
// merry go round again // merry go round again
linebuf.clear(); linebuf.clear();
outbuf.clear(); outbuf.clear();
read_result = stdin.read_line(&mut linebuf); read_result = reader.read_line(&mut linebuf);
} }
println!("</pre>"); writeln!(writer, "</pre>").expect("write should not fail");
} }
fn main() { fn main() {
@ -311,8 +308,14 @@ fn main() {
.and_then(OsStr::to_str) .and_then(OsStr::to_str)
.map(|s| s.to_string()); .map(|s| s.to_string());
let stdin = io::stdin();
let mut in_handle = stdin.lock();
let stdout = io::stdout();
let mut out_handle = stdout.lock();
match args.extension.as_ref().map(String::as_str) { match args.extension.as_ref().map(String::as_str) {
Some("md") if args.about_filter => format_markdown(), Some("md") if args.about_filter => format_markdown(&mut in_handle, &mut out_handle),
_ => format_code(&args), _ => format_code(&mut in_handle, &mut out_handle, &args),
} }
} }