From a0cdf880c32264165f1af87d7f0465d3923ffe88 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 24 Aug 2024 14:20:27 +0100 Subject: nicer ToC for HTML --- lib/html.source.sh | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'lib/html.source.sh') diff --git a/lib/html.source.sh b/lib/html.source.sh index 1666239..f30cb22 100644 --- a/lib/html.source.sh +++ b/lib/html.source.sh @@ -30,7 +30,6 @@ html::make_heading () { local -r id=$(tr -cd 'A-Za-z0-9' <<< "$text") if [ "$HTML_VARIANT_TO_USE" = exact ]; then - #echo "$(html::encode "$text")
" echo "$(html::encode "$text")
" else echo "$(html::encode "$text")
" @@ -139,6 +138,33 @@ html::source_highlight () { 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 + + text="${text/ /}" + local -r id=$(tr -cd 'A-Za-z0-9' <<< "$text") + echo "$(html::encode "$text")" +} + # Convert Gemtext to HTML html::fromgmi () { local is_list=no @@ -148,9 +174,8 @@ html::fromgmi () { while IFS='' read -r line; do if [[ "$is_list" == yes ]]; then - if [[ "$line" == '* '* ]]; then - echo "
  • $(html::encode "${line/\* /}")
  • " | - html::process_inline + if [[ "$line" == '* '* ]]; then + echo "
  • $(html::list::encode "${line/\* /}")
  • " | html::process_inline else is_list=no if [ "$HTML_VARIANT_TO_USE" = exact ]; then @@ -180,8 +205,7 @@ $line" '* '*) is_list=yes echo "