about summary refs log tree commit diff stats
path: root/mynah
blob: cf9c876e62caf4e0ae86d9597c59b08039decd7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bash

# requires pandoc
#          coreutils
#          uuidgen
#          tidy
#          the_silver_searcher (could use grep instead)

set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
    set -o xtrace
fi

SITENAME="wiki"
SITEURL="https://elioat.tilde.institute/wiki"
SITEDESCRIPTION="a little wiki"
if [[ -f updated ]]; then
   LASTUPDATE=$(<updated)
fi
TIMESTAMP=$(gdate +"%s")
RFC822=$(gdate -R)
RSSHEADER='<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>
<![CDATA[ '${SITENAME}']]>
</title>
<link>'${SITEURL}'</link>
<atom:link href="'${SITEURL}'/feed.rss" rel="self" type="application/rss+xml"/>
<description>
<![CDATA[ '${SITEDESCRIPTION}' ]]>
</description>'
RSSEND='</channel></rss>'

build_logs() {
    pandoc changelog.md -o public/changelog.html
    cd content
    rm ../archive.md
    echo -e "# Archive\n" > ../archive.md
    for file in *;
    do
        archive_entry="- [${file%.*}](${SITEURL}/${file%.*}.html)"
        echo -e "$archive_entry" >> ../archive.md
    done
    cd ..
    pandoc -c styles.css -s archive.md -o public/archive.html
}

build() {
    cd content
    i=0

    rm ../public/feed.xml
    touch ../public/feed.xml
    echo -e "${RSSHEADER}" > ../public/feed.xml

    for file in *; 
    do
        file_last_updated=$(stat -f %m "$file")
        if [ "$file_last_updated" -gt "$LASTUPDATE" ]; 
        then
            (( i++ ))
            updated_at="- [${file%.*}](${SITEURL}/${file%.*}.html), ${RFC822}"
            echo -e "$updated_at\n$(cat ../changelog.md)" > ../changelog.md
            pandoc -c ../styles.css -s "${file}" -o ../public/"${file%.*}".html
            rss_entry='<item>
<title>
<![CDATA[ the page "'${file%.*}'" was updated! ]]>
</title>
<link>'${SITEURL}'/'${file%.*}.html'</link>
<guid>'${SITEURL}/${file%.*}.html#$(uuidgen)'</guid>
<pubDate>'${RFC822}'</pubDate>
<description>
<![CDATA[ 
<p><a href="'${SITEURL}/${file%.*}.html'">'${file%.*}'</a> updated!</p><br><br><pre><code>'$(fold -sw 80 <"$file")'</code></pre>  ]]>
</description>
</item>'
            echo -e "$rss_entry" >> ../public/feed.xml
        fi
    done
    cd ..
    echo -e "$RSSEND" >> public/feed.xml
    # this is stupid and superfluous, but I was bugged by the weirdly formatted RSS output
    tidy \
        -xml \
        --show-body-only yes \
        --indent auto \
        --indent-spaces 2 \
        --quiet yes \
        -w 80 \
        -m \
        --force-output y \
        --wrap 0 \
            public/feed.xml
    if [ $i -gt 0 ];
    then
        build_logs "$@"
    fi
    echo "$TIMESTAMP" > updated
    echo "updated" $i "items."
}

help_text() {
    echo '
    mynah, a little wiki bird

    usage: ./mynah
                     -b(uild)....to build the site
                     -s(earch)...to search for a pattern across all wiki entries
                     -d(eploy)...to publish the public directory to a host
                     -h(elp).....to display this message
                     -n(est).....to start a new wiki
'
}

if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
    help_text "$@"
    exit
elif [[ "${1-}" =~ ^-*b(uild)?$ ]]; then
    build "$@"
    exit
elif [[ "${1-}" =~ ^-*s(earch)?$ ]]; then
    ag "$2" content/*
    exit
elif [[ "${1-}" =~ ^-*d(eploy)?$ ]]; then
    scp -r public/* elioat@tilde.institute:~/public_html/wiki/
    exit
elif [[ "${1-}" =~ ^-*n(est)?$ ]]; then
    if [[ -f content ]]; then
        echo -e "   
            a content directory already exists 
            so you can't build a new nest here
            "
    else
        mkdir content public
        touch archive.md changelog.md updated public/feed.xml
    fi
    exit
fi

cd "$(dirname "$0")"

main() {
    help_text "$@"
}

main "$@"