-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbuilder.nu
More file actions
182 lines (151 loc) · 7.63 KB
/
builder.nu
File metadata and controls
182 lines (151 loc) · 7.63 KB
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
;; site builder
(set $siteName "programming-nu")
(set $siteTitle "Programming Nu")
(set $siteSubtitle "<a href=\"https://github.com/programming-nu\">github.com/programming-nu</a>")
(set $siteDescription "The Nu Language Website")
(set $siteAddress "http://programming-nu.github.io")
(load "RadYAML")
(load "template")
(class NSObject (+ objectWithYAML:(id) yaml is (yaml YAMLValue)))
(class NSString
(- (id) markdownToHTML is
(set x (NSString stringWithShellCommand:"markdown" standardInput:(self dataUsingEncoding:NSUTF8StringEncoding)))
x))
(class NSDictionary
(- (id) bodyAsHTML is
(set body (self valueForKey:"body"))
(case (self valueForKey:"format")
("markdown" (body markdownToHTML))
(else body)))
(- (id) extendedAsHTML is
(set extended (self valueForKey:"extended"))
(case (self valueForKey:"format")
("markdown" (extended markdownToHTML))
(else extended))))
(class NSDate
(- descriptionWithCalendarFormat:format is
(self descriptionWithCalendarFormat:format timeZone:(NSTimeZone timeZoneWithName:"America/Los_Angeles") locale:nil))
;; Get a nice representation of a date for display.
(- descriptionForBlog is
(set s (self descriptionWithCalendarFormat:"%A %B %e, %Y" timeZone:(NSTimeZone timeZoneWithName:"America/Los_Angeles") locale:nil))
(s stringByReplacingOccurrencesOfString:" 0" withString:" "))
;;(- (id) descriptionForBlog is (self descriptionWithCalendarFormat:"%A, %d %b %Y"))
;; Get a y-m-d representation of a date.
(- (id) ymd is
(self descriptionWithCalendarFormat:"%Y-%m-%d"))
(- (id) monthAndYear is
(self descriptionWithCalendarFormat:"%B %Y"))
;; Get an RFC822-compliant representation of a date.
(- (id) rfc822 is
(set result ((NSMutableString alloc) init))
(result appendString:
(self descriptionWithCalendarFormat:"%a, %d %b %Y %H:%M:%S "
timeZone:(NSTimeZone localTimeZone) locale:nil))
(result appendString:((NSTimeZone localTimeZone) abbreviation))
result)
;; Get an RFC1123-compliant representation of a date.
(- (id) rfc1123 is
(set result ((NSMutableString alloc) init))
(result appendString:
(self descriptionWithCalendarFormat:"%a, %d %b %Y %H:%M:%S "
timeZone:(NSTimeZone timeZoneWithName:"GMT") locale:nil))
(result appendString:((NSTimeZone timeZoneWithName:"GMT") abbreviation))
result)
;; Get an RFC3339-compliant representation of a date.
(- (id) rfc3339 is
(set result ((NSMutableString alloc) init))
(result appendString:
(self descriptionWithCalendarFormat:"%Y-%m-%dT%H:%M:%S"
timeZone:(NSTimeZone localTimeZone) locale:nil))
(set offset (/ ((NSTimeZone localTimeZone) secondsFromGMT) 3600))
(cond ((< offset -9) (result appendString:"#{offset}:00"))
((< offset 0) (result appendString:"-0#{(* -1 offset)}:00"))
(t (result appendString:"-0#{offset}:00")))
result)
(- (id) path is
(self descriptionWithCalendarFormat:"%Y/%m/%d" timeZone:nil locale:nil)))
(macro render-partial (name)
`(try
(set __name ,name)
(set __result (eval (NuTemplate codeForString:(NSString stringWithContentsOfFile:"views/_#{__name}.nuhtml"))))
(unless __result
;; log expanded template for debugging
(puts "error in template _#{__name}.nuhtml")
(puts ((NuTemplate scriptForString:(NSString stringWithContentsOfFile:"views/_#{__name}.nuhtml")) stringValue)))
__result
(catch (__exception)
(puts "error in template _#{__name}.nuhtml (#{(__exception name)} #{(__exception reason)})")
"")))
(macro render-page (name)
`(try
(set __name ,name)
(eval (NuTemplate codeForString:(NSString stringWithContentsOfFile:"views/#{__name}.nuhtml")))
(catch (__exception)
(puts "error in template #{__name}.nuhtml (#{(__exception name)} #{(__exception reason)})")
"")))
(macro render-xml (name)
`(try
(set __name ,name)
(eval (NuTemplate codeForString:(NSString stringWithContentsOfFile:"views/#{__name}.nuxml")))
(catch (__exception)
(puts "error in template #{__name}.nuxml (#{(__exception name)} #{(__exception reason)})")
"")))
(puts "reading site description")
(set pages (array))
(set pageFiles ((NSString stringWithShellCommand:"ls pages") lines))
(puts (+ "reading " (pageFiles count) " pages"))
(pageFiles each:
(do (pageFile)
(set info (NSObject objectWithYAML:(NSString stringWithContentsOfFile:(+ "pages/" pageFile "/info.yml"))))
(set body (NSString stringWithContentsOfFile:(+ "pages/" pageFile "/body.txt")))
(set extended (NSString stringWithContentsOfFile:(+ "pages/" pageFile "/extended.txt")))
(info setObject:body forKey:"body")
(info setObject:extended forKey:"extended")
(info creationDate:((info creationDate:) stringByReplacingOccurrencesOfString:"-0800" withString:"-08:00"))
(info creationDate:((info creationDate:) stringByReplacingOccurrencesOfString:"-0700" withString:"-07:00"))
(info setObject:(NSDate dateWithString:(info "creationDate")) forKey:"creationDate")
(info setObject:YES forKey:"permanent")
(pages << info)))
(set posts (array))
(set postFiles ((NSString stringWithShellCommand:"ls posts | sort -r") lines))
(puts (+ "reading " (postFiles count) " posts"))
(postFiles each:
(do (postFile)
(set info (NSObject objectWithYAML:(NSString stringWithContentsOfFile:(+ "posts/" postFile "/info.yml"))))
(set body (NSString stringWithContentsOfFile:(+ "posts/" postFile "/body.txt")))
(set extended (NSString stringWithContentsOfFile:(+ "posts/" postFile "/extended.txt")))
(info setObject:body forKey:"body")
(info setObject:extended forKey:"extended")
(info creationDate:((info creationDate:) stringByReplacingOccurrencesOfString:"-0800" withString:"-08:00"))
(info creationDate:((info creationDate:) stringByReplacingOccurrencesOfString:"-0700" withString:"-07:00"))
(info setObject:(NSDate dateWithString:(info "creationDate")) forKey:"creationDate")
(info setObject:(+ "/posts/" ((info "creationDate") path) "/" (info "permalink")) forKey:"linkForDate")
(posts << info)))
(puts "building site")
;; create the site directory
(system "rm -rf public")
(system "mkdir -p public")
(system "cp -rp assets/* public")
;; build the index page
(set index-page (render-page "index"))
(index-page writeToFile:"public/index.html" atomically:NO)
;; render pages
(pages each:
(do (post)
(puts (+ "rendering " (post "permalink")))
((render-page "show") writeToFile:(+ "public/" (post "permalink") ".html") atomically:NO)))
;; render posts
(posts each:
(do (post)
(puts (+ "rendering " (post "permalink")))
(set path (+ "public/posts/" ((post "creationDate") path)))
(system (+ "mkdir -p " path))
((render-page "show") writeToFile:(+ path "/" (post "permalink") ".html") atomically:NO)))
;; render feeds
(set feedposts (posts subarrayWithRange:'(0 7)))
(let (path "public/xml/rss20")
(system (+ "mkdir -p " path))
((render-xml "rss") writeToFile:(+ path "/feed.xml") atomically:NO))
(let (path "public/xml/atom10")
(system (+ "mkdir -p " path))
((render-xml "atom") writeToFile:(+ path "/feed.xml") atomically:NO))