From 7c7bd025a90f7327d380e636443d28854f674844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Schalk=20W=2E=20Cronj=C3=A9?= Date: Wed, 25 Jul 2018 18:18:24 +0200 Subject: [PATCH] Set PDF theme via task properties (#240) --- README.adoc | 9 ++ .../AsciidoctorPdfTaskFunctionalSpec.groovy | 32 ++++- .../docs/asciidoc/pdf-theme/basic-theme.yml | 43 +++++++ .../gradle/jvm/AsciidoctorPdfTask.groovy | 115 +++++++++++++++++- 4 files changed, 190 insertions(+), 9 deletions(-) create mode 100644 asciidoctor-gradle-jvm/src/intTest/projects/normal/src/docs/asciidoc/pdf-theme/basic-theme.yml diff --git a/README.adoc b/README.adoc index e004ea2f2..381a04762 100644 --- a/README.adoc +++ b/README.adoc @@ -463,6 +463,15 @@ When applying `org.asciidoctor.jvm.pdf` it creates a single task of type `org.as * Not to copy any resources to the output directory * It will set also a default version for `asciidoctorj-pdf` artifact. To override set `asciidoctorj.pdfVersion` or `asciidoctorPdf.asciidoctorj.pdfVersion`. +The `AsciidoctorPdfTask` task type has the following additional methods: + +[horizontal] +fontsDir:: Directory for custom PDF fonts + Specify a directory in any form acceptable to `project.file`. Using this instead of directly setting the `pdf-fontsdir` attribute means that Gradle will be able to check out of date status dependent on the content of this folder. +stylesDir:: Directory for finding a theme. + Specify a directory in any form acceptable to `project.file`. Using this instead of directly setting the `pdf-stylesdir` attribute means that Gradle will be able to check out of date status dependent on the content of this folder. +styleName: Name of the PDF theme. + == The AsciidoctorEpub Plugin When applying `org.asciidoctor.jvm.epub` it creates a single task of type `org.asciidoctor.gradle.jvm.epub.AsciidoctorEpubTask` which is then configured to: diff --git a/asciidoctor-gradle-jvm/src/intTest/groovy/org/asciidoctor/gradle/jvm/AsciidoctorPdfTaskFunctionalSpec.groovy b/asciidoctor-gradle-jvm/src/intTest/groovy/org/asciidoctor/gradle/jvm/AsciidoctorPdfTaskFunctionalSpec.groovy index 79189c6f3..86de37036 100644 --- a/asciidoctor-gradle-jvm/src/intTest/groovy/org/asciidoctor/gradle/jvm/AsciidoctorPdfTaskFunctionalSpec.groovy +++ b/asciidoctor-gradle-jvm/src/intTest/groovy/org/asciidoctor/gradle/jvm/AsciidoctorPdfTaskFunctionalSpec.groovy @@ -108,12 +108,33 @@ asciidoctorPdf { void 'Pdf generation can be run in JAVA_EXEC process mode'() { given: getBuildFile(""" -asciidoctorPdf { - sourceDir 'src/docs/asciidoc' + asciidoctorPdf { + sourceDir 'src/docs/asciidoc' + + inProcess = JAVA_EXEC + } + """) - inProcess = JAVA_EXEC -} -""") + when: + getGradleRunner([DEFAULT_TASK]).build() + + then: + verifyAll { + new File(testProjectDir.root, DEFAULT_OUTPUT_FILE).exists() + } + + } + + void 'Custom theme for PDF'() { + given: + getBuildFile(""" + asciidoctorPdf { + sourceDir 'src/docs/asciidoc' + styleName 'basic' + stylesDir 'src/docs/asciidoc/pdf-theme' + fontsDir 'src/docs/asciidoc/pdf-theme' + } + """) when: getGradleRunner([DEFAULT_TASK, '-s']).build() @@ -122,7 +143,6 @@ asciidoctorPdf { verifyAll { new File(testProjectDir.root, DEFAULT_OUTPUT_FILE).exists() } - } File getBuildFile(String extraContent) { diff --git a/asciidoctor-gradle-jvm/src/intTest/projects/normal/src/docs/asciidoc/pdf-theme/basic-theme.yml b/asciidoctor-gradle-jvm/src/intTest/projects/normal/src/docs/asciidoc/pdf-theme/basic-theme.yml new file mode 100644 index 000000000..f00216a5b --- /dev/null +++ b/asciidoctor-gradle-jvm/src/intTest/projects/normal/src/docs/asciidoc/pdf-theme/basic-theme.yml @@ -0,0 +1,43 @@ +# Example modified from the example at https://github.com/asciidoctor/asciidoctor-maven-examples +title_page: + align: left + +page: + layout: portrait + margin: [0.75in, 1in, 0.75in, 1in] + size: A4 +base: + font_color: #333333 + line_height_length: 17 + line_height: $base_line_height_length / $base_font_size +vertical_rhythm: $base_line_height_length +heading: + font_color: #FF8000 + font_size: 17 + font_style: bold + line_height: 1.2 + margin_bottom: $vertical_rhythm +link: + font_color: #009900 +outline_list: + indent: $base_font_size * 1.5 +header: + height: 0.75in + line_height: 1 + recto_content: + center: '{document-title}' + verso_content: + center: '{document-title}' +footer: + height: 0.75in + line_height: 1 + recto_content: + right: '{chapter-title} | *{page-number}*' + verso_content: + left: '*{page-number}* | {chapter-title}' +image: + align: center +caption: + align: center + font_color: #FF0000 + font_size: 10 \ No newline at end of file diff --git a/asciidoctor-gradle-jvm/src/main/groovy/org/asciidoctor/gradle/jvm/AsciidoctorPdfTask.groovy b/asciidoctor-gradle-jvm/src/main/groovy/org/asciidoctor/gradle/jvm/AsciidoctorPdfTask.groovy index ff698aec9..e816e66b0 100644 --- a/asciidoctor-gradle-jvm/src/main/groovy/org/asciidoctor/gradle/jvm/AsciidoctorPdfTask.groovy +++ b/asciidoctor-gradle-jvm/src/main/groovy/org/asciidoctor/gradle/jvm/AsciidoctorPdfTask.groovy @@ -17,19 +17,31 @@ package org.asciidoctor.gradle.jvm import groovy.transform.CompileStatic import org.asciidoctor.gradle.internal.AsciidoctorUtils +import org.gradle.api.Project +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputDirectory +import org.gradle.api.tasks.Optional +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.util.PatternSet import org.gradle.util.GradleVersion import org.gradle.workers.WorkerExecutor +import org.ysb33r.grolifant.api.StringUtils import javax.inject.Inject -/** +/** Asciidoctor task that is specialises in PDF conversion. + * * @since 2.0.0 * @author Schalk W. Cronjé */ @CompileStatic class AsciidoctorPdfTask extends AbstractAsciidoctorTask { + private Object fontsDir + private Object stylesDir + private Object styleName + @Inject AsciidoctorPdfTask(WorkerExecutor we) { super(we) @@ -39,9 +51,74 @@ class AsciidoctorPdfTask extends AbstractAsciidoctorTask { } + /** The directory where custom are fonts to be found. + * + * @return Directory or {@code null} is no directory was set. + */ + @InputDirectory + @PathSensitive(PathSensitivity.RELATIVE) + @Optional + File getFontsDir() { + this.fontsDir != null ? project.file(this.fontsDir) : null + } + + /** Specify a directory where to load custom fonts from. + * + * This will set the {@code pdf-fontsdir} attribute + * + * @param f Directory where custom fonts can be found. anything convertible with {@link Project#file} + * can be used. + */ + void setFontsDir(Object f) { + this.fontsDir = f + } + + /** The directory where custom theme is to be found. + * + * @return Directory or {@code null} is no directory was set. + */ + @InputDirectory + @PathSensitive(PathSensitivity.RELATIVE) + @Optional + File getStylesDir() { + this.stylesDir != null ? project.file(this.stylesDir) : null + } + + /** Specify a directory where to load custom theme from. + * + * This will set the {@code pdf-stylesdir} attribute + * + * @param f Directory where custom syles can be found. anything convertible with {@link Project#file} + * can be used. + */ + void setStylesDir(Object f) { + this.stylesDir = f + } + + /** The theme to use. + * + * @return Theme name or {@code null} if no theme was set. + */ + @Input + @Optional + String getStyleName() { + this.styleName != null ? StringUtils.stringize(this.styleName) : null + } + + /** The name of the YAML theme file to load. + * + * If the name ends with {@code .yml}, it’s assumed to be the complete name of a file. + * Otherwise, {@code -theme.yml} is appended to the name to make the file name (i.e., {@code -theme.yml}). + * + * @param s Name of style (theme). + */ + void setStyleName(Object s) { + this.styleName = s + } + /** Selects a final process mode of PDF processing. * - * If the system is running on Windows with a GRadle version which wtill has classpath leakage problems + * If the system is running on Windows with a Gradle version which still has classpath leakage problems * it will switch to using {@link #JAVA_EXEC}. * * @return Process mode to use for execution. @@ -65,7 +142,39 @@ class AsciidoctorPdfTask extends AbstractAsciidoctorTask { @Override protected PatternSet getDefaultSecondarySourceDocumentPattern() { PatternSet ps = defaultSourceDocumentPattern - ps.include '*-theme.ym*l' + ps.include '*-theme.y*ml' ps } + + /** A task may add some default attributes. + * + * If the user specifies any of these attributes, then those attributes will not be utilised. + * + * The default implementation will add {@code includedir} + * + * @param workingSourceDir Directory where source files are located. + * + * @return A collection of default attributes. + */ + @Override + protected Map getTaskSpecificDefaultAttributes(File workingSourceDir) { + Map attrs = super.getTaskSpecificDefaultAttributes(workingSourceDir) + + File fonts = getFontsDir() + if(fonts != null) { + attrs['pdf-fontsdir'] = fonts.absolutePath + } + + File styles = getStylesDir() + if(styles != null) { + attrs['pdf-stylesdir'] = styles.absolutePath + } + + String theme = getStyleName() + if(theme != null) { + attrs['pdf-style'] = theme + } + + attrs + } }