# Convert specia characters to their HTML codes html::encode () { $SED ' s|\&|\&|g; s|<|\<|g; s|>|\>|g; s|'\''|\'|g; ' <<< "$@" } # Make a HTML paragraph. html::make_paragraph () { local -r text="$1"; shift if [ -n "$text" ]; then echo "$(html::encode "$text")
" else echo '
' fi } # Make a HTML header. html::make_heading () { local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift local -r level="$1"; shift local -r id="$(generate::internal_link_id "$text")" echo "$(html::encode "$text")
" } # Make a HTML quotation html::make_quote () { local -r quote="${1/> }" echo "$(html::encode "$quote")
" } # Make a HTML image html::make_img () { local link="$1"; shift local descr="$1"; shift if [ -z "$descr" ]; then echo "
" else echo "$descr
" fi } # Make a HTML hyperlink html::make_link () { local link="$1"; shift local descr="$1"; shift if ! $GREP -F -q '://' <<< "$link"; then link=${link/.gmi/.html} fi if [[ -z "$descr" ]]; then descr="$link" fi local mastodon_verify='' if [[ "$link" = "$MASTODON_URI" ]]; then mastodon_verify=" rel='me'" fi echo "$descr
" } html::process_inline () { $SED -E "s|\`([^\`]+)\`|\\1|g" } html::theme () { local -r html_base_dir="$CONTENT_BASE_DIR/html" log INFO "Installing theme $HTML_THEME_DIR" html::theme::styles "$html_base_dir" html::theme::webfonts "$html_base_dir" } html::theme::styles () { local -r html_base_dir="$1"; shift log INFO 'Installing theme CSS files' cp "$HTML_CSS_STYLE" "$html_base_dir/" while read -r section_dir; do local override_source="$HTML_THEME_DIR/style-$(basename "$section_dir")-override.css" local override_dest="$section_dir/style-override.css" if [ ! -f "$override_source" ]; then touch "$override_dest" # Empty override else cp "$override_source" "$override_dest" fi done < <(find "$html_base_dir" -mindepth 1 -maxdepth 1 -type d | $GREP -E -v '(\.git)') } html::theme::webfonts () { local -r html_base_dir="$1"; shift log INFO 'Installing theme webfonts' set +u if [ -f "$HTML_WEBFONT_TEXT" ]; then cp "$HTML_WEBFONT_TEXT" "$html_base_dir/text.ttf" fi if [ -f "$HTML_WEBFONT_HEADING" ]; then cp "$HTML_WEBFONT_HEADING" "$html_base_dir/heading.ttf" elif [ -f "$HTML_WEBFONT_TEXT" ]; then cp "$HTML_WEBFONT_TEXT" "$html_base_dir/heading.ttf" fi if [ -f "$HTML_WEBFONT_CODE" ]; then cp "$HTML_WEBFONT_CODE" "$html_base_dir/code.ttf" fi if [ -f "$HTML_WEBFONT_HANDNOTES" ]; then cp "$HTML_WEBFONT_HANDNOTES" "$html_base_dir/handnotes.ttf" fi if [ -f "$HTML_WEBFONT_TYPEWRITER" ]; then cp "$HTML_WEBFONT_TYPEWRITER" "$html_base_dir/typewriter.ttf" fi set -u } html::source_highlight () { local -r bare_text="$1"; shift local -r language="$1"; shift if [[ -z "$language" || -z "$SOURCE_HIGHLIGHT" ]]; then echo '
'
        html::encode "$bare_text"
        echo '
' else local style_css='' if [ -n "$SOURCE_HIGHLIGHT_CSS" ]; then style_css="--style-css-file=$SOURCE_HIGHLIGHT_CSS" fi $SOURCE_HIGHLIGHT --src-lang="$language" $style_css <<< "$bare_text" | $SED 's|||; s|||;' fi } html::list::encode () { local text="$1"; shift if [[ "$text" != '⇢ '* ]]; then # No ToC html::encode "$text" return fi local -i toc_indent=0 # If there's a . (dot) in the liste element, it then indicates a ToC element while [[ "$text" == '⇢ '* ]]; do text="$($SED 's/⇢ //' <<< "$text")" : $(( toc_indent++ )) done while [ $toc_indent -ge 2 ]; do echo -n '⇢ ' : $(( toc_indent-- )) done local -r id="$(generate::internal_link_id "$text")" echo "$(html::encode "$text")" } # Convert Gemtext to HTML html::fromgmi () { local is_list=no local is_bare=no local bare_text='' local language='' while IFS='' read -r line; do if [[ "$is_list" == yes ]]; then if [[ "$line" == '* '* ]]; then echo "
  • $(html::list::encode "${line/\* /}")
  • " | html::process_inline else is_list=no echo "
    " fi continue elif [[ "$is_bare" == yes ]]; then if [[ "$line" == '```'* ]]; then html::source_highlight "$bare_text" "$language" is_bare=no bare_text='' language='' elif [ -z "$bare_text" ]; then bare_text="$line" else bare_text="$bare_text $line" fi continue fi case "$line" in '* '*) is_list=yes echo "