style(cheddar): Reformat with rustfmt

Change-Id: I9a0764f23c1269447a8b18f3128074fc58f70456
Reviewed-on: https://cl.tvl.fyi/c/depot/+/492
Reviewed-by: glittershark <grfn@gws.fyi>
This commit is contained in:
Vincent Ambo 2020-06-19 03:39:53 +01:00 committed by tazjin
parent d1524a5a6f
commit dbb4a57bbf

View file

@ -1,26 +1,24 @@
use clap::{Arg, App}; use clap::{App, Arg};
use comrak::arena_tree::Node; use comrak::arena_tree::Node;
use comrak::nodes::{Ast, AstNode, NodeValue, NodeCodeBlock, NodeHtmlBlock}; use comrak::nodes::{Ast, AstNode, NodeCodeBlock, NodeHtmlBlock, NodeValue};
use comrak::{Arena, parse_document, format_html, ComrakOptions}; use comrak::{format_html, parse_document, Arena, ComrakOptions};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::io;
use std::io::BufRead; use std::io::BufRead;
use std::io::Write; use std::io::Write;
use std::io;
use std::path::Path; use std::path::Path;
use syntect::dumps::from_binary; use syntect::dumps::from_binary;
use syntect::easy::HighlightLines; use syntect::easy::HighlightLines;
use syntect::highlighting::ThemeSet; use syntect::highlighting::ThemeSet;
use syntect::parsing::{SyntaxSet, SyntaxReference}; use syntect::parsing::{SyntaxReference, SyntaxSet};
use syntect::util::LinesWithEndings; use syntect::util::LinesWithEndings;
use syntect::html::{ use syntect::html::{
IncludeBackground, append_highlighted_html_for_styled_line, start_highlighted_html_snippet, IncludeBackground,
append_highlighted_html_for_styled_line,
start_highlighted_html_snippet,
}; };
lazy_static! { lazy_static! {
@ -81,7 +79,10 @@ fn should_continue(res: &io::Result<usize>) -> bool {
} }
// This function is taken from the Comrak documentation. // This function is taken from the Comrak documentation.
fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F) where F : Fn(&'a AstNode<'a>) { fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F)
where
F: Fn(&'a AstNode<'a>),
{
f(node); f(node);
for c in node.children() { for c in node.children() {
iter_nodes(c, f); iter_nodes(c, f);
@ -95,7 +96,11 @@ fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F) where F : Fn(&'a AstNode<'a>)
// ASCII characters, anyways). // ASCII characters, anyways).
fn find_syntax_case_insensitive(info: &str) -> Option<&'static SyntaxReference> { fn find_syntax_case_insensitive(info: &str) -> Option<&'static SyntaxReference> {
// TODO(tazjin): memoize this lookup // TODO(tazjin): memoize this lookup
SYNTAXES.syntaxes().iter().rev().find(|&s| info.eq_ignore_ascii_case(&s.name)) SYNTAXES
.syntaxes()
.iter()
.rev()
.find(|&s| info.eq_ignore_ascii_case(&s.name))
} }
// Replaces code-block inside of a Markdown AST with HTML blocks rendered by // Replaces code-block inside of a Markdown AST with HTML blocks rendered by
@ -119,9 +124,7 @@ fn highlight_code_block(code_block: &NodeCodeBlock) -> NodeValue {
for line in LinesWithEndings::from(&code) { for line in LinesWithEndings::from(&code) {
let regions = hl.highlight(line, &SYNTAXES); let regions = hl.highlight(line, &SYNTAXES);
append_highlighted_html_for_styled_line( append_highlighted_html_for_styled_line(&regions[..], IncludeBackground::No, &mut buf);
&regions[..], IncludeBackground::No, &mut buf,
);
} }
buf.push_str("</pre>"); buf.push_str("</pre>");
@ -151,17 +154,17 @@ fn has_callout<'a>(node: &Node<'a, RefCell<Ast>>) -> Option<Callout> {
Some(child) => match &child.value { Some(child) => match &child.value {
NodeValue::Text(text) => { NodeValue::Text(text) => {
if text.starts_with("TODO".as_bytes()) { if text.starts_with("TODO".as_bytes()) {
return Some(Callout::Todo) return Some(Callout::Todo);
} else if text.starts_with("WARNING".as_bytes()) { } else if text.starts_with("WARNING".as_bytes()) {
return Some(Callout::Warning) return Some(Callout::Warning);
} else if text.starts_with("QUESTION".as_bytes()) { } else if text.starts_with("QUESTION".as_bytes()) {
return Some(Callout::Question) return Some(Callout::Question);
} else if text.starts_with("TIP".as_bytes()) { } else if text.starts_with("TIP".as_bytes()) {
return Some(Callout::Tip) return Some(Callout::Tip);
} }
return None return None;
}, }
_ => return None, _ => return None,
}, },
_ => return None, _ => return None,
@ -185,7 +188,9 @@ fn format_callout_paragraph(callout: Callout) -> NodeValue {
fn format_markdown<R: BufRead, W: Write>(reader: &mut R, writer: &mut W) { 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();
reader.read_to_string(&mut buffer).expect("reading should work"); reader
.read_to_string(&mut buffer)
.expect("reading should work");
drop(reader); drop(reader);
buffer buffer
}; };
@ -213,12 +218,14 @@ fn format_markdown<R: BufRead, W: Write>(reader: &mut R, writer: &mut W) {
let mut ast = node.data.borrow_mut(); let mut ast = node.data.borrow_mut();
let new = match &ast.value { let new = match &ast.value {
NodeValue::CodeBlock(code) => Some(highlight_code_block(code)), NodeValue::CodeBlock(code) => Some(highlight_code_block(code)),
NodeValue::Paragraph => if let Some(callout) = has_callout(node) { NodeValue::Paragraph => {
if let Some(callout) = has_callout(node) {
node.insert_after(&p_close); node.insert_after(&p_close);
Some(format_callout_paragraph(callout)) Some(format_callout_paragraph(callout))
} else { } else {
None None
}, }
}
_ => None, _ => None,
}; };
@ -227,8 +234,7 @@ fn format_markdown<R: BufRead, W: Write>(reader: &mut R, writer: &mut W) {
} }
}); });
format_html(root, &MD_OPTS, writer) format_html(root, &MD_OPTS, writer).expect("Markdown rendering failed");
.expect("Markdown rendering failed");
} }
fn format_code<R: BufRead, W: Write>(reader: &mut R, writer: &mut W, args: &Args) { fn format_code<R: BufRead, W: Write>(reader: &mut R, writer: &mut W, args: &Args) {
@ -240,7 +246,8 @@ fn format_code<R: BufRead, W: Write>(reader: &mut R, writer: &mut W, args: &Args
// Set up the highlighter // Set up the highlighter
let theme = &THEMES.themes["InspiredGitHub"]; let theme = &THEMES.themes["InspiredGitHub"];
let syntax = args.lang_override let syntax = args
.lang_override
.and_then(|l| SYNTAXES.find_syntax_by_name(l)) .and_then(|l| SYNTAXES.find_syntax_by_name(l))
.or_else(|| match args.extension { .or_else(|| match args.extension {
Some(ref ext) => SYNTAXES.find_syntax_by_extension(ext), Some(ref ext) => SYNTAXES.find_syntax_by_extension(ext),
@ -286,13 +293,13 @@ fn main() {
// and what file extension has been supplied. // and what file extension has been supplied.
let matches = App::new("cheddar") let matches = App::new("cheddar")
.about("TVL's syntax highlighter") .about("TVL's syntax highlighter")
.arg(Arg::with_name("about-filter") .arg(
Arg::with_name("about-filter")
.help("Run as a cgit about-filter (renders Markdown)") .help("Run as a cgit about-filter (renders Markdown)")
.long("about-filter") .long("about-filter")
.takes_value(false)) .takes_value(false),
.arg(Arg::with_name("filename") )
.help("File to render") .arg(Arg::with_name("filename").help("File to render").index(1))
.index(1))
.get_matches(); .get_matches();
let mut args = Args::default(); let mut args = Args::default();