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.
# File lib/asciidoctor/backends/base_template.rb, line 20 def self.inherited(klass) if self == BaseTemplate @template_classes ||= [] @template_classes << klass else self.superclass.inherited(klass) end end
# File lib/asciidoctor/backends/base_template.rb, line 14 def initialize(view, backend, eruby) @view = view @backend = backend @eruby = eruby end
# File lib/asciidoctor/backends/base_template.rb, line 29 def self.template_classes @template_classes end
create template matter to insert an attribute if the variable has a value
# File lib/asciidoctor/backends/base_template.rb, line 93 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
# File lib/asciidoctor/backends/docbook45.rb, line 24 def common_attrs(id, role, reftext) %Q(#{id && " #{@backend == 'docbook5' ? 'xml:id' : 'id'}=\"#{id}\""}#{role && " role=\"#{role}\""}#{reftext && " xreflabel=\"#{reftext}\""}) end
# File lib/asciidoctor/backends/docbook45.rb, line 28 def common_attrs_erb %q(<%= template.common_attrs(@id, role, reftext) %>) end
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 73 def compact(str) str.gsub(BLANK_LINE_PATTERN, '').gsub(LINE_FEED_ENTITY, EOL) end
# File lib/asciidoctor/backends/docbook45.rb, line 32 def content(node) node.blocks? ? node.content : "<simpara>#{node.content}</simpara>" end
# File lib/asciidoctor/backends/docbook45.rb, line 36 def content_erb %q(<%= blocks? ? content : "<simpara>#{content}</simpara>" %>) end
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 84 def preserve_endlines(str, node) node.renderer.compact ? str.gsub(EOL, LINE_FEED_ENTITY) : str end
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 47 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
# 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
# File lib/asciidoctor/backends/base_template.rb, line 88 def template raise "You chilluns need to make your own template" end
# 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