Class: Asciidoctor::Converter::TemplateConverter
- Defined in:
- lib/asciidoctor/converter/template.rb
Overview
will be issued.
Constant Summary collapse
- DEFAULT_ENGINE_OPTIONS =
{ erb: { trim: 0 }, # TODO line 466 of haml/compiler.rb sorts the attributes; file an issue to make this configurable # NOTE AsciiDoc syntax expects HTML/XML output to use double quotes around attribute values haml: { format: :xhtml, attr_wrapper: '"', escape_html: false, escape_attrs: false, ugly: true }, slim: { disable_escape: true, sort_attrs: false, pretty: false }, }
Constants included from DefaultFactory
Class Attribute Summary collapse
- .caches ⇒ Object readonly
Attributes included from Asciidoctor::Converter
Class Method Summary collapse
Instance Method Summary collapse
-
#convert(node, template_name = nil, opts = nil) ⇒ String
Convert an AbstractNode to the backend format using the named template.
-
#handles?(name) ⇒ Boolean
Checks whether there is a Tilt template registered with the specified name.
-
#initialize(backend, template_dirs, opts = {}) ⇒ TemplateConverter
constructor
A new instance of TemplateConverter.
-
#register(name, template) ⇒ Object
Registers a Tilt template with this converter.
-
#templates ⇒ Hash
Retrieves the templates that this converter manages.
Methods inherited from Base
Methods included from Asciidoctor::Converter
Methods included from DefaultFactory
Methods included from Factory
#converters, #create, create, default, #for, new
Methods included from Logging
#logger, #message_with_context
Constructor Details
#initialize(backend, template_dirs, opts = {}) ⇒ TemplateConverter
Returns a new instance of TemplateConverter.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/asciidoctor/converter/template.rb', line 53 def initialize backend, template_dirs, opts = {} Helpers.require_library 'tilt' unless defined? ::Tilt.new @backend = backend @templates = {} @template_dirs = template_dirs @eruby = opts[:eruby] @safe = opts[:safe] @active_engines = {} @engine = opts[:template_engine] @engine_options = {}.tap {|accum| DEFAULT_ENGINE_OPTIONS.each {|engine, engine_opts| accum[engine] = engine_opts.merge } } if opts[:htmlsyntax] == 'html' # if not set, assume xml since this converter is also used for DocBook (which doesn't specify htmlsyntax) @engine_options[:haml][:format] = :html5 @engine_options[:slim][:format] = :html end @engine_options[:slim][:include_dirs] = template_dirs.reverse.map {|dir| ::File. dir } if (overrides = opts[:template_engine_options]) overrides.each do |engine, override_opts| (@engine_options[engine] ||= {}).update override_opts end end case opts[:template_cache] when true logger.warn 'optional gem \'concurrent-ruby\' is not available. This gem is recommended when using the default template cache.' unless defined? ::Concurrent::Map @caches = self.class.caches when ::Hash @caches = opts[:template_cache] else @caches = {} # the empty Hash effectively disables caching end scan end |
Class Attribute Details
.caches ⇒ Object (readonly)
45 46 47 |
# File 'lib/asciidoctor/converter/template.rb', line 45 def caches @caches end |
Class Method Details
.clear_caches ⇒ Object
47 48 49 50 |
# File 'lib/asciidoctor/converter/template.rb', line 47 def clear_caches @caches[:scans].clear @caches[:templates].clear end |
Instance Method Details
#convert(node, template_name = nil, opts = nil) ⇒ String
Convert an AbstractNode to the backend format using the named template.
Looks for a template that matches the value of the template name or, if the template name is not specified, the value of the AbstractNode#node_name property.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/asciidoctor/converter/template.rb', line 98 def convert node, template_name = nil, opts = nil unless (template = @templates[template_name ||= node.node_name]) raise %(Could not find a custom template to handle transform: #{template_name}) end # Slim doesn't include helpers in the template's execution scope (like HAML), so do it ourselves node.extend ::Slim::Helpers if (defined? ::Slim::Helpers) && (::Slim::Template === template) # NOTE opts become locals in the template if template_name == 'document' (template.render node, opts).strip else (template.render node, opts).rstrip end end |
#handles?(name) ⇒ Boolean
Checks whether there is a Tilt template registered with the specified name.
120 121 122 |
# File 'lib/asciidoctor/converter/template.rb', line 120 def handles? name @templates.key? name end |
#register(name, template) ⇒ Object
Registers a Tilt template with this converter.
137 138 139 140 141 142 |
# File 'lib/asciidoctor/converter/template.rb', line 137 def register name, template if (template_cache = @caches[:templates]) template_cache[template.file] = template end @templates[name] = template end |
#templates ⇒ Hash
Retrieves the templates that this converter manages.
127 128 129 |
# File 'lib/asciidoctor/converter/template.rb', line 127 def templates @templates.merge end |