Skip to content

Commit a0c56a5

Browse files
Make syntax highlighting work for all languages in clippy lints page
1 parent a69614b commit a0c56a5

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

tests/compile-test.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use clippy_config::ClippyConfiguration;
88
use clippy_lints::LintInfo;
99
use clippy_lints::declared_lints::LINTS;
1010
use clippy_lints::deprecated_lints::{DEPRECATED, DEPRECATED_VERSION, RENAMED};
11-
use pulldown_cmark::{Options, Parser, html};
11+
use pulldown_cmark::{CodeBlockKind, CowStr, Event, Options, Parser, Tag, html};
1212
use rinja::Template;
1313
use rinja::filters::Safe;
1414
use serde::Deserialize;
@@ -394,11 +394,32 @@ struct Renderer<'a> {
394394
lints: &'a Vec<LintMetadata>,
395395
}
396396

397+
struct CodeBlockModifier<I>(I);
398+
399+
impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlockModifier<I> {
400+
type Item = Event<'a>;
401+
402+
fn next(&mut self) -> Option<Self::Item> {
403+
let Some(event) = self.0.next() else { return None };
404+
if let Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(ref lang))) = event {
405+
if lang
406+
.split(',')
407+
.any(|lang| ["", "rust", "ignore", "should_panic", "no_run", "compile_fail"].contains(&lang))
408+
{
409+
return Some(Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(CowStr::Borrowed(
410+
"rust",
411+
)))));
412+
}
413+
}
414+
Some(event)
415+
}
416+
}
417+
397418
impl Renderer<'_> {
398419
fn markdown(input: &str) -> Safe<String> {
399420
let parser = Parser::new_ext(input, Options::all());
400421
let mut html_output = String::new();
401-
html::push_html(&mut html_output, parser);
422+
html::push_html(&mut html_output, CodeBlockModifier(parser));
402423
// Oh deer, what a hack :O
403424
Safe(html_output.replace("<table", "<table class=\"table\""))
404425
}

util/gh-pages/script.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,8 @@ function onEachLazy(lazyArray, func) {
140140

141141
function highlightIfNeeded(lintId) {
142142
const elem = document.getElementById(lintId);
143-
const call = el => {
144-
hljs.highlightElement(el.parentElement)
145-
el.classList.add("highlighted");
146-
};
147-
onEachLazy(elem.querySelectorAll("pre > code.language-rust:not(.highlighted)"), call);
148-
onEachLazy(elem.querySelectorAll("pre > code.language-no_run:not(.highlighted)"), call);
143+
const call = el => hljs.highlightElement(el);
144+
onEachLazy(elem.querySelectorAll("pre > code:not(.hljs)"), call);
149145
}
150146

151147
function expandLint(lintId) {
@@ -197,9 +193,11 @@ function toggleExpansion(expand) {
197193
onEachLazy(
198194
document.querySelectorAll("article"),
199195
expand ? el => {
200-
el.classList.remove("collapsed");
201-
highlightIfNeeded(el);
202-
} : el => el.classList.add("collapsed"),
196+
el.querySelector("input[type='checkbox']").checked = true;
197+
highlightIfNeeded(el.id);
198+
} : el => {
199+
el.querySelector("input[type='checkbox']").checked = false;
200+
},
203201
);
204202
}
205203

0 commit comments

Comments
 (0)