Class: Jekyll::Generators::AsciiDocPreprocessor

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-asciidoc.rb

Overview

Promotes select AsciiDoc attributes to Jekyll front matter

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jekyll-asciidoc.rb', line 89

def generate(site)
  asciidoc_converter = JEKYLL_MIN_VERSION_3 ?
      site.find_converter_instance(Jekyll::Converters::AsciiDocConverter) :
      site.getConverterImpl(Jekyll::Converters::AsciiDocConverter)
  asciidoc_converter.setup
  unless (page_attr_prefix = site.config['asciidoc_page_attribute_prefix']).empty?
    page_attr_prefix = %(#{page_attr_prefix}-)
  end
  page_attr_prefix_l = page_attr_prefix.length

  site.pages.each do |page|
    if asciidoc_converter.matches(page.ext)
      next unless (doc = asciidoc_converter.load_header(page.content))

      page.data['title'] = doc.doctitle if doc.header?
      page.data['author'] = doc.author if doc.author

      unless (additional_page_data = SafeYAML.load(doc.attributes
          .select {|name| name.start_with?(page_attr_prefix) }
          .map {|name, val| %(#{name[page_attr_prefix_l..-1]}: #{val}) }
          .join("\n"))).empty?
        page.data.update(additional_page_data)
      end

      page.data['layout'] = 'default' unless page.data.key? 'layout'
    end
  end

  (JEKYLL_MIN_VERSION_3 ? site.posts.docs : site.posts).each do |post|
    if asciidoc_converter.matches(JEKYLL_MIN_VERSION_3 ? post.data['ext'] : post.ext)
      next unless (doc = asciidoc_converter.load_header(post.content))

      post.data['title'] = doc.doctitle if doc.header?
      post.data['author'] = doc.author if doc.author
      post.data['date'] = DateTime.parse(doc.revdate).to_time if doc.attr? 'revdate'

      unless (additional_page_data = SafeYAML.load(doc.attributes
          .select {|name| name.start_with?(page_attr_prefix) }
          .map {|name, val| %(#{name[page_attr_prefix_l..-1]}: #{val}) }
          .join("\n"))).empty?
        post.data.update(additional_page_data)
      end

      post.data['layout'] = 'post' unless post.data.key? 'layout'
    end
  end
end