Class: Jekyll::AsciiDoc::Integrator

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

Overview

Registers before and after render hooks to set contextual attributes, promotes eligible AsciiDoc attributes to page variables, and applies certain page-level settings.

Constant Summary collapse

NewLine =
Utils::NewLine
StandaloneOptionLine =
Utils::StandaloneOptionLine

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jekyll-asciidoc/integrator.rb', line 14

def generate site
  @converter = converter = (Utils.get_converter site).setup

  if defined? ::Jekyll::Hooks
    before_render_callback = converter.method :before_render
    after_render_callback = converter.method :after_render
    [:pages, :documents].each do |collection_name|
      ::Jekyll::Hooks.register collection_name, :pre_render, &before_render_callback
      ::Jekyll::Hooks.register collection_name, :post_render, &after_render_callback
    end
  end

  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

  # NOTE posts were migrated to a collection named 'posts' in Jekyll 3
  site.posts.select! do |post|
    (converter.matches post.ext) ? (integrate post, 'posts') : true
  end if site.respond_to? :posts=

  site.collections.each do |name, collection|
    next unless collection.write?
    collection.docs.select! do |doc|
      (converter.matches doc.extname) ? (integrate doc, name) : true
    end
  end

  if (attrs = site.config['asciidoctor'][:attributes]) &&
      ((attrs['source-highlighter'] || '').chomp '@') == 'pygments' &&
      ((attrs['pygments-css'] || '').chomp '@') != 'style' && (attrs.fetch 'pygments-stylesheet', '')
    generate_pygments_stylesheet site, attrs
  end
end

#generate_pygments_stylesheet(site, attrs) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jekyll-asciidoc/integrator.rb', line 96

def generate_pygments_stylesheet site, attrs
  css_base = site.source
  unless (css_dir = (attrs['stylesdir'] || '').chomp '@').empty? || (css_dir.start_with? '/')
    css_dir = %(/#{css_dir})
  end
  if (css_name = attrs['pygments-stylesheet']).nil_or_empty?
    css_name = 'asciidoc-pygments.css'
  end
  css_file = ::File.join css_base, css_dir, css_name
  css_style = (attrs['pygments-style'] || 'vs').chomp '@'
  css = ::Asciidoctor::Stylesheets.instance.pygments_stylesheet_data css_style
  # NOTE apply stronger CSS rule for general text color
  css = css.sub '.listingblock .pygments  {', '.listingblock .pygments, .listingblock .pygments code {'
  if site.static_files.any? {|f| f.path == css_file }
    ::IO.write css_file, css unless css == (::IO.read css_file)
  else
    ::Asciidoctor::Helpers.mkdir_p ::File.dirname css_file
    ::IO.write css_file, css
    site.static_files << (::Jekyll::StaticFile.new site, css_base, css_dir, css_name)
  end
end

#integrate(document, collection_name = nil) ⇒ Object

Integrate the page-related attributes from the AsciiDoc document header into the data Array of the specified Page, Post or Document.

document - the Page, Post or Document instance to integrate. collection_name - the String name of the collection to which this

document belongs (optional, default: nil).

Returns a [Boolean] indicating whether the document should be published.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jekyll-asciidoc/integrator.rb', line 62

def integrate document, collection_name = nil
  document.extend Document
  document.content = [%(:#{@page_attr_prefix}layout: _auto), document.content] * NewLine unless document.data.key? 'layout'
  return unless (doc = @converter.load_header document)

  document.data['title'] = doc.doctitle if doc.header?
  document.data['author'] = doc.author if doc.author
  document.data['date'] = (::DateTime.parse doc.revdate).to_time if collection_name == 'posts' && (doc.attr? 'revdate')

  no_prefix = (prefix_size = @page_attr_prefix.length).zero?
  unless (adoc_header_data = doc.attributes
      .each_with_object({}) {|(key, val), accum|
        if no_prefix || ((key.start_with? @page_attr_prefix) && key = key[prefix_size..-1])
          accum[key] = ::String === val ? (Utils.parse_yaml_value val) : val
        end
      }).empty?
    document.data.update adoc_header_data
  end

  case document.data['layout']
  when nil
    document.content = %(#{StandaloneOptionLine}#{document.content}) unless document.data.key? 'layout'
  when '', '_auto'
    layout = collection_name ? (collection_name.chomp 's') : 'page'
    document.data['layout'] = (document.site.layouts.key? layout) ? layout : 'default'
  when false
    document.data.delete 'layout'
    document.content = %(#{StandaloneOptionLine}#{document.content})
  end

  document.extend NoLiquid unless document.data['liquid']
  document.data.fetch 'published', true
end