/ test / script / check-toc
check-toc
 1  #!/usr/bin/env bash
 2  
 3  set -e
 4  set -u
 5  
 6  # This script checks that the table of contents for the supported tools is
 7  # sorted, and that the table matches the files.
 8  
 9  toc_section_start_line="$(
10      grep -m1 -n '^7\..*\*ale-other-integration-options\*' doc/ale.txt \
11      | sed 's/\([0-9]*\).*/\1/' \
12  )"
13  toc_start_offset="$( \
14      tail -n +"$toc_section_start_line" doc/ale.txt \
15      | grep -m1 -n '^  .*\.\.\.' \
16      | sed 's/\([0-9]*\).*/\1/' \
17  )"
18  # shellcheck disable=SC2003
19  toc_start_line="$(expr "$toc_section_start_line" + "$toc_start_offset" - 1)"
20  toc_section_size="$( \
21      tail -n +"$toc_start_line" doc/ale.txt \
22      | grep -m1 -n '^===*$' \
23      | sed 's/\([0-9]*\).*/\1/' \
24  )"
25  # shellcheck disable=SC2003
26  toc_end_line="$(expr "$toc_start_line" + "$toc_section_size" - 4)"
27  
28  toc_file="$(mktemp -t table-of-contents.XXXXXXXX)"
29  heading_file="$(mktemp -t headings.XXXXXXXX)"
30  tagged_toc_file="$(mktemp -t ale.txt.XXXXXXXX)"
31  sorted_toc_file="$(mktemp -t sorted-ale.txt.XXXXXXXX)"
32  
33  sed -n "$toc_start_line,$toc_end_line"p doc/ale.txt \
34      | sed 's/^  \( *[^.][^.]*\)\.\.*|\(..*\)|/\1, \2/' \
35      > "$toc_file"
36  
37  # Get all of the doc files in a natural sorted order.
38  doc_files="$(/usr/bin/env ls -1v doc | grep '^ale-' | sed 's/^/doc\//' | paste -sd ' ' -)"
39  
40  # shellcheck disable=SC2086
41  grep -h '\*ale-.*-options\|^[a-z].*\*ale-.*\*$' $doc_files \
42      | sed 's/^/  /' \
43      | sed 's/ALE Shell Integration/ALE sh Integration/' \
44      | sed 's/ALE BibTeX Integration/ALE bib Integration/' \
45      | sed 's/  ALE \(.*\) Integration/\1/' \
46      | sed 's/ *\*\(..*\)\*$/, \1/' \
47      | tr '[:upper:]' '[:lower:]' \
48      | sed 's/objective-c/objc/' \
49      | sed 's/c++/cpp/' \
50      > "$heading_file"
51  
52  exit_code=0
53  in_section=0
54  section_index=0
55  
56  # Prefix numbers to table of contents entries so that sections aren't mixed up
57  # with sub-sections when they are sorted.
58  while read -r; do
59      if [[ "$REPLY" =~ ^\  ]]; then
60          if ! ((in_section)); then
61              let section_index='section_index + 1'
62              in_section=1
63          fi
64      else
65          if ((in_section)); then
66              let section_index='section_index + 1'
67              in_section=0
68          fi
69      fi
70  
71      echo "$section_index $REPLY" >> "$tagged_toc_file"
72  done < "$toc_file"
73  
74  # Sort the sections and sub-sections and remove the tags.
75  sort -sn "$tagged_toc_file" | sed 's/[0-9][0-9]* //' > "$sorted_toc_file"
76  
77  echo 'Check for bad ToC sorting:'
78  echo
79  diff -U2 "$sorted_toc_file" "$toc_file" || exit_code=$?
80  
81  echo 'Check for mismatched ToC and headings:'
82  echo
83  diff -U3 "$toc_file" "$heading_file" || exit_code=$?
84  
85  rm "$toc_file"
86  rm "$heading_file"
87  rm "$tagged_toc_file"
88  rm "$sorted_toc_file"
89  
90  exit "$exit_code"