git.iko.soy/typeable/pandoc
on 79fdbce help sign in
typeable/pandoc/html2markdown
69 lines 1.7 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%% -- *} # only the part before ' -- ' delimiters is relevant
27set -- $ARGS
29REST=${ALL#$ARGS}; REST=${REST# -- }
30PANDOC_OPTS=${REST:-$PANDOC_OPTS}
32infile=$1
33shift
35if [ -n "$@" ]; then
36 echo >&2 "Warning: extra arguments '$@' will be ignored!"
37fi
39if [ -z "$infile" ] || [ -f $infile ]; then
40 tidy -utf8 $infile 2>/dev/null | \
41 pandoc $PANDOC_OPTS -r html -w markdown -s | \
42 iconv -f utf-8
43else
44 # Treat given argument as an URL. Locate a
45 # sensible text based browser (note the order).
46 for p in wget lynx w3m curl links w3c; do
47 if pathfind $p; then
48 DUMPER=$p
49 break
50 fi
51 done
52 # Setup proper options.
53 case "$DUMPER" in
54 wget) OPT="-O-" ;;
55 lynx) OPT="-source" ;;
56 w3m) OPT="-dump_source" ;;
57 curl) OPT="" ;;
58 links) OPT="-source" ;;
59 w3c) OPT="-n -get" ;;
60 "") printf "Needs a program to fetch the URL " >&2
61 printf "(e.g. wget, w3m, lynx, w3c, or curl)." >&2
62 exit 1 ;;
63 esac
64 # Fetch and feed to pandoc.
65 $DUMPER $OPT $infile 2>/dev/null | \
66 tidy -utf8 2>/dev/null | \
67 pandoc $PANDOC_OPTS -r html -w markdown -s | \
68 iconv -f utf-8
69fi