git.iko.soy/typeable/pandoc
on master help sign in
Portability fixes and various cleanups in wrapper scripts:
3ed8fc8roktas
html2markdown+17−14
@@ -2,22 +2,22 @@
2# converts html to markdown
2# converts html to markdown
3# uses an available program to fetch URL and tidy to normalize it first
3# uses an available program to fetch URL and tidy to normalize it first
4
4
5−[ -n "$(which pandoc)" ] || {
5+for p in pandoc tidy; do
6− echo >&2 "You need 'pandoc' to use this program!"
6+ which $p >/dev/null 2>&1 || {
7− exit 1
8−}
9−[ -n "$(which tidy)" ] || {
10− echo >&2 "You need 'tidy' to use this program!"
7+ echo >&2 "You need '$p' to use this program!"
11− exit 1
8+ exit 1
12−}
9+ }
10+done
13
11
14−if [ -z "$1" ] || [ -f $1 ]; then
12+if [ -z "$1" ] || [ -f $1 ]; then
13+ tidy -utf8 $1 2>/dev/null | \
15− tidy -utf8 $1 2>/dev/null | pandoc $PANDOC_OPTS -r html -w markdown -s | iconv -f utf-8
14+ pandoc $PANDOC_OPTS -r html -w markdown -s | \
15+ iconv -f utf-8
16else
16else
17 # Treat given argument as an URL. Locate a
17 # Treat given argument as an URL. Locate a
18 # sensible text based browser (note the order).
18 # sensible text based browser (note the order).
19 for p in wget lynx w3m curl links w3c; do
19 for p in wget lynx w3m curl links w3c; do
20− if which $p >/dev/null; then
20+ if which $p >/dev/null 2>&1; then
21 DUMPER=$p
21 DUMPER=$p
22 break
22 break
23 fi
23 fi
@@ -30,10 +30,13 @@ else
30 curl) OPT="" ;;
30 curl) OPT="" ;;
31 links) OPT="-source" ;;
31 links) OPT="-source" ;;
32 w3c) OPT="-n -get" ;;
32 w3c) OPT="-n -get" ;;
33− "") echo -n >&2 "Needs a program to fetch the URL "
33+ "") printf "Needs a program to fetch the URL " >&2
34− echo -n >&2 "(e.g. wget, w3m, lynx, w3c, or curl)."
34+ printf "(e.g. wget, w3m, lynx, w3c, or curl)." >&2
35 exit 1 ;;
35 exit 1 ;;
36 esac
36 esac
37 # Fetch and feed to pandoc.
37 # Fetch and feed to pandoc.
38+ $DUMPER $OPT $1 2>/dev/null | \
39+ tidy -utf8 2>/dev/null | \
38− $DUMPER $OPT $1 2>/dev/null | tidy -utf8 2>/dev/null | pandoc $PANDOC_OPTS -r html -w markdown -s | iconv -f utf-8
40+ pandoc $PANDOC_OPTS -r html -w markdown -s | \
41+ iconv -f utf-8
39fi
42fi
latex2markdown+8−4
@@ -1,7 +1,11 @@
1#!/bin/sh -e
1#!/bin/sh -e
2# runs pandoc to convert latex to markdown
2# runs pandoc to convert latex to markdown
3−[ -n "$(which pandoc)" ] || {
3+
4+for p in pandoc; do
5+ which $p >/dev/null 2>&1 || {
4− echo >&2 "You need 'pandoc' to use this program!"
6+ echo >&2 "You need '$p' to use this program!"
5− exit 1
7+ exit 1
6−}
8+ }
9+done
10+
7iconv -t utf-8 $* | pandoc $PANDOC_OPTS -r latex -w markdown -s | iconv -f utf-8
11iconv -t utf-8 $* | pandoc $PANDOC_OPTS -r latex -w markdown -s | iconv -f utf-8
markdown2html+9−5
@@ -1,7 +1,11 @@
1#!/bin/sh -e
1#!/bin/sh -e
2−# converts markdown to HTML
2+# converts markdown to HTML
3−[ -n "$(which pandoc)" ] || {
3+
4+for p in pandoc; do
5+ which $p >/dev/null 2>&1 || {
4− echo >&2 "You need 'pandoc' to use this program!"
6+ echo >&2 "You need '$p' to use this program!"
5− exit 1
7+ exit 1
6−}
8+ }
9+done
10+
7iconv -t utf-8 $* | pandoc $PANDOC_OPTS | iconv -f utf-8
11iconv -t utf-8 $* | pandoc $PANDOC_OPTS | iconv -f utf-8
markdown2latex+8−4
@@ -1,7 +1,11 @@
1#!/bin/sh -e
1#!/bin/sh -e
2# converts markdown to latex
2# converts markdown to latex
3−[ -n "$(which pandoc)" ] || {
3+
4+for p in pandoc; do
5+ which $p >/dev/null 2>&1 || {
4− echo >&2 "You need 'pandoc' to use this program!"
6+ echo >&2 "You need '$p' to use this program!"
5− exit 1
7+ exit 1
6−}
8+ }
9+done
10+
7iconv -t utf-8 $* | pandoc $PANDOC_OPTS -w latex -s | iconv -f utf-8
11iconv -t utf-8 $* | pandoc $PANDOC_OPTS -w latex -s | iconv -f utf-8
markdown2pdf+13−13
@@ -1,14 +1,12 @@
1#!/bin/sh
1#!/bin/sh
2# converts markdown to latex, then uses latex to make a PDF
2# converts markdown to latex, then uses latex to make a PDF
3
3
4−[ -n "$(which pandoc)" ] || {
4+for p in pandoc pdflatex; do
5− echo >&2 "You need 'pandoc' to use this program!"
5+ which $p >/dev/null 2>&1 || {
6− exit 1
7−}
8−[ -n "$(which pdflatex)" ] || {
9− echo >&2 "You need 'pdflatex' to use this program!"
6+ echo >&2 "You need '$p' to use this program!"
10− exit 1
7+ exit 1
11−}
8+ }
9+done
12
10
13outfile=
11outfile=
14for option; do
12for option; do
@@ -58,8 +56,9 @@ TEMP=${TMPDIR-/tmp}/markdown2pdf.$$
58trap "status=$?; rm -rf $TEMP; exit $status" 0 INT
56trap "status=$?; rm -rf $TEMP; exit $status" 0 INT
59mkdir -p $TEMP
57mkdir -p $TEMP
60
58
59+iconv -t utf-8 $infile | \
61−iconv -t utf-8 $infile | pandoc $PANDOC_OPTS -w latex -s | iconv -f utf-8 > $TEMP/$BASE.tex
60+pandoc $PANDOC_OPTS -w latex -s | \
62−(
61+iconv -f utf-8 > $TEMP/$BASE.tex && (
63 cd $TEMP
62 cd $TEMP
64 if ! pdflatex -interaction=batchmode $BASE.tex >/dev/null 2>&1; then
63 if ! pdflatex -interaction=batchmode $BASE.tex >/dev/null 2>&1; then
65 echo >&2 "LaTeX errors:"
64 echo >&2 "LaTeX errors:"
@@ -71,12 +70,13 @@ iconv -t utf-8 $infile | pandoc $PANDOC_OPTS -w latex -s | iconv -f utf-8 > $TEM
71is_target_exists=
70is_target_exists=
72if [ -f $outfile ]; then
71if [ -f $outfile ]; then
73 is_target_exists=1
72 is_target_exists=1
73+ mv -f $outfile $outfile~
74fi
74fi
75
75
76−mv --suffix=~ --backup $TEMP/$BASE.pdf $outfile
76+mv -f $TEMP/$BASE.pdf $outfile
77
77
78−echo -n >&2 "Created $outfile"
78+printf "Created $outfile" >&2
79[ -z "$is_target_exists" ] || {
79[ -z "$is_target_exists" ] || {
80− echo -n >&2 " (previous file has been backed up as '$outfile~')"
80+ printf " (previous file has been backed up as '$outfile~')" >&2
81}
81}
82echo >&2 .
82echo >&2 .