Skip to content

Commit

Permalink
resolves asciidoctor#1126 automatically set stylesdir if style ends w…
Browse files Browse the repository at this point in the history
…ith .yml

- if pdf-style value ends with .yml, and pdf-stylesdir is not set, set pdf-stylesdir to directory of pdf-style value
- resolve extended themes relative to calculated value of pdf-stylesdir
  • Loading branch information
mojavelinux committed Jul 5, 2019
1 parent b67f86a commit 8eeb1cf
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
* allow page background position to be controlled using position attribute on image macro (#1124)
* change default background position to center (#1124)
* set enable_file_requests_with_root and enable_web_requests options for inline SVGs (#683)
* automatically set pdf-stylesdir if pdf-style ends with .yml and pdf-stylesdir is not specified (#1126)
* replace hyphens with underscores in top-level theme keys
* allow hyphens to be used in variable references in theme (#1122)
* resolve null color value in theme to nil (aka not set)
Expand Down
2 changes: 2 additions & 0 deletions docs/theming-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ base:

An image is specified either as a bare image path or as an inline image macro as found in the AsciiDoc syntax.
Images are currently resolved relative to the value of the `pdf-stylesdir` attribute.
(If `pdf-style` is a path that ends in `.yml`, and `pdf-stylesdir` is not set, then the images are resolved relative to the directory of the path specified by `pdf-style`).

The following image types (and corresponding file extensions) are supported:

Expand Down Expand Up @@ -3621,6 +3622,7 @@ pdf-stylesdir:: The directory where the theme file is located.
_Specifying an absolute path is recommended._
+
If you use images in your theme, image paths are resolved relative to this directory.
If `pdf-style` ends with `.yml`, and `pdf-stylesdir` is not specified, then `pdf-stylesdir` defaults to the directory of the path specified by `pdf-style`.

pdf-style:: The name of the YAML theme file to load.
If the name ends with `.yml`, it's assumed to be the complete name of a file and is resolved relative to `pdf-stylesdir`, if specified, otherwise the current directory.
Expand Down
9 changes: 7 additions & 2 deletions lib/asciidoctor-pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,13 @@ def init_pdf doc

def load_theme doc
@theme ||= begin
@stylesdir = doc.attr 'pdf-stylesdir'
doc.options[:pdf_theme] || (ThemeLoader.load_theme (doc.attr 'pdf-style'), @stylesdir)
if (theme = doc.options[:pdf_theme])
@stylesdir = theme.__dir__ || (doc.attr 'pdf-stylesdir') || ThemeLoader::ThemesDir
else
theme = ThemeLoader.load_theme (doc.attr 'pdf-style'), (doc.attr 'pdf-stylesdir')
@stylesdir = theme.__dir__ || ThemeLoader::ThemesDir
end
theme
end
end

Expand Down
37 changes: 19 additions & 18 deletions lib/asciidoctor-pdf/theme_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ def to_s
end

def self.resolve_theme_file theme_name = nil, theme_path = nil
theme_name ||= 'default'
# if .yml extension is given, don't append -theme.yml
if (theme_name.end_with? '.yml')
# if .yml extension is given, assume it's a path (don't append -theme.yml)
if ((theme_name ||= 'default').end_with? '.yml')
# FIXME restrict to jail!
# QUESTION why are we not using expand_path in this case?
theme_path ? (::File.join theme_path, theme_name) : theme_name
theme_file = ::File.expand_path theme_name, theme_path
theme_path ||= ::File.dirname theme_file
else
# QUESTION should we append '-theme.yml' or just '.yml'?
::File.expand_path %(#{theme_name}-theme.yml), (theme_path || ThemesDir)
theme_file = ::File.expand_path %(#{theme_name}-theme.yml), (theme_path || (theme_path = ThemesDir))
end
[theme_file, theme_path]
end

def self.resolve_theme_asset asset_path, theme_path = nil
Expand All @@ -61,20 +60,22 @@ def self.resolve_theme_asset asset_path, theme_path = nil

# NOTE base theme is loaded "as is" (no post-processing)
def self.load_base_theme
::OpenStruct.new ::SafeYAML.load_file BaseThemePath
(::OpenStruct.new ::SafeYAML.load_file BaseThemePath).tap {|theme| theme.__dir__ = ThemesDir }
end

def self.load_theme theme_name = nil, theme_path = nil
if (theme_file = resolve_theme_file theme_name, theme_path) == BaseThemePath
theme_file, theme_path = resolve_theme_file theme_name, theme_path
if theme_file == BaseThemePath
load_base_theme
elsif theme_file == DefaultThemePath
load_file theme_file, nil, theme_path
else
theme_data = load_file theme_file, nil, theme_path
# QUESTION should we enforce any other fallback values?
theme_data.base_align ||= 'left'
theme_data.code_font_family ||= (theme_data.literal_font_family || 'Courier')
theme_data.conum_font_family ||= (theme_data.literal_font_family || 'Courier')
unless theme_file == DefaultThemePath
# QUESTION should we enforce any other fallback values?
theme_data.base_align ||= 'left'
theme_data.code_font_family ||= (theme_data.literal_font_family || 'Courier')
theme_data.conum_font_family ||= (theme_data.literal_font_family || 'Courier')
end
theme_data.__dir__ = theme_path
theme_data
end
end
Expand All @@ -92,11 +93,11 @@ def self.load_file filename, theme_data = nil, theme_path = nil
theme_data = theme_data ? (::OpenStruct.new theme_data.to_h.merge load_base_theme.to_h) : load_base_theme
next
elsif extend_file == 'default'
extend_file = resolve_theme_file extend_file, (extend_theme_path = ThemesDir)
extend_file, extend_theme_path = resolve_theme_file extend_file
elsif extend_file.start_with? './'
extend_file = resolve_theme_file extend_file, (extend_theme_path = (::File.dirname ::File.absolute_path filename))
extend_file, extend_theme_path = resolve_theme_file extend_file, (::File.dirname filename)
else
extend_file = resolve_theme_file extend_file, (extend_theme_path = theme_path)
extend_file, extend_theme_path = resolve_theme_file extend_file, theme_path
end
theme_data = load_file extend_file, theme_data, extend_theme_path
end
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/running-header-outside-fixtures-theme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extends: default
header:
height: 36
recto:
columns: '>40% =20% <40%'
left_content: text
center_content: image:fixtures/green-bar.svg[pdfwidth=0.5in]
right_content: text
8 changes: 8 additions & 0 deletions spec/fixtures/running-header-theme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extends: default
header:
height: 36
recto:
columns: '>40% =20% <40%'
left_content: text
center_content: image:green-bar.svg[pdfwidth=0.5in]
right_content: text
29 changes: 29 additions & 0 deletions spec/running_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,35 @@
(expect to_file).to visually_match 'running-content-image-width.pdf'
end

it 'should resolve image target relative to stylesdir', integration: true do
[
{
'pdf-style' => 'running-header',
'pdf-stylesdir' => fixtures_dir,
},
{
'pdf-style' => 'fixtures/running-header-outside-fixtures-theme.yml',
'pdf-stylesdir' => (File.dirname fixtures_dir),
},
].each_with_index do |attribute_overrides, idx|
to_file = to_pdf_file <<~'EOS', %(running-content-image-from-stylesdir-#{idx}.pdf), attribute_overrides: attribute_overrides
[.text-center]
content
EOS
(expect to_file).to visually_match 'running-content-image.pdf'
end
end

it 'should resolve image target relative to theme file when stylesdir is not set', integration: true do
attribute_overrides = { 'pdf-style' => (fixture_file 'running-header-theme.yml', relative: true) }
to_file = to_pdf_file <<~'EOS', 'running-content-image-from-style.pdf', attribute_overrides: attribute_overrides
[.text-center]
content
EOS

(expect to_file).to visually_match 'running-content-image.pdf'
end

it 'should warn and replace image with alt text if image is not found' do
[true, false].each do |block|
(expect {
Expand Down
18 changes: 17 additions & 1 deletion spec/theme_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@
it 'should look for file ending in -theme.yml when resolving custom theme' do
theme = subject.load_theme 'custom', fixtures_dir
(expect theme.base_font_family).to eql 'Times-Roman'
(expect theme.__dir__).to eql fixtures_dir
end

it 'should set __dir__ to dirname of theme file if theme path not set' do
theme = subject.load_theme fixture_file 'custom-theme.yml'
(expect theme.base_font_family).to eql 'Times-Roman'
(expect theme.__dir__).to eql fixtures_dir
end

it 'should load specified file ending with .yml if path is not given' do
Expand All @@ -149,12 +156,21 @@
(expect theme.base_font_family).to eql 'Times-Roman'
end

it 'should load extended themes relative theme file when theme_path is not specified' do
theme = subject.load_theme fixture_file 'extended-custom-theme.yml'
(expect theme.__dir__).to eql fixtures_dir
(expect theme.base_align).to eql 'justify'
(expect theme.base_font_family).to eql 'Times-Roman'
(expect theme.base_font_color).to eql 'FF0000'
end

it 'should ensure required keys are set' do
theme = subject.load_theme 'extends-nil-empty-theme.yml', fixtures_dir
(expect theme.__dir__).to eql fixtures_dir
(expect theme.base_align).to eql 'left'
(expect theme.code_font_family).to eql 'Courier'
(expect theme.conum_font_family).to eql 'Courier'
(expect theme.to_h.keys).to have_size 3
(expect theme.to_h.keys).to have_size 4
end

it 'should not overwrite required keys with default values if already set' do
Expand Down

0 comments on commit 8eeb1cf

Please sign in to comment.