summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2022-05-17 13:20:50 -0600
committerGitHub <noreply@github.com>2022-05-17 13:20:50 -0600
commit907c9c7cc09da11b0348161d43fffd0d483c9656 (patch)
treefca0691c73d3b56e036d3bb71bdbbaf871ff080e
parent7ad2e8bbd0074422337b9fef0943844b4bc1ed3c (diff)
resolves #412 replace docdir attribute reference in value of pdf-themesdir and pdf-fontsdir attributes (PR #2183)
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--docs/modules/theme/pages/apply-theme.adoc4
-rw-r--r--lib/asciidoctor/pdf/converter.rb7
-rw-r--r--spec/converter_spec.rb13
-rw-r--r--spec/fixtures/custom-theme.yml2
-rw-r--r--spec/fixtures/hello-with-custom-theme.adoc3
6 files changed, 26 insertions, 4 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 2a188020..d1847c9e 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -10,6 +10,7 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co
Enhancements::
* allow hyphenation to be turned on and configured using the `base-hyphens` key in the theme (#2161)
+* replace `docdir` attribute reference in value of `pdf-themesdir` and `pdf-fontsdir` attributes (if not already replaced) (#412)
* split out `start_title_page` method from `ink_title_page` to make customization of the title page simpler using an extended converter
* introduce `start_toc_page` method to handle positioning cursor at first page of TOC
diff --git a/docs/modules/theme/pages/apply-theme.adoc b/docs/modules/theme/pages/apply-theme.adoc
index e4e232c2..9be9a9da 100644
--- a/docs/modules/theme/pages/apply-theme.adoc
+++ b/docs/modules/theme/pages/apply-theme.adoc
@@ -12,6 +12,8 @@ If the name ends with `.yml`, it's assumed to be the complete name of a file and
Otherwise, `-theme.yml` is appended to the name to make the file name (i.e., `<name>-theme.yml`) and is resolved relative to `pdf-themesdir`, if specified, otherwise the built-in themes dir.
pdf-themesdir:: The directory where the theme file is located.
+If the path is relative, the value is resolved starting from the current working directory.
+You can use the `+{docdir}+` token as the first path segment to create an absolute path starting from the directory of the document.
_Specifying an absolute path is recommended._
+
If you use images in your theme, image paths are resolved relative to this directory.
@@ -20,6 +22,8 @@ If `pdf-theme` ends with `.yml`, and `pdf-themesdir` is not specified, then `pdf
pdf-fontsdir:: The directory or directories where the fonts used by your theme, if any, are located.
Multiple entries must be separated by either a comma or a semicolon.
To reference a file inside a JAR file on the classpath, prefix with the path with `uri:classloader:` (AsciidoctorJ only).
+If the path is relative, the value is resolved starting from the current working directory.
+You can use the `+{docdir}+` token as the first path segment to create an absolute path starting from the directory of the document.
_Specifying an absolute path is recommended._
== Load a theme
diff --git a/lib/asciidoctor/pdf/converter.rb b/lib/asciidoctor/pdf/converter.rb
index dff23fb9..95ea4bd2 100644
--- a/lib/asciidoctor/pdf/converter.rb
+++ b/lib/asciidoctor/pdf/converter.rb
@@ -362,7 +362,7 @@ module Asciidoctor
@ppbook = nil
end
# QUESTION: should ThemeLoader handle registering fonts instead?
- register_fonts theme.font_catalog, (doc.attr 'pdf-fontsdir', 'GEM_FONTS_DIR')
+ register_fonts theme.font_catalog, ((doc.attr 'pdf-fontsdir')&.sub '{docdir}', (doc.attr 'docdir')) || 'GEM_FONTS_DIR'
default_kerning theme.base_font_kerning != 'none'
@fallback_fonts = Array theme.font_fallbacks
@allow_uri_read = doc.attr? 'allow-uri-read'
@@ -539,9 +539,10 @@ module Asciidoctor
@theme ||= begin # rubocop:disable Naming/MemoizedInstanceVariableName
if (theme = doc.options[:pdf_theme])
theme = theme.dup
- @themesdir = ::File.expand_path theme.__dir__ || (doc.attr 'pdf-themesdir') || ::Dir.pwd
+ @themesdir = ::File.expand_path theme.__dir__ ||
+ (user_themesdir = ((doc.attr 'pdf-themesdir')&.sub '{docdir}', (doc.attr 'docdir')) || ::Dir.pwd)
elsif (theme_name = doc.attr 'pdf-theme')
- theme = ThemeLoader.load_theme theme_name, (user_themesdir = doc.attr 'pdf-themesdir')
+ theme = ThemeLoader.load_theme theme_name, (user_themesdir = (doc.attr 'pdf-themesdir')&.sub '{docdir}', (doc.attr 'docdir'))
@themesdir = theme.__dir__
else
@themesdir = (theme = ThemeLoader.load_theme).__dir__
diff --git a/spec/converter_spec.rb b/spec/converter_spec.rb
index 8f6d7763..bacb1192 100644
--- a/spec/converter_spec.rb
+++ b/spec/converter_spec.rb
@@ -251,6 +251,19 @@ describe Asciidoctor::PDF::Converter do
end
end
+ it 'should resolve pdf-themesdir relative to the current working directory' do
+ input_file = Pathname.new fixture_file 'hello-with-custom-theme.adoc'
+ relative_themesdir = fixture_file '.', relative: true
+ pdf = to_pdf input_file, analyze: true, attribute_overrides: { 'pdf-themesdir' => relative_themesdir }
+ (expect pdf.find_text font_name: 'Times-Roman').to have_size pdf.text.size
+ end
+
+ it 'should replace {docdir} token in value of pdf-themesdir' do
+ input_file = Pathname.new fixture_file 'hello-with-custom-theme.adoc'
+ pdf = to_pdf input_file, analyze: true, attribute_overrides: { 'pdf-themesdir' => '{docdir}' }
+ (expect pdf.find_text font_name: 'Times-Roman').to have_size pdf.text.size
+ end
+
it 'should set text color to black when default-for-print theme is specified' do
pdf = to_pdf <<~EOS, analyze: true
= Document Title
diff --git a/spec/fixtures/custom-theme.yml b/spec/fixtures/custom-theme.yml
index 00ae5173..06fe8220 100644
--- a/spec/fixtures/custom-theme.yml
+++ b/spec/fixtures/custom-theme.yml
@@ -1,2 +1,2 @@
base:
- font_family: Times-Roman
+ font-family: Times-Roman
diff --git a/spec/fixtures/hello-with-custom-theme.adoc b/spec/fixtures/hello-with-custom-theme.adoc
new file mode 100644
index 00000000..637cf3d8
--- /dev/null
+++ b/spec/fixtures/hello-with-custom-theme.adoc
@@ -0,0 +1,3 @@
+:pdf-theme: custom
+
+hello