Skip to content

Commit

Permalink
Set PDF theme via task properties (asciidoctor#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysb33r committed Jul 25, 2018
1 parent db01d93 commit 7c7bd02
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -122,7 +143,6 @@ asciidoctorPdf {
verifyAll {
new File(testProjectDir.root, DEFAULT_OUTPUT_FILE).exists()
}

}

File getBuildFile(String extraContent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 <name>-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.
Expand All @@ -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<String, Object> getTaskSpecificDefaultAttributes(File workingSourceDir) {
Map<String, Object> 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
}
}

0 comments on commit 7c7bd02

Please sign in to comment.