git.iko.soy/typeable/pandoc
on 3ed8fc8 help sign in
typeable/pandoc/html2markdown
42 lines 1.2 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
5for p in pandoc tidy; do
6 which $p >/dev/null 2>&1 || {
7 echo >&2 "You need '$p' to use this program!"
8 exit 1
9 }
10done
12if [ -z "$1" ] || [ -f $1 ]; then
13 tidy -utf8 $1 2>/dev/null | \
14 pandoc $PANDOC_OPTS -r html -w markdown -s | \
15 iconv -f utf-8
16else
17 # Treat given argument as an URL. Locate a
18 # sensible text based browser (note the order).
19 for p in wget lynx w3m curl links w3c; do
20 if which $p >/dev/null 2>&1; then
21 DUMPER=$p
22 break
23 fi
24 done
25 # Setup proper options.
26 case "$DUMPER" in
27 wget) OPT="-O-" ;;
28 lynx) OPT="-source" ;;
29 w3m) OPT="-dump_source" ;;
30 curl) OPT="" ;;
31 links) OPT="-source" ;;
32 w3c) OPT="-n -get" ;;
33 "") printf "Needs a program to fetch the URL " >&2
34 printf "(e.g. wget, w3m, lynx, w3c, or curl)." >&2
35 exit 1 ;;
36 esac
37 # Fetch and feed to pandoc.
38 $DUMPER $OPT $1 2>/dev/null | \
39 tidy -utf8 2>/dev/null | \
40 pandoc $PANDOC_OPTS -r html -w markdown -s | \
41 iconv -f utf-8
42fi