~jbauer/paritybit.ca

paritybit.ca/publish -rwxr-xr-x 6.5 KiB View raw
                                                                                
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
#!/bin/bash
#
# publish.sh
#
# Copyright (C) 2019  Jake Bauer
#
# A utility to publish posts/guides/projects 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/\>.

# $1 = flag which determines which action to take
# $2 = file to act upon

mainFeed="public/feeds/sitewide-feed.xml"
descriptionFile="/tmp/publish-description.txt"
destination="paritybit.ca:uploads/"
date=$(date +"%a, %d %b %Y %T %z")
miniDate=$(date +"%F")
filename=$(basename -s .md -- "$2")
pageTitle=$(head -n 1 "$2" | cut -d' ' -f2-)
entry=""

if [ "$1" = "-b" ]; then
    publishBlog=1
    baseURL="https://www.paritybit.ca/blog"
    basePage="blog"
    categoryPage="html/blog.html"
    newString="New Blog Post:"
    article="html/blog/$filename.html"
    feed="feeds/blog-feed.xml"
elif [ "$1" = "-p" ]; then
    publishProject=1
    baseURL="https://www.paritybit.ca/projects"
    basePage="projects"
    newString="New Project:"
    categoryPage="html/projects.html"
    article="html/projects/$filename.html"
    feed="feeds/projects-feed.xml"
elif [ "$1" = "-g" ]; then
    publishGuide=1
    baseURL="https://www.paritybit.ca/guides"
    basePage="guides"
    newString="New Guide:"
    categoryPage="html/guides.html"
    article="html/guides/$filename.html"
    feed="feeds/guides-feed.xml"
else
    cat << EOF
Usage: publish <-b|-p|-g> <page_to_publish>
Options:
    -b — Publish a blog post
    -p — Publish a project
    -g — Publish a guide
Examples:
    publish -b pages/blog/sample-post.md
    publish -p pages/projects/sample-project.md
    publish -g pages/guides/sample-guide.md"
EOF
    exit 1
fi

if [ "$2" = "" ]; then
    echo "Missing second argument."
    exit 2
fi

if [ ! -f "$2" ]; then
    echo "File $2 not found."
    exit 2
fi

if [ ! -x "$(command -v rsync)" ]; then
    echo "rsync is needed but was not found."
    exit 3
fi

if [ ! -x "$(command -v ed)" ]; then
    echo "ed is needed but was not found."
    exit 3
fi

# Make sure that there is an up-to-date compiled version of the page
./compile "$2"

# Update the relevant category page
if [ $publishBlog ]; then
    awk -v pt="$pageTitle" -v bp="$basePage" -v f="$filename" -v d="$miniDate"\
    '{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
elif [ $publishProject ]; then
    while true; do
        read -p "Is this a web-utility? " yn
        case $yn in
            [Yy]* ) webUtil=1; break;;
            [Nn]* ) webUtil=0; break;;
            * ) echo "Please answer Y|y or N|n.";;
        esac
    done
    if [ "$webUtil" = "1" ]; then
        sed -i "/web-utils/a \ \ \ \ \<li\>\<a href=\"$basePage\/$filename\"\>$pageTitle\<\/a\>\<\/li\>" pages/$basePage.md
    else
        # The meta text of the .md becomes the description on the projects.html page
        metaText=$(sed '3!d' pages/"$basePage"/"$filename".md | cut -d# -f 2)
        # Remove leading whitespace, surrounding quotes, forward slash characters
        metaTextClean=$(echo -e "${metaText}" | sed -e 's/^[[:space:]]*//')
        metaTextClean=$(sed -e 's/\//\\\//g' <<< "$metaTextClean")
        metaTextClean=$(sed -e 's/^"//' -e 's/"$//' <<< "$metaTextClean")
        sed -i "/normal-projects/a \ \ \ \ \<li\>\<a href=\"$basePage\/$filename\"\>$pageTitle\<\/a\>: $metaTextClean\<\/li\>" pages/$basePage.md
    fi
else
    sed -i "/<ul>/a \ \ \ \ \<li\>\<a href=\"$basePage\/$filename\"\>$pageTitle\<\/a\>\<\/li\>" pages/$basePage.md
fi
./compile pages/$basePage.md

# Update the home page to show the new published article and remove oldest entry
sed -i "/<\/div>/a $miniDate  \<a class=\"feed-item\" href=\"$basePage\/$filename\"\>$newString $pageTitle\<\/a\>\n" pages/home.md
ed -s pages/home.md <<< $'8,$g/### What is a Parity Bit/-2,-1d\nw'
./compile pages/home.md

tput setaf 3; tput bold; tput smul
echo "### PUBLISHING: $filename"
tput sgr0
printf "  Creating RSS <item>...       "

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

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

# Set <link> and <guid>
entry="${entry}
    <link>$baseURL/$filename</link>"
entry="${entry}
    <guid>$baseURL/$filename</guid>"

# Set pubDate using date command
entry="${entry}
    <pubDate>$date</pubDate>"

if [ $publishBlog ]; then
    # Get content of \<page\>-content.html and make description
    pageContent=$(cat build/blog/"$filename"-content.html)
    entry="${entry}
    <description><![CDATA[$pageContent]]></description>"
else
    # Otherwise get description from user
    echo "REPLACE THIS TEXT WITH A DESCRIPTION FOR THE PROJECT/GUIDE." \
        > $descriptionFile
    /usr/bin/editor $descriptionFile
    description=$(cat $descriptionFile)
    entry="${entry}
    <description><![CDATA[$description]]></description>"
fi

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

tput setaf 2; tput bold
echo "[DONE]"
tput sgr0

# Update the main feed and the relevant category feed
printf "  Updating feed files...       "

mv "$mainFeed" "$mainFeed.bak"
head -n 8 "$mainFeed.bak" > "$mainFeed"
echo "$entry" >> "$mainFeed"
tail -n +9 "$mainFeed.bak" >> "$mainFeed"

mv "public/$feed" "public/$feed.bak"
head -n 8 "public/$feed.bak" > "public/$feed"
echo "$entry" >> "public/$feed"
tail -n +9 "public/$feed.bak" >> "public/$feed"

tput setaf 2; tput bold
echo "[DONE]"
tput sgr0

# Update the sitemap
printf "  Updating sitemap.xml         "
baseURLClean=$(sed -e 's/\//\\\//g' <<< "$baseURL")
sed -i "/$baseURLClean</a \ \ \ \ \ \ \ \<url\>\<loc\>$baseURLClean\/$filename\<\/loc\>\<\/url\>" public/sitemap.xml

tput setaf 2; tput bold
echo "[DONE]"
tput sgr0

# Upload the new feeds plus the new article and updated pages to the website
printf "  Uploading pages and feeds... "

cd public && rsync -rR html/home.html sitemap.xml feeds/sitewide-feed.xml \
"$feed" "$categoryPage" "$article" "$destination" && cd ..

tput setaf 2; tput bold
echo "[DONE]"
tput sgr0

tput bold
echo "### PUBLISHED: $filename | Visit: $baseURL/$filename"
tput sgr0

exit 0