Skip to content

Commit 1135d53

Browse files
committed
Support for asciidoc templates in gdoc
1 parent 05c86c4 commit 1135d53

21 files changed

+889
-118
lines changed

grails-docs/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies {
1212
'org.yaml:snakeyaml:1.14',
1313
"org.codehaus.groovy:groovy-ant:$groovyVersion"
1414

15+
compile 'org.asciidoctor:asciidoctorj:1.5.4'
1516
compile('org.xhtmlrenderer:core-renderer:R8') {
1617
exclude group: 'bouncycastle', module:'bcprov-jdk14'
1718
}

grails-docs/src/main/groovy/grails/doc/DocPublisher.groovy

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515

1616
package grails.doc
1717

18+
import grails.doc.asciidoc.AsciiDocEngine
1819
import grails.doc.internal.*
1920
import groovy.io.FileType
2021
import groovy.text.Template
2122

2223
import org.apache.commons.logging.LogFactory
24+
import org.radeox.api.engine.WikiRenderEngine
2325
import org.radeox.engine.context.BaseInitialRenderContext
26+
import org.radeox.engine.context.BaseRenderContext
2427
import org.yaml.snakeyaml.Yaml
2528

29+
import java.util.regex.Pattern
30+
2631
/**
2732
* Coordinated the DocEngine the produce documentation based on the gdoc format.
2833
*
@@ -46,6 +51,8 @@ class DocPublisher {
4651
File images
4752
/** The directory containing any CSS to use (will override defaults) **/
4853
File css
54+
/** The directory containing any fonts to use (will override defaults) **/
55+
File fonts
4956
/** The directory containing any Javascript to use (will override defaults) **/
5057
File js
5158
/** The directory cotnaining any templates to use (will override defaults) **/
@@ -80,13 +87,19 @@ class DocPublisher {
8087
String logo
8188
/** HTML markup that renders the right logo */
8289
String sponsorLogo
90+
/**
91+
* The source repository
92+
*/
93+
String sourceRepo
8394

8495
/** Properties used to configure the DocEngine */
8596
Properties engineProperties
8697

98+
boolean asciidoc = false
99+
87100
def output
88-
private context
89-
private engine
101+
private BaseRenderContext context
102+
private WikiRenderEngine engine
90103
private customMacros = []
91104

92105
DocPublisher() {
@@ -151,7 +164,9 @@ class DocPublisher {
151164
ant.mkdir(dir: "$refDocsDir/ref")
152165

153166
String imgsDir = new File(refDocsDir, calculatePathToResources("img")).path
167+
File fontsDir = new File(refDocsDir, calculatePathToResources("fonts"))
154168
ant.mkdir(dir: imgsDir)
169+
ant.mkdir(dir: fontsDir )
155170
String cssDir = new File(refDocsDir, calculatePathToResources("css")).path
156171
ant.mkdir(dir: cssDir)
157172
String jsDir = new File(refDocsDir, calculatePathToResources("js")).path
@@ -167,6 +182,7 @@ class DocPublisher {
167182
fileset(dir: images)
168183
}
169184
}
185+
170186
ant.copy(todir: cssDir, overwrite: true) {
171187
fileset(dir: "${docResources}/css")
172188
}
@@ -175,6 +191,11 @@ class DocPublisher {
175191
fileset(dir: css)
176192
}
177193
}
194+
if (fonts && fonts.exists()) {
195+
ant.copy(todir: fontsDir, overwrite: true, failonerror:false) {
196+
fileset(dir: fonts)
197+
}
198+
}
178199
ant.copy(todir: jsDir, overwrite: true) {
179200
fileset(dir: "${docResources}/js")
180201
}
@@ -201,12 +222,15 @@ class DocPublisher {
201222
def guideSrcDir = new File(src, "guide")
202223
def yamlTocFile = new File(guideSrcDir, TOC_FILENAME)
203224
def guide
225+
def ext = asciidoc ? ".adoc" : ".gdoc"
204226
if (yamlTocFile.exists()) {
205-
guide = new YamlTocStrategy(new FileResourceChecker(guideSrcDir)).generateToc(yamlTocFile)
227+
def tocStrategy = new YamlTocStrategy(new FileResourceChecker(guideSrcDir), ext)
228+
guide = tocStrategy.generateToc(yamlTocFile)
206229

207230
// A set of all gdoc files.
208231
def files = []
209-
guideSrcDir.traverse(type: FileType.FILES, nameFilter: ~/^.+\.gdoc$/) {
232+
def pattern = asciidoc ? ~/^.+\.adoc$/ : ~/^.+\.gdoc$/
233+
guideSrcDir.traverse(type: FileType.FILES, nameFilter: pattern) {
210234
// We need relative file paths with '/' separators, since those
211235
// are what are stored in the UserGuideNodes.
212236
files << (it.absolutePath - guideSrcDir.absolutePath)[1..-1].
@@ -222,7 +246,8 @@ class DocPublisher {
222246
}
223247
}
224248
else {
225-
def files = guideSrcDir.listFiles()?.findAll { it.name.endsWith(".gdoc") } ?: []
249+
250+
def files = guideSrcDir.listFiles()?.findAll { it.name.endsWith(ext) } ?: []
226251
guide = new LegacyTocStrategy().generateToc(files)
227252
}
228253

@@ -247,8 +272,8 @@ class DocPublisher {
247272
def refCategories = files.collect { f ->
248273
new Expando(
249274
name: f.name,
250-
usage: new File("${src}/ref/${f.name}.gdoc"),
251-
sections: f.listFiles().findAll { it.name.endsWith(".gdoc") }.sort())
275+
usage: new File("${src}/ref/${f.name}$ext"),
276+
sections: f.listFiles().findAll { it.name.endsWith(ext) }.sort())
252277
}
253278

254279
def fullToc = new StringBuilder()
@@ -257,6 +282,7 @@ class DocPublisher {
257282
def vars = [
258283
encoding: encoding,
259284
title: title,
285+
docTitle: title,
260286
subtitle: subtitle,
261287
footer: footer, // TODO - add a way to specify footer
262288
authors: authors,
@@ -272,7 +298,8 @@ class DocPublisher {
272298
resourcesPath: calculatePathToResources(pathToRoot),
273299
prev: null,
274300
next: null,
275-
legacyLinks: legacyLinks
301+
legacyLinks: legacyLinks,
302+
sourceRepo: sourceRepo,
276303
]
277304

278305
// Build the user guide sections first.
@@ -311,15 +338,16 @@ class DocPublisher {
311338
vars.section = section
312339

313340
new File("${refDocsDir}/ref/${section}").mkdirs()
314-
def textiles = f.listFiles().findAll { it.name.endsWith(".gdoc")}.sort()
315-
def usageFile = new File("${src}/ref/${section}.gdoc")
341+
def textiles = f.listFiles().findAll { it.name.endsWith(ext)}.sort()
342+
def usageFile = new File("${src}/ref/${section}${ext}")
316343
if (usageFile.exists()) {
317344
def data = usageFile.getText("UTF-8")
318345
context.set(DocEngine.SOURCE_FILE, usageFile)
319346
context.set(DocEngine.CONTEXT_PATH, pathToRoot)
320347
context.set(DocEngine.API_CONTEXT_PATH, vars.resourcesPath)
348+
output.warn "Rendering document file $usageFile.name"
321349
vars.content = engine.render(data, context)
322-
350+
vars.sourcePath = "ref/${section}/$usageFile.name"
323351
new File("${refDocsDir}/ref/${section}/Usage.html").withWriter(encoding) {out ->
324352
template.make(vars).writeTo(out)
325353
}
@@ -330,8 +358,9 @@ class DocPublisher {
330358
context.set(DocEngine.SOURCE_FILE, txt.name)
331359
context.set(DocEngine.CONTEXT_PATH, pathToRoot)
332360
context.set(DocEngine.API_CONTEXT_PATH, vars.resourcesPath)
361+
output.warn "Rendering document file $txt.name"
333362
vars.content = engine.render(data, context)
334-
363+
vars.sourcePath = "ref/${section}/$txt.name"
335364
new File("${refDocsDir}/ref/${section}/${name}.html").withWriter(encoding) {out ->
336365
template.make(vars).writeTo(out)
337366
}
@@ -404,6 +433,8 @@ class DocPublisher {
404433
varsCopy.path = path
405434
varsCopy.level = level
406435
varsCopy.sectionToc = section.children
436+
varsCopy.sourcePath = section.file
437+
output.warn "Rendering document file $sourceFile.name"
407438
varsCopy.content = engine.render(sourceFile.getText("UTF-8"), context)
408439

409440
// First create the section content, which usually consists of a header
@@ -506,7 +537,12 @@ class DocPublisher {
506537
context = new BaseInitialRenderContext()
507538
initContext(context, "..")
508539

509-
engine = new DocEngine(context)
540+
if(asciidoc) {
541+
engine = new AsciiDocEngine(context)
542+
}
543+
else {
544+
engine = new DocEngine(context)
545+
}
510546

511547
engine.engineProperties = props
512548
context.renderEngine = engine
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package grails.doc.asciidoc
2+
3+
import grails.doc.DocEngine
4+
import groovy.transform.InheritConstructors
5+
import org.asciidoctor.Options
6+
import org.asciidoctor.OptionsBuilder
7+
import org.radeox.api.engine.context.RenderContext
8+
9+
import static org.asciidoctor.Asciidoctor.Factory.create;
10+
import org.asciidoctor.Asciidoctor;
11+
/**
12+
* Created by graemerocher on 26/09/2016.
13+
*/
14+
@InheritConstructors
15+
class AsciiDocEngine extends DocEngine {
16+
Asciidoctor asciidoctor = create();
17+
18+
@Override
19+
String render(String content, RenderContext context) {
20+
asciidoctor.convert(content,
21+
new OptionsBuilder()
22+
.headerFooter(false)
23+
.attributes(
24+
'imagesdir': '../img',
25+
'source-highlighter':'coderay'
26+
)
27+
.get()
28+
)
29+
}
30+
31+
}

grails-docs/src/main/groovy/grails/doc/gradle/PublishGuide.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class PublishGuide extends DefaultTask {
2929
@InputDirectory File resourcesDir = new File(project.projectDir, "resources")
3030
@Input List propertiesFiles = []
3131
@Input String language = ""
32+
@Input boolean asciidoc = false
33+
@Input @Optional String sourceRepo
3234

3335
Collection macros = []
3436
File workDir = project.buildDir as File
@@ -49,15 +51,17 @@ class PublishGuide extends DefaultTask {
4951

5052
def publisher = new DocPublisher(sourceDir, targetDir)
5153
publisher.ant = project.ant
54+
publisher.asciidoc = asciidoc
5255
publisher.workDir = workDir
5356
publisher.apiDir = "${project.outputDir}" as File
5457
publisher.language = language ?: ''
58+
publisher.sourceRepo = sourceRepo
5559
publisher.images = project.file("${resourcesDir}/img")
5660
publisher.css = project.file("${resourcesDir}/css")
61+
publisher.fonts = project.file("${resourcesDir}/fonts")
5762
publisher.js = project.file("${resourcesDir}/js")
5863
publisher.style = project.file("${resourcesDir}/style")
5964
publisher.version = props."grails.version"
60-
publisher.logo = '<a href="http://grails.org" target="_blank"><img alt="Grails Logo" title="The Grails Framework" src="${path}/img/grails.png" border="0"/></a>'
6165

6266
// Override doc.properties properties with their language-specific counterparts (if
6367
// those are defined). You just need to add entries like es.title or pt_PT.subtitle.

grails-docs/src/main/groovy/grails/doc/internal/YamlTocStrategy.groovy

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import org.yaml.snakeyaml.Yaml
88
class YamlTocStrategy {
99
private final parser = new Yaml()
1010
private resourceChecker
11+
private String ext = ".gdoc"
1112

12-
YamlTocStrategy(resourceChecker) {
13+
YamlTocStrategy(resourceChecker, String ext = ".gdoc") {
1314
this.resourceChecker = resourceChecker
15+
this.ext = ext
1416
}
1517

1618
UserGuideNode generateToc(yaml) {
@@ -72,7 +74,7 @@ class YamlTocStrategy {
7274
}
7375

7476
// First check whether the gdoc file exists in the root directory.
75-
def filePath = "${basename}.gdoc"
77+
def filePath = "${basename}$ext"
7678
if (resourceChecker.exists(filePath)) {
7779
return filePath
7880
}
@@ -89,7 +91,7 @@ class YamlTocStrategy {
8991
// intro/whatsNew/changelog/$basename.gdoc
9092
//
9193
for (i in 1..pathElements.size()) {
92-
filePath = "${pathElements[-1..-i].join('/')}/${basename}.gdoc"
94+
filePath = "${pathElements[-1..-i].join('/')}/${basename}$ext"
9395
if (resourceChecker.exists(filePath)) {
9496
return filePath
9597
}

grails-docs/src/main/resources/grails/doc/doc.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ api.java.=http://docs.oracle.com/javase/7/docs/api/
1010
source.tag.regex=/\s*?def\s+?[a-zA-Z]+?\s*?=\s*?\{\s*?attrs\s*?,{0,1}\s*?body{0,1}\s*?->.+?/
1111

1212
# logos
13-
logo=<a href="http://grails.org" target="_blank"><img alt="The Grails Framework" src="../img/grails.png" border="0"/></a>
13+
logo=<a href="http://grails.org" target="_blank"><img alt="The Grails Framework" src="../img/grails-cupsonly-logo-white.svg" border="0"/></a>

0 commit comments

Comments
 (0)