summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-04-30 13:06:57 +0300
committerPaul Buetow <paul@buetow.org>2024-04-30 13:06:57 +0300
commite0f9a7722da62dec49353abff27cce9caf409113 (patch)
treea5be1febd5a5fa055b3eafdc76fc2da327ca87da
parentf64c090bac0741963acef6193440d56fc6fbd48f (diff)
add id's to the headers in html, for ToC references later
-rw-r--r--IDEA.md2
-rw-r--r--lib/html.source.sh14
-rw-r--r--lib/template.source.sh6
3 files changed, 13 insertions, 9 deletions
diff --git a/IDEA.md b/IDEA.md
index 6fd1d4a..5f28b4f 100644
--- a/IDEA.md
+++ b/IDEA.md
@@ -13,7 +13,5 @@ There will be some more scripting required to get the page order and ToC correct
## More ideas
-* Use GNU `source-highlight` for code highlighting in bare-blocks. Can add lang in block beginning. See also https://www.gnu.org/savannah-checkouts/gnu/src-highlite/style_examples.html
-* Automatic ToC generation.
* Sitemap generation.
* More output formats. Gopher? Groff? Plain text? PDF via Pandoc? .sh with interactive menus?
diff --git a/lib/html.source.sh b/lib/html.source.sh
index 8f4da2f..683ea99 100644
--- a/lib/html.source.sh
+++ b/lib/html.source.sh
@@ -27,12 +27,13 @@ html::make_paragraph () {
html::make_heading () {
local -r text=$($SED -E 's/^#+ //' <<< "$1"); shift
local -r level="$1"; shift
+ local -r id=$(tr -cd 'A-Za-z0-9' <<< "$text")
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 />"
+ echo "<h${level} style='display: inline' id='${id}'>$(html::encode "$text")</h${level}><br />"
else
- echo "<h${level}>$(html::encode "$text")</h${level}><br />"
+ echo "<h${level} id='${id}'>$(html::encode "$text")</h${level}><br />"
fi
}
@@ -300,13 +301,16 @@ html::test::exact () {
assert::equals "$(html::make_paragraph "$line")" "<span>echo foo 2&gt;&amp;1</span><br />"
line='# Header 1'
- assert::equals "$(html::make_heading "$line" 1)" "<h1 style='display: inline'>Header 1</h1><br />"
+ local id=$(tr -cd 'A-Za-z0-9' <<< "$line")
+ assert::equals "$(html::make_heading "$line" 1)" "<h1 style='display: inline' id='${id}'>Header 1</h1><br />"
line='## Header 2'
- assert::equals "$(html::make_heading "$line" 2)" "<h2 style='display: inline'>Header 2</h2><br />"
+ id=$(tr -cd 'A-Za-z0-9' <<< "$line")
+ assert::equals "$(html::make_heading "$line" 2)" "<h2 style='display: inline' id='${id}'>Header 2</h2><br />"
line='### Header 3'
- assert::equals "$(html::make_heading "$line" 3)" "<h3 style='display: inline'>Header 3</h3><br />"
+ id=$(tr -cd 'A-Za-z0-9' <<< "$line")
+ assert::equals "$(html::make_heading "$line" 3)" "<h3 style='display: inline' id='${id}'>Header 3</h3><br />"
line='> This is a quote'
assert::equals "$(html::make_quote "$line")" "<span class='quote'>This is a quote</span><br />"
diff --git a/lib/template.source.sh b/lib/template.source.sh
index 29d0bf4..519f2cf 100644
--- a/lib/template.source.sh
+++ b/lib/template.source.sh
@@ -93,12 +93,14 @@ template::inline::index () {
# To generate a table of contents
template::inline::toc () {
echo '```'
- echo 'Table of contents:'
+ echo 'Table of contents'
+ echo '================='
+ echo ''
< "$(basename "$CURRENT_TPL")" $SED -E -n '
/^```/,/^```/! {
/^#+ / {
s/#/ /g
- s/^ //
+ s/^ //
p
}
}