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 85
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) 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 97
def attrvalue(key, sibling = true)
  delimiter = sibling ? ' ' : ''
  # example: <% if attr? 'foo' %><%= attr 'foo' %><% end %>
  %Q(<% if attr? '#{key}' %>#{delimiter}<%= attr '#{key}' %><% end %>)
end
common_attrs(id, role, reftext) click to toggle source
# File lib/asciidoctor/backends/docbook45.rb, line 23
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 27
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 65
def compact(str)
  str.gsub(BLANK_LINES_PATTERN, '').gsub(LINE_FEED_ENTITY, "\n")
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 104
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 76
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
  if tmpl.equal? :content
    result = node.content
  #elsif tmpl.is_a?(String)
  #  result = tmpl
  else
    result = tmpl.result(node.get_binding(self))
  end

  if (@view == 'document' || @view == 'embedded') && node.renderer.compact
    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 5
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 10
def style_class(sibling = true)
  attrvalue(:style, sibling)
end
tag(name, key) click to toggle source
# File lib/asciidoctor/backends/docbook45.rb, line 3
def tag(name, key)
  type = key.is_a?(Symbol) ? :attr : :var
  key = key.to_s
  if type == :attr
    # example: <% if attr? 'foo' %><bar><%= attr 'foo' %></bar><% end %>
    %Q(<% if attr? '#{key}' %><#{name}><%= attr '#{key}' %></#{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 80
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 14
def title_div(opts = {})
  %Q(<% if title? %><div class="title">#{opts.has_key?(:caption) ? '<%= @caption %>' : ''}<%= title %></div><% end %>)
end
title_tag(optional = true) click to toggle source
# File lib/asciidoctor/backends/docbook45.rb, line 15
def title_tag(optional = true)
  if optional
    %q{<%= title? ? "<title>#{title}</title>" : '' %>}
  else
    %q{<title><%= title %></title>}
  end
end