diff options
Diffstat (limited to 'tools/cheddar/src/lib.rs')
-rw-r--r-- | tools/cheddar/src/lib.rs | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/tools/cheddar/src/lib.rs b/tools/cheddar/src/lib.rs index e5d7aad1e6..be8bc7f82f 100644 --- a/tools/cheddar/src/lib.rs +++ b/tools/cheddar/src/lib.rs @@ -8,13 +8,11 @@ use lazy_static::lazy_static; use regex::Regex; use std::cell::RefCell; use std::collections::HashMap; -use std::env; use std::ffi::OsStr; -use std::io; -use std::io::BufRead; -use std::io::Write; +use std::io::{BufRead, Write}; use std::path::Path; -use syntect::dumps::from_binary; +use std::{env, io}; +use syntect::dumps::from_uncompressed_data; use syntect::easy::HighlightLines; use syntect::highlighting::{Theme, ThemeSet}; use syntect::parsing::{SyntaxReference, SyntaxSet}; @@ -35,7 +33,9 @@ lazy_static! { // Note that the syntax set is included from the path pointed to // by the BAT_SYNTAXES environment variable at compile time. This // variable is populated by Nix and points to TVL's syntax set. - static ref SYNTAXES: SyntaxSet = from_binary(include_bytes!(env!("BAT_SYNTAXES"))); + static ref SYNTAXES: SyntaxSet = from_uncompressed_data(include_bytes!(env!("BAT_SYNTAXES"))) + .expect("failed to deserialise SyntaxSet"); + pub static ref THEMES: ThemeSet = ThemeSet::load_defaults(); // Configure Comrak's Markdown rendering with all the bells & @@ -78,6 +78,10 @@ lazy_static! { Shortlink { pattern: Regex::new(r#"\b(?P<type>b|cl)/(?P<dest>\d+)\b"#).unwrap(), replacement: "[$type/$dest](https://$type.tvl.fyi/$dest)", + }, + Shortlink { + pattern: Regex::new(r#"\br/(?P<dest>\d+)\b"#).unwrap(), + replacement: "[r/$dest](https://code.tvl.fyi/commit/?id=refs/r/$dest)", } ]; } @@ -151,8 +155,11 @@ fn highlight_code_block(code_block: &NodeCodeBlock) -> NodeValue { let mut buf = BLOCK_PRE.to_string(); for line in LinesWithEndings::from(&code) { - let regions = hl.highlight(line, &SYNTAXES); - append_highlighted_html_for_styled_line(®ions[..], IncludeBackground::No, &mut buf); + let regions = hl + .highlight_line(line, &SYNTAXES) + .expect("highlight_line failed"); + append_highlighted_html_for_styled_line(®ions[..], IncludeBackground::No, &mut buf) + .expect("appending HTML failed"); } buf.push_str("</pre>"); @@ -226,6 +233,7 @@ fn format_callout_paragraph(callout: Callout) -> NodeValue { pub fn format_markdown_with_shortlinks<R: BufRead, W: Write>( reader: &mut R, writer: &mut W, + tagfilter: bool, shortlinks: &[Shortlink], ) { let document = { @@ -237,7 +245,13 @@ pub fn format_markdown_with_shortlinks<R: BufRead, W: Write>( }; let arena = Arena::new(); - let root = parse_document(&arena, &linkify_shortlinks(document, shortlinks), &MD_OPTS); + + let mut opts = MD_OPTS.clone(); + if !tagfilter { + opts.extension.tagfilter = false; + } + + let root = parse_document(&arena, &linkify_shortlinks(document, shortlinks), &opts); // This node must exist with a lifetime greater than that of the parsed AST // in case that callouts are encountered (otherwise insertion into the tree @@ -273,11 +287,11 @@ pub fn format_markdown_with_shortlinks<R: BufRead, W: Write>( } }); - format_html(root, &MD_OPTS, writer).expect("Markdown rendering failed"); + format_html(root, &opts, writer).expect("Markdown rendering failed"); } -pub fn format_markdown<R: BufRead, W: Write>(reader: &mut R, writer: &mut W) { - format_markdown_with_shortlinks(reader, writer, &TVL_LINKS) +pub fn format_markdown<R: BufRead, W: Write>(reader: &mut R, writer: &mut W, tagfilter: bool) { + format_markdown_with_shortlinks(reader, writer, tagfilter, &TVL_LINKS) } fn find_syntax_for_file(filename: &str) -> &'static SyntaxReference { @@ -315,13 +329,16 @@ pub fn format_code<R: BufRead, W: Write>( // newlines to be efficient, and those are stripped in the lines // iterator. while should_continue(&read_result) { - let regions = hl.highlight(&linebuf, &SYNTAXES); + let regions = hl + .highlight_line(&linebuf, &SYNTAXES) + .expect("highlight_line failed"); append_highlighted_html_for_styled_line( ®ions[..], IncludeBackground::IfDifferent(bg), &mut outbuf, - ); + ) + .expect("appending highlighted HTML failed"); // immediately output the current state to avoid keeping // things in memory |