blob: 0d779cbc5fbefb3981321675bb0724f6c8b983fd [file] [log] [blame]
/*
* Copyright 2003-2014 the original author or authors.
*
* 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.
*/
apply plugin: 'org.asciidoctor.gradle.asciidoctor'
asciidoctor {
def (full, major, minor, patch, flavor) = (groovyVersion =~ /(\d+)\.(\d++)\.(\d+)(?:-(.+))?/)[0]
logDocuments = true
sourceDir = project.file('src/spec/doc')
options = [
attributes: [
'rootProjectDir': rootProject.projectDir,
'source-highlighter': 'prettify',
groovyversion: groovyVersion,
'groovy-major-version': major,
'groovy-minor-version': minor,
'groovy-patch-version': patch,
'groovy-full-version': groovyVersion,
'groovy-short-version': "${major}.${minor}",
doctype: 'book',
revnumber: groovyVersion,
icons: 'font',
toc2: '',
specfolder: 'src/spec/doc',
linkcss: '',
stylesheet: "assets/css/style.css",
encoding: 'utf-8',
toclevels: 10,
numbered: ''
]
]
}
// skip the asciidoctor task if there's no directory with asciidoc files
asciidoctor.onlyIf { project.file('src/spec/doc').exists() }
task asciidoctorAssets(type:Copy) {
from project.fileTree('src/spec/assets')
into "${asciidoctor.outputDir}/assets"
into "${rootProject.asciidoctor.outputDir}/assets"
}
asciidoctor.finalizedBy asciidoctorAssets
def adocSanityCheck = { file, text, errors ->
Set localErrors = []
text.eachLine(1) { line,i ->
if (line =~ /tag:[a-zA-Z0-9]/) {
localErrors << "line $i misses semicolon. Should be tag::\n $line"
}
if (line =~ /end:[a-zA-Z0-9]/) {
localErrors << "line $i misses semicolon. Should be end::\n $line"
}
if (line =~ /(tag|end)::[^\[\]]$/) {
localErrors << "line $i contains incorrect tag definition (misses []):\n $line"
}
}
localErrors.collect(errors) { " $file, $it" }
}
def htmlOutputSanityCheck = { file, text, errors ->
Set localErrors = []
text.eachLine(1) { line,i ->
if (line =~ /^={1,5} /) {
localErrors << "line $i starting with asciidoctor raw markup:\n$line"
}
if (line =~ /<code class=".+?"><\/code>/) {
localErrors << "contains empty code block, probably incorrect import of a tag."
}
}
localErrors.collect(errors) { " $file, $it" }
}
asciidoctor {
def errors = new LinkedHashSet<String>()
doFirst {
def specTestDir = file('src/spec/test')
if (specTestDir.exists()) {
specTestDir.eachFileRecurse { file ->
if (file.isFile()) {
adocSanityCheck(file, file.getText('utf-8'), errors)
}
}
}
if (errors) {
throw new GradleException("Incorrect Asciidoctor input:\n${errors.join('\n')}")
}
}
doLast {
def scripts = '''<link rel="stylesheet" href="assets/css/view-example.css">
<script src='assets/js/jquery-2.1.1.min.js'></script>
<script src='assets/js/view-example.js'></script>'''
def baseUrls = [
jdk: "http://docs.oracle.com/javase/8/docs/api/index.html",
gjdk: "http://docs.groovy-lang.org/${version}/html/groovy-jdk/index.html",
gapi: "http://docs.groovy-lang.org/${version}/html/gapi/index.html",
]
// gapi macro expansion
outputDir.eachFileMatch(~'.*html') { File file ->
def text = file.getText('UTF-8')
text = text.replaceAll(/(gapi|gjdk|jdk)::([a-zA-Z0-9$.#]+)(?:\[([^\]]*)\])?/) { m ->
def (className, anchor) = m[2].split('#') as List
def label = m[3] ?: '<code>' + className + '</code>'
"<a href='${calculateDocUrl(baseUrls[m[1]], className, anchor)}' target='_blank'>$label</a>"
}
text = text.replaceAll('</head>', "$scripts</head>")
htmlOutputSanityCheck(file, text, errors)
file.write(text, 'UTF-8')
}
if (errors) {
throw new GradleException("Incorrect Asciidoctor output:\n${errors.join('\n')}")
}
}
}
String calculateDocUrl(String baseUrl, String className, String anchor) {
if (className == "index") return baseUrl
return baseUrl + "?" + className.replace('.', '/') + '.html' + (anchor ? '#' + anchor : '')
}