Class: Awestruct::Handlers::AsciidoctorHandler

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

Constant Summary collapse

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

Constants inherited from TiltHandler

TiltHandler::INTERPOLATION_CHAIN, TiltHandler::NON_INTERPOLATION_CHAIN

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.



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

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

  @site = site
  @front_matter = front_matter
end

Instance Method Details

#content_line_offsetObject



60
61
62
63
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 60

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

#front_matterObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 44

def front_matter
  @front_matter ||= {}
  parse_header()
  if @delegate
    @front_matter.update @delegate.front_matter
    # push front matter forward as well
    @delegate.front_matter.replace @front_matter
    @front_matter
  end
end

#inherit_front_matter(page) ⇒ Object



119
120
121
122
123
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 119

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

#optionsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 79

def options
  opts = super
  opts[:attributes] ||= {}
  opts[:attributes].update(@front_matter.inject({}) {|collector, (key,val)|
    collector["page-#{key}"] = "#{val}@"
    collector
  })
  # Keep only values that can be coerced to as string
  types = [String, Numeric, TrueClass, FalseClass, Date, Time]
  opts[:attributes].update(@site.inject({}) {|collector, (key,val)|
    collector["site-#{key}"] = "#{val}@" if types.detect {|t| val.kind_of? t }
    collector
  })
  opts[:attributes]['env'] = @site
  opts[:attributes]['env-site'] = true
  opts[:attributes]['awestruct-version'] = Awestruct::VERSION
  if @front_matter['header_footer']
    opts[:header_footer] = true
  end
  path_expanded = File.expand_path path
  opts[:attributes]['docdir'] = File.dirname path_expanded
  opts[:attributes]['docfile'] = path_expanded
  opts[:attributes]['docname'] = simple_name
  path_mtime = path.mtime
  opts[:attributes]['docdate'] = docdate = path_mtime.strftime('%Y-%m-%d')
  opts[:attributes]['doctime'] = doctime = path_mtime.strftime('%H:%M:%S %Z')
  opts[:attributes]['docdatetime'] = %(#{docdate} #{doctime})
  # TODO once Asciidoctor 1.5.0 is release, we should set the base_dir as a jail
  # we can't do this before 1.5.0 due to a bug in how includes are resolved
  if (Object.const_defined? 'Asciidoctor') && Asciidoctor::VERSION >= '1.5.0'
    opts[:base_dir] ||= @site.config.dir
  end
  opts
end

#parse_document_attributes(content) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 135

def parse_document_attributes(content)
  template = ::Tilt::new(delegate.path.to_s, delegate.content_line_offset + 1, options)
  headers = template.parse_headers(content, /^(?:page|awestruct)\-(?=.)/).inject({'interpolate' => false}) 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
  headers
end

#parse_headerObject



125
126
127
128
129
130
131
132
133
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 125

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



55
56
57
58
# File 'lib/awestruct/handlers/asciidoctor_handler.rb', line 55

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

#rendered_content(context, with_layouts) ⇒ Object



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

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