chriswarbo-net: 6154c48abe73a89113fcf4663bbdd5fa408b0b46
1: #!/usr/bin/env bash
2:
3: # Check calling convention
4: [[ -n "$1" ]] || {
5: echo "showPost requires an argument" 1>&2
6: exit 1
7: }
8:
9: [[ -n "$BASE_DIR" ]] || {
10: echo "showPost requires BASE_DIR" 1>&2
11: exit 1
12: }
13:
14: [[ -n "$STRIP_PREFIX" ]] || {
15: echo "showPost requires STRIP_PREFIX" 1>&2
16: exit 1
17: }
18:
19:
20: function getTitle {
21: # Get the contents of <title />
22: TTL=$(xidel - -s --extract "/html/head/title/text()" < "$1")
23: [[ -n "$TTL" ]] && {
24: echo "$TTL"
25: return
26: }
27:
28: TTL=$(xidel - -s --extract "//h1/text()" < "$1")
29: [[ -n "$TTL" ]] && {
30: echo "No <title /> element for '$1', falling back to <h1 />" 1>&2
31: echo "$TTL"
32: return
33: }
34:
35: TTL=$(basename "$1" .html)
36: [[ -n "$TTL" ]] && {
37: echo "No <title /> element for '$1', falling back to filename" 1>&2
38: echo "$TTL"
39: return
40: }
41:
42: TTL="Untitled"
43: echo "No <title /> element for '$1', falling back to '$TTL'" 1>&2
44: echo "$TTL"
45: }
46:
47: function getDate {
48: # Dates are in filenames
49: basename "$1" | cut -c 1-10
50: }
51:
52: function getUrl {
53: # Strip local prefix off argument to get the post's URL
54: F=$(echo "$1" | replace "$STRIP_PREFIX" '')
55: echo "$BASE_DIR/$F"
56: }
57:
58: function renderTime {
59: DATE=$(getDate "$1")
60: printf '<time class="dt-published" datetime="%s">%s</time>' "$DATE" "$DATE"
61: }
62:
63: function renderLink {
64: TITLE=$(getTitle "$1")
65: URL=$(getUrl "$1")
66: printf '<a class="p-name u-url" href="/%s">%s</a>' "$URL" "$TITLE"
67: }
68:
69: function extraClasses {
70: if grep -F "$(basename "$1")" < "$RANTS" > /dev/null
71: then
72: printf 'rant'
73: else
74: printf ' '
75: fi
76: }
77:
78: function renderEntry {
79: TIME=$(renderTime "$1")
80: LINK=$(renderLink "$1")
81: CLSS=$(extraClasses "$1")
82: printf '<li class="h-entry %s">%s - %s</li>' "$CLSS" "$LINK" "$TIME"
83: }
84:
85: renderEntry "$1"
Generated by git2html.