Skip to content

Commit b522e7a

Browse files
Generate lint list in HTML directly instead of JS
1 parent 43e3384 commit b522e7a

File tree

8 files changed

+152
-106
lines changed

8 files changed

+152
-106
lines changed

.github/deploy.sh

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ echo "Making the docs for master"
99
mkdir out/master/
1010
cp util/gh-pages/index.html out/master
1111
cp util/gh-pages/script.js out/master
12-
cp util/gh-pages/lints.json out/master
1312
cp util/gh-pages/style.css out/master
1413

1514
if [[ -n $TAG_NAME ]]; then

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ out
3434

3535
# gh pages docs
3636
util/gh-pages/lints.json
37+
util/gh-pages/index.html
3738

3839
# rustfmt backups
3940
*.rs.bk

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ toml = "0.7.3"
3939
walkdir = "2.3"
4040
filetime = "0.2.9"
4141
itertools = "0.12"
42+
pulldown-cmark = "0.11"
43+
rinja = { version = "0.3", default-features = false, features = ["config"] }
4244

4345
# UI test dependencies
4446
clippy_utils = { path = "clippy_utils" }

clippy_dev/src/serve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn run(port: u16, lint: Option<String>) -> ! {
1919
});
2020

2121
loop {
22-
if mtime("util/gh-pages/lints.json") < mtime("clippy_lints/src") {
22+
if mtime("util/gh-pages/index.html") < mtime("clippy_lints/src") {
2323
Command::new(env::var("CARGO").unwrap_or("cargo".into()))
2424
.arg("collect-metadata")
2525
.spawn()

rinja.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[general]
2+
dirs = ["util/gh-pages/"]
3+
default_syntax = "mixed"
4+
5+
[[syntax]]
6+
name = "mixed"
7+
expr_start = "{("
8+
expr_end = ")}"

tests/compile-test.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ 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};
12+
use rinja::{Template, filters::Safe};
1113
use serde::{Deserialize, Serialize};
1214
use test_utils::IS_RUSTC_TEST_SUITE;
1315
use ui_test::custom_flags::Flag;
@@ -385,6 +387,22 @@ fn ui_cargo_toml_metadata() {
385387
}
386388
}
387389

390+
#[derive(Template)]
391+
#[template(path = "index_template.html")]
392+
struct Renderer<'a> {
393+
lints: &'a Vec<LintMetadata>,
394+
}
395+
396+
impl<'a> Renderer<'a> {
397+
fn markdown(&self, input: &str) -> Safe<String> {
398+
let parser = Parser::new_ext(input, Options::all());
399+
let mut html_output = String::new();
400+
html::push_html(&mut html_output, parser);
401+
// Oh deer, what a hack :O
402+
Safe(html_output.replace("<table", "<table class=\"table\""))
403+
}
404+
}
405+
388406
#[derive(Deserialize)]
389407
#[serde(untagged)]
390408
enum DiagnosticOrMessage {
@@ -447,8 +465,7 @@ impl DiagnosticCollector {
447465
.collect();
448466
metadata.sort_unstable_by(|a, b| a.id.cmp(&b.id));
449467

450-
let json = serde_json::to_string_pretty(&metadata).unwrap();
451-
fs::write("util/gh-pages/lints.json", json).unwrap();
468+
fs::write("util/gh-pages/index.html", Renderer { lints: &metadata }.render().unwrap()).unwrap();
452469
});
453470

454471
(Self { sender }, handle)
@@ -487,7 +504,7 @@ impl Flag for DiagnosticCollector {
487504
}
488505
}
489506

490-
#[derive(Debug, Serialize)]
507+
#[derive(Debug)]
491508
struct LintMetadata {
492509
id: String,
493510
id_location: Option<&'static str>,
@@ -559,4 +576,14 @@ impl LintMetadata {
559576
applicability: Applicability::Unspecified,
560577
}
561578
}
579+
580+
fn applicability_str(&self) -> &str {
581+
match self.applicability {
582+
Applicability::MachineApplicable => "MachineApplicable",
583+
Applicability::HasPlaceholders => "HasPlaceholders",
584+
Applicability::MaybeIncorrect => "MaybeIncorrect",
585+
Applicability::Unspecified => "Unspecified",
586+
_ => panic!("needs to update this code"),
587+
}
588+
}
562589
}

util/gh-pages/index.html util/gh-pages/index_template.html

+47-56
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,8 @@ <h1>Clippy Lints</h1>
5555
</div>
5656
</noscript>
5757

58-
<div ng-cloak>
59-
60-
<div class="alert alert-info" role="alert" ng-if="loading">
61-
Loading&#x2026;
62-
</div>
63-
<div class="alert alert-danger" role="alert" ng-if="error">
64-
Error loading lints!
65-
</div>
66-
67-
<div class="panel panel-default" ng-show="data">
58+
<div>
59+
<div class="panel panel-default">
6860
<div class="panel-body row">
6961
<div id="upper-filters" class="col-12 col-md-5">
7062
<div class="btn-group" filter-dropdown>
@@ -188,9 +180,7 @@ <h1>Clippy Lints</h1>
188180
<div class="col-12 col-md-5 search-control">
189181
<div class="input-group">
190182
<label class="input-group-addon" id="filter-label" for="search-input">Filter:</label>
191-
<input type="text" class="form-control filter-input" placeholder="Keywords or search string (`S` or `/` to focus)" id="search-input"
192-
ng-model="search" ng-blur="updatePath()" ng-keyup="$event.keyCode == 13 && updatePath()"
193-
ng-model-options="{debounce: 50}" />
183+
<input type="text" class="form-control filter-input" placeholder="Keywords or search string (`S` or `/` to focus)" id="search-input" onblur="updatePath()" onchange="handleInputChanged()" />
194184
<span class="input-group-btn">
195185
<button class="filter-clear btn" type="button" ng-click="search = ''; updatePath();">
196186
Clear
@@ -208,56 +198,57 @@ <h1>Clippy Lints</h1>
208198
</div>
209199
</div>
210200
</div>
211-
<!-- The order of the filters should be from most likely to remove a lint to least likely to improve performance. -->
212-
<article class="panel panel-default" id="{{lint.id}}" ng-repeat="lint in data | filter:bySearch | filter:byGroups | filter:byLevels | filter:byVersion | filter:byApplicabilities">
213-
<header class="panel-heading" ng-click="open[lint.id] = !open[lint.id]">
214-
<h2 class="panel-title">
215-
<div class="panel-title-name">
216-
<span>{{lint.id}}</span>
217-
<a href="#{{lint.id}}" class="anchor label label-default"
218-
ng-click="openLint(lint); $event.preventDefault(); $event.stopPropagation()">&para;</a>
219-
<a href="" id="clipboard-{{lint.id}}" class="anchor label label-default" ng-click="copyToClipboard(lint); $event.stopPropagation()">
220-
&#128203;
221-
</a>
222-
</div>
201+
{% for lint in lints %}
202+
<article class="panel panel-default" id="{{lint.id}}">
203+
<header class="panel-heading" ng-click="open[lint.id] = !open[lint.id]">
204+
<h2 class="panel-title">
205+
<div class="panel-title-name">
206+
<span>{(lint.id)}</span>
207+
<a href="#{{lint.id}}" class="anchor label label-default"
208+
ng-click="openLint(lint); $event.preventDefault(); $event.stopPropagation()">&para;</a>
209+
<a href="" id="clipboard-{{lint.id}}" class="anchor label label-default" ng-click="copyToClipboard(lint); $event.stopPropagation()">
210+
&#128203;
211+
</a>
212+
</div>
223213

224-
<div class="panel-title-addons">
225-
<span class="label label-lint-group label-default label-group-{{lint.group}}">{{lint.group}}</span>
214+
<div class="panel-title-addons">
215+
<span class="label label-lint-group label-default label-group-{{lint.group}}">{(lint.group)}</span>
226216

227-
<span class="label label-lint-level label-lint-level-{{lint.level}}">{{lint.level}}</span>
217+
<span class="label label-lint-level label-lint-level-{{lint.level}}">{(lint.level)}</span>
228218

229219

230-
<span class="label label-doc-folding" ng-show="open[lint.id]">&minus;</span>
231-
<span class="label label-doc-folding" ng-hide="open[lint.id]">&plus;</span>
232-
</div>
233-
</h2>
234-
</header>
220+
<span class="label label-doc-folding" ng-show="open[lint.id]">&minus;</span>
221+
<span class="label label-doc-folding" ng-hide="open[lint.id]">&plus;</span>
222+
</div>
223+
</h2>
224+
</header>
235225

236-
<div class="list-group lint-docs" ng-if="open[lint.id]" ng-class="{collapse: true, in: open[lint.id]}">
237-
<div class="list-group-item lint-doc-md" ng-bind-html="lint.docs | markdown"></div>
238-
<div class="lint-additional-info-container">
239-
<!-- Applicability -->
240-
<div class="lint-additional-info-item">
241-
<span> Applicability: </span>
242-
<span class="label label-default label-applicability">{{lint.applicability}}</span>
243-
<a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/enum.Applicability.html#variants">(?)</a>
244-
</div>
245-
<!-- Clippy version -->
246-
<div class="lint-additional-info-item">
247-
<span>{{lint.group == "deprecated" ? "Deprecated" : "Added"}} in: </span>
248-
<span class="label label-default label-version">{{lint.version}}</span>
249-
</div>
250-
<!-- Open related issues -->
251-
<div class="lint-additional-info-item">
252-
<a href="https://github.com/rust-lang/rust-clippy/issues?q=is%3Aissue+{{lint.id}}">Related Issues</a>
253-
</div>
254-
<!-- Jump to source -->
255-
<div class="lint-additional-info-item" ng-if="lint.id_location">
256-
<a href="https://github.com/rust-lang/rust-clippy/blob/{{docVersion}}/{{lint.id_location}}">View Source</a>
226+
<div class="list-group lint-docs" ng-class="{collapse: true, in: open[lint.id]}">
227+
<div class="list-group-item lint-doc-md">{(markdown(lint.docs))}</div>
228+
<div class="lint-additional-info-container">
229+
{# Applicability #}
230+
<div class="lint-additional-info-item">
231+
<span> Applicability: </span>
232+
<span class="label label-default label-applicability">{( lint.applicability_str() )}</span>
233+
<a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/enum.Applicability.html#variants">(?)</a>
234+
</div>
235+
<!-- Clippy version -->
236+
<div class="lint-additional-info-item">
237+
<span>{% if lint.group == "deprecated" %}Deprecated{% else %} Added{% endif %} in: </span>
238+
<span class="label label-default label-version">{(lint.version)}</span>
239+
</div>
240+
<!-- Open related issues -->
241+
<div class="lint-additional-info-item">
242+
<a href="https://github.com/rust-lang/rust-clippy/issues?q=is%3Aissue+{{lint.id}}">Related Issues</a>
243+
</div>
244+
<!-- Jump to source -->
245+
<div class="lint-additional-info-item" ng-if="lint.id_location">
246+
<a href="https://github.com/rust-lang/rust-clippy/blob/{{docVersion}}/clippy_lints/{{lint.id_location.path}}">View Source</a>
247+
</div>
257248
</div>
258249
</div>
259-
</div>
260-
</article>
250+
</article>
251+
{% endfor %}
261252
</div>
262253
</div>
263254

util/gh-pages/script.js

+63-45
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
11
(function () {
2-
const md = window.markdownit({
3-
html: true,
4-
linkify: true,
5-
typographer: true,
6-
highlight: function (str, lang) {
7-
if (lang && hljs.getLanguage(lang)) {
8-
try {
9-
return '<pre class="hljs"><code>' +
10-
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
11-
'</code></pre>';
12-
} catch (__) {}
13-
}
14-
15-
return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
16-
}
17-
});
18-
192
function scrollToLint(lintId) {
203
const target = document.getElementById(lintId);
214
if (!target) {
@@ -41,15 +24,6 @@
4124
}
4225

4326
angular.module("clippy", [])
44-
.filter('markdown', function ($sce) {
45-
return function (text) {
46-
return $sce.trustAsHtml(
47-
md.render(text || '')
48-
// Oh deer, what a hack :O
49-
.replace('<table', '<table class="table"')
50-
);
51-
};
52-
})
5327
.directive('filterDropdown', function ($document) {
5428
return {
5529
restrict: 'A',
@@ -470,27 +444,17 @@
470444
// Set up the filters from the URL parameters before we start loading the data
471445
loadFromURLParameters();
472446

473-
$http.get('./lints.json')
474-
.success(function (data) {
475-
$scope.data = data;
476-
$scope.loading = false;
477-
478-
const selectedGroup = getQueryVariable("sel");
479-
if (selectedGroup) {
480-
selectGroup($scope, selectedGroup.toLowerCase());
481-
}
447+
const selectedGroup = getQueryVariable("sel");
448+
if (selectedGroup) {
449+
selectGroup($scope, selectedGroup.toLowerCase());
450+
}
482451

483-
scrollToLintByURL($scope, $location);
452+
scrollToLintByURL($scope, $location);
484453

485-
setTimeout(function () {
486-
const el = document.getElementById('filter-input');
487-
if (el) { el.focus() }
488-
}, 0);
489-
})
490-
.error(function (data) {
491-
$scope.error = data;
492-
$scope.loading = false;
493-
});
454+
setTimeout(function () {
455+
const el = document.getElementById('filter-input');
456+
if (el) { el.focus() }
457+
}, 0);
494458
});
495459
})();
496460

@@ -505,6 +469,58 @@ function getQueryVariable(variable) {
505469
}
506470
}
507471

472+
window.searchState = {
473+
timeout: null,
474+
inputElem: document.getElementById("search-input"),
475+
clearInputTimeout: () => {
476+
if (searchState.timeout !== null) {
477+
clearTimeout(searchState.timeout);
478+
searchState.timeout = null
479+
}
480+
},
481+
resetInputTimeout: () => {
482+
searchState.clearInputTimeout();
483+
setTimeout(searchState.filterLints, 50);
484+
},
485+
filterLints: () => {
486+
let searchStr = searchState.value.trim().toLowerCase();
487+
if (searchStr.startsWith("clippy::")) {
488+
searchStr = searchStr.slice(8);
489+
}
490+
const terms = searchStr.split(" ");
491+
492+
onEachLazy(document.querySelectorAll("article"), lint => {
493+
// Search by id
494+
if (lint.id.indexOf(searchStr.replaceAll("-", "_")) !== -1) {
495+
el.style.display = "";
496+
return;
497+
}
498+
// Search the description
499+
// The use of `for`-loops instead of `foreach` enables us to return early
500+
const docsLowerCase = lint.docs.toLowerCase();
501+
for (index = 0; index < terms.length; index++) {
502+
// This is more likely and will therefore be checked first
503+
if (docsLowerCase.indexOf(terms[index]) !== -1) {
504+
continue;
505+
}
506+
507+
if (lint.id.indexOf(terms[index]) !== -1) {
508+
continue;
509+
}
510+
511+
return false;
512+
}
513+
});
514+
},
515+
};
516+
517+
function handleInputChanged(event) {
518+
if (event.target !== document.activeElement) {
519+
return;
520+
}
521+
searchState.resetInputTimeout();
522+
}
523+
508524
function storeValue(settingName, value) {
509525
try {
510526
localStorage.setItem(`clippy-lint-list-${settingName}`, value);
@@ -627,3 +643,5 @@ if (prefersDark.matches && !theme) {
627643
}
628644
let disableShortcuts = loadValue('disable-shortcuts') === "true";
629645
document.getElementById("disable-shortcuts").checked = disableShortcuts;
646+
647+
hljs.highlightAll();

0 commit comments

Comments
 (0)