2# converts HTML from a URL, file, or stdin to markdown
3# uses an available program to fetch URL and tidy to normalize it first
6SYNOPSIS="converts HTML from a URL, file, or STDIN to markdown-formatted text."
13err () { echo "$*" | fold -s -w ${COLUMNS:-110} >&2; }
14errn () { printf "$*" | fold -s -w ${COLUMNS:-110} >&2; }
17 err "$1 - $2" # short description
18 err "See the $1(1) man page for usage."
23 oldifs="$IFS"; IFS=':'
25 if [ -x "$_p/$*" ] && [ -f "$_p/$*" ]; then
34for p in pandoc $REQUIRED; do
36 err "You need '$p' to use this program!"
41CONF=$(pandoc --dump-args "$@" 2>&1) || {
43 echo "$CONF" | sed -e '/^pandoc \[OPTIONS\] \[FILES\]/,$d' >&2
44 [ $errcode -eq 2 ] && usage "$THIS" "$SYNOPSIS"
48OUTPUT=$(echo "$CONF" | sed -ne '1p')
49ARGS=$(echo "$CONF" | sed -e '1d')
53 url="${1:?internal error: grab_url_with: url required}"
60 if [ -n "$cmdline" ]; then
61 eval "set -- $cmdline"
67 if [ -z "$prog" ]; then
68 # Locate a sensible web grabber (note the order).
69 for p in wget lynx w3m curl links w3c; do
77 errn "$THIS: Couldn't find a program to fetch the file from URL "
78 err "(e.g. wget, w3m, lynx, w3c, or curl)."
83 err "$THIS: No such web grabber '$prog' found; aborting."
88 # Setup proper base options for known grabbers.
91 wget) base_opts="-O-" ;;
92 lynx) base_opts="-source" ;;
93 w3m) base_opts="-dump_source" ;;
95 links) base_opts="-source" ;;
96 w3c) base_opts="-n -get" ;;
97 *) err "$THIS: unhandled web grabber '$prog'; hope it succeeds."
100 err "$THIS: invoking '$prog $base_opts $prog_opts $url'..."
101 eval "set -- $base_opts $prog_opts"
105# Parse command-line arguments
107 while [ $# -gt 0 ]; do
111 # extract encoding from after =
112 encoding="${wholeopt#*=}" ;;
113 -e|--encoding|-encoding)
118 # extract encoding from after =
119 grabber="\"${wholeopt#*=}\"" ;;
120 -g|--grabber|-grabber)
124 if [ -z "$argument" ]; then
127 err "Warning: extra argument '$1' will be ignored."
144if [ -n "$argument" ] && ! [ -f "$argument" ]; then
145 # Treat given argument as an URL.
149# As a security measure refuse to proceed if mktemp is not available.
150pathfind mktemp || { err "Couldn't find 'mktemp'; aborting."; exit 1; }
152# Avoid issues with /tmp directory on Windows/Cygwin
154cygwin=$(uname | sed -ne '/^CYGWIN/p')
155if [ -n "$cygwin" ]; then
161THIS_TEMPDIR="$(mktemp -d -t $THIS.XXXXXXXX)" || exit 1
165 [ -z "$THIS_TEMPDIR" ] || rm -rf "$THIS_TEMPDIR"
166 exit $exitcode' 0 1 2 3 13 15
168if [ -n "$inurl" ]; then
169 err "Attempting to fetch file from '$inurl'..."
171 grabber_out=$THIS_TEMPDIR/grabber.out
172 grabber_log=$THIS_TEMPDIR/grabber.log
173 if ! grab_url_with "$inurl" "$grabber" 1>$grabber_out 2>$grabber_log; then
174 errn "grab_url_with failed"
175 if [ -f $grabber_log ]; then
176 err " with the following error log."
185 argument="$grabber_out"
188if [ -z "$encoding" ] && [ "x$argument" != "x" ]; then
189 # Try to determine character encoding if not specified
190 # and input is not STDIN.
193 LC_ALL=C tr 'A-Z' 'a-z' |
194 sed -ne '/<meta .*content-type.*charset=/ {
195 s/.*charset=["'\'']*\([-a-zA-Z0-9]*\).*["'\'']*/\1/p
200if [ -n "$encoding" ] && pathfind iconv; then
201 alias to_utf8='iconv -f "$encoding" -t utf-8'
206htmlinput=$THIS_TEMPDIR/htmlinput
208if [ -z "$argument" ]; then
209 to_utf8 > $htmlinput # read from STDIN
210elif [ -f "$argument" ]; then
211 to_utf8 "$argument" > $htmlinput # read from file
213 err "File '$argument' not found."
217if ! cat $htmlinput | pandoc --ignore-args -r html -w markdown "$@" ; then
218 err "Failed to parse HTML. Trying again with tidy..."
219 tidy -q -asxhtml -utf8 $htmlinput | \
220 pandoc --ignore-args -r html -w markdown "$@"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list