Skip to content

Commit

Permalink
resolves asciidoctor#193 allow page layout to be soft set in site config
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Dec 14, 2018
1 parent ca7b86b commit 80d0cfd
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 20 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
== Unreleased

* drop unsupported versions of Ruby and Jekyll
* handle case when document body is empty (#179)
* don't crash if document body is empty (#179)
* honor layout defined in frontmatter defaults (#187)
* allow page layout to be soft set in site config (#193)
* set asciidoc property to true on all AsciiDoc pages (#189)
* set asciidoc property to true on any (AsciiDoc) page enriched by this plugin (i.e., page.asciidoc) (#189)
* don't call nil_or_empty? outside of an Asciidoctor context (#142)
Expand Down
35 changes: 31 additions & 4 deletions lib/jekyll-asciidoc/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def initialize config
page_attr_prefix = old_page_attr_prefix_def ? (old_page_attr_prefix_val || '') :
((asciidoc_config.key? 'page_attribute_prefix') ? '' : DefaultPageAttributePrefix)
end
asciidoc_config['page_attribute_prefix'] = page_attr_prefix.chomp '-'
asciidoc_config['page_attribute_prefix'] = (page_attr_prefix = page_attr_prefix.chomp '-').empty? ? '' : %(#{page_attr_prefix}-)
asciidoc_config['require_front_matter_header'] = !!asciidoc_config['require_front_matter_header']
asciidoc_config.extend Configured

Expand Down Expand Up @@ -172,11 +172,11 @@ def clear_paths
@page_context.delete :paths
end

def load_header document
def load_header document, options = {}
setup
record_paths document, source_only: true
# NOTE merely an optimization; if this doesn't match, the header still gets isolated by the processor
header = (document.content.split HeaderBoundaryRx, 2)[0] || ''
# NOTE merely an optimization; if this doesn't match, the header still gets extracted by the processor
header = (header = document.content) && HeaderBoundaryRx =~ header ? $` : ''
case @asciidoc_config['processor']
when 'asciidoctor'
opts = @asciidoctor_config.merge parse_header_only: true
Expand All @@ -188,6 +188,9 @@ def load_header document
end
opts[:attributes] = opts[:attributes].merge paths
end
if (layout_attr = resolve_default_layout document, opts[:attributes])
opts[:attributes] = opts[:attributes].merge layout_attr
end
# NOTE return instance even if header is empty since attributes may be inherited from config
doc = ::Asciidoctor.load header, opts
else
Expand Down Expand Up @@ -276,6 +279,30 @@ def resolve_attribute_refs text, attrs
end
end

def resolve_default_layout document, attributes
layout_attr_name = %(#{document.site.config['asciidoc']['page_attribute_prefix']}layout)
if attributes.key? layout_attr_name
if ::String === (layout = attributes[layout_attr_name])
if layout == '~@'
layout = 'none@'
elsif (layout.end_with? '@') && ((document.data.key? 'layout') || document.data['layout'])
layout = %(#{(layout = document.data['layout']).nil? ? 'none' : layout}@)
else
layout = nil
end
elsif layout.nil?
layout = 'none'
else
layout = layout.to_s
end
elsif (document.data.key? 'layout') || document.data['layout']
layout = %(#{(layout = document.data['layout']).nil? ? 'none' : layout}@)
else
layout = '@'
end
layout ? { layout_attr_name => layout } : nil
end

# Register pre and post render callbacks for saving and clearing contextual AsciiDoc attributes, respectively.
::Jekyll::Hooks.tap do |hooks|
hooks.register [:pages, :documents], :pre_render, &(method :before_render)
Expand Down
12 changes: 4 additions & 8 deletions lib/jekyll-asciidoc/integrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ def self.get_instance site
def generate site
@converter = converter = (Converter.get_instance site).setup

unless (@page_attr_prefix = site.config['asciidoc']['page_attribute_prefix']).empty?
@page_attr_prefix = %(#{@page_attr_prefix}-)
end

site.pages.select! do |page|
(converter.matches page.ext) ? (integrate page) : true
end
Expand Down Expand Up @@ -54,10 +50,9 @@ def generate site
#
# Returns a [Boolean] indicating whether the document should be published.
def integrate document, collection_name = nil
data = document.data
document.content = [%(:#{@page_attr_prefix}layout: _auto), document.content] * NewLine unless data['layout']
return true unless (doc = @converter.load_header document)

data = document.data
data['asciidoc'] = true
# NOTE id is already reserved in Jekyll for another purpose, so we'll map id to docid instead
data['docid'] = doc.id if doc.id
Expand All @@ -67,9 +62,10 @@ def integrate document, collection_name = nil
data['date'] = ::Jekyll::Utils.parse_date doc.revdate, %(Document '#{document.relative_path}' does not have a valid revdate in the AsciiDoc header.)
end

no_prefix = (prefix_size = @page_attr_prefix.length) == 0
page_attr_prefix = document.site.config['asciidoc']['page_attribute_prefix']
no_prefix = (prefix_size = page_attr_prefix.length) == 0
unless (adoc_data = doc.attributes.each_with_object({}) {|(key, val), accum|
if no_prefix || ((key.start_with? @page_attr_prefix) && key = key[prefix_size..-1])
if no_prefix || ((key.start_with? page_attr_prefix) && key = key[prefix_size..-1])
accum[key] = ::String === val ? (parse_yaml_value val) : val
end
}).empty?
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/site_wide_fallback_layout/_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gems:
- jekyll-asciidoc
asciidoctor:
attributes:
- page-layout=default@
15 changes: 15 additions & 0 deletions spec/fixtures/site_wide_fallback_layout/_layouts/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ page.title }}</title>
</head>
<body>
<div class="page-content">
{{ content }}
</div>
<footer>
<p>Footer for default layout.</p>
</footer>
</body>
</html>
15 changes: 15 additions & 0 deletions spec/fixtures/site_wide_fallback_layout/_layouts/simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ page.title }}</title>
</head>
<body>
<div class="page-content">
{{ content }}
</div>
<footer>
<p>Footer for simple layout.</p>
</footer>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= Page Title
:page-layout: simple

Lorem ipsum.
6 changes: 6 additions & 0 deletions spec/fixtures/site_wide_fallback_layout/in-front-matter.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
layout: simple
---
= Page Title

Lorem ipsum.
3 changes: 3 additions & 0 deletions spec/fixtures/site_wide_fallback_layout/not-set.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= Page Title

Lorem ipsum.
6 changes: 5 additions & 1 deletion spec/fixtures/site_wide_standalone_layout/standalone.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// only standalone if page-layout=false is set in site config
---
layout: overridden
---
// standalone because page-layout=false is set in site config
= Standalone Page
:page-layout: also-overridden

Lorem ipsum.
41 changes: 35 additions & 6 deletions spec/jekyll-asciidoc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
expect(site.config['asciidoc']['ext']).to eql('asciidoc,adoc,ad')
end

it 'should use page as page attribute prefix by default' do
it 'should use page- as page attribute prefix by default' do
expect(site.config['asciidoc']).to be_a(::Hash)
expect(site.config['asciidoc']['page_attribute_prefix']).to eql('page')
expect(site.config['asciidoc']['page_attribute_prefix']).to eql('page-')
end

it 'should not require a front matter header by default' do
Expand Down Expand Up @@ -122,7 +122,7 @@

it 'should migrate asciidoc_page_attribute_prefix key' do
expect(site.config['asciidoc']).to be_a(::Hash)
expect(site.config['asciidoc']['page_attribute_prefix']).to eql('jekyll')
expect(site.config['asciidoc']['page_attribute_prefix']).to eql('jekyll-')
end
end

Expand All @@ -142,7 +142,7 @@

it 'should use new key page_attribute_prefix over legacy key' do
expect(site.config['asciidoc']).to be_a(::Hash)
expect(site.config['asciidoc']['page_attribute_prefix']).to eql('pg')
expect(site.config['asciidoc']['page_attribute_prefix']).to eql('pg-')
end
end

Expand Down Expand Up @@ -343,9 +343,9 @@

before(:each) { site.process }

it 'should strip trailing hyphen from page attribute prefix value' do
it 'should strip trailing hyphen from page attribute prefix config value' do
expect(site.config['asciidoc']).to be_a(::Hash)
expect(site.config['asciidoc']['page_attribute_prefix']).to eql('jekyll')
expect(site.config['asciidoc']['page_attribute_prefix']).to eql('jekyll-')
end

it 'should recognize page attributes with alternate page attribute prefix' do
Expand Down Expand Up @@ -587,6 +587,35 @@
end
end

describe 'use site-wide fallback layout' do
let :name do
'site_wide_fallback_layout'
end

before(:each) { site.process }

it 'should use layout defined in front matter if page-layout is soft set in site config' do
file = output_file 'in-front-matter.html'
expect(::File).to exist(file)
contents = ::File.read file
expect(contents).to include('<p>Footer for simple layout.</p>')
end

it 'should use layout defined in AsciiDoc header if page-layout is soft set in site config' do
file = output_file 'in-asciidoc-header.html'
expect(::File).to exist(file)
contents = ::File.read file
expect(contents).to include('<p>Footer for simple layout.</p>')
end

it 'should use layout defined in site config if not set in page' do
file = output_file 'not-set.html'
expect(::File).to exist(file)
contents = ::File.read file
expect(contents).to include('<p>Footer for default layout.</p>')
end
end

describe 'explicit site time' do
let :name do
'explicit_site_time'
Expand Down

0 comments on commit 80d0cfd

Please sign in to comment.