Class: Awestruct::Tilt::AsciidoctorTemplate

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/awestruct/handlers/template/asciidoc.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.engine_initialized?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/awestruct/handlers/template/asciidoc.rb', line 9

def self.engine_initialized?
  defined? ::Asciidoctor::Document
end

Instance Method Details

#allows_script?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/awestruct/handlers/template/asciidoc.rb', line 28

def allows_script?
  false
end

#evaluate(scope, locals, &block) ⇒ Object



21
22
23
24
25
26
# File 'lib/awestruct/handlers/template/asciidoc.rb', line 21

def evaluate(scope, locals, &block)
  if scope.class == ::Awestruct::Context
    scope[:document] = ::Asciidoctor.load(data, options) unless scope.key? :document
    @output ||= scope[:document].convert
  end
end

#initialize_engineObject



13
14
15
# File 'lib/awestruct/handlers/template/asciidoc.rb', line 13

def initialize_engine
  require_template_library 'asciidoctor'
end

#parse_headers(content, filter = /.*/) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/awestruct/handlers/template/asciidoc.rb', line 32

def parse_headers(content, filter = /.*/)
  doc = ::Asciidoctor.load(content, {:parse_header_only => true})
  filtered = doc.attributes.select{|k,v| k =~ filter}.inject({}) do |hash, (k,v)|
    hash[k.gsub(filter, '')] = v
    hash
  end

  filtered['title'] = filtered['doctitle'] = doc.doctitle
  filtered['date'] ||= doc.attributes['revdate'] unless doc.attributes['revdate'].nil?
  if (cnt = doc.attributes['authorcount'].to_i) > 1
    authors = []
    (1..cnt).each do |idx|
      author = {}
      author[:name] = doc.attributes["author_#{idx}"]
      if doc.attributes.has_key? "email_#{idx}"
        author[:email] = doc.attributes["email_#{idx}"]
      end
      authors << author
    end
    filtered['author'] = authors.first[:name]
    filtered['email'] = authors.first[:email] if authors.first.has_key? :email
    filtered['authors'] = authors.to_yaml
  elsif !doc.attributes['author'].nil?
    author = {}
    author[:name] = doc.attributes['author']
    if doc.attributes.has_key? 'email'
      author[:email] = doc.attributes['email']
    end
    filtered['author'] = author[:name]
    filtered['authors'] = [author].to_yaml
  end

  filtered
end

#prepareObject



17
18
19
# File 'lib/awestruct/handlers/template/asciidoc.rb', line 17

def prepare
  options[:header_footer] = false if options[:header_footer].nil?
end