summaryrefslogtreecommitdiffstats
path: root/src/doc/rustc-dev-guide/ci/check_line_lengths.sh
blob: 31cda5c65e911d62363fb8b741c9755282efacf7 (plain)
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
#!/usr/bin/env bash

if [ "$1" == "--help" ]; then
  echo 'Usage:' "[MAX_LINE_LENGTH=n] $0 [file ...]"
  exit 1
fi

if [ "$MAX_LINE_LENGTH" == "" ]; then
    MAX_LINE_LENGTH=100
fi

if [ "$1" == "" ]; then
  shopt -s globstar
  files=( src/**/*.md )
else
  files=( "$@" )
fi

echo "Checking line lengths in all source files <= $MAX_LINE_LENGTH chars..."

echo "Offending files and lines:"
(( bad_lines = 0 ))
(( inside_block = 0 ))
for file in "${files[@]}"; do
  echo "$file"
  (( line_no = 0 ))
  while IFS="" read -r line || [[ -n "$line" ]] ; do
    (( line_no++ ))
    if [[ "$line" =~ ^'```' ]] ; then
      (( inside_block = !$inside_block ))
      continue
    fi
    if ! (( $inside_block )) \
        && ! [[ "$line" =~ " | "|"-|-"|"://"|"]:"|\[\^[^\ ]+\]: ]] \
        && (( "${#line}" > $MAX_LINE_LENGTH )) ; then
      (( bad_lines++ ))
      echo -e "\t$line_no : $line"
    fi
  done < "$file"
done

echo "$bad_lines offending lines found."
(( $bad_lines == 0 ))