public
Description: The web site for the Nu programming language
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/programmingnu.git
programmingnu / builder.nu
100644 175 lines (144 sloc) 7.021 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
;; site builder
 
(set $siteName "programming.nu")
(set $siteTitle "Programming Nu")
(set $siteSubtitle "Website for the Nu programming language.")
(set $siteDescription "The Nu Language Website")
(set $siteAddress "http://programming.nu")
 
(load "YAML")
(load "template")
(load "NuMarkdown")
 
(class NSObject (+ objectWithYAML:(id) yaml is (self fromYAML:yaml)))
 
(class NSString
     
     (- (id) markdownToHTML is (NuMarkdown convert:self)))
     
(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
     
     ;; Get a nice representation of a date for display.
     (- (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
     (try
         (set __name (eval (car margs)))
         (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
     (try
         (set __name (eval (car margs)))
         (eval (NuTemplate codeForString:(NSString stringWithContentsOfFile:"views/#{__name}.nuhtml")))
         (catch (__exception)
                (puts "error in template #{__name}.nuhtml (#{(__exception name)} #{(__exception reason)})")
                "")))
 
(macro render-xml
     (try
         (set __name (eval (car margs)))
         (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 setObject:(NSDate dateWithNaturalLanguageString:(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 setObject:(NSDate dateWithNaturalLanguageString:(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 site")
(system "mkdir -p site/public")
(system "cp nunja.nu site/site.nu")
(system "cp -rp assets/* site/public")
 
;; build the index page
(set index-page (render-page "index"))
(index-page writeToFile:"site/public/index" atomically:NO)
(index-page writeToFile:"site/public/index.html" atomically:NO)
 
;; render pages
(pages each:
       (do (post)
           (puts (+ "rendering " (post "permalink")))
           ((render-page "show") writeToFile:(+ "site/public/" (post "permalink")) atomically:NO)))
 
;; render posts
(posts each:
       (do (post)
           (puts (+ "rendering " (post "permalink")))
           (set path (+ "site/public/posts/" ((post "creationDate") path)))
           (system (+ "mkdir -p " path))
           ((render-page "show") writeToFile:(+ path "/" (post "permalink")) atomically:NO)))
 
;; render feeds
(set feedposts (posts subarrayWithRange:'(0 7)))
 
(let (path "site/public/xml/rss20")
     (system (+ "mkdir -p " path))
     ((render-xml "rss") writeToFile:(+ path "/feed.xml") atomically:NO))
(let (path "site/public/xml/atom10")
     (system (+ "mkdir -p " path))
     ((render-xml "atom") writeToFile:(+ path "/feed.xml") atomically:NO))