# 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
if [ "$HTML_VARIANT_TO_USE" = exact ]; then
if [ -n "$text" ]; then
echo "$(html::encode "$text")
"
else
echo '
'
fi
elif [ -n "$text" ]; then
echo "
$(html::encode "$text")
" fi } # Make a HTML header. html::make_heading () { local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift local -r level="$1"; shift if [ "$HTML_VARIANT_TO_USE" = exact ]; then #echo "$(html::encode "$text")$(html::encode "$quote")
" fi } # Make a HTML image html::make_img () { local link="$1"; shift local descr="$1"; shift if [ -z "$descr" ]; then echo "'
;;
'# '*)
html::make_heading "$line" 1 | html::process_inline
;;
'## '*)
html::make_heading "$line" 2 | html::process_inline
;;
'### '*)
html::make_heading "$line" 3 | html::process_inline
;;
'> '*)
html::make_quote "$line" | html::process_inline
;;
'=> '*)
generate::make_link html "$line" | html::process_inline
;;
*)
if [[ "$is_plain" == no ]]; then
html::make_paragraph "$line" | html::process_inline
else
html::make_paragraph "$line"
fi
;;
esac
done
}
# Test default HTML variant.
html::test::default () {
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='echo foo 2>&1'
assert::equals "$(html::make_paragraph "$line")" 'echo foo 2>&1
'
line='> This is a quote'
assert::equals "$(html::make_quote "$line")" "This is a quote
"
line='Testing: `hello_world.sh --debug` :-) `another one`!'
assert::equals "$(echo "$line" | html::process_inline)" \
"Testing: hello_world.sh --debug :-) another one!"
line='=> https://example.org'
assert::equals "$(generate::make_link html "$line")" \
"https://example.org
"
line='=> index.html'
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")" \
"
"
}
# Test exact HTML variant.
html::test::exact () {
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='echo foo 2>&1'
assert::equals "$(html::make_paragraph "$line")" "echo foo 2>&1
"
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
"
}
html::test () {
HTML_VARIANT_TO_USE=default html::test::default
HTML_VARIANT_TO_USE=exact html::test::exact
}