Commit f1d5e4b2 authored by Lysander Trischler's avatar Lysander Trischler

Implement profiles for gal

parent cec6e149
......@@ -2,9 +2,21 @@
while true; do
case "$1" in
-h|--help) echo "$0 [-h|--help] [-u|--nav-url URL] [-t|--nav-title TITLE] [--] TITLE"; exit 0;;
-h|--help) echo "$0 [-h|--help] [-u|--nav-url URL] [-t|--nav-title TITLE] [-p|--profile PROFILE] [--] TITLE"; exit 0;;
-u|--nav-url) nav_url="$2"; shift 2;;
-t|--nav-title) nav_title="$2"; shift 2;;
-p|--profile)
profile_file="$(readlink -f "$0").$2.sh"; shift 2
if [ ! -e "$profile_file" ]; then
echo "$0: Profile file $profile_file not found" >&2
exit 3
fi
if [ ! -r "$profile_file" ]; then
echo "$0: Profile file $profile_file is not readable" >&2
exit 4
fi
# shellcheck source=gal.natur.sh
. "$profile_file";;
--) shift; break;;
-*) echo "$0: Unknown option '$1', use '--' to separate page title from options" >&2; exit 1;;
*) break;;
......@@ -31,3 +43,8 @@ done
if [ -n "$nav_url" ]; then
echo "<hr /><nav><a href=$nav_url>${nav_title:-$nav_url}</a></nav>" >> index.html
fi
# gal_hook would be defined in the profile
if type gal_hook > /dev/null; then
gal_hook
fi
#!/bin/sh
me=gal.natur.sh
gal_natur_sh_version=1.0
# configuration for others to adapt to their needs
html_file="$HOME/pictures/natur.html"
atom_file="$HOME/pictures/natur.atom"
atom_base_url=https://lyse.isobeef.org
atom_self_url="$atom_base_url/natur.atom"
atom_author=Lyse
# gal profile information
nav_url=../natur.html
nav_title=Natur
# Print the unique line number of the search string in the file. If there are
# multiple matching lines, it will print an error to stderr and return 2.
# Returns 0 on a match, 1 if no match was found.
# Usage: unique_line_number PATTERN FILE
unique_line_number() {
line_numbers="$(grep -no "$1" "$2" | cut -d : -f 1 | uniq)"
if [ -z "$line_numbers" ]; then
return 1
fi
if [ "$(echo "$line_numbers" | wc -l)" -gt 1 ]; then
echo "$me: Multiple lines found for '$1' in $2: $(echo "$line_numbers" | tr '\n' ' ')" >&2
return 2
fi
echo "$line_numbers"
}
gal_hook() {
if [ "$(basename "$PWD")" = web ]; then
dir="$(dirname "$PWD")"
else
dir="$PWD"
fi
dir="$(basename "$dir")"
test -e "$html_file" || cat >> "$html_file" <<EOF
<!DOCTYPE html>
<meta charset=utf-8 />
<title>Fotogalerien über die Natur</title>
<link rel=alternate type=application/atom+xml title=Atomfeed href=natur.atom />
<style>*{font-family:sans-serif}</style>
<h1>Fotogalerien über die Natur</h1>
<p>Dies ist eine Übersicht aller Fotogalerien, die mit dem Stichwort Natur versehen sind. Es gibt auch einen <a href=natur.atom>Atomfeed</a> dazu.
<ul>
</ul>
EOF
entry="<li><a href=$dir/>$title</a></li>"
tmp_file="$(mktemp)"
replace_line_number="$(unique_line_number "$dir/" "$html_file")"
if [ $? -gt 1 ]; then exit 10; fi
{
head -n 8 "$html_file"
if [ -n "$replace_line_number" ]; then
head -n "$((replace_line_number - 1))" "$html_file" | tail -n +9
echo "$entry"
tail -n "+$((replace_line_number + 1))" "$html_file"
else
echo "$entry"
tail -n +9 "$html_file"
fi
} > "$tmp_file"
mv "$tmp_file" "$html_file"
test -e "$atom_file" || cat >> "$atom_file" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
<link rel="self" type="application/atom+xml" href="$atom_self_url" />
<id>$atom_self_url</id>
<title>Fotogalerien über die Natur</title>
<generator>gal.natur.sh/$gal_natur_sh_version</generator>
<category term="Natur" />
<updated>WILL BE REPACED IMMEDIATELY</updated>
</feed>
EOF
now="$(date -Iseconds)"
entry="<entry><id>$atom_base_url/$dir/</id><title>$title</title><updated>$now</updated><author><name>$atom_author</name></author><link rel=\"alternate\" href=\"$atom_base_url/$dir/\" /><content type=\"html\"><![CDATA[<h1>$title</h1>"
for img in ??.jpg; do
entry="$entry <a href=\"$atom_base_url/$dir/$img\" style=\"cursor:zoom-in\"><img src=\"$atom_base_url/$dir/$(basename "$img" .jpg)-vorschau.jpg\" alt=\"$title\" /></a>"
done
entry="$entry]]></content></entry>"
tmp_file="$(mktemp)"
replace_line_number="$(unique_line_number "$atom_base_url/$dir/" "$atom_file")"
if [ $? -gt 1 ]; then exit 11; fi
{
head -n 7 "$atom_file"
echo "<updated>$now</updated>"
if [ -n "$replace_line_number" ]; then
head -n "$((replace_line_number - 1))" "$atom_file" | tail -n +9
echo "$entry"
tail -n "+$((replace_line_number + 1))" "$atom_file"
else
echo "$entry"
tail -n +9 "$atom_file"
fi
} > "$tmp_file"
mv "$tmp_file" "$atom_file"
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment