~jbauer/paritybit.ca

paritybit.ca/publish -rwxr-xr-x 6.9 KiB
5fc6e588Jake Bauer Add gemini homepage 3 hours 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/sh

# publish
#
# Copyright (C) 2019,2020  Jake Bauer
#
# A utility to publish blog posts to paritybit.ca.
#
# This program 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.
#
# This program 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 this program.  If not, see \<https://www.gnu.org/licenses/\>.

# POSIX Shell "Strict Mode"
set -o errexit
set -o nounset
IFS=$(printf '\n\t')

if [ ! -x "$(command -v rsync)" ]; then
    echo "rsync is needed but was not found."
    exit 1
fi
if [ -x "$(command -v nvim)" ]; then
    vim_ver="nvim"
elif [ ! -x "$(command -v vim)" ]; then
    vim_ver="vim"
else
    echo "Either vim or nvim is needed but was not found."
    exit 1
fi
if [ -z "$1" ]; then
    echo "No arguments given; nothing to do."
    exit 1
fi

# Define colours
CLEAR="\033[0m"
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"

# The RSS feed to edit
mainFeed="public/feeds/sitewide-feed.xml"
# Where to upload files
remote="paritybit.ca:uploads/"
# The main base page on the site
baseURL="https://www.paritybit.ca/blog"
# Name of the base page
basePage="blog"
# The path to the compiled base page
categoryPage="html/blog.html"
# Set to false to disable auto uploading files
autoUpload="true"
# Set to false to disable auto posting statuses on Mastodon
autoPost="true"
# Social media API endpoint
socialMediaURL="https://pleroma.paritybit.ca/api/v1/statuses"
# Social media API access token
accessToken=""

# Prints the message passed as the 1st argument in red.
print_error_msg()
{
    printf "$RED[ee] %s\n$CLEAR" "$1"
}

# Prints the message passed as the 1st argument in green.
print_success_msg()
{
    printf "$GREEN[ok] %s\n$CLEAR" "$1"
}

# Prints the message passed as the 1st argument in yellow.
print_header_msg()
{
    printf "$YELLOW[hh] %s\n$CLEAR" "$1"
}

# Prints the message passed as the 1st argument with default colours.
print_info_msg()
{
    printf "$CLEAR[ii] %s\n" "$1"
}

# Populate the [DATE] fields of the blog post
populate_date()
{
    print_info_msg "Populating [DATE] fields"
    sed -i -e "s/\[DATE\]/$shortDate/g" "$file"
    ./compile -n "$file"
}

# Update the main blog list page
update_base_page()
{
    print_info_msg "Updating base page"
    awk -v pt="$pageTitle" -v bp="$basePage" -v f="$fileName" -v d="$shortDate" \
        '{print} /^<ul>/ && !n {print "    <li>"d" <a href=\""bp"/"f"\">"pt"</a></li>"; n++}' \
    pages/"$basePage".md > tmp.md && mv tmp.md pages/"$basePage".md
    ./compile -n pages/$basePage.md
}


# Update the home page to show the new published article and remove oldest entry
update_home_page()
{
    print_info_msg "Updating home page"
    sed -i -e "/<\/div>/a\\
$shortDate  \<a class=\"feed-item\" href=\"$basePage\/$fileName\"\>$pageTitle\<\/a\>\n" \
        pages/home.md
    echo "/### What is a Parity Bit
    2k2dd
    :wq" > /tmp/script.vim
    "$vim_ver" -s /tmp/script.vim pages/home.md
    ./compile -s -n pages/home.md
}

create_rss_entry()
{
    print_info_msg "Creating RSS entry"

    # Opening tag
    rssEntry="${rssEntry}<item>"

    # Set <title>
    rssEntry="${rssEntry}
        <title>$pageTitle</title>"

    # Set <link> and <guid>
    rssEntry="${rssEntry}
        <link>$baseURL/$fileName</link>"
    rssEntry="${rssEntry}
        <guid>$baseURL/$fileName</guid>"

    # Set pubDate
    rssEntry="${rssEntry}
        <pubDate>$date</pubDate>"

    # Fill out content
    pageContent=$(cat build/blog/"$fileName"-content.html)
    rssEntry="${rssEntry}
    <description><![CDATA[$pageContent]]></description>"

    # Close the entry
    rssEntry="${rssEntry}
    </item>"
}

update_rss_feed()
{
    print_info_msg "Updating RSS feed"
    mv "$mainFeed" "$mainFeed.bak"
    head -n 8 "$mainFeed.bak" > "$mainFeed"
    echo "$rssEntry" >> "$mainFeed"
    tail -n +9 "$mainFeed.bak" >> "$mainFeed"
}

update_sitemap()
{
    print_info_msg "Updating sitemap"
    echo "$baseURL" > /tmp/base-url.txt
    sed -i -e 's/\//\\\//g' /tmp/base-url.txt
    baseURLClean=$(cat /tmp/base-url.txt)
    sed -i -e "/$baseURLClean</a\\
        <url\>\<loc\>$baseURLClean\/$fileName\<\/loc\>\<\/url\>" \
        public/sitemap.xml
}

# Upload the new feed plus the new article and updated pages to the website
auto_upload()
{
    if [ "$autoUpload" != "true" ]; then
        return 0
    fi
    print_info_msg "Auto-uploading files"
    cd public && rsync -rR html/home.html sitemap.xml feeds/sitewide-feed.xml \
    "$categoryPage" "$article" "$remote" && cd ..
}

# Post a status update on Pleroma linking to the new blog post
auto_post()
{
    if [ "$autoPost" != "true" ]; then
        return 0
    fi

    print_info_msg "Generating social media post"
    meta="$(head -n3 "$file" | tail -n1 \
        | cut -d'#' -f2 | sed -e 's/^[[:space:]]*//')"
    printf "New Blog Post: %s\n\n%s/%s\n\n%s\n\n#100DaysToOffload" \
        "$pageTitle" "$baseURL" "$fileName" "$meta" > /tmp/status.txt
    uniqueId="$(md5sum /tmp/status.txt | awk '{print $1}')"

    echo ""
    cat /tmp/status.txt
    echo ""

    resp="$(curl -s -S -i -X POST \
        -H "Idempotency-Key: $uniqueId" \
        -H "Authorization: Bearer $accessToken" \
        -F status="$(cat /tmp/status.txt)" \
        -F visibility=public \
        "$socialMediaURL")"
    httpCode="$(echo "$resp" | grep HTTP/ | awk '{print $2}')"

    if [ "$httpCode" = "200" ]; then
        print_success_msg "Successfully posted status update"
    else
        print_error_msg "ERROR: Failed to post status update"
        echo "$resp"
    fi
}

for file in "$@"; do
    if [ ! -f "$file" ]; then
        print_error_msg "ERROR: File $file not found or is not a file."
        continue
    fi

    date=$(date +"%a, %d %b %Y %T %z")
    shortDate=$(date +"%F")
    fileName=$(basename -s .md -- "$file")
    pageTitle=$(head -n 1 "$file" | cut -d' ' -f2-)
    article="html/blog/$fileName.html"
    rssEntry=""

    print_header_msg "PUBLISHING: $file"

    # Thanks to: https://stackoverflow.com/a/27875395/12865517
    printf "Are you sure you wish to publish this post? [y/N]: "
    old_stty_cfg=$(stty -g)
    stty raw -echo
    answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done )
    stty "$old_stty_cfg"
    if echo "$answer" | grep -iq "^y" ;then
        echo ""
        print_success_msg "Publishing..."
    else
        echo ""
        print_error_msg "Refusing to publish"
        continue
    fi

    populate_date
    update_base_page
    update_home_page
    create_rss_entry
    update_rss_feed
    update_sitemap
    auto_upload
    auto_post

    print_success_msg "PUBLISHED: $file"
    print_success_msg "Visit: $baseURL/$fileName.html"

done