File: asciidoctor.gradle

package info (click to toggle)
groovy 2.4.21-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 30,708 kB
  • sloc: java: 157,114; xml: 728; sh: 357; makefile: 64; javascript: 15
file content (152 lines) | stat: -rw-r--r-- 5,524 bytes parent folder | download | duplicates (3)
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
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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')

    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: '',
            sectanchors: ''
    ])

    extensions {

        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",
        ]

        baseUrls.each { macroName, baseURL ->
            inlinemacro(name: macroName) {
                parent, target, attributes ->
                    def (className, anchor) = target.split('#') as List
                    options = [
                            "type"  : ":link",
                            "target": calculateDocUrl(baseURL, className, anchor)
                    ]

                    createInline(parent, "anchor", attributes.text?:target, attributes, options).render()
            }
        }
    }
}

// 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}/html5/assets"
    into "${rootProject.asciidoctor.outputDir}/html5/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."
        }
        if (line =~ /(gapi|jdk|gjdk):(.+?)/) {
            localErrors << "line $i starting with asciidoctor raw markup:\n$line"
        }
    }
    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-min-2.1.1.js'></script>
<script src='assets/js/view-example.js'></script>'''

        // gapi macro expansion
        outputDir.eachFileMatch(~'.*html') { File file ->
            def text = file.getText('UTF-8')
            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 : '')
}