# 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 "
"
}
# 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 "
"
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::add_extras () {
local -r html_base_dir="$CONTENT_BASE_DIR/html"
cp "$HTML_CSS_STYLE" "$html_base_dir/"
while read -r section_dir; do
local override_source="./extras/html/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)')
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
}
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 "

this
is
a
bare block
'
assert::equals "$(html::fromgmi <<< "$input_block")" "$output_block"
if [ -n "$SOURCE_HIGHLIGHT" ]; then
input_block='```bash
if [ -z $foo ]; then
echo $foo
fi
```'
assert::contains "$(html::fromgmi <<< "$input_block")" 'GNU source-highlight'
fi
}