diff options
Diffstat (limited to 'lib/html.source.sh')
| -rw-r--r-- | lib/html.source.sh | 208 |
1 files changed, 157 insertions, 51 deletions
diff --git a/lib/html.source.sh b/lib/html.source.sh index d99220b..371866a 100644 --- a/lib/html.source.sh +++ b/lib/html.source.sh @@ -1,9 +1,10 @@ -# Convert special characters to their HTML codes +# Convert specia characters to their HTML codes html::encode () { $SED ' s|\&|\&|g; s|<|\<|g; s|>|\>|g; + s|'\''|\'|g; ' <<< "$@" } @@ -11,7 +12,13 @@ html::encode () { html::make_paragraph () { local -r text="$1"; shift - if [[ -n "$text" ]]; then + if [ "$HTML_VARIANT_TO_USE" = exact ]; then + if [ -n "$text" ]; then + echo "<span>$(html::encode "$text")</span><br />" + else + echo '<br />' + fi + elif [ -n "$text" ]; then echo "<p>$(html::encode "$text")</p>" fi } @@ -20,13 +27,23 @@ html::make_paragraph () { html::make_heading () { local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift local -r level="$1"; shift - echo "<h${level}>$(html::encode "$text")</h${level}>" + + if [ "$HTML_VARIANT_TO_USE" = exact ]; then + #echo "<span class='h${level}'>$(html::encode "$text")</span><br />" + echo "<h${level} style='display: inline'>$(html::encode "$text")</h${level}><br />" + else + echo "<h${level}>$(html::encode "$text")</h${level}><br />" + fi } # Make a HTML quotation html::make_quote () { local -r quote="${1/> }" - echo "<p class=\"quote\"><i>$(html::encode "$quote")</i></p>" + if [ "$HTML_VARIANT_TO_USE" = exact ]; then + echo "<span class='quote'>$(html::encode "$quote")</span><br />" + else + echo "<p class='quote'><i>$(html::encode "$quote")</i></p>" + fi } # Make a HTML image @@ -35,12 +52,10 @@ html::make_img () { local descr="$1"; shift if [ -z "$descr" ]; then - echo -n "<a href=\"$link\"><img src=\"$link\" /></a>" + echo "<a href='$link'><img src='$link' /></a><br />" else - echo -n "<a href=\"$link\"><img alt=\"$descr\" title=\"$descr\" src=\"$link\" /></a>" + echo "<a href='$link'><img alt='$descr' title='$descr' src='$link' /></a><br />" fi - - echo "<br />" } # Make a HTML hyperlink @@ -56,17 +71,16 @@ html::make_link () { descr="$link" fi - echo "<a class=\"textlink\" href=\"$link\">$descr</a><br />" -} + local mastadon_verify='' + if [[ "$link" = "$MASTADON_URI" ]]; then + mastadon_verify=" rel='me'" + fi -# Make inline code! -html::process_inline_code () { - $SED -E 's|`([^`]+)`|<span class="inlinecode">\1</span>|g' + echo "<a class='textlink' href='$(html::encode "$link")'$mastadon_verify>$descr</a><br />" } html::process_inline () { - # As of now we only inlinde "code blocks", but we can chain more here later! - html::process_inline_code + $SED -E "s|\`([^\`]+)\`|<span class='inlinecode'>\\1</span>|g" } html::add_extras () { @@ -82,16 +96,41 @@ html::add_extras () { cp "$override_source" "$override_dest" fi done < <(find "$html_base_dir" -mindepth 1 -maxdepth 1 -type d | $GREP -E -v '(\.git)') - cp "$HTML_WEBFONT_TEXT" "$html_base_dir/text.ttf" - cp "$HTML_WEBFONT_CODE" "$html_base_dir/code.ttf" - cp "$HTML_WEBFONT_HANDNOTES" "$html_base_dir/handnotes.ttf" - cp "$HTML_WEBFONT_TYPEWRITER" "$html_base_dir/typewriter.ttf" + + if [ -f "$HTML_WEBFONT_TEXT" ]; then + cp "$HTML_WEBFONT_TEXT" "$html_base_dir/text.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 '<pre>' + html::encode "$bare_text" + echo '</pre>' + else + $SOURCE_HIGHLIGHT --src-lang="$language" <<< "$bare_text" | + $SED 's|<tt>||; s|</tt>||;' + fi } # Convert Gemtext to HTML html::fromgmi () { local is_list=no - local is_plain=no + local is_bare=no + local bare_text='' + local language='' while IFS='' read -r line; do if [[ "$is_list" == yes ]]; then @@ -100,16 +139,25 @@ html::fromgmi () { html::process_inline else is_list=no - echo "</ul>" + if [ "$HTML_VARIANT_TO_USE" = exact ]; then + echo "</ul><br />" + else + echo "</ul>" + fi fi continue - elif [[ "$is_plain" == yes ]]; then + elif [[ "$is_bare" == yes ]]; then if [[ "$line" == '```'* ]]; then - echo "</pre>" - is_plain=no + html::source_highlight "$bare_text" "$language" + is_bare=no + bare_text='' + language='' + elif [ -z "$bare_text" ]; then + bare_text="$line" else - html::encode "$line" + bare_text="$bare_text +$line" fi continue fi @@ -122,8 +170,8 @@ html::fromgmi () { html::process_inline ;; '```'*) - is_plain=yes - echo '<pre>' + language=$(cut -d'`' -f4 <<< "$line") + is_bare=yes ;; '# '*) html::make_heading "$line" 1 | html::process_inline @@ -141,18 +189,16 @@ html::fromgmi () { 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 + html::make_paragraph "$line" | html::process_inline ;; esac done } -# Test HTML package. -html::test () { +# Test default HTML variant. +html::test::default () { + MASTADON_URI='' + local line='Hello world! This is a paragraph.' assert::equals "$(html::make_paragraph "$line")" '<p>Hello world! This is a paragraph.</p>' @@ -165,39 +211,99 @@ html::test () { line='echo foo 2>&1' assert::equals "$(html::make_paragraph "$line")" '<p>echo foo 2>&1</p>' - line='# Header 1' - assert::equals "$(html::make_heading "$line" 1)" '<h1>Header 1</h1>' - - line='## Header 2' - assert::equals "$(html::make_heading "$line" 2)" '<h2>Header 2</h2>' - - line='### Header 3' - assert::equals "$(html::make_heading "$line" 3)" '<h3>Header 3</h3>' - line='> This is a quote' - assert::equals "$(html::make_quote "$line")" '<p class="quote"><i>This is a quote</i></p>' + assert::equals "$(html::make_quote "$line")" "<p class='quote'><i>This is a quote</i></p>" line='Testing: `hello_world.sh --debug` :-) `another one`!' - assert::equals "$(echo "$line" | html::process_inline_code)" \ - 'Testing: <span class="inlinecode">hello_world.sh --debug</span> :-) <span class="inlinecode">another one</span>!' + assert::equals "$(echo "$line" | html::process_inline)" \ + "Testing: <span class='inlinecode'>hello_world.sh --debug</span> :-) <span class='inlinecode'>another one</span>!" line='=> https://example.org' assert::equals "$(generate::make_link html "$line")" \ - '<a class="textlink" href="https://example.org">https://example.org</a><br />' + "<a class='textlink' href='https://example.org'>https://example.org</a><br />" + + line="=> https://example.org/foo'bar" + assert::equals "$(generate::make_link html "$line")" \ + "<a class='textlink' href='https://example.org/foo'bar'>https://example.org/foo'bar</a><br />" line='=> index.html' assert::equals "$(generate::make_link html "$line")" \ - '<a class="textlink" href="index.html">index.html</a><br />' + "<a class='textlink' href='index.html'>index.html</a><br />" line='=> http://example.org Description of the link' assert::equals "$(generate::make_link html "$line")" \ - '<a class="textlink" href="http://example.org">Description of the link</a><br />' + "<a class='textlink' href='http://example.org'>Description of the link</a><br />" + + # Test Mastadon verification. + MASTADON_URI='https://fosstodon.org/@snonux' + line='=> https://fosstodon.org/@snonux Me at Mastadon' + assert::equals "$(generate::make_link html "$line")" \ + "<a class='textlink' href='https://fosstodon.org/@snonux' rel='me'>Me at Mastadon</a><br />" + MASTADON_URI='' line='=> http://example.org/image.png' assert::equals "$(generate::make_link html "$line")" \ - '<a href="http://example.org/image.png"><img src="http://example.org/image.png" /></a><br />' + "<a href='http://example.org/image.png'><img src='http://example.org/image.png' /></a><br />" line='=> http://example.org/image.png Image description' assert::equals "$(generate::make_link html "$line")" \ - '<a href="http://example.org/image.png"><img alt="Image description" title="Image description" src="http://example.org/image.png" /></a><br />' + "<a href='http://example.org/image.png'><img alt='Image description' title='Image description' src='http://example.org/image.png' /></a><br />" + + + local input_block='``` +this + is + a + bare block +```' + + local output_block='<pre> +this + is + a + bare block +</pre>' + + 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 +} + +# Test exact HTML variant. +html::test::exact () { + local line='Hello world! This is a paragraph.' + assert::equals "$(html::make_paragraph "$line")" "<span>Hello world! This is a paragraph.</span><br />" + + line='' + assert::equals "$(html::make_paragraph "$line")" '<br />' + + line='Foo &<>& Bar!' + assert::equals "$(html::make_paragraph "$line")" "<span>Foo &<>& Bar!</span><br />" + + line='echo foo 2>&1' + assert::equals "$(html::make_paragraph "$line")" "<span>echo foo 2>&1</span><br />" + + line='# Header 1' + assert::equals "$(html::make_heading "$line" 1)" "<h1 style='display: inline'>Header 1</h1><br />" + + line='## Header 2' + assert::equals "$(html::make_heading "$line" 2)" "<h2 style='display: inline'>Header 2</h2><br />" + + line='### Header 3' + assert::equals "$(html::make_heading "$line" 3)" "<h3 style='display: inline'>Header 3</h3><br />" + + line='> This is a quote' + assert::equals "$(html::make_quote "$line")" "<span class='quote'>This is a quote</span><br />" +} + +html::test () { + HTML_VARIANT_TO_USE=default html::test::default + HTML_VARIANT_TO_USE=exact html::test::exact } |
