File: update-tocs

package info (click to toggle)
rust-derive-deftly 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,652 kB
  • sloc: perl: 1,032; sh: 373; python: 227; makefile: 11
file content (75 lines) | stat: -rwxr-xr-x 1,399 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
#
# usage: maint/update-tocs [--check]
#
# Updates the embedded Table of Contents in the following files:
process_files () {
    process_file doc/reference.md
    process_file macros/HACKING.md
}
# We do it like this because we don't want to have this done by
# a build.rs, which would have to run on every build.
#
# The CI will check that the TOC is up to date, but this is only
# treated as a warning.

set -e -o pipefail

case "$*" in
    --check) install=false;;
    '') install=true;;
    *) echo >&2 'bad usage'; exit 1;;
esac

problems=false

process_file () {
    file=$1
    perl <$file -wne '
	next unless s{^\#(\#+)\s+}{};
	my $depth = length($1);
	next if $depth > 2;
	print "   " x $depth, "* ";
	chomp;
	my $txt = $_;
	s{--+}{}g;
	s{[^- \w]}{}g;
	y/ A-Z/-a-z/;
	print "[$txt](#$_)\n";
    ' | perl -wne '
	BEGIN {
	    $toc = do { local $/ = undef; <STDIN>; };
	    $toc =~ s{\n+$}{}s;
	}
	print unless $skip;
	if (m{^\Q<!--##toc##-->}) {
            $skip = 1;
	    print $toc, "\n\n";
	    next;
    	}
	if (!m{^ +\*}) {
            $skip = 0;
	}
    ' $file >$file.new

    if $install; then
	mv -f $file.new $file
    else
	set +e
	diff -u $file $file.new
	rc=$?
	set -e
	case $rc in
	    0) ;;
	    1) problems=true ;;
	    *) exit $rc ;;
	esac
    fi
}

process_files

if $problems; then
    echo 'Documentation TOC mismatch - rerun maint/update-tocs'
    exit 1
fi