#!/bin/sh
# replace all * with _ for easier processing
#
_pre_emph () {
while IFS= read -r line; do
while [ "$line" != "${line%%\**}" ]; do
printf "%s_" "${line%%\**}"
line="${line#*\*}"
done
printf "%s\n" "${line}"
done
}
# fix any misaligned and closign tags
#
_post_emph () {
while IFS= read -r line; do
# TODO: avoid this problem entirely?
local wrong="" right=""
while [ "$line" != "${line%%${wrong}*}" ]; do
printf "%s${right}" "${line%%${wrong}*}"
line="${line#*${wrong}}"
done
printf "%s\n" "${line}"
done
}
# parse emphasis in a line
#
# emph [bound]
%s " "$line" || printf "%s " "$line" empty=false ;; esac done $empty || { printf "
\n" } } # parse ref-style links # _ref () { } # parse links # _a_img () { local open="[" mid="](" close=")" while IFS= read -r line; do next="$line" while [ "$next" != "${next#*$close}" ]; do before="${next%%$open*}" text=${next#*$open} text=${text%%$mid*} url=${next#*$mid} url=${url%%$close*} title=${url#* } url=${url%% *} [ "$title" != "$url" ] \ && title=" title=$title" \ || title= case "$before" in *!) h="%s" "
" \
| _post_emph \
| _a_img \
| _h 6 \
| _h 5 \
| _h 4 \
| _h 3 \
| _h 2 \
| _h 1
}
md2html