summaryrefslogtreecommitdiff
path: root/lib/generate.source.sh
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-01 17:39:53 +0200
committerPaul Buetow <paul@buetow.org>2026-03-01 17:39:53 +0200
commit6136b8a79a7f0ce5e35dc07cd88965953bd0e1af (patch)
treea916024f725f8de6e79d85cabfaecfb723ac65b7 /lib/generate.source.sh
parent309c63c420c425a55ee2a91dd03c77138305a05d (diff)
Extract generate::extract_title helper to eliminate duplication
Title extraction ($SED pattern + quote sanitization) was duplicated in generate, atomfeed, gemfeed, notes, and template modules. Now centralized with unit tests for heading extraction and empty files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'lib/generate.source.sh')
-rw-r--r--lib/generate.source.sh18
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/generate.source.sh b/lib/generate.source.sh
index 544ae08..17322e7 100644
--- a/lib/generate.source.sh
+++ b/lib/generate.source.sh
@@ -53,6 +53,13 @@ generate::safe_overwrite () {
fi
}
+# Extract the first heading from a .gmi file as a title, with double quotes
+# replaced by single quotes for safe embedding in feeds and HTML attributes.
+generate::extract_title () {
+ local -r file="$1"; shift
+ $SED -n '/^# / { s/# //; p; q; }' "$file" | tr '"' "'"
+}
+
# Add other docs (e.g. images, videos) from Gemtext to output format.
# Skips copying if the output file already exists and is newer than the source.
generate::fromgmi_add_docs () {
@@ -119,7 +126,7 @@ generate::_to_output_format () {
dest=${dest/.gmi/.$format}
local dest_dir=$(dirname "$dest")
- local title=$($SED -n '/^# / { s/# //; p; q; }' "$src" | tr '"' "'")
+ local title=$(generate::extract_title "$src")
if [[ -z "$title" ]]; then
title="$SUBTITLE"
fi
@@ -337,5 +344,14 @@ generate::test () {
assert::equals "$(cat "$tmp_dir/file")" 'different content'
assert::equals "$(test -f "$tmp_dir/file.tmp" && echo exists || echo gone)" 'gone'
+ # Test generate::extract_title: extracts first heading and sanitizes quotes
+ echo '# My "Great" Title' > "$tmp_dir/test.gmi"
+ echo '## Not this one' >> "$tmp_dir/test.gmi"
+ assert::equals "$(generate::extract_title "$tmp_dir/test.gmi")" "My 'Great' Title"
+
+ # Test generate::extract_title: file with no heading returns empty
+ echo 'Just a paragraph' > "$tmp_dir/noheading.gmi"
+ assert::equals "$(generate::extract_title "$tmp_dir/noheading.gmi")" ''
+
rm -rf "$tmp_dir"
}