~chambln/cosine.blue

ref: 2862b6de51533a9dea9f6aee7fce1671e013d4be cosine.blue/bin/make -rwxr-xr-x 5.8 KiB
2862b6deGregory Chamberlain Fix template 2 months ago
                                                                                
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/sh

# cosine.blue -- static site generator
# Copyright (C) 2020  Gregory L Chamberlain <greg@cosine.blue>

# This file is part of cosine.blue.
#
# cosine.blue is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cosine.blue is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cosine.blue.  If not, see
# <https://www.gnu.org/licenses/>.

. share/lib.sh

html() {
    echo '<!DOCTYPE html>'
    {
        :|{
            printf '<meta %s />\n' \
                   'charset="UTF-8"' \
                   'name="viewport" width="device-width" initial-scale="1"'
            x title "$(quote "${pagetitle:-$title}")"

            [ -n "${description-}" ] &&
                printf '<meta name="description" content="%s" />\n' \
                       "$(quote "$description")"

            # Embed CSS directly in the HTML to reduce the number of requests.
            x style < share/style.css

            # Provide an empty favicon to prevent request for ‘favicon.ico’.
            echo '<link rel="icon" href="data:;base64,iVBORw0KGgo=" />'

            # Link to the RSS feed.
            echo '<link type="application/rss+xml" rel="alternate" href="/rss.xml" title="cosine.blue" />'

            # Link to the canonical path for this page, removing any
            # final ‘index.html’ segment.
            case $canonical in
                index.html) canonical='' ;;
                *) canonical=${canonical%/index.html} ;;
            esac
            printf '<link rel="canonical" href="%s">\n' https://cosine.blue/"$canonical"
        } | x head
        {
            [ _"${layout-}" = _solo ] || x small < share/header.html | x header
            {
                : | {
                    if [ -n "${title-}" ]; then
                        x h1 "$title"
                    fi
                    if [ -n "${description-}" ]; then
                        x p "$description"
                    fi
                    if [ -n "${date-}" ]; then
                        {
                            x time "$date"
                            [ -n "${log-}" ] &&
                              printf ' &mdash; <a href="%s">History</a>' "$log"
                        } | x p
                    fi
                    if [ -n "${cover_img_src-}" ]; then
                        {
                            # shellcheck disable=SC2154
                            alt=${cover_img_alt:+ alt=\"Cover image: $cover_img_alt\"}
                            x "img src=\"$cover_img_src\"$alt"
                            [ -n "${cover_img_caption-}" ] &&
                                x figcaption "$cover_img_caption" | x small
                        } | x figure
                    fi
                } | x header
                cat
            } | x 'main id="content"'
            [ _"${layout-}" = _solo ] || x small < share/footer.html | x footer
        } | x body
    } | x 'html lang="en-GB"'
}

article() {
    x article
    x hr
    x p <<EOF
To leave a comment, please send a <a
href="https://useplaintext.email/">plaintext</a> email to <a
href="mailto:%7Echambln/public-inbox@lists.sr.ht?subject=Re:%20$title"><code>~chambln/public-inbox@lists.sr.ht</code></a>
and it will show up in <a
href="https://lists.sr.ht/~chambln/public-inbox">my public inbox</a>.
EOF
}

rss_item() {
    basename=${1##*/}
    # shellcheck disable=SC1090
    . env/"$basename".sh
    x title "${pagetitle:-$title}"
    x author "greg@cosine.blue (Gregory Chamberlain)"
    x pubDate "$(date -ud "$date" +'%a, %d %b %Y %T +0000')"
    x guid "https://cosine.blue/${basename%.*}.html"
    # shellcheck disable=SC2094
    case "$1" in
        *.md) cmark ;;
        *.html.sh) sh ;;
        *.html) cat ;;
    esac < "$1" | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' | x description
}

rss() {
    {
        x title cosine.blue
        x link https://cosine.blue
        x description 'Blog by Gregory Chamberlain.'
        x webMaster "greg@cosine.blue (Gregory Chamberlain)"
        x copyright 'GPLv3+ or CC BY-SA 4.0'
        echo '<atom:link href="https://cosine.blue/rss.xml" rel="self" type="application/rss+xml" />'
        find src/article -type f | sort -r | while IFS= read -r item
        do
            [ -f "$item" ] || continue
            rss_item "$item" | x item
        done
    } | x channel | x 'rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"'
}

set -eu

PATH=bin:$PATH

rm -fr www
mkdir -p www

rss > www/rss.xml

for src in src/* src/*/*; do [ -f "$src" ] && (
    log=https://git.sr.ht/~chambln/cosine.blue/log/master/item/$src
    bn=${src##*/}
    # shellcheck disable=SC1090
    [ -e env/"$bn".sh ] && . env/"$bn".sh
    unset canonical
    case $src in
        */article/*.md)
            canonical=${bn%.md}.html
            cmark < "$src" | article | html > www/"$canonical"
            ;;
        src/article/*.html)
            canonical=$bn
            article < "$src" | html > www/"$bn"
            ;;
        *.md)
            canonical=${bn%.md}.html
            cmark < "$src" | html > www/"${bn%.md}.html"
            ;;
        *.html.sh)
            canonical=${bn%.sh}
            sh < "$src" | html > www/"$canonical"
            ;;
        *.html)
            canonical=$bn
            html < "$src" > www/"$canonical"
            ;;
        *) cp -f "$src" www
    esac

    case $src in
        src/photos/*)
            mkdir -p www/thumbnail
            convert "$src" -resize 480x www/thumbnail/"$bn"
            ;;
    esac
) done