1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#!/usr/bin/env sh
WORKDIR=$PWD
OUTDIR=/var/www/blog/
verbose=0 # Variables to be evaluated as shell arithmetic should be initialized to a default or validated beforehand.
if [ ! "$(command -v cmark)" ]; then
echo "cmark not installed in $PATH"
fi
usage="$(basename "$0") [-h] [-v -w WORKDIR -f FILE]
where:
-w set the working directory (default: ${WORKDIR})
-o set the output directory (default: ${OUTDIR})
-h show this help text
-v verbose output (multiple flags increase verbosity)
(ex: '-v -v')
"
while :; do
case $1 in
-h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit.
echo "$usage"
exit
;;
-v)
verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity.
;;
-w) # Takes an option argument, ensuring it has been specified.
if [ -n "$2" ]; then
WORKDIR=$2
shift
fi
;;
-w=?*)
WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-w=) # Handle the case of an empty --file=
printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2
exit 1
;;
-o) # Takes an option argument, ensuring it has been specified.
if [ -n "$2" ]; then
OUTDIR=$2
shift
fi
;;
-o=?*)
OUTDIR=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-o=) # Handle the case of an empty --file=
printf 'ERROR: "-o" requires a non-empty option argument.\n' >&2
exit 1
;;
--) # End of all options.
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: If no more options then break out of the loop.
break ;;
esac
shift
done
#### HEADER ####
HEADER=$(cat <<EOF
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Glue Code Journal</title> <style type="text/css"> body{margin: 40px auto; max-width: 650px; line-height: 1.6; font-size: 18px; color: #444; padding: 0 10px} h1, h2, h3{line-height: 1.2} img{width: 100%; max-width: 100%; height: auto;}</style> </head>
EOF
)
#### FOOTER ####
FOOTER=$(cat <<EOF
</body></html>
EOF
)
### PROCESS Files ####
# Make out dir
if [ ! -d "${OUTDIR}" ]; then mkdir ${OUTDIR} ; fi
MDLIST=$(eval "find ${WORKDIR} -type f -name \"*.md\" ")
for m in $MDLIST; do
# shortpath trims the working directory prefix and the '.git' suffix from the directory
shortpath=$(eval "echo $m | sed -e 's,${WORKDIR}/,,' ")
dirpath=$(eval "echo $m | sed -e 's,${WORKDIR}/,,' -e 's,/.*.md$,,' ")
filename=$(eval "echo $m | sed -e 's,${WORKDIR}/,,' -e 's,${dirpath}/,,' -e 's,\.md,,' ")
if [ $verbose -gt 0 ]; then
echo "Processing: ${shortpath}"
fi
OUTPATH="${OUTDIR}/${dirpath}/${filename}.html"
# Output directly to file if $dirpath ends in ".md" for root-level files
case $dirpath in *.md)
OUTPATH="${OUTDIR}/${filename}.html"
;;
*)
# Make the outdir if not ending in ".md"
if [ ! -d "${OUTDIR}/${dirpath}" ]; then mkdir ${OUTDIR}/${dirpath} ; fi
esac
echo $HEADER > "${OUTPATH}"
cmark --safe --validate-utf8 -t html --width 80 $m >> "${OUTPATH}"
echo $FOOTER >> "${OUTPATH}"
if [ $verbose -gt 0 ]; then
echo "Created: ${OUTPATH}"
fi
done
|