# Convert special characters to their HTML codes html::encode () { $SED ' s|\&|\&|g; s|<|\<|g; s|>|\>|g; ' <<< "$@" } # Make a HTML paragraph. html::make_paragraph () { local -r text="$1"; shift test -n "$text" && echo "
$(html::encode "$text")
" } # Make a HTML header. html::make_heading () { local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift local -r level="$1"; shift echo "$(html::encode "$quote")
" } # Make a HTML image html::make_img () { local link="$1"; shift local descr="$1"; shift if [ -z "$descr" ]; then echo -n ""
;;
'# '*)
html::make_heading "$line" 1
;;
'## '*)
html::make_heading "$line" 2
;;
'### '*)
html::make_heading "$line" 3
;;
'> '*)
html::make_quote "$line"
;;
'=> '*)
generate::make_link html "$line"
;;
*)
html::make_paragraph "$line"
;;
esac
done
}
# Test HTML package.
html::test () {
local line='Hello world! This is a paragraph.'
assert::equals "$(html::make_paragraph "$line")" 'Hello world! This is a paragraph.
'
line=''
assert::equals "$(html::make_paragraph "$line")" ''
line='Foo &<>& Bar!'
assert::equals "$(html::make_paragraph "$line")" 'Foo &<>& Bar!
'
line='# Header 1'
assert::equals "$(html::make_heading "$line" 1)" 'Header 1
'
line='## Header 2'
assert::equals "$(html::make_heading "$line" 2)" 'Header 2
'
line='### Header 3'
assert::equals "$(html::make_heading "$line" 3)" 'Header 3
'
line='> This is a quote'
assert::equals "$(html::make_quote "$line")" 'This is a quote
'
line='=> https://example.org'
assert::equals "$(generate::make_link html "$line")" \
'https://example.org
'
line='=> index.gmi'
assert::equals "$(generate::make_link html "$line")" \
'index.html
'
line='=> http://example.org Description of the link'
assert::equals "$(generate::make_link html "$line")" \
'Description of the link
'
line='=> http://example.org/image.png'
assert::equals "$(generate::make_link html "$line")" \
'
'
line='=> http://example.org/image.png Image description'
assert::equals "$(generate::make_link html "$line")" \
'Image description:
'
}