Module: YARD::Templates::Engine
Relationships & Source Files | |
Defined in: | lib/yard/templates/engine.rb |
Overview
This module manages all creation, handling and rendering of Template
objects.
- To create a template object at a path, use .template.
- To render a template, call .render.
- To register a template path in the lookup paths, call .register_template_path.
Class Attribute Summary
Class Method Summary
-
.generate(objects, options = {}) ⇒ void
Passes a set of objects to the
:fulldoc
template for full documentation generation. -
.register_template_path(path) ⇒ void
Registers a new template path in .template_paths
-
.render(options = {}) ⇒ String
Renders a template on a code object using a set of default (overridable) options.
-
.template(*path) ⇒ Template
Creates a template module representing the path.
-
.template!(path, full_paths = nil) ⇒ Template
Forces creation of a template at
path
within afull_path
. -
.with_serializer(object, serializer) { ... }
Serializes the results of a block with a
serializer
object. -
.find_template_paths(from_template, path) ⇒ Array<String>
private
Searches through the registered .template_paths and returns all full directories that have the
path
within them on disk. -
.set_default_options(options = {}) ⇒ void
private
Sets default options on the options hash.
-
.template_module_name(path) ⇒ String
private
The name of the module that represents a
path
Class Attribute Details
.template_paths ⇒ Array<String> (rw)
# File 'lib/yard/templates/engine.rb', line 14
attr_accessor :template_paths
Class Method Details
.find_template_paths(from_template, path) ⇒ Array<String> (private)
Searches through the registered .template_paths and returns
all full directories that have the path
within them on disk.
# File 'lib/yard/templates/engine.rb', line 160
def find_template_paths(from_template, path) paths = template_paths.dup paths = from_template.full_paths + paths if from_template paths.inject([]) do |acc, tp| full_path = File.cleanpath(File.join(tp, path)) acc.unshift(full_path) if File.directory?(full_path) acc end.uniq end
.generate(objects, options = {}) ⇒ void
This method returns an undefined value.
Passes a set of objects to the :fulldoc
template for full documentation generation.
This is called by ::YARD::CLI::Yardoc
to most commonly perform HTML
documentation generation.
.register_template_path(path) ⇒ void
This method returns an undefined value.
Registers a new template path in .template_paths
# File 'lib/yard/templates/engine.rb', line 20
def register_template_path(path) template_paths.push(path) unless template_paths.include?(path) end
.render(options = {}) ⇒ String
Renders a template on a code object using
a set of default (overridable) options. Either the :object
or :type
keys must be provided.
If a :serializer
key is provided and :serialize
is not set to
false, the rendered contents will be serialized through the ::YARD::Serializers::Base
object. See .with_serializer.
# File 'lib/yard/templates/engine.rb', line 81
def render( = {}) = ( ) mod = template( .template, .type, .format) if .serializer && .serialize != false with_serializer( .object, .serializer) { mod.run( ) } else mod.run( ) end end
.set_default_options(options = {}) ⇒ void
(private)
This method returns an undefined value.
Sets default options on the options hash
# File 'lib/yard/templates/engine.rb', line 140
def ( = {}) if .is_a?(Hash) = TemplateOptions.new.tap do |o| o.reset_defaults o.update( ) end end .type ||= .object.type if .object end
.template(*path) ⇒ Template
Creates a template module representing the path. Searches on disk
for the first directory named path
(joined by '/') within the
template paths and builds a template module for. All other matching
directories in other template paths will be included in the
generated module as mixins (for overriding).
# File 'lib/yard/templates/engine.rb', line 34
def template(*path) from_template = nil from_template = path.shift if path.first.is_a?(Template) path = path.join('/') full_paths = find_template_paths(from_template, path) path = File.cleanpath(path).gsub('../', '') raise ArgumentError, "No such template for #{path}" if full_paths.empty? mod = template!(path, full_paths) mod end
.template!(path, full_paths = nil) ⇒ Template
Forces creation of a template at path
within a full_path
.
# File 'lib/yard/templates/engine.rb', line 52
def template!(path, full_paths = nil) full_paths ||= [path] full_paths = [full_paths] unless full_paths.is_a?(Array) name = template_module_name(full_paths.first) begin; return const_get(name); rescue NameError; nil end mod = const_set(name, Module.new) mod.send(:include, Template) mod.send(:initialize, path, full_paths) mod end
.template_module_name(path) ⇒ String (private)
The name of the module that represents a path
# File 'lib/yard/templates/engine.rb', line 175
def template_module_name(path) 'Template_' + path.to_s.gsub(/[^a-z0-9]/i, '_') end
.with_serializer(object, serializer) { ... }
Serializes the results of a block with a serializer
object.
# File 'lib/yard/templates/engine.rb', line 114
def with_serializer(object, serializer) output = nil filename = serializer.serialized_path(object) if serializer.respond_to?(:basepath) filename = File.join(serializer.basepath, filename) end log.capture("Generating #{filename}", nil) do serializer.before_serialize if serializer output = yield if serializer serializer.serialize(object, output) serializer.after_serialize(output) end end output end