From d492bc4ff0281378abe1784a9ad7e467378e3c23 Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Sun, 29 May 2016 01:59:46 -0600 Subject: [PATCH] resolves #57 make front matter header optional --- README.adoc | 15 +++++++-------- lib/jekyll-asciidoc.rb | 18 +++++++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/README.adoc b/README.adoc index 0e57dd6..22d65ad 100644 --- a/README.adoc +++ b/README.adoc @@ -117,7 +117,7 @@ There are a few conditions that must be met for an AsciiDoc file to be qualified . The file must have an AsciiDoc file extension (see <>). . The name of the file must not begin with a dot (`.`) or underscore (`_`).footnoteref:[excluded_files,These files are excluded by Jekyll.] . The file must not be located in a folder whose name begins with a dot (`.`) or underscore (`_`), except for posts.footnoteref:[excluded_files] -. The document must begin with a {uri-front-matter}[front matter header] (and cannot have a Byte Order Mark, or BOM). +//. The document may begin with a {uri-front-matter}[front matter header] (in which case it cannot begin with a Byte Order Mark, or BOM). Here's a sample AsciiDoc file that meets these criteria: @@ -171,15 +171,14 @@ If you want the Liquid template preprocessor to be applied to an AsciiDoc file ( :page-liquid: ---- -IMPORTANT: Each AsciiDoc file must begin with a {uri-front-matter}[front matter header]. -Otherwise, Jekyll won't treat the file as a page, but rather as a static file. -The front matter header must be the very first character of the file. -If the file begins with whitespace or a Byte Order Mark (BOM), then the front matter header won't be seen. +IMPORTANT: AsciiDoc files may include a {uri-front-matter}[front matter header] for declaring page settings and variables. +If present, the front matter header must be the very first character of the file. +The front matter header won't be seen if the file begins with whitespace or a Byte Order Mark (BOM). -TIP: You can use an empty front matter header, as shown in the second example above. -In this case, you define all the document metadata (e.g., layout) using AsciiDoc attributes instead of in the front matter. +TIP: You can exclude the front matter header, as shown in the first example above, or leave it empty, as shown in the second example. +In these cases, you'll define all the page metadata (e.g., layout) using AsciiDoc attributes instead of in the front matter. You can also use a combination of both. -In this case, the attributes defined in the AsciiDoc header take precedence. +When combined, the attributes defined in the AsciiDoc header take precedence. You can now build your site using: diff --git a/lib/jekyll-asciidoc.rb b/lib/jekyll-asciidoc.rb index 1b6d568..6c6d8e7 100644 --- a/lib/jekyll-asciidoc.rb +++ b/lib/jekyll-asciidoc.rb @@ -1,6 +1,15 @@ JEKYLL_MIN_VERSION_3 = Gem::Version.new(Jekyll::VERSION) >= Gem::Version.new('3.0.0') unless defined? JEKYLL_MIN_VERSION_3 module Jekyll + module AsciiDoc + module Utils + extend self + def has_front_matter?(delegate_method, asciidoc_ext_re, path) + ::File.extname(path) =~ asciidoc_ext_re ? true : delegate_method.call(path) + end + end + end + module Converters class AsciiDocConverter < Converter IMPLICIT_ATTRIBUTES = %W( @@ -17,7 +26,7 @@ def initialize(config) @config = config config['asciidoc'] ||= 'asciidoctor' asciidoc_ext = (config['asciidoc_ext'] ||= 'asciidoc,adoc,ad') - config['asciidoc_ext_re'] = /^\.(?:#{asciidoc_ext.tr ',', '|'})$/i + asciidoc_ext_re = (config['asciidoc_ext_re'] = /^\.(?:#{asciidoc_ext.tr ',', '|'})$/i) config['asciidoc_page_attribute_prefix'] ||= 'page' unless (asciidoctor_config = (config['asciidoctor'] ||= {})).frozen? # NOTE convert keys to symbols @@ -29,6 +38,13 @@ def initialize(config) attributes.unshift('notitle', 'hardbreaks', 'idprefix', 'idseparator=-', 'linkattrs') attributes.concat(IMPLICIT_ATTRIBUTES) end + if JEKYLL_MIN_VERSION_3 && !config['asciidoc_require_front_matter'] + if (del_method = ::Jekyll::Utils.method(:has_yaml_header?)) + new_method = ::Jekyll::AsciiDoc::Utils.method(:has_front_matter?) + new_method = new_method.to_proc unless new_method.respond_to?(:curry) # Ruby < 2.2 + del_method.owner.define_singleton_method(:has_yaml_header?, new_method.curry[del_method][asciidoc_ext_re]) + end + end asciidoctor_config.freeze end end