Skip to content

Commit

Permalink
resolves asciidoctor#82 restructure configuration keys
Browse files Browse the repository at this point in the history
- move asciidoc keys to Hash under asciidoc key
- use old keys if new keys aren't specified
- add warning if old structure is detected
  • Loading branch information
mojavelinux committed Jun 4, 2016
1 parent f32fffe commit be01661
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions lib/jekyll-asciidoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,48 @@ class AsciiDocConverter < Converter
highlighter_suffix %(\n)

def initialize(config)
@setup = false
(@config = config)['asciidoc'] ||= 'asciidoctor'
asciidoc_ext = (config['asciidoc_ext'] ||= 'asciidoc,adoc,ad')
asciidoc_ext_re = (config['asciidoc_ext_re'] = /^\.(?:#{asciidoc_ext.tr ',', '|'})$/ix)
config['asciidoc_page_attribute_prefix'] ||= 'page'
unless (asciidoctor_config = (config['asciidoctor'] ||= {})).frozen?
if String === (asciidoc_config = (config['asciidoc'] ||= {}))
::Jekyll.logger.warn 'jekyll-asciidoc: The AsciiDoc config should be defined using a Hash (under the `asciidoc` key) instead of discrete entries.'
asciidoc_config = config['asciidoc'] = { 'processor' => asciidoc_config }
end
unless asciidoc_config.frozen?
asciidoc_config['processor'] ||= 'asciidoctor'
old_asciidoc_ext = config.delete('asciidoc_ext')
asciidoc_ext = (asciidoc_config['ext'] ||= (old_asciidoc_ext || 'asciidoc,adoc,ad'))
asciidoc_ext_re = (asciidoc_config['ext_re'] = /^\.(?:#{asciidoc_ext.tr ',', '|'})$/ix)
old_page_attr_prefix_def = config.key?('asciidoc_page_attribute_prefix')
old_page_attr_prefix_val = config.delete('asciidoc_page_attribute_prefix')
asciidoc_config['page_attribute_prefix'] ||= (old_page_attr_prefix_def ? old_page_attr_prefix_val : 'page')
asciidoc_config['require_front_matter_header'] = false unless asciidoc_config.key?('require_front_matter_header')

asciidoctor_config = (config['asciidoctor'] ||= {})
asciidoctor_config.replace(::Hash[asciidoctor_config.map {|key, val| [key.to_sym, val] }])
asciidoctor_config[:safe] ||= 'safe'
(asciidoctor_config[:attributes] ||= []).tap do |attributes|
attributes.unshift('notitle', 'idprefix', 'idseparator=-', 'linkattrs')
attributes.concat(IMPLICIT_ATTRIBUTES)
(asciidoctor_config[:attributes] ||= []).tap do |attrs|
attrs.unshift('notitle', 'idprefix', 'idseparator=-', 'linkattrs')
attrs.concat(IMPLICIT_ATTRIBUTES)
end
if ::Jekyll::MIN_VERSION_3 && !config['asciidoc_require_front_matter']

if ::Jekyll::MIN_VERSION_3 && !asciidoc_config['require_front_matter']
if (del_method = ::Jekyll::Utils.method(:has_yaml_header?))
unless (new_method = ::Jekyll::AsciiDoc::Utils.method(:has_front_matter?)).respond_to?(:curry)
new_method = new_method.to_proc # Ruby < 2.2
end
del_method.owner.define_singleton_method(del_method.name, new_method.curry[del_method][asciidoc_ext_re])
end
end

asciidoc_config.freeze
asciidoctor_config.freeze
end
@setup = false
@config = config
end

def setup
return self if @setup
@setup = true
case @config['asciidoc']
case (processor = @config['asciidoc']['processor'])
when 'asciidoctor'
begin
require 'asciidoctor' unless defined? ::Asciidoctor::VERSION
Expand All @@ -61,15 +75,15 @@ def setup
raise ::FatalException.new('Missing dependency: asciidoctor')
end
else
STDERR.puts %(Invalid AsciiDoc processor: #{@config['asciidoc']})
STDERR.puts %(Invalid AsciiDoc processor: #{processor})
STDERR.puts ' Valid options are [ asciidoctor ]'
raise ::FatalException.new(%(Invalid AsciiDoc processor: #{@config['asciidoc']}))
raise ::FatalException.new(%(Invalid AsciiDoc processor: #{processor}))
end
self
end

def matches(ext)
ext =~ @config['asciidoc_ext_re']
ext =~ @config['asciidoc']['ext_re']
end

def output_ext(ext)
Expand All @@ -82,11 +96,11 @@ def convert(content)
if (standalone = content.start_with?(STANDALONE_HEADER))
content = content[STANDALONE_HEADER.length..-1]
end
case @config['asciidoc']
case (processor = @config['asciidoc']['processor'])
when 'asciidoctor'
::Asciidoctor.convert(content, @config['asciidoctor'].merge(header_footer: standalone))
else
warn 'Unknown AsciiDoc converter. Passing through unparsed content.'
warn %(Unknown AsciiDoc processor: #{processor}. Passing through unparsed content.)
content
end
end
Expand All @@ -95,12 +109,12 @@ def load_header(content)
setup
# NOTE merely an optimization; if this doesn't match, the header still gets isolated by the processor
header = content.split(HEADER_BOUNDARY_RE, 2)[0]
case @config['asciidoc']
case (processor = @config['asciidoc']['processor'])
when 'asciidoctor'
# NOTE return a document even if header is empty because attributes may be inherited from config
::Asciidoctor.load(header, @config['asciidoctor'].merge(parse_header_only: true))
else
warn 'Unknown AsciiDoc converter. Cannot load document header.'
warn %(Unknown AsciiDoc processor: #{processor}. Cannot load document header.)
end
end
end
Expand All @@ -122,7 +136,7 @@ def generate(site)
@converter = (::Jekyll::MIN_VERSION_3 ?
site.find_converter_instance(::Jekyll::Converters::AsciiDocConverter) :
site.getConverterImpl(::Jekyll::Converters::AsciiDocConverter)).setup
unless (@page_attr_prefix = site.config['asciidoc_page_attribute_prefix']).empty?
unless (@page_attr_prefix = site.config['asciidoc']['page_attribute_prefix']).empty?
@page_attr_prefix = %(#{@page_attr_prefix.chomp '-'}-)
end

Expand Down

0 comments on commit be01661

Please sign in to comment.