git.iko.soy/typeable/pandoc
on 5a66fa0 help sign in
typeable/pandoc/html2markdown
62 lines 1.6 kB history raw
1#!/bin/sh -e
2# converts html to markdown
3# uses an available program to fetch URL and tidy to normalize it first
5pathfind () { # portable which(1), code taken from Debian Developer's Reference
6 OLDIFS="$IFS"
7 IFS=:
8 for _p in $PATH; do
9 if [ -x "$_p/$*" ]; then
10 IFS="$OLDIFS"
11 return 0
12 fi
13 done
14 IFS="$OLDIFS"
15 return 1
18for p in pandoc tidy; do
19 pathfind $p || {
20 echo >&2 "You need '$p' to use this program!"
21 exit 1
22 }
23done
25ALL="$*"
26ARGS=${ALL%%-- *}
27REST=${ALL#$ARGS}
29REST=${REST#--}
30PANDOC_OPTS=${REST:-$PANDOC_OPTS}
32if [ -z "$1" ] || [ -f $1 ]; then
33 tidy -utf8 $1 2>/dev/null | \
34 pandoc $PANDOC_OPTS -r html -w markdown -s | \
35 iconv -f utf-8
36else
37 # Treat given argument as an URL. Locate a
38 # sensible text based browser (note the order).
39 for p in wget lynx w3m curl links w3c; do
40 if pathfind $p; then
41 DUMPER=$p
42 break
43 fi
44 done
45 # Setup proper options.
46 case "$DUMPER" in
47 wget) OPT="-O-" ;;
48 lynx) OPT="-source" ;;
49 w3m) OPT="-dump_source" ;;
50 curl) OPT="" ;;
51 links) OPT="-source" ;;
52 w3c) OPT="-n -get" ;;
53 "") printf "Needs a program to fetch the URL " >&2
54 printf "(e.g. wget, w3m, lynx, w3c, or curl)." >&2
55 exit 1 ;;
56 esac
57 # Fetch and feed to pandoc.
58 $DUMPER $OPT $1 2>/dev/null | \
59 tidy -utf8 2>/dev/null | \
60 pandoc $PANDOC_OPTS -r html -w markdown -s | \
61 iconv -f utf-8
62fi