]> git.plutz.net Git - shellwiki/commitdiff
faster page_title function (without full md parsing)
authorPaul Hänsch <paul@plutz.net>
Tue, 15 Sep 2026 13:13:22 +0000 (15:13 +0200)
committerPaul Hänsch <paul@plutz.net>
Tue, 15 Sep 2026 13:13:22 +0000 (15:13 +0200)
tools.sh

index e067e4799064a43c07a967f27fc1035e3b06b688..f0a96ebaf67256cf87aaa0bd22bd81aca240991c 100755 (executable)
--- a/tools.sh
+++ b/tools.sh
@@ -227,27 +227,29 @@ page_title() {
 
   if mdfile="$(mdfile "${1}")"; then
     PAGE_TITLE="$(
-      # pick title from %title pragma
       sed -nE '
-        s;^%title[ \t]+([[:graph:]][[:print:]]+)\r?$;\1;p; tQ;
-        b; :Q q;
+        s;\r$;;;
+        /^%title[ \t]+([[:graph:]][[:print:]]+)$/bTITLE;
+        /^##?[^#]+/bHHEAD;
+        /^(---+|===+)$/bLHEAD;
+        h;b;
+
+        :TITLE
+        s;^%title[ \t]+;;;
+        p;q;
+
+        :HHEAD
+        s;[ \t]*\{[ \t]*([.#]?([0-9a-zA-Z]+-)*[0-9a-zA-Z]+[ \t]+)*([.#]?([0-9a-zA-Z]+-)*[0-9a-zA-Z]+)[ \t]*\}$;;;
+        s;^##?[ \t]*;;; s;[ \t]*#+$;;;
+        /^.+$/{p; q;}
+        b;
+
+        :LHEAD
+        x;
+        s;[ \t]*\{[ \t]*([.#]?([0-9a-zA-Z]+-)*[0-9a-zA-Z]+[ \t]+)*([.#]?([0-9a-zA-Z]+-)*[0-9a-zA-Z]+)[ \t]*\}$;;;
+        /^.+$/{p; q;}
       ' "$mdfile"
     )"
-    [ ! "${PAGE_TITLE}" ] && PAGE_TITLE="$(
-      # pick title from first h1/h2 headline
-      awk -f "${_EXEC}/cgilite/markdown.awk" <"$mdfile" \
-      | sed -nE '
-        s;^.*<h1[^>]*>(.*>)?([^<]+)(<.*)?</h1>.*$;\2;; tQ;
-        s;^.*<h2[^>]*>(.*>)?([^<]+)(<.*)?</h2>.*$;\2;; tQ;
-        b; :Q
-        # reverse escapes of cgilite HTML function,
-        # to prevent later double escaping
-        # later escaping must not be omited
-        s/&lt;/</g; s/&gt;/>/g;  s/&quot;/'\"'/g; s/&#x27;/'\''/g;
-        s/&#x5B;/[/g; s/&#x5D;/]/g;  s/&#x0D;/\r/g; s/&amp;/\&/g;
-        p; q;
-      '
-    )"
   fi
   if [ ! "${PAGE_TITLE}" ]; then
     # use last part of page URL as title