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."
11 url="${1:?internal error: grab_url_with: url required}"
18 if [ -n "$cmdline" ]; then
19 eval "set -- $cmdline"
25 if [ -z "$prog" ]; then
26 # Locate a sensible web grabber (note the order).
27 for p in wget lynx w3m curl links w3c; do
35 errn "$THIS: Couldn't find a program to fetch the file from URL "
36 err "(e.g. wget, w3m, lynx, w3c, or curl)."
41 err "$THIS: No such web grabber '$prog' found; aborting."
46 # Setup proper base options for known grabbers.
49 wget) base_opts="-O-" ;;
50 lynx) base_opts="-source" ;;
51 w3m) base_opts="-dump_source" ;;
53 links) base_opts="-source" ;;
54 w3c) base_opts="-n -get" ;;
55 *) err "$THIS: unhandled web grabber '$prog'; hope it succeeds."
58 err "$THIS: invoking '$prog $base_opts $prog_opts $url'..."
59 eval "set -- $base_opts $prog_opts"
63# Parse command-line arguments
65 while [ $# -gt 0 ]; do
69 # extract encoding from after =
70 encoding="${wholeopt#*=}" ;;
71 -e|--encoding|-encoding)
76 # extract encoding from after =
77 grabber="\"${wholeopt#*=}\"" ;;
78 -g|--grabber|-grabber)
82 if [ -z "$argument" ]; then
85 err "Warning: extra argument '$1' will be ignored."
102if [ -n "$argument" ] && ! [ -f "$argument" ]; then
103 # Treat given argument as an URL.
107if [ -n "$inurl" ]; then
108 err "Attempting to fetch file from '$inurl'..."
112 grabber_out=$THIS_TEMPDIR/grabber.out
113 grabber_log=$THIS_TEMPDIR/grabber.log
114 if ! grab_url_with "$inurl" "$grabber" 1>$grabber_out 2>$grabber_log; then
115 errn "grab_url_with failed"
116 if [ -f $grabber_log ]; then
117 err " with the following error log."
126 argument="$grabber_out"
129if [ -z "$encoding" ] && [ "x$argument" != "x" ]; then
130 # Try to determine character encoding if not specified
131 # and input is not STDIN.
134 LC_ALL=C tr 'A-Z' 'a-z' |
135 sed -ne '/<meta .*content-type.*charset=/ {
136 s/.*charset=["'\'']*\([-a-zA-Z0-9]*\).*["'\'']*/\1/p
141if [ -n "$encoding" ] && pathfind iconv; then
142 alias to_utf8='iconv -f "$encoding" -t utf-8'
147if [ -z "$argument" ]; then
148 tidy -asxhtml -utf8 2>/dev/null | pandoc --ignore-args -r html -w markdown "$@"
150 if [ -f "$argument" ]; then
151 to_utf8 "$argument" |
152 tidy -asxhtml -utf8 2>/dev/null | pandoc --ignore-args -r html -w markdown "$@"
154 err "File '$argument' not found."
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