Project

General

Profile

1 7431 aaronmk
#!/bin/sh
2
# Translates a Redmine HTML page to a thesaurus
3
# Usage: self <page
4
5
sedEreFlag="$(test "$(uname)" = Darwin && echo E || echo r)"
6
7
sed () { "$(which sed)" -"$sedEreFlag" "$@";}
8
9 7442 aaronmk
ambigTerm=
10 7431 aaronmk
term=
11 7442 aaronmk
sed -n 's/^.*<h([1-4])[^>]*><a href="#[^>]+>([^<]+).*$/\1 \2/p'\
12 7431 aaronmk
|while read -r line; do
13
    set -- $line # split using IFS
14
    level="$1" name="$2"
15 7442 aaronmk
16
    # Handle synonyms
17 7445 aaronmk
    if test "$level" = 3; then echo "$name,$term"
18 7442 aaronmk
    else
19
        term="$name"
20
21
        # Handle ambiguous terms
22 7455 aaronmk
        if test "${term#\?}" != "$term"; then # ambiguous term (starts with ?)
23 7442 aaronmk
            ambigTerm="$term"
24 7455 aaronmk
        elif test "$level" = 4; then # alternative of ambiguous term
25 7445 aaronmk
            echo "$ambigTerm,$term"
26 7442 aaronmk
        else # term not related to ambiguous terms
27 7455 aaronmk
            ambigTerm= # clear any ambiguous term in effect
28 7442 aaronmk
        fi
29 7431 aaronmk
    fi
30
done