class Asciidoctor::BaseTemplate

An abstract base class that provides methods for definining and rendering the backend templates. Concrete subclasses must implement the template method.

NOTE we must use double quotes for attribute values in the HTML/XML output to prevent quote processing. This requirement seems hackish, but AsciiDoc has this same issue.

Attributes

eruby[R]
view[R]

Public Class Methods

inherited(klass) click to toggle source
# File lib/asciidoctor/backends/base_template.rb, line 18
def self.inherited(klass)
  @template_classes ||= []
  @template_classes << klass
end
new(view, eruby) click to toggle source
# File lib/asciidoctor/backends/base_template.rb, line 13
def initialize(view, eruby)
  @view = view
  @eruby = eruby
end
template_classes() click to toggle source
# File lib/asciidoctor/backends/base_template.rb, line 23
def self.template_classes
  @template_classes
end

Public Instance Methods

attribute(name, key) click to toggle source

create template matter to insert an attribute if the variable has a value

# File lib/asciidoctor/backends/base_template.rb, line 87
def attribute(name, key)
  type = key.is_a?(Symbol) ? :attr : :var
  if type == :attr
    # example: <% if attr? 'foo' %> bar="<%= attr 'foo' %>"<% end %>
    %Q(<% if attr? '#{key}' %> #{name}="<%= attr '#{key}' %>"<% end %>)
  else
    # example: <% if foo %> bar="<%= foo %>"<% end %>
    %Q(<% if #{key} %> #{name}="<%= #{key} %>"<% end %>)
  end
end
attrvalue(key, sibling = true, inherit = true) click to toggle source

create template matter to insert a style class if the variable has a value

# File lib/asciidoctor/backends/base_template.rb, line 99
def attrvalue(key, sibling = true, inherit = true)
  delimiter = sibling ? ' ' : ''
  if inherit
    # example: <% if attr? 'foo' %><%= attr 'foo' %><% end %>
    %Q(<% if attr? '#{key}' %>#{delimiter}<%= attr '#{key}' %><% end %>)
  else
    # example: <% if attr? 'foo', nil, false %><%= attr 'foo', nil, false %><% end %>
    %Q(<% if attr? '#{key}', nil, false %>#{delimiter}<%= attr '#{key}', nil, false %><% end %>)
  end
end
common_attrs(id, role, reftext) click to toggle source
# File lib/asciidoctor/backends/docbook45.rb, line 24
def common_attrs(id, role, reftext)
  %Q(#{id && " id=\"#{id}\""}#{role && " role=\"#{role}\""}#{reftext && " xreflabel=\"#{reftext}\""})
end
common_attrs_erb() click to toggle source
# File lib/asciidoctor/backends/docbook45.rb, line 28
def common_attrs_erb
  %q(<%= template.common_attrs(@id, (attr 'role'), (attr 'reftext')) %>)
end
compact(str) click to toggle source

Public: Compact blank lines in the provided text. This method also restores every HTML line feed entity found with an endline character.

text - the String to process

returns the text with blank lines removed and HTML line feed entities converted to an endline character.

# File lib/asciidoctor/backends/base_template.rb, line 67
def compact(str)
  str.gsub(BLANK_LINE_PATTERN, '').gsub(LINE_FEED_ENTITY, "\n")
end
content(node) click to toggle source
# File lib/asciidoctor/backends/docbook45.rb, line 32
def content(node)
  node.blocks? ? node.content.chomp : "<simpara>#{node.content.chomp}</simpara>"
end
content_erb() click to toggle source
# File lib/asciidoctor/backends/docbook45.rb, line 36
def content_erb
  %q(<%= blocks? ? content.chomp : "<simpara>#{content.chomp}</simpara>" %>)
end
id() click to toggle source

create template matter to insert an id if one is specified for the block

# File lib/asciidoctor/backends/base_template.rb, line 111
def id
  attribute('id', '@id')
end
preserve_endlines(str, node) click to toggle source

Public: Preserve endlines by replacing them with the HTML line feed entity.

If the compact flag on the document's renderer is true, perform the replacement. Otherwise, return the text unprocessed.

text - the String to process node - the concrete instance of Asciidoctor::AbstractNode being rendered

# File lib/asciidoctor/backends/base_template.rb, line 78
def preserve_endlines(str, node)
  node.renderer.compact ? str.gsub("\n", LINE_FEED_ENTITY) : str
end
render(node = Object.new, locals = {}) click to toggle source

Public: Render this template in the execution context of the supplied concrete instance of Asciidoctor::AbstractNode.

This method invokes the template method on this instance to retrieve the template data and then evaluates that template in the context of the supplied concrete instance of Asciidoctor::AbstractNode. This instance is accessible to the template data via the local variable named 'template'.

If the compact flag on the document's renderer is true and the view context is document or embedded, then blank lines in the output are compacted. Otherwise, the rendered output is returned unprocessed.

node - The concrete instance of AsciiDoctor::AbstractNode to render locals - A Hash of additional variables. Not currently in use.

# File lib/asciidoctor/backends/base_template.rb, line 41
def render(node = Object.new, locals = {})
  tmpl = template
  case tmpl
  when :invoke_result
    return result(node)
  when :content
    result = node.content
  else
    result = tmpl.result(node.get_binding(self))
  end

  if (@view == 'document' || @view == 'embedded') &&
      node.renderer.compact && !node.document.nested?
    compact result
  else
    result
  end
end
role_class() click to toggle source

create template matter to insert a style class from the role attribute if specified

# File lib/asciidoctor/backends/html5.rb, line 7
def role_class
  attrvalue('role')
end
style_class(sibling = true) click to toggle source

create template matter to insert a style class from the style attribute if specified

# File lib/asciidoctor/backends/html5.rb, line 12
def style_class(sibling = true)
  attrvalue('style', sibling, false)
end
tag(name, key, dynamic = false) click to toggle source
# File lib/asciidoctor/backends/docbook45.rb, line 3
def tag(name, key, dynamic = false)
  type = key.is_a?(Symbol) ? :attr : :var
  key = key.to_s
  if type == :attr
    key_str = dynamic ? %Q("#{key}") : "'#{key}'"
    # example: <% if attr? 'foo' %><bar><%= attr 'foo' %></bar><% end %>
    %Q(<% if attr? #{key_str} %><#{name}><%= attr #{key_str} %></#{name}><% end %>)
  else
    # example: <% unless foo.to_s.empty? %><bar><%= foo %></bar><% end %>
    %Q(<% unless #{key}.to_s.empty? %><#{name}><%= #{key} %></#{name}><% end %>)
  end
end
template() click to toggle source
# File lib/asciidoctor/backends/base_template.rb, line 82
def template
  raise "You chilluns need to make your own template"
end
title_div(opts = {}) click to toggle source
# File lib/asciidoctor/backends/html5.rb, line 16
def title_div(opts = {})
  if opts.has_key? :caption
    %q(<% if title? %><div class="title"><%= @caption %><%= title %></div><% end %>)
  else
    %q(<% if title? %><div class="title"><%= title %></div><% end %>)
  end
end
title_tag(optional = true) click to toggle source
# File lib/asciidoctor/backends/docbook45.rb, line 16
def title_tag(optional = true)
  if optional
    %Q(<%= title? ? "\n<title>\#{title}</title>" : nil %>)
  else
    %Q(\n<title><%= title %></title>)
  end
end