aboutsummaryrefslogtreecommitdiff
path: root/html2markdown
blob: 4320ce9b18427db4da9518175fdc448a1f67d8b3 (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
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/sh -e
# converts html to markdown
# uses an available program to fetch URL and tidy to normalize it first

pathfind () { # portable which(1), code taken from Debian Developer's Reference
    OLDIFS="$IFS"
    IFS=:
    for _p in $PATH; do
        if [ -x "$_p/$*" ]; then
            IFS="$OLDIFS"
            return 0
        fi
    done
    IFS="$OLDIFS"
    return 1
}

for p in pandoc tidy; do
    pathfind $p || {
        echo >&2 "You need '$p' to use this program!"
        exit 1
    }
done

if [ -z "$1" ] || [ -f $1 ]; then
    tidy -utf8 $1 2>/dev/null | \
    pandoc $PANDOC_OPTS -r html -w markdown -s | \
    iconv -f utf-8
else
    # Treat given argument as an URL.  Locate a
    # sensible text based browser (note the order).
    for p in wget lynx w3m curl links w3c; do
        if pathfind $p; then
            DUMPER=$p
            break
        fi
    done
    # Setup proper options.
    case "$DUMPER" in
        wget)  OPT="-O-" ;;
        lynx)  OPT="-source" ;;
        w3m)   OPT="-dump_source" ;;
        curl)  OPT="" ;;
        links) OPT="-source" ;;
        w3c)   OPT="-n -get" ;;
        "")    printf "Needs a program to fetch the URL " >&2
               printf "(e.g. wget, w3m, lynx, w3c, or curl)." >&2
               exit 1 ;;
    esac
    # Fetch and feed to pandoc.
    $DUMPER $OPT $1 2>/dev/null | \
    tidy -utf8 2>/dev/null | \
    pandoc $PANDOC_OPTS -r html -w markdown -s | \
    iconv -f utf-8
fi