-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·200 lines (170 loc) · 6.18 KB
/
build
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
#!/usr/bin/env tclsh
package require fileutil
# Get the modification time of a file with the W3C datetime format.
# Refer to the link below for the W3C datetime format:
# https://www.w3.org/TR/NOTE-datetime
proc file-lastmod {file} {
file stat $file fstat
return [clock format $fstat(ctime) -format {%Y-%m-%dT%H:%M:%SZ} -timezone :UTC]
}
# Encode and escape special characters into the hex format '%XX'.
# Refer to the link below for the list of characters that are escape free:
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
proc encode-url {url} {
foreach char [split $url {}] {
if {[string match {[A-Za-z0-9-_.!~*'();/?:@&=+$,#]} $char]} {
append eurl $char
} else {
binary scan [encoding convertto utf-8 $char] cu* nums
foreach num $nums {
append eurl [format {%%%X} $num]
}
}
}
# TODO: escape characters that are excepted here
# but should be escaped in HTML, e.g. &
return $eurl
}
proc rst2html {rst html title} {
if {[file exist $html]} {
file stat $rst rstat
file stat $html hstat
file stat src/template.html tstat
if {$hstat(ctime) > $rstat(ctime) && $hstat(ctime) > $tstat(ctime)} {
return
}
}
# create parent directories
file mkdir [file dirname $html]
exec rst2html5.py \
--strict \
--link-stylesheet \
--initial-header-level 2 \
--stylesheet "/statics/css/style.css" \
--template "src/template.html" \
--title "$title | an9wer's blog" \
$rst $html
puts "$html <- $rst"
}
# collect information of each post
foreach rst [lsort -decreasing [fileutil::findByPattern src -glob {[0-9][0-9]_*.rst}]] {
dict set post rst $rst
dict set post html [regsub {^src(.*)\.rst$} $rst {docs\1.html}]
dict set post url [regsub {^src(.*)\.rst$} $rst {\1.html} ]
dict set post date [regsub {^src/(\d+)/(\d+)/(\d+)_.*$} $rst {\1-\2-\3} ]
dict set post title [regsub {^src/.*_(.*).rst$} $rst {\1} ]
set pf [open $rst r]
while {[gets $pf line] >= 0} {
if {[regexp {^\s+:tags: (.*)$} $line]} {
dict set post tags [regsub {^\s+:tags: (.*)$} $line {\1}]
break
}
}
close $pf
lappend posts $post
}
# main
# generate the 'posts.rst' file
set pf [open src/posts.rst w]
foreach post $posts {
dict with post {
puts $pf ".. container:: posts\n"
puts $pf "\t`$title <[encode-url $url]>`_\n"
puts $pf "\t$tags | $date\n"
}
}
close $pf
# generate the "index" html page
rst2html src/posts.rst docs/index.html posts
# generate the "posts" html page
rst2html src/posts.rst docs/posts.html posts
# generate the "moments" html page
rst2html src/moments.rst docs/moments.html moments
# generate the "tools" html page
rst2html src/tools.rst docs/tools.html tools
# generate the "about" html page
rst2html src/about.rst docs/about.html about
# generate html page for each post
foreach post $posts {
dict with post {
rst2html $rst $html $title
}
}
# generate the sitemap
# refer to the link below for the sitemap specification:
# https://www.sitemaps.org/protocol.html
set dm an9wer.github.io
set sf [open docs/sitemap.xml w]
puts $sf {<?xml version="1.0" encoding="UTF-8"?>}
puts $sf {<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">}
puts $sf "\t<url>"
puts $sf "\t\t<loc>https://$dm/index.html</loc>"
puts $sf "\t\t<lastmod>[file-lastmod docs/index.html]</lastmod>"
puts $sf "\t\t<changefreq>weekly</changefreq>"
puts $sf "\t\t<priority>0.3</priority>"
puts $sf "\t</url>"
puts $sf "\t<url>"
puts $sf "\t\t<loc>https://$dm/posts.html</loc>"
puts $sf "\t\t<lastmod>[file-lastmod docs/posts.html]</lastmod>"
puts $sf "\t\t<changefreq>weekly</changefreq>"
puts $sf "\t\t<priority>0.3</priority>"
puts $sf "\t</url>"
puts $sf "\t<url>"
puts $sf "\t\t<loc>https://$dm/about.html</loc>"
puts $sf "\t\t<lastmod>[file-lastmod docs/about.html]</lastmod>"
puts $sf "\t\t<changefreq>monthly</changefreq>"
puts $sf "\t\t<priority>0.3</priority>"
puts $sf "\t</url>"
foreach post $posts {
dict with post {
puts $sf "\t<url>"
puts $sf "\t\t<loc>https://$dm[encode-url $url]</loc>"
puts $sf "\t\t<lastmod>[file-lastmod $html]</lastmod>"
puts $sf "\t\t<changefreq>monthly</changefreq>"
puts $sf "\t\t<priority>0.5</priority>"
puts $sf "\t</url>"
}
}
puts $sf {</urlset>}
close $sf
puts "docs/sitemap.xml"
# generate the RSS feed
# refer the link below for the RSS specification:
# https://www.rssboard.org/rss-specification
set dm an9wer.github.io
set rf [open docs/rss.xml w]
puts $rf {<?xml version="1.0" encoding="UTF-8"?>}
puts $rf {<rss version="2.0">}
puts $rf "\t<channel>"
puts $rf "\t\t<link>https://$dm/posts.html</link>"
puts $rf "\t\t<title>an9wer's blog</title>"
puts $rf "\t\t<category>blog</category>"
puts $rf "\t\t<webMaster>an9wer@gmail.com (Runney Wu)</webMaster>"
puts $rf "\t\t<managingEditor>an9wer@gmail.com (Runney Wu)</managingEditor>"
puts $rf "\t\t<copyright>CC BY-NC-SA</copyright>"
puts $rf "\t\t<pubDate>[clock format [clock seconds] -format {%a, %d %b %Y %H:%M:%S %Z} -timezone :UTC]</pubDate>"
puts $rf "\t\t<lastBuildDate>[clock format [clock seconds] -format {%a, %d %b %Y %H:%M:%S %Z} -timezone :UTC]</lastBuildDate>"
puts $rf "\t\t<docs>https://www.rssboard.org/rss-specification</docs>"
puts $rf "\t\t<generator>https://github.com/an9wer/an9wer.github.io/blob/master/build</generator>"
puts $rf "\t\t<ttl>[expr 60 * 24]</ttl>"
puts $rf "\t\t<image>"
puts $rf "\t\t\t<url>https://$dm/statics/images/avatar.jpg</url>"
puts $rf "\t\t\t<title>an9wer's blog</title>"
puts $rf "\t\t\t<link>https://$dm/posts.html</link>"
puts $rf "\t\t</image>"
foreach post $posts {
dict with post {
puts $rf "\t\t<item>"
puts $rf "\t\t\t<title>$title</title>"
puts $rf "\t\t\t<link>https://$dm[encode-url $url]</link>"
# TODO: description
puts $rf "\t\t\t<pubDate>[clock format [clock scan $date -format "%Y-%m-%d" -timezone :UTC] -format {%a, %d %b %Y %H:%M:%S %Z} -timezone :UTC]</pubDate>"
puts $rf "\t\t\t<author>an9wer@gmail.com</author>"
puts $rf "\t\t\t<category>$tags</category>"
puts $rf "\t\t</item>"
}
}
puts $rf "\t</channel>"
puts $rf "</rss>"
close $rf
puts "docs/rss.xml"