]> Git in Space - ssg/blob - ssg
Allow searching up for prefix files
[ssg] / ssg
1 #!/usr/bin/env bash
2
3 set -e
4
5 function usage() {
6 echo "Usage: $0 FILE"
7 exit
8 }
9
10 function error() {
11 echo "$1"
12 exit 1
13 }
14
15 # make sure args are right
16 [[ -z "$1" ]] && usage
17 [[ -n "$2" ]] && usage
18
19 # make sure file exists
20 [[ -d "$1" ]] && error "'$1' is a directory"
21 [[ -r "$1" ]] || error "File '$1' does not exist or cannot be read"
22
23 # get working path and file
24 dir="$(dirname "$1")"
25 file="$(realpath "$1")"
26 echo "Working in '$(pwd)'" > /dev/stderr
27 echo "Processing file '$file'" > /dev/stderr
28
29 # stock functions
30
31 function include() { # include a file
32 # include FILE
33 cat "$1"
34 }
35
36 function index() { # index current directory
37 find "$(dirname "$THISFILE")" -maxdepth 1 -name "${1:-*}" | sort \
38 | while read f ; do
39 name="$(basename "$f")"
40 [[ "$(stat -c %F "$f")" == "directory" ]] && name+='/'
41
42 # omit .cf and PREFIX/POSTFIX files
43 [[ "$name" == *.cf ]] && continue
44 [[ "$name" == "PREFIX" ]] && continue
45 [[ "$name" == "POSTFIX" ]] && continue
46
47 printf "%s <a href=\"%s\">%s</a>%$((34 - ${#name}))s %-17s %16s<br/>" \
48 "$(stat -c %A "$f")" \
49 "$name" "$name" "" \
50 "$(date '+%d-%m-%Y %H:%M' -d "@$(stat -c %Y "$f")")" \
51 "$(stat -c %s "$f")"
52 done
53 }
54
55 function indexh() { # index with header
56 printf "<b>---------- %-34s %-17s %16s</b><br/>" "name" "date" "size"
57 index "$@"
58 }
59
60 function git-history() {
61 git log --follow --format='%h %ad: %s<br/>' \
62 --date='format:%d-%m-%Y' "$THISFILE" \
63 | tr -d "\n"
64 }
65
66 function title() { # title
67 echo "<title>$1</title>"
68 }
69
70 function titlef() { # custom title using $FILE
71 echo "<title>$1$(basename "$THISFILE" | rev | cut -d'.' -f2- | rev)$2</title>"
72 }
73
74 # load user functions
75 libdir="${XDG_DATA_HOME:-$HOME/.local/share}/ssg"
76 echo "Loading libraries from '$libdir'" > /dev/stderr
77 for f in $libdir/** ; do
78 [[ -r "$f" ]] && source "$f" && echo "Loaded '$f'" > /dev/stderr
79 done
80
81 # load local functions
82 if [[ -d "./ssg" ]] ; then
83 echo "Loading local libraries from '$(pwd)/ssg'" > /dev/stderr
84 for f in ./ssg/** ; do
85 [[ -r "$f" ]] && source "$f" && echo "Loaded '$f'" > /dev/stderr
86 done
87 fi
88
89 function evaluate() {
90 while IFS= read l ; do
91 if [[ "$l" == ';;'* ]] ; then
92 echo "running command '${l:2}'" > /dev/stderr
93 eval "THISFILE='$file' ${l:2}"
94 else
95 echo "$l"
96 fi
97 done
98 }
99
100 function get-affix() {
101 name="$1"
102 shift
103 target="$1"
104 while :; do
105 [[ -f "$target/$name" ]] && break
106 target="$(dirname "$target")"
107 done
108 echo "$target/$name"
109 }
110
111 # evaluate
112 pre="$(get-affix PREFIX "$(dirname "$file")")"
113 suf="$(get-affix SUFFIX "$(dirname "$file")")"
114 if [[ -r "$pre" ]] ; then
115 echo "Prefix file '$pre' available" > /dev/stderr
116 evaluate < "$pre"
117 fi
118 evaluate < "$file"
119 if [[ -r "$suf" ]] ; then
120 echo "Suffix file '$suf' available" > /dev/stderr
121 evaluate < "$suf"
122 fi