#!/bin/sh BLOCK= BUFFER= process() { while read -r LINE do if test "$BLOCK" = verbatim then printf '%s\n' "$LINE" continue fi set -- $LINE case "$LINE" in ('```') if test "$BLOCK" = verbatim then BLOCK= else BLOCK=verbatim fi ;; ('') if test "$BLOCK" = verbatim then bufpush $'\n' else bufclose fi ;; ('=>'*) link "$@" ;; ('#'*) header "$@" ;; ('*'*) shift; blknew list "$*" ;; ('>'*) shift; blknew quote "$*" ;; (*) shift; blknew paragraph "$*" ;; esac done bufclose } blknew() { test "$BLOCK" = "$1" || bufclose bufpush "$(printf "$(eval echo "\$format_$BLOCK")" "$@")" BLOCK="$1" } bufclose() { if test -z "$BLOCK" then "$COLLAPSE_BLANKS" && return else newline; return fi # blockp "$BLOCK" || return # fillp $BLOCK && buffill "$(fillp $BLOCK)" # TODO: escape shit printf '%s%s%s\n' \ "$(echo eval "\$opener_$BLOCK")" \ "$BUFFER" \ "$(echo eval "\$closer_$BLOCK")" BLOCK= } buffill() { # buffill WIDTH if test $1 -lt 0 then BUFFER="$(printf '%s\n' "$BUFFER" | tr '\n' ' ')" else out= nline=0 printf '%s\n' "$BUFFER" | sed 's/[ \t]\+/\n/g' | while read -r word do if test $((nline + ${#word})) -ge "$1" then out="${out}"${out:+$'\n'}"${word}" nline=${#word} else out="${out}${out:+ }${word}" nline=$((nline + ${#word} + 1)) fi done BUFFER="$out" fi } bufpush() { BUFFER="${BUFFER}$@"; } fillp() { : } header() { bufclose lvl=${#1}; shift bufpush "$(printf "$(eval echo "\$format_h$lvl")" "$*")" BLOCK=header } link() { url="$2"; shift 2 if test "$BLOCK" = paragraph #&& ! blockp link then bufpush "$(printf "$format_link" "$url" "$*")" else blknew linklist "$url" "$*" fi } newline() { printf '\n'; } html() { format_link='%s\n' format_h1='

%s

\n' format_h2='

%s

\n' format_h3='

%s

\n' opener_verbatim='
'
	closer_verbatim='
\n' format_verbatim='%s\n' opener_paragraph='

' closer_paragraph='

\n' format_paragraph='%s\n' opener_quote='
' closer_quote='
\n' format_quote='%s\n' opener_list='\n' format_list='
  • %s
  • ' opener_linklist='' format_linklist="$(printf "$format_list" "$format_link")" } main() { html process "$@" } main "$@"