forked from grails/grails-doc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Generate.groovy
238 lines (196 loc) · 7.56 KB
/
Generate.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
import org.grails.doc.DocEngine
import org.grails.doc.PdfBuilder
import org.radeox.api.engine.*
import org.radeox.engine.context.BaseInitialRenderContext
def ant = new AntBuilder()
BASEDIR = System.getProperty("base.dir") ?: '.'
GRAILS_HOME = System.getProperty("grails.home")
CONTEXT_PATH = DocEngine.CONTEXT_PATH
SOURCE_FILE = DocEngine.SOURCE_FILE
props = new Properties()
new File("${BASEDIR}/resources/doc.properties").withInputStream {input ->
props.load(input)
}
new File("${GRAILS_HOME}/build.properties").withInputStream {input ->
props.load(input)
}
title = props.title
version = props."grails.version"
authors = props.author
def compare = [equals: { false },
compare: {o1, o2 ->
def idx1 = o1.name[0..o1.name.indexOf(' ') - 1]
def idx2 = o2.name[0..o2.name.indexOf(' ') - 1]
def nums1 = idx1.split(/\./).findAll { it.trim() != ''}*.toInteger()
def nums2 = idx2.split(/\./).findAll { it.trim() != ''}*.toInteger()
// pad out with zeros to ensure accurate comparison
while (nums1.size() < nums2.size()) {
nums1 << 0
}
while (nums2.size() < nums1.size()) {
nums2 << 0
}
def result = 0
for (i in 0..<nums1.size()) {
result = nums1[i].compareTo(nums2[i])
if (result != 0) break
}
result
}] as Comparator
files = new File("${BASEDIR}/src/guide").listFiles().findAll { it.name.endsWith(".gdoc") }.sort(compare)
context = new BaseInitialRenderContext()
context.set(CONTEXT_PATH, "..")
context.setParameters([:]) // required by some macros
ant = new AntBuilder()
cache = [:]
engine = new DocEngine(context)
templateEngine = new groovy.text.SimpleTemplateEngine()
context.setRenderEngine(engine)
/* Guide documentation */
book = [:]
for (f in files) {
def chapter = f.name[0..-6]
book[chapter] = f
}
chaptersOnlyToc = new StringBuffer()
fullToc = new StringBuffer()
toc = new StringBuffer()
soloToc = new StringBuffer()
fullContents = new StringBuffer()
chapterContents = new StringBuffer()
chapterTitle = null
chapterHeader = null
chapterToc = new StringBuffer()
void writeChapter() {
new File("${BASEDIR}/output/guide/${chapterTitle}.html").withWriter {
template.make(title:chapterTitle,
header:chapterHeader,
toc:chapterToc.toString(),
content:chapterContents.toString(),
path:"..").writeTo(it)
}
chapterToc.delete(0,chapterToc.size()) // clear buffer
chapterContents.delete(0,chapterContents.size()) // clear buffer
}
ant.mkdir(dir: "${BASEDIR}/output/guide")
ant.mkdir(dir: "${BASEDIR}/output/guide/pages")
new File("${BASEDIR}/resources/style/guideItem.html").withReader("UTF-8") {reader ->
template = templateEngine.createTemplate(reader)
for (entry in book) {
//println "Generating documentation for $entry.key"
def title = entry.key
def level = 0
def matcher = (title =~ /^(\S+?)\.? /) // drops last '.' of "xx.yy. "
if (matcher.find()) {
level = matcher.group(1).split(/\./).size() - 1
}
def margin = level * 10
// level 0=h1, (1..n)=h2
def hLevel = level==0 ? 1 : 2
def header = "<h$hLevel><a name=\"${title}\">${title}</a></h$hLevel>"
// links to anchor, not page
def tocEntry = "<div class=\"tocItem\" style=\"margin-left:${margin}px\"><a href=\"#${title}\">${title}</a></div>"
if (level == 0) {
if (chapterTitle) // initially null, to collect sections
writeChapter()
chapterTitle = title // after previous used to write prev chapter
chapterHeader = header
// links to page, not anchor
chaptersOnlyToc << "<div class=\"tocItem\" style=\"margin-left:${margin}px\"><a href=\"${chapterTitle}.html\">${chapterTitle}</a></div>"
}
else {
chapterToc << tocEntry
chapterContents << header
} // level 0=h1, (1..n)=h2
fullToc << tocEntry
context.set(SOURCE_FILE, entry.value)
context.set(CONTEXT_PATH, "..")
def body = engine.render(entry.value.text, context)
fullContents << header << body
chapterContents << body
new File("${BASEDIR}/output/guide/pages/${title}.html").withWriter("UTF-8") {
template.make(title:title, header:header,
toc:"", content:body, path:"../..").writeTo(it)
}
}
}
if (chapterTitle) // write final chapter collected (if any seen)
writeChapter()
/* Resources */
ant.mkdir(dir: "${BASEDIR}/output")
ant.mkdir(dir: "${BASEDIR}/output/img")
ant.mkdir(dir: "${BASEDIR}/output/css")
ant.mkdir(dir: "${BASEDIR}/output/ref")
ant.copy(file: "${BASEDIR}/resources/style/index.html", todir: "${BASEDIR}/output")
ant.copy(todir: "${BASEDIR}/output/img") {
fileset(dir: "${BASEDIR}/resources/img")
}
ant.copy(todir: "${BASEDIR}/output/css") {
fileset(dir: "${BASEDIR}/resources/css")
}
ant.copy(todir: "${BASEDIR}/output/ref") {
fileset(dir: "${BASEDIR}/resources/style/ref")
}
/* Reference documentation */
vars = [
title: props.title,
subtitle: props.subtitle,
footer: props.footer,
authors: props.authors,
version: props."grails.version",
copyright: props.copyright,
toc: fullToc.toString(),
body: fullContents.toString()
]
new File("${BASEDIR}/resources/style/layout.html").withReader("UTF-8") {reader ->
template = templateEngine.createTemplate(reader)
new File("${BASEDIR}/output/guide/single.html").withWriter("UTF-8") {out ->
template.make(vars).writeTo(out)
}
vars.toc = chaptersOnlyToc
vars.body = ""
new File("${BASEDIR}/output/guide/index.html").withWriter("UTF-8") {out ->
template.make(vars).writeTo(out)
}
}
menu = new StringBuffer()
void writeReferenceItem(File file, String path, String section, String name) {
context.set(SOURCE_FILE, file)
context.set(CONTEXT_PATH, path)
def divClass = (name == "Usage") ? "menuUsageItem" : "menuItem"
menu << "<div class='${divClass}'><a href=\"${section}/${name}.html\" target=\"mainFrame\">${name}</a></div>"
def content = engine.render(file.text, context)
new File("${BASEDIR}/output/ref/${section}/${name}.html").withWriter("UTF-8") {
template.make(content:content).writeTo(it)
}
}
files = new File("${BASEDIR}/src/ref").listFiles().toList().sort()
reference = [:]
new File("${BASEDIR}/resources/style/referenceItem.html").withReader("UTF-8") {reader ->
template = templateEngine.createTemplate(reader)
for (f in files) {
if (f.directory && !f.name.startsWith(".")) {
def section = f.name
menu << "<h1 class=\"menuTitle\">${section}</h1>"
new File("${BASEDIR}/output/ref/${section}").mkdirs()
def usageFile = new File("${BASEDIR}/src/ref/${section}.gdoc")
if (usageFile.exists()) {
writeReferenceItem(usageFile, "../..", section, "Usage")
}
def items = f.listFiles().findAll{it.name.endsWith(".gdoc")}.sort()
for (item in items) {
//println "Generating reference item: ${name}"
writeReferenceItem(item, "../..", section, item.name[0..-6])
}
}
}
}
vars.menu = menu
new File("${BASEDIR}/resources/style/menu.html").withReader("UTF-8") {reader ->
template = templateEngine.createTemplate(reader)
new File("${BASEDIR}/output/ref/menu.html").withWriter("UTF-8") {out ->
template.make(vars).writeTo(out)
}
}
PdfBuilder.build(BASEDIR)
println "Done. Look at output/index.html"