diff --git a/http/.gitignore b/http/.gitignore index 82e4abd..f6b18c2 100644 --- a/http/.gitignore +++ b/http/.gitignore @@ -1,2 +1,4 @@ out vendor +src/microlog +src/blog diff --git a/http/Makefile b/http/Makefile index 9e468f7..eb8e440 100644 --- a/http/Makefile +++ b/http/Makefile @@ -1,8 +1,18 @@ all: clean out -out: $(shell find src -type f) $(shell find share -type f) $(shell find bin) +out: microlog blog $(shell find src -type f) $(shell find share -type f) $(shell find bin) ./bin/mkws https://joeac.net +microlog: src/microlog + +src/microlog: $(shell find -L blogs/microlog -type f) bin/blog + ./bin/blog -i microlog -l microlog -t + +blog: src/blog + +src/blog: $(shell find -L blogs/blog -type f) bin/blog + ./bin/blog -i blog -l blog + .PHONY: clean clean: - rm -rf out + rm -rf out src/microlog src/blog diff --git a/http/bin/blog b/http/bin/blog new file mode 100755 index 0000000..a16a692 --- /dev/null +++ b/http/bin/blog @@ -0,0 +1,88 @@ +#!/bin/sh -e +export PATH="$(dirname $(realpath $0)):$PATH" +export BLOGS="$(realpath blogs)" +export SRC="$(realpath src)" +export LAYOUTS="$(realpath share/layouts)" + +usage() +{ + >&2 cat << USAGE +usage: blog -i LOG [-l LAYOUT] [-t] + +options: + + -i LOG + The name of the source directory under \$BLOGS where log posts live. + + -l LAYOUT + If provided, the layout under \$LAYOUTS to use for the generated + upphtml files. If not provided, uses the default layout. + + -t + If enabled, use filenames as post titles +USAGE +} + +log_name="" +layout="default" +use_filenames_for_titles=0 + +while getopts "i:l:t" opt +do + case $opt in + i) + log_name="$OPTARG" + ;; + l) + layout_name="$OPTARG" + ;; + t) + use_filenames_for_titles=1 + ;; + esac +done + +if [ -z "${log_name}" ] +then + usage + exit 1 +fi + +if ! [ -d "${BLOGS}/${log_name}" ] +then + >&2 echo "there is no directory '${input_log}' under ${BLOGS}. options: $(ls common)" + usage + exit 1 +fi + +layout="${LAYOUTS}/${layout_name}.layout.upphtml" +if [ "${layout_name}" = "default" ] +then + layout_ext="" +else + layout_ext=".${layout_name}" +fi +if ! [ -f "${layout}" ] +then + echo >&2 "Warning: no layout found at $(basename "${LAYOUTS}")/${layout_name}.layout.upphtml" +fi + +rm -rf "${SRC}/${log_name}/" +mkdir -p "${SRC}/${log_name}" + +for entry in $(find -L "${BLOGS}/${log_name}" -type f) +do + basename="$(basename "$entry")" + name="${basename%%\.*}" + dest="${SRC}/${log_name}/${name}${layout_ext}.upphtml" + if [ $use_filenames_for_titles -eq 1 ] + then + title="$name" + cat "$entry" | gmi2upphtml "$title" > "$dest" + else + title="$(grep --regexp "^\# " --max-count 1 "$entry" | cut -c3-)" + cat "$entry" | grep --regexp "^\# " --invert-match | gmi2upphtml "$title" > "$dest" + fi + echo "${title}" > "${dest}.args" + echo "Processed ${entry#$(dirname ${BLOGS})/} to ${dest#$(dirname ${SRC})/}" +done diff --git a/http/bin/gmi2upphtml b/http/bin/gmi2upphtml new file mode 100755 index 0000000..608f4e8 --- /dev/null +++ b/http/bin/gmi2upphtml @@ -0,0 +1,176 @@ +#!/bin/sh -e + +post_title="${1:-}" + +is_in_list=0 +is_in_link_block=0 +is_in_preformatted_block=0 +is_in_blockquote=0 + +if_in_list_then_close() +{ + if [ $is_in_list -ne 0 ] + then + echo "" + is_in_list=0 + fi +} + +if_in_link_block_then_close() +{ + if [ $is_in_link_block -ne 0 ] + then + echo "

" + is_in_link_block=0 + fi +} + +if_in_preformatted_block_then_close() +{ + if [ $is_in_preformatted_block -ne 0 ] + then + echo "" + is_in_preformatted_block=0 + fi +} + +if_in_blockquote_then_close() +{ + if [ $is_in_blockquote -ne 0 ] + then + echo "" + is_in_blockquote=0 + fi +} + +close_all_open_blocks() +{ + if_in_list_then_close + if_in_link_block_then_close + if_in_preformatted_block_then_close + if_in_blockquote_then_close +} + +escape_upphtml() +{ + echo "$1" | sed "s/\`/\\\\\`/g" | sed "s/\"/\\\\\"/g" | sed "s/\\\$/\\\\\$/g" +} + +close_all_open_blocks_except() +{ + except="$1" + if [ "$except" != "list" ] + then + if_in_list_then_close + fi + if [ "$except" != "link" ] + then + if_in_link_block_then_close + fi + if [ "$except" != "pre" ] + then + if_in_preformatted_block_then_close + fi + if [ "$except" != "blockquote" ] + then + if_in_blockquote_then_close + fi +} + +if [ -z "$post_title" ] +then + read first_line + if [ "$(echo "$first_line" | cut -c1-2)" != "# " ] + then + echo >&2 "Error: no [post_title] argument provided and no top-level heading at top of the document." + exit 1 + fi + post_title="${first_line### }" +fi + +echo "#! +export TITLE=\"${post_title} | joeac's blog\" +export DESCRIPTION=\"${post_title} | joeac's blog\" +pp \"\${SHARE}\"/components/meta.upphtml +#!" + +while read line +do + if [ "$(echo $line | sed "s/[[:space:]]//g")" = "" ] + then + continue + fi + + if [ "$(echo $line | cut -c1)" = "#" ] + then + title="$(escape_upphtml "${line#*# }")" + heading="${line%%$title}" + heading="${heading%% }" + heading_level="${#heading}" + if [ "$(echo "$line" | cut -c$(( $heading_level + 1 )) )" = " " ] + then + close_all_open_blocks + echo "${title}" + continue + fi + fi + + if [ "$(echo "$line" | cut -c1-2)" = "=>" ] + then + close_all_open_blocks_except link + href="$(escape_upphtml "$(echo "$line" | sed "s/^=>[[:space:]]*\([[:alnum:][:punct:]]\+\).*$/\1/")")" + title="$(escape_upphtml "$(echo "$line" | sed "s/^=>[[:space:]]*[[:alnum:][:punct:]]\+[[:space:]]\(.*\)$/\1/")")" + if [ $is_in_link_block -eq 0 ] + then + echo "

" + else + echo "
" + fi + is_in_link_block=1 + echo "=> ${title}" + continue + fi + + if [ "$( echo "$line" | cut -c1-2)" = "* " ] + then + close_all_open_blocks_except list + if [ $is_in_list -eq 0 ] + then + echo "