|
| 1 | +#!/usr/bin/env zsh |
| 2 | +#--------------------------------------------------------------------------- |
| 3 | +# Checks that all external links, those starting with http*, have "targets". |
| 4 | +#--------------------------------------------------------------------------- |
| 5 | + |
| 6 | +default_path="docs" |
| 7 | + |
| 8 | +help() { |
| 9 | + cat << EOF |
| 10 | +A fairly crude, but mostly effective tool that checks URLs in markdown |
| 11 | +files to make sure they all have "targets". It prints locations that |
| 12 | +appears to be missing "{:target=...}". |
| 13 | +
|
| 14 | +It doesn't exit with an error if such links are found, because in some |
| 15 | +cases, this might be intentional. |
| 16 | +
|
| 17 | +Usage: $script [-h|--help] [-n|--noop] [path1 ...] |
| 18 | +
|
| 19 | +Where the arguments are the following: |
| 20 | +-h | --help Print this message and exit |
| 21 | +-n | --noop Just print the commands but don't make changes. |
| 22 | +-v | --verbose Print the paths as they are processed. Mostly useful |
| 23 | + when no problems are found and you are paranoid nothing |
| 24 | + was checked. ;) |
| 25 | +path1 ... Check these paths. Directories will be visited recursively. |
| 26 | + (Default: All markdown files under "$default_path") |
| 27 | +EOF |
| 28 | +} |
| 29 | + |
| 30 | +error() { |
| 31 | + for arg in "$@" |
| 32 | + do |
| 33 | + echo "ERROR: $arg" |
| 34 | + done |
| 35 | + help |
| 36 | + exit 1 |
| 37 | +} |
| 38 | + |
| 39 | +paths=() |
| 40 | +: ${VERBOSE=} |
| 41 | +while [[ $# -gt 0 ]] |
| 42 | +do |
| 43 | + case $1 in |
| 44 | + -h|--h*) |
| 45 | + help |
| 46 | + exit 0 |
| 47 | + ;; |
| 48 | + -n|--n*) |
| 49 | + NOOP=echo |
| 50 | + ;; |
| 51 | + -v|--v*) |
| 52 | + VERBOSE=echo |
| 53 | + ;; |
| 54 | + -*) |
| 55 | + error "Unrecognized option: $1" |
| 56 | + ;; |
| 57 | + *) |
| 58 | + paths+=("$1") |
| 59 | + ;; |
| 60 | + esac |
| 61 | + shift |
| 62 | +done |
| 63 | + |
| 64 | +[[ ${#paths[@]} -gt 0 ]] || paths=("$default_path") |
| 65 | + |
| 66 | +eg=$(which egrep) |
| 67 | +# Use a somewhat complicated script to find the URLs starting |
| 68 | +# with http, print only the matches and then filter out the |
| 69 | +# URLs that contain "target". It won't work perfectly, but ... |
| 70 | +for path in "${paths[@]}" |
| 71 | +do |
| 72 | + if [[ -n "$VERBOSE" ]] |
| 73 | + then |
| 74 | + dir=$([[ -d "$path" ]] && echo "(directory)") |
| 75 | + echo "$path $dir" |
| 76 | + fi |
| 77 | + $eg -nHoR '\(https?[^)]+\)(\S*)' \ |
| 78 | + --include '*.markdown' --include '*.md' \ |
| 79 | + --exclude-dir '_site' --exclude-dir '_sass' \ |
| 80 | + $path | $eg -v target |
| 81 | +done |
0 commit comments