Skip to content

Commit d2a55ea

Browse files
committed
chore: strip TOC from readme in rustdoc
Rustdoc already puts a mini-toc in the sidebar, so filling up the page above the fold with a full TOC seems pointless. Also moves the munge script into a separate file for better editability.
1 parent 9ec71f8 commit d2a55ea

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

.cargo/munge-readme.pl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env perl
2+
use strict;
3+
use warnings;
4+
5+
local $/ = undef; # slurp mode; no input separators -> one big input
6+
$_ = <>; # read whole input (files on command line, fallback to stdin)
7+
8+
# replace "ignore" code blocks with "text"
9+
s/```ignore/```text/g;
10+
# remove lines with admonition markers
11+
s/^.*\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\].*\n//mg;
12+
13+
# handle TOC markers
14+
my $start = "<!-- mdformat-toc start";
15+
my $end = "<!-- mdformat-toc end -->";
16+
17+
if (index($_, $start) == -1 && index($_, $end) == -1) {
18+
# both absent → do nothing
19+
}
20+
elsif (index($_, $start) == -1 || index($_, $end) == -1) {
21+
die "Error: only one TOC marker present\n";
22+
}
23+
else {
24+
s/$start.*?$end//s;
25+
}
26+
27+
print;

.cargo/rustdoc-wrapper.sh

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
# that are not understood by rustdoc. Remove them so rustdoc doesn't complain.
44
if [ "$CARGO_CRATE_NAME" = "core_crypto" ]; then
55
tempfile=$(mktemp)
6-
# we do not want to expand the contained backticks as expressions here;
7-
# they are code fences
8-
#shellcheck disable=SC2016
9-
perl -ne '
10-
s/```ignore/```text/g;
11-
print unless /\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]/
12-
' "$CARGO_MANIFEST_DIR/../README.md" > "$tempfile"
6+
cwd="$(dirname "$(realpath "$0")")"
7+
perl "$cwd/munge-readme.pl" "$CARGO_MANIFEST_DIR/../README.md" > "$tempfile"
138
export STRIPPED_README_PATH="$tempfile"
149
fi
1510
rustdoc "$@"

0 commit comments

Comments
 (0)