git.iko.soy/typeable/pandoc
on 4eec413 help sign in
typeable/pandoc/html2markdown
39 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
5[ -n "$(which pandoc)" ] || {
6 echo >&2 "You need 'pandoc' to use this program!"
7 exit 1
8}
9[ -n "$(which tidy)" ] || {
10 echo >&2 "You need 'tidy' to use this program!"
11 exit 1
14if [ -z "$1" ] || [ -f $1 ]; then
15 tidy -utf8 $1 2>/dev/null | pandoc -r html -w markdown -s
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; 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 "") echo -n >&2 "Needs a program to fetch the URL "
34 echo -n >&2 "(e.g. wget, w3m, lynx, w3c, or curl)."
35 exit 1 ;;
36 esac
37 # Fetch and feed to pandoc.
38 $DUMPER $OPT $1 2>/dev/null | tidy -utf8 2>/dev/null | pandoc -r html -w markdown -s
39fi