Skip to content

Commit 39b8259

Browse files
committed
Add PdfPublisher test
1 parent 4f1b591 commit 39b8259

File tree

581 files changed

+672509
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

581 files changed

+672509
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ target
3636

3737
/.idea/
3838
/grails-test-suite-uber/configured/
39+
/grails-docs/src/test/resources/docs/guide/single.pdf

grails-docs/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
runtime "org.slf4j:slf4j-api:$slf4jVersion"
2121

2222
runtime('com.lowagie:itext:2.0.8')
23+
2324
compile 'org.jsoup:jsoup:1.7.3'
2425
testCompile "junit:junit:$junitVersion"
2526
testCompile("org.spockframework:spock-core:${spockVersion}") {

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PdfBuilder {
6565
cleanupHtml(htmlFile, xml)
6666
}
6767

68-
static boolean cleanHtml = Boolean.getBoolean("grails.docs.clean.html")
68+
static boolean cleanHtml = System.getProperty('grails.docs.clean.html') == null ? true : Boolean.getBoolean("grails.docs.clean.html")
6969
static boolean debugPdf = Boolean.getBoolean("grails.docs.debug.pdf")
7070

7171
private static String cleanupHtml(File htmlFile, String xml) {
@@ -112,23 +112,27 @@ class PdfBuilder {
112112
pre, code {
113113
font-size: 10px;
114114
}
115-
.contribute-btn, #navigation, #ref-button { display: none; }
115+
.toc-item { margin-bottom: 2px; }
116+
.toc-item strong { margin-right: 2px; }
117+
.contribute-btn, #navigation, #ref-button, #toggle-col1 { display: none; }
116118
.paragraph, table, h2, h3, h4, h5, h6, li, pre, code {
117119
width: 595px;
118120
}
119121
</style>
120122
"""
121123
}
122124

123-
static void createPdf(String xml, File outputFile, File urlBase) {
124-
def dbf = DocumentBuilderFactory.newInstance()
125+
static Document createDocument(String xml) {
126+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance()
125127
dbf.validating = false
126128
dbf.setFeature "http://apache.org/xml/features/nonvalidating/load-external-dtd", false
127129
dbf.setFeature "http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false
128-
130+
129131
DocumentBuilder builder = dbf.newDocumentBuilder()
130-
Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")))
132+
builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")))
133+
}
131134

135+
static void createPdfWithDocument(Document doc, File outputFile, File urlBase) {
132136
ITextRenderer renderer = new ITextRenderer()
133137
renderer.setDocument(doc, urlBase.toURI().toString())
134138

@@ -142,4 +146,10 @@ class PdfBuilder {
142146
outputStream?.close()
143147
}
144148
}
149+
150+
static void createPdf(String xml, File outputFile, File urlBase) {
151+
Document doc = createDocument(xml)
152+
createPdfWithDocument(doc, outputFile, urlBase)
153+
154+
}
145155
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package grails.doc
2+
3+
import groovy.transform.CompileStatic
4+
import org.w3c.dom.Document
5+
6+
@CompileStatic
7+
class PdfPublisher {
8+
9+
static void publishPdfFromHtml(File outputDir, String child, String pdfName) {
10+
PdfBuilder pdfBuilder = new PdfBuilder()
11+
File currFile = new File(outputDir, child)
12+
String xml = pdfBuilder.createXml(currFile, outputDir.absolutePath)
13+
Document doc = pdfBuilder.createDocument(xml)
14+
File outputFile = new File(currFile.parentFile, pdfName)
15+
File urlBase = new File(outputDir, "guide/single.html")
16+
pdfBuilder.createPdfWithDocument(doc, outputFile, urlBase)
17+
}
18+
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package grails.doc.gradle
1616

1717
import grails.doc.PdfBuilder
18+
import grails.doc.PdfPublisher
1819
import org.gradle.api.DefaultTask
1920
import org.gradle.api.tasks.*
2021

@@ -30,14 +31,9 @@ class PublishPdf extends DefaultTask {
3031

3132
@TaskAction
3233
def publish() {
33-
def outputDir = new File(outputDirectory, language ?: "")
34+
File outputDir = new File(outputDirectory, language ?: "")
3435
try {
35-
def currFile = new File(outputDir, "guide/single.html")
36-
def pdfBuilder = new PdfBuilder()
37-
def xml = pdfBuilder.createXml(currFile, outputDir.absolutePath)
38-
pdfBuilder.createPdf xml,
39-
new File(currFile.parentFile, pdfName),
40-
new File(outputDir, "guide/single.html")
36+
PdfPublisher.publishPdfFromHtml(outputDir, "guide/single.html", pdfName)
4137
}
4238
catch (Exception ex) {
4339
ex.printStackTrace()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package grails.doc
2+
3+
import spock.lang.Specification
4+
5+
class PdfPublisherSpec extends Specification {
6+
7+
void "generate pdf from sample docs"() {
8+
given:
9+
String sampleDocsFolderPath = 'src/test/resources/docs'
10+
File sampleDocsFolder = new File(sampleDocsFolderPath)
11+
String pdfName = 'single.pdf'
12+
String child = "guide/single.html"
13+
14+
expect:
15+
sampleDocsFolder.exists()
16+
!new File("${sampleDocsFolderPath}/guide/${pdfName}").exists()
17+
18+
when:
19+
PdfPublisher.publishPdfFromHtml(sampleDocsFolder, child, pdfName)
20+
21+
then:
22+
noExceptionThrown()
23+
new File("${sampleDocsFolderPath}/guide/${pdfName}").exists()
24+
25+
cleanup:
26+
new File("${sampleDocsFolderPath}/guide/${pdfName}").delete()
27+
}
28+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*
2+
Dummy stylesheet allowing for some customisation of pdf output without having to copy and modify the other stylesheets.
3+
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
Hidden blocks for translations
3+
*/
4+
.hidden-block {
5+
display: none;
6+
background-color: #DED;
7+
color: #622;
8+
padding: 0.5em 0;
9+
}

0 commit comments

Comments
 (0)