summaryrefslogtreecommitdiff
path: root/lib/generate.source.sh
diff options
context:
space:
mode:
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"
}