Class: Awestruct::Handlers::AsciidoctorHandler

Inherits:
BaseTiltHandler show all
Defined in:
lib/awestruct/handlers/asciidoctor_handler.rb

Constant Summary collapse

CHAIN =
Awestruct::HandlerChain.new( Awestruct::Handlers::AsciidoctorMatcher.new(),
  Awestruct::Handlers::FileHandler,
  Awestruct::Handlers::FrontMatterHandler,
  Awestruct::Handlers::AsciidoctorHandler,
  Awestruct::Handlers::LayoutHandler
)

Instance Attribute Summary

Attributes inherited from BaseHandler

#delegate, #site

Instance Method Summary collapse

Methods inherited from BaseTiltHandler

#content_syntax, #double_extension?, #input_extension, #output_extension, #output_filename, #simple_name, #source_file_name

Methods inherited from BaseHandler

#content_syntax, #dependencies, #execute_shell, #input_mtime, #output_extension, #output_filename, #output_path, #path, #relative_source_path, #simple_name, #stale?, #to_chain

Constructor Details

#initialize(site, delegate) ⇒ AsciidoctorHandler

Returns a new instance of AsciidoctorHandler.



29
30
31
32
33
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 29

def initialize(site, delegate)
  super( site, delegate )

  @front_matter = {}
end

Instance Method Details

#content_line_offsetObject



46
47
48
49
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 46

def content_line_offset
  parse_header()
  @delegate.content_line_offset if @delegate
end

#front_matterObject



36
37
38
39
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 36

def front_matter
  parse_header()
  @front_matter.merge @delegate.front_matter if @delegate
end

#inherit_front_matter(page) ⇒ Object



84
85
86
87
88
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 84

def inherit_front_matter(page)
  parse_header()
  page.inherit_front_matter_from(front_matter)
  super
end

#optionsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 64

def options
  opts = super
  if opts[:attributes].nil?
    opts[:attributes] = @front_matter 
  else
    opts[:attributes] = opts[:attributes].merge @front_matter
  end
  opts[:attributes]['awestruct'] = true
  opts[:attributes]['awestruct-version'] = Awestruct::VERSION
  if @front_matter['header_footer']
    opts[:header_footer] = true
  end
  opts
end

#parse_document_attributes(content) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 100

def parse_document_attributes(content)
  template = Tilt::new(delegate.path.to_s, delegate.content_line_offset + 1, options)
  template.parse_headers(content, /^awestruct\-/).inject({}) do |hash, (k,v)|
    unless v.nil?
      hash[k] = v.empty? ? v : YAML.load(v)
      if hash[k].kind_of? Time
        # use DateTime to preserve timezone information
        hash[k] = DateTime.parse(v)
      end
    end
    hash
  end
end

#parse_headerObject



90
91
92
93
94
95
96
97
98
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 90

def parse_header
  return if @parsed_parts

  content = delegate.raw_content
  unless content.nil?
    @front_matter = parse_document_attributes(content)
  end
  @parsed_parts = true
end

#raw_contentObject



41
42
43
44
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 41

def raw_content
  parse_header()
  @delegate.raw_content if @delegate
end

#rendered_content(context, with_layouts) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 51

def rendered_content(context, with_layouts)
  parse_header()
  types = [String, Numeric, TrueClass, FalseClass, Array]
  @front_matter.merge!(context.page.inject({}) do |hash, (k,v)|
    hash[k.to_s] = v if not k.to_s.start_with?('__') and types.detect { |t| v.kind_of? t }
    hash
  end)
  if with_layouts && !context.page.layout
    @front_matter['header_footer'] = true
  end
  super
end