module YARD::Templates::Engine

def find_template_paths(from_template, path)

Returns:
  • (Array) - a list of full paths that are existing

Parameters:
  • path (String) -- the path component to search for in the
  • from_template (Template) -- if provided, allows a relative
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

def generate(objects, options = {})

Returns:
  • (void) -

Parameters:
  • options (Hash) -- (see {render})
  • objects (Array) -- a list of {CodeObjects::Base}
def generate(objects, options = {})
  options = set_default_options(options)
  options.objects = objects
  options.object = Registry.root
  template(options.template, :fulldoc, options.format).run(options)
end

def register_template_path(path)

Returns:
  • (void) -

Parameters:
  • path (String) -- a new template path
def register_template_path(path)
  template_paths.push(path) unless template_paths.include?(path)
end

def render(options = {})

Returns:
  • (String) - the rendered template

Options Hash: (**options)
  • :template (Symbol) -- the default template
  • :type (Symbol) -- the :object's type.
  • :format (Symbol) -- the default format

Parameters:
  • options (Hash) -- the options hash

Other tags:
    Example: Renders without an object -
    Example: Renders an object with html formatting -
def render(options = {})
  options = set_default_options(options)
  mod = template(options.template, options.type, options.format)
  if options.serializer && options.serialize != false
    with_serializer(options.object, options.serializer) { mod.run(options) }
  else
    mod.run(options)
  end
end

def set_default_options(options = {})

Returns:
  • (void) -

Options Hash: (**options)
  • :template (Symbol) -- the default template
  • :type (Symbol) -- the :object's type, if provided
  • :format (Symbol) -- the default format

Parameters:
  • options (Hash) -- the options hash
def set_default_options(options = {})
  if options.is_a?(Hash)
    options = TemplateOptions.new.tap do |o|
      o.reset_defaults
      o.update(options)
    end
  end
  options.type ||= options.object.type if options.object
  options
end

def template(*path)

Returns:
  • (Template) - the module representing the template

Raises:
  • (ArgumentError) - if the path does not exist within one of the

Parameters:
  • path (Array) -- a list of path components
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

def template!(path, full_paths = nil)

Returns:
  • (Template) - the template module representing the +path+

Parameters:
  • full_paths (Array) -- the full path on disk of the template
  • path (String) -- the path name of the template
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

def template_module_name(path)

Returns:
  • (String) - the module name

Parameters:
  • path (String) -- the path to generate a module name for
def template_module_name(path)
  'Template_' + path.to_s.gsub(/[^a-z0-9]/i, '_')
end

def with_serializer(object, serializer)

Other tags:
    See: Serializers::Base -

Other tags:
    Yieldreturn: - the contents to serialize

Other tags:
    Yield: - a block whose result will be serialize

Parameters:
  • serializer (Serializers::Base) -- the serializer object
  • object (CodeObjects::Base) -- the code object to serialize
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