This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
forked from tagbangers/wallride-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
317 lines (269 loc) · 10.3 KB
/
build.gradle
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright 2013, The Thymeleaf Project (http://www.thymeleaf.org/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import nz.net.ultraq.lesscss.LessCSSCompiler
import org.apache.tools.ant.filters.*
import org.gradle.api.plugins.jetty.internal.Monitor
import java.text.SimpleDateFormat
/**
* Gradle build script for the Thymeleaf Docs project, converts the
* documentation files, in Markdown format, into HTML and PDF formats.
*
* @author Emanuel Rabina
*/
/*
* NOTE: More information about the MarkDown format and extensions that can be used:
* http://johnmacfarlane.net/pandoc/demo/example9/pandocs-markdown.html
*/
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jetty'
project.webAppDirName = "${buildDir}/jetty"
// These two variables should be updated when creating a new version of the docs
project.version = '3.0.0.RELEASE'
def documentDate = new Date().clearTime()
documentDate.set(
year: 2016,
month: Calendar.MAY,
date: 8)
def conversions = [
articles: ['html'],
tutorials: ['html', 'ebook', 'pdf']
]
def docsDir = file("${projectDir}/docs")
def imagesDir = file("${projectDir}/images")
def scriptsDir = file("${projectDir}/scripts")
def stylesDir = file("${projectDir}/styles")
def templatesDir = file("${projectDir}/templates")
def srcDir = file("${buildDir}/tmp")
def outputDir = file("${buildDir}/site")
def docNames = [] as Set
def docTypes = [:]
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'nz.net.ultraq.lesscss:lesscss-compiler:1.0.2'
}
}
/**
* Copy all docs and resources to the source dir, from where they will be fed to
* pandoc. Also, during this copy operation markdown files will be filtered so
* that Gradle properties are resolved in their metadata sections.
*
* Note that, for doc files, the name of the folder they are immediately in will
* be considered their "document type", and this will have an influence on the
* type of conversions that will be applied to them.
*/
task copyResources << {
def documentVersion = new SimpleDateFormat('yyyyMMdd - dd MMMM yyyy', Locale.ENGLISH).format(documentDate)
// Copy docs files to the src folder. For markdown files, apply property filtering and build the name-type map
copy {
from docsDir
into srcDir
filesMatching('**/*.md') {
// We need a file path relative to the 'docs' folder
def docFileName = (it.relativePath.toString() - ".md").replace('\\', '/')
def docFileType = it.file.parentFile.name
// The parent folder in some cases might be a version number (e.g. tutorials for 2.1, 3.0...) In
// such case, we need to obtain the document type from the parent of the parent. Note three types
// of version specifications are allowed: '2.1', '2.1.4', '2.1.x'
if (docFileType ==~ /\d+\.\d+(\.\d+)?/ || docFileType ==~ /\d+\.\d+(\.x)?/) {
docFileType = it.file.parentFile.parentFile.name
}
docNames.add(docFileName)
docTypes.put(docFileName, docFileType)
// Property filtering, this will replace all @gradle-properties@ (mainly used in markdown metadata)
filter(ReplaceTokens, tokens: [
documentVersion: documentVersion,
projectVersion : project.version
])
}
}
// Copy all resource directories straight over
copy {
from scriptsDir
into "${srcDir.path}/scripts"
}
copy {
from imagesDir
into "${srcDir.path}/images"
}
// Copy over stylesheets, compiling LessCSS files into CSS
copy {
from(stylesDir) {
exclude '**/*.less'
}
into "${srcDir.path}/styles"
}
LessCSSCompiler compiler = new LessCSSCompiler()
fileTree(stylesDir) { include '**/*.less' }.each { lessFile ->
def lessFileBase = lessFile.name - '.less'
compiler.compile(lessFile, file("${srcDir.path}/styles/${lessFileBase}.css"))
}
// Generate output directory structure
mkdir outputDir
mkdir "${outputDir.path}/doc"
}
/**
* Generate HTML versions of the Thymeleaf documentation.
*/
task generateDocsHTML(
dependsOn: copyResources,
description: "Generate HTML docs from Thymeleaf's markdown docs") << {
docNames.each { docName ->
def docType = docTypes.get(docName)
if (conversions[docType].contains('html')) {
println "Generating HTML doc for ${docName} (${docType})..."
def outputFile = file("${outputDir.path}/doc/${docName}.html")
file(outputFile.parent).mkdirs()
execute([
"pandoc",
"--write=html5",
"--template=" + file("${templatesDir.path}/${docType}.html"),
"--toc",
"--toc-depth=4",
"--section-divs",
"--no-highlight",
"--smart",
"--output=" + outputFile,
"${srcDir.path}/${docName}.md"
])
}
}
// Copy over resources needed for the HTML docs
copy {
from srcDir
into "${outputDir.path}/doc"
exclude '**/*.md'
}
}
/**
* Generate e-books of the Thymeleaf documentation.
*/
task generateDocsEbook(
dependsOn: copyResources,
description: "Generate e-books from Thymeleaf's markdown docs") << {
// Currently only limited to tutorials
docNames.each { docName ->
def docType = docTypes.get(docName)
if (conversions[docType].contains('ebook')) {
println "Generating E-Book docs for ${docName} (${docType})..."
def epubOutputFile = file("${outputDir.path}/doc/${docName}.epub")
def mobiOutputFile = file("${outputDir.path}/doc/${docName}.mobi")
file(epubOutputFile.parent).mkdirs()
def inputFile = file("${srcDir.path}/${docName}.md")
execute([
"pandoc",
"--write=epub",
"--template=" + file("${templatesDir.path}/${docType}.epub"),
"--toc",
"--toc-depth=4",
"--section-divs",
"--smart",
"--output=" + epubOutputFile,
inputFile
], file(inputFile.parent)) // We need to set the working dir in order for pandoc to find images
execute([
"ebook-convert",
epubOutputFile,
mobiOutputFile
])
}
}
}
/**
* Jetty configuration, including a fix for Jetty not stopping, obtained from
* the bug report here: http://issues.gradle.org/browse/GRADLE-2263
*/
[jettyRun, jettyStop]*.stopPort = 8081
[jettyRun, jettyStop]*.stopKey = 'stop'
jettyRun.doLast {
/*
* THIS IS A WORKAROUND! THE CURRENT VERSION OF THIS TASK DOESN'T START A WATCHER IN DAEMON MODE
*
* If starting the monitor fails, it may be because the jetty task was updated to fix this issue
* When that happens, we shouldn't need the custom task any more
*
* Copied From: AbstractJettyRunTask
*/
if (getStopPort() != null && getStopPort() > 0 && getStopKey() != null) {
Monitor monitor = new Monitor(getStopPort(), getStopKey(), server.getProxiedObject());
monitor.start();
}
}
/**
* Generate PDF versions of the Thymeleaf documentation. Uses a Jetty server to
* host the HTML documents, and wkhtmltopdf which uses the server to read files
* via the http:// protocol (otherwise the same-origin restriction kicks in, as
* do some file:// bugs on Windows) and saves them as PDF documents.
*/
task generateDocsPDF(
dependsOn: generateDocsHTML,
description: "Generate PDF documents from Thymeleaf's HTML docs") << {
// Copy over the HTML documents into a directory from which we can host them
// using the Jetty server
copy {
from "${outputDir.path}/doc"
into "${project.webAppDirName}"
}
jettyRun.daemon = true
jettyRun.execute()
// Generate the PDF documents from the modified HTML documents
fileTree(webAppDirName) { include '*/**/*.html' }.each { docFile ->
def folderBase = relativePath(file(webAppDirName))
def docName = (relativePath(docFile).substring(folderBase.length() + 1) - ".html")
def docType = docTypes.get(docName)
if (conversions[docType].contains('pdf')) {
println "Generating PDF doc for ${docName} (${docType})..."
execute([
"wkhtmltopdf",
"--print-media-type",
"--dpi", "300",
"--javascript-delay", "1000",
"--margin-bottom", "15",
"--footer-spacing", "5",
"--footer-font-size", "8",
"--footer-font-name", "'Ubuntu'",
"--footer-right", "Page [page] of [topage]",
"--quiet",
"http://localhost:8080/thymeleaf-docs/${docName}.html",
file("${outputDir.path}/doc/${docName}.pdf")
])
}
}
jettyStop.execute()
}
/**
* Generate HTML, E-book and PDF versions of the Thymeleaf documentation.
*/
task generateDocs(
dependsOn: [generateDocsHTML, generateDocsEbook, generateDocsPDF],
description: "Generate HTML, E-book and PDF documents from Thymeleaf's markdown docs") {
// Just an aggregator task
}
/**
* Execute the given command and wait for the process to complete.
*
* @param command
*/
def execute(command, workingDir = projectDir) {
def proc = command.execute(null, workingDir)
proc.waitForProcessOutput(System.out, System.err)
if (proc.exitValue() > 0) {
println "${proc.err.text}"
}
}