/
rizzo.groovy
268 lines (237 loc) · 13.1 KB
/
rizzo.groovy
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
266
267
268
import java.text.SimpleDateFormat
import groovy.text.SimpleTemplateEngine
import com.petebevin.markdown.MarkdownProcessor
@Grab('com.madgag:markdownj-core:0.4.1')
class Post {
String title
String name
Date dateCreated
Date lastUpdated
String summary
String content
List tags = []
}
class Tag {
String name
List posts = []
String toString(){ name }
}
def cl = new CliBuilder(usage: 'groovy rizzo -s "source" -d "destination"')
cl.s(longOpt:'source', args:1, required:true, 'Location of website source')
cl.d(longOpt:'destination', args:1, required:true, 'Location in which to place generated website')
def opt = cl.parse(args)
if(!opt){
return
} else {
def posts = []
def tags = []
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy h:mm a")
SimpleDateFormat outputFormatter = new SimpleDateFormat("EEEE, MMMMM d, yyyy 'at' h:mm a")
SimpleDateFormat archiveFormatter = new SimpleDateFormat("MMMMM d, yyyy")
SimpleDateFormat feedFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
SimpleDateFormat itemIdDateFormatter = new SimpleDateFormat("yyyy-MM-dd")
def sourceExists = new File("${opt.d}").exists()
def siteConfig = new ConfigSlurper().parse(new File("${opt.s}/site-config.groovy").toURL())
def metadata
if(!new File("${opt.s}/meta.groovy").exists()){
new File("${opt.s}/meta.groovy").write("published = \"${formatter.format(new Date())}\"")
}
metadata = new ConfigSlurper().parse(new File("${opt.s}/meta.groovy").toURL())
Date lastPublished = formatter.parse(metadata.published)
def begin = "${opt.s}/posts/"; def h_begin = "${opt.s}/pages/"
def home = new File("${opt.s}/templates/home.html")
def home_mid = new File("${opt.s}/templates/home_mid.html")
def page = new File("${opt.s}/templates/page.html")
def post = new File("${opt.s}/templates/post.html")
def feed = new File("${opt.s}/templates/feed.xml"); def entry = new File("${opt.s}/templates/entry.xml")
def fozziwig = new SimpleTemplateEngine()
def mdProcessor = new MarkdownProcessor()
new File("${opt.d}/archives/").mkdirs()
new File("${h_begin}").eachFile { file ->
if(file.name.endsWith('.html')) {
def name = file.name[0 .. file.name.lastIndexOf('.')-1]
List pageText = file.readLines()
def currentPost = new Post(title:pageText[0], name:name, lastUpdated:formatter.parse(pageText[1].toString()))
pageText = pageText[2..-1]
currentPost.content = pageText.join("\n")
if(currentPost.lastUpdated > lastPublished || !sourceExists){
def scrooge = ["postTitle" : currentPost.title, "postName" : currentPost.name, "content" : currentPost.content, "lastUpdated" : """
<br />
<p style="font-size:smaller; text-align:right;">(Updated ${outputFormatter.format(currentPost.lastUpdated)})</p>""", "config" : siteConfig]
new File("${opt.d}/${name}.html").write("${fozziwig.createTemplate(page).make(scrooge)}")
}
}
}
new File("${h_begin}").eachFile { file ->
if(file.name.endsWith('.md')) {
def name = file.name[0 .. file.name.lastIndexOf('.')-1]
List pageText = file.readLines()
def currentPost = new Post(title:pageText[0], name:name, lastUpdated:formatter.parse(pageText[1].toString()))
pageText = pageText[2..-1]
currentPost.content = mdProcessor.markdown(pageText.join("\n"))
if(currentPost.lastUpdated > lastPublished || !sourceExists){
def scrooge = ["postTitle" : currentPost.title, "postName" : currentPost.name, "content" : currentPost.content, "lastUpdated" : """
<br />
<p style="font-size:smaller; text-align:right;">(Updated ${outputFormatter.format(currentPost.lastUpdated)})</p>""", "config" : siteConfig]
new File("${opt.d}/${name}.html").write("${fozziwig.createTemplate(page).make(scrooge)}")
}
}
}
new File("${begin}").eachFile { file ->
if(file.name.endsWith('.html')) {
def name = file.name[0 .. file.name.lastIndexOf('.')-1]
List postText = file.readLines()
def currentPost = new Post(title:postText[0], name:name, dateCreated:formatter.parse(postText[1].toString()), lastUpdated:formatter.parse(postText[2].toString()), summary:postText[4])
List tagList = postText[3].split(", ") as List
tagList.each { currentPost.tags << new Tag(name:"$it") }
postText = postText[5..-1]
currentPost.content = postText.join("\n")
posts << currentPost
def postTags = null
if(!currentPost.tags.isEmpty()){
postTags = "; "
postTags += currentPost.tags.sort{it.name}.collect{"<a href=\"/tags/${it.name}.html\">${it.name}</a>"}.join(", ")
}
currentPost.tags.each { postTag ->
def currentTag
if(!tags.find{it.name.contains("$postTag")}){
currentTag = new Tag(name:postTag)
currentTag.posts << currentPost
tags << currentTag
} else {
currentTag = tags.find{it.name.contains("$postTag")}
currentTag.posts << currentPost
}
}
if(currentPost.lastUpdated > lastPublished || !sourceExists){
def scrooge = ["postTitle" : currentPost.title, "postName" : currentPost.name, "postDate" : outputFormatter.format(currentPost.dateCreated), "postTags" : postTags ?: "", "content" : currentPost.content, "config" : siteConfig]
new File("${opt.d}/archives/${currentPost.name}.html").write("${fozziwig.createTemplate(post).make(scrooge)}")
}
}
}
new File("${begin}").eachFile { file ->
if(file.name.endsWith('.md')) {
def name = file.name[0 .. file.name.lastIndexOf('.')-1]
List postText = file.readLines()
def currentPost = new Post(title:postText[0], name:name, dateCreated:formatter.parse(postText[1].toString()), lastUpdated:formatter.parse(postText[2].toString()), summary:postText[4])
List tagList = postText[3].split(", ") as List
tagList.each { currentPost.tags << new Tag(name:"$it") }
postText = postText[5..-1]
currentPost.content = mdProcessor.markdown(postText.join("\n"))
posts << currentPost
def postTags = null
if(!currentPost.tags.isEmpty()){
postTags = "; "
postTags += currentPost.tags.sort{it.name}.collect{"<a href=\"/tags/${it.name}.html\">${it.name}</a>"}.join(", ")
}
currentPost.tags.each { postTag ->
def currentTag
if(!tags.find{it.name.contains("$postTag")}){
currentTag = new Tag(name:postTag)
currentTag.posts << currentPost
tags << currentTag
} else {
currentTag = tags.find{it.name.contains("$postTag")}
currentTag.posts << currentPost
}
}
if(currentPost.lastUpdated > lastPublished || !sourceExists){
def scrooge = ["postTitle" : currentPost.title, "postName" : currentPost.name, "postDate" : outputFormatter.format(currentPost.dateCreated), "postTags" : postTags ?: "", "content" : currentPost.content, "config" : siteConfig]
new File("${opt.d}/archives/${currentPost.name}.html").write("${fozziwig.createTemplate(post).make(scrooge)}")
}
}
}
def gonzo = new AntBuilder()
new File("${opt.d}/css/").mkdirs()
gonzo.copy(todir: "${opt.d}/css/") {
fileset(dir : "${opt.s}/css/")
}
new File("${opt.d}/images/").mkdirs()
gonzo.copy(todir: "${opt.d}/images/") {
fileset(dir : "${opt.s}/images/")
}
new File("${opt.d}/files/").mkdirs()
gonzo.copy(todir: "${opt.d}/files/") {
fileset(dir : "${opt.s}/files/")
}
new File("${opt.d}/tags/").mkdirs()
tags.each{ tag ->
tag.posts = tag.posts.sort{ it.dateCreated }.reverse()
String tagMid = """
<p></p>
<table>
"""
tag.posts.each { currentPost ->
tagMid += """
<tr>
<td valign="top" class="date"><span class="arc_date">${archiveFormatter.format(currentPost.dateCreated)}</span></td>
<td valign="top"><a href="/archives/${currentPost.name}.html">${currentPost.title}</a></td>
</tr>
"""
}
tagMid += """
</table>
"""
def dickens = ["postTitle" : "Archives for “${tag.name}”", "postName" : tag.name, "postUpdate" : "", "content" : tagMid, "config": siteConfig]
new File("${opt.d}/tags/${tag.name}.html").write("${fozziwig.createTemplate(page).make(dickens)}")
def max = tag.posts.size() > 20 ? 19 : tag.posts.size() - 1
def tagFeed = new File("${opt.d}/tags/${tag.name}.xml")
String entries = ""
tag.posts[0..max].each { cp ->
def itemIdDate = itemIdDateFormatter.format(cp.dateCreated)
def cratchit = ["postTitle" : cp.title, "postLink" : "http://${siteConfig.site.domain}${siteConfig.site.base}/archives/${cp.name}.html", "postSummary" : cp.summary, "postContent" : cp.content, "itemId" : "tag:${siteConfig.site.domain},${itemIdDate}:/${cp.name}/", "itemDate" : feedFormatter.format(cp.dateCreated), "itemUpdatedDate" : feedFormatter.format(cp.lastUpdated)]
entries += "${fozziwig.createTemplate(entry).make(cratchit)}"
}
def tim = ["siteDomain" : siteConfig.site.domain, "feedUrl" : "http://${siteConfig.site.domain}${siteConfig.site.base}/tags/${tag.name}.xml", "tagName" : tag.name, "feedTitle" : "${siteConfig.site.name} : ${tag.name}", "siteName" : siteConfig.site.name, "lastUpdated" : feedFormatter.format(new Date()), "authorName" : siteConfig.author.name, "authorEmail" : siteConfig.author.email, "entries" : entries]
tagFeed.write("${fozziwig.createTemplate(feed).make(tim)}")
}
posts = posts.sort{ it.dateCreated }.reverse()
File rootIndex = new File("${opt.d}/index.html")
String homeContent = ""
def max = posts.size() > 5 ? 4 : posts.size() - 1
posts[0..max].each { currentPost ->
def postTags = null
if(!currentPost.tags.isEmpty()){
postTags = "; "
postTags += currentPost.tags.sort{it.name}.collect{"<a href=\"/${it.name}/\">${it.name}</a>"}.join(", ")
}
def pd = ["postTitle" : currentPost.title, "postLink" : "http://${siteConfig.site.domain}${siteConfig.site.base}/archives/${currentPost.name}.html", "postDate" : outputFormatter.format(currentPost.dateCreated), "postTags" : postTags ?: "", "content" : currentPost.content]
homeContent += fozziwig.createTemplate(home_mid).make(pd)
}
def fred = ["content" : homeContent, "config" :siteConfig]
rootIndex.write("${fozziwig.createTemplate(home).make(fred)}")
String tagList = "<p>"
tagList += tags.sort{it.name}.collect{"<a href=\"/${it.name}/\">${it.name}</a> (${it.posts.size()})"}.join(" ")
tagList += "</p>"
def thingsNSuch = ["postTitle" : "Tags", "postName" : "tags", "config" : siteConfig, "postUpdate" : "", "content" : tagList]
new File("${opt.d}/tags.html").write("${fozziwig.createTemplate(page).make(thingsNSuch)}")
new File("${opt.d}/archives/").mkdirs()
File arcIndex = new File("${opt.d}/archives.html")
String archiveContent = "<table>"
posts.each { currentPost ->
archiveContent += """
<tr>
<td valign="top" class="date"><span class="arc_date">${archiveFormatter.format(currentPost.dateCreated)}</span></td>
<td valign="top"><a href="/archives/${currentPost.name}.html">${currentPost.title}</a></td>
</tr>
"""
}
archiveContent += """
<p></p>
<table>
"""
def archive = ["postTitle" : "Archives", "postName" : "archives", "postUpdate" : "", "content" : archiveContent, "config" : siteConfig]
arcIndex.write("${fozziwig.createTemplate(page).make(archive)}")
def siteFeed = new File("${opt.d}/feed.xml")
max = posts.size() > 20 ? 19 : posts.size() - 1
String feedEntries = ""
posts[0..max].each { currentPost ->
def itemIdDate = itemIdDateFormatter.format(currentPost.dateCreated)
def cratchit = ["postTitle" : currentPost.title, "postSummary" : currentPost.summary, "postContent" : currentPost.content, "itemId" : "tag:${siteConfig.site.domain},${itemIdDate}:/archives/${currentPost.name}.html", "postLink" : "http://${siteConfig.site.domain}${siteConfig.site.base}/archives/${currentPost.name}.html", "itemDate" : feedFormatter.format(currentPost.dateCreated), "itemUpdatedDate" : feedFormatter.format(currentPost.lastUpdated)]
feedEntries += "${fozziwig.createTemplate(entry).make(cratchit)}"
}
def feedBits = ["feedUrl" : "http://${siteConfig.site.domain}${siteConfig.site.base}/feed.xml", "feedTitle" : siteConfig.site.name, "lastUpdated" : feedFormatter.format(new Date()), "authorName" : siteConfig.author.name, "authorEmail" : siteConfig.author.email, "entries" : feedEntries, "siteDomain" : siteConfig.site.domain]
siteFeed.write("${fozziwig.createTemplate(feed).make(feedBits)}")
new File("${opt.s}/meta.groovy").delete()
new File("${opt.s}/meta.groovy").write("published = \"${formatter.format(new Date())}\"")
}