Module: YARD::Templates::Template
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
ClassMethods
|
|
Instance Chain:
|
|
Defined in: | lib/yard/templates/template.rb |
Class Attribute Summary
-
.extra_includes ⇒ Array<Module, Proc>
rw
Extra includes are mixins that are included after a template is created.
ClassMethods
- Extended
Class Method Summary
-
.include_extra(template, options) ⇒ void
Includes the .extra_includes modules into the template object.
- .included(klass) Internal use only Internal use only
ClassMethods
- Extended
find_file | Searches for a file identified by |
find_nth_file | Searches for the nth file (where n = |
full_paths, initialize, is_a?, | |
new | Creates a new template object to be rendered with #run |
reset_full_paths | Resets cache for |
run, | |
S | Alias for creating a |
T | Alias for creating Engine.template. |
include_inherited, include_parent, load_setup_rb |
Instance Attribute Summary
- #class rw
- #options rw
- #options=(value) rw
- #section rw
Helpers::BaseHelper
- Included
Instance Method Summary
- #erb(section) { ... } ⇒ String
-
#file(basename, allow_inherited = false) ⇒ String
Returns the contents of a file.
-
#init
Initialization called on the template.
- #initialize(opts = TemplateOptions.new)
- #inspect
-
#run(opts = nil, sects = sections, start_at = 0, break_first = false) {|opts| ... } ⇒ String
Runs a template on
sects
using extra options. -
#sections(*args)
Sets the sections (and subsections) to be rendered for the template.
-
#superb(sect = section, &block) ⇒ String
Calls the ERB file from the last inherited template with #section.erb.
-
T(*path) ⇒ Template
Loads a template specified by path.
-
#yieldall(opts = nil, &block)
Yields all subsections with any extra options.
- #add_options(opts = nil) private
- #cache(section) private
- #cache_filename(section) private
- #render_section(section, &block) private
- #set_ivars private
- #with_section private
Helpers::MethodHelper
- Included
Helpers::BaseHelper
- Included
#format_object_title, #format_object_type, | |
#format_source | Indents and formats source code. |
#format_types | Formats a list of return types for output and links each type. |
#globals | An object that keeps track of global state throughout the entire template rendering process (including any sub-templates). |
#h | Escapes text. |
#link_file | Links to an extra file. |
#link_include_file | Include a file as a docstring in output. |
#link_include_object | Includes an object's docstring into output. |
#link_object | Links to an object with an optional title. |
#link_url | Links to a URL. |
#linkify | Links objects or URLs. |
#run_verifier | Runs a list of objects against the |
Class Attribute Details
.extra_includes ⇒ Array<Module, Proc
> (rw)
Extra includes are mixins that are included after a template is created. These mixins can be registered by plugins to operate on templates and override behaviour.
Note that this array can be filled with modules or proc objects. If a proc object is given, the proc will be called with the #options hash containing relevant template information like the object, format, and more. The proc should return a module or nil if there is none.
# File 'lib/yard/templates/template.rb', line 25
attr_accessor :extra_includes
Class Method Details
.include_extra(template, options) ⇒ void
This method returns an undefined value.
Includes the .extra_includes modules into the template object.
# File 'lib/yard/templates/template.rb', line 38
def include_extra(template, ) extra_includes.each do |mod| mod = mod.call( ) if mod.is_a?(Proc) next unless mod.is_a?(Module) template.extend(mod) end end
.included(klass)
# File 'lib/yard/templates/template.rb', line 29
def included(klass) klass.extend(ClassMethods) end
Instance Attribute Details
#class (rw)
[ GitHub ]# File 'lib/yard/templates/template.rb', line 7
attr_accessor :class, :section
#options (rw)
[ GitHub ]# File 'lib/yard/templates/template.rb', line 8
attr_reader :
#options=(value) (rw)
[ GitHub ]# File 'lib/yard/templates/template.rb', line 337
def (value) @options = value set_ivars end
#section (rw)
[ GitHub ]# File 'lib/yard/templates/template.rb', line 7
attr_accessor :class, :section
Instance Method Details
#add_options(opts = nil) (private)
[ GitHub ]# File 'lib/yard/templates/template.rb', line 399
def (opts = nil) return(yield) if opts.nil? && block_given? cur_opts = if block_given? self. = .merge(opts) if block_given? value = yield self. = cur_opts value end end
#cache(section) (private)
# File 'lib/yard/templates/template.rb', line 378
def cache(section) content = @cache[section.to_sym] return content if content file = cache_filename(section) @cache_filename[section.to_sym] = file raise ArgumentError, "no template for section '#{section}' in #{self.class.path}" unless file @cache[section.to_sym] = IO.read(file) end
#cache_filename(section) (private)
[ GitHub ]#erb(section) { ... } ⇒ String
# File 'lib/yard/templates/template.rb', line 285
def erb(section, &block) method_name = ErbCache.method_for(cache_filename(section)) do erb_with(cache(section), cache_filename(section)) end send(method_name, &block) end
#file(basename, allow_inherited = false) ⇒ String
Returns the contents of a file. If allow_inherited
is set to true
,
use +{{super
}}+ inside the file contents to insert the contents
of the file from an inherited template. For instance, if templates/b
inherits from templates/a
and file "test.css" exists in both directories,
both file contents can be retrieved by having templates/b/test.css
look
like:
{{super
}}
...
body { css styles here }
p.class { other styles }
# File 'lib/yard/templates/template.rb', line 312
def file(basename, allow_inherited = false) file = self.class.find_file(basename) raise ArgumentError, "no file for '#{basename}' in #{self.class.path}" unless file data = IO.read(file) if allow_inherited superfile = self.class.find_nth_file(basename, 2) data.gsub!('{{{__super__}}}', superfile ? IO.read(superfile) : "") end data end
#init
Initialization called on the template. Override this in a 'setup.rb' file in the template's path to implement a template
# File 'lib/yard/templates/template.rb', line 239
def init end
#initialize(opts = TemplateOptions.new)
[ GitHub ]# File 'lib/yard/templates/template.rb', line 186
def initialize(opts = TemplateOptions.new) opts_class = opts.class opts_class = TemplateOptions if opts_class == Hash @cache = {} @cache_filename = {} @sections = [] @options = opts_class.new (opts) Template.include_extra(self, ) init end
#inspect
[ GitHub ]#render_section(section, &block) (private)
[ GitHub ]# File 'lib/yard/templates/template.rb', line 364
def render_section(section, &block) section = section.name if section.is_a?(Section) case section when Section, String, Symbol if respond_to?(section) send(section, &block) else erb(section, &block) end when Module, Template section.run(, &block) if section.is_a?(Template) end || "" end
#run(opts = nil, sects = sections, start_at = 0, break_first = false) {|opts| ... } ⇒ String
Runs a template on sects
using extra options. This method should
not be called directly. Instead, call the class method ClassMethods#run
# File 'lib/yard/templates/template.rb', line 252
def run(opts = nil, sects = sections, start_at = 0, break_first = false, &block) out = String.new("") return out if sects.nil? sects = sects[start_at..-1] if start_at > 0 sects = Section.new(nil, sects) unless sects.is_a?(Section) (opts) do sects.each do |s| self.section = s subsection_index = 0 value = render_section(section) do |*args| value = with_section do run(args.first, section, subsection_index, true, &block) end subsection_index += 1 value end out << (value || "") break if break_first end end out end
#sections(*args)
Sets the sections (and subsections) to be rendered for the template
#set_ivars (private)
[ GitHub ]# File 'lib/yard/templates/template.rb', line 393
def set_ivars .each do |k, v| instance_variable_set("@#{k}", v) end end
#superb(sect = section, &block) ⇒ String
Calls the ERB file from the last inherited template with #section.erb
# File 'lib/yard/templates/template.rb', line 330
def superb(sect = section, &block) filename = self.class.find_nth_file(erb_file_for(sect), 2) return "" unless filename method_name = ErbCache.method_for(filename) { erb_with(IO.read(filename), filename) } send(method_name, &block) end
T(*path) ⇒ Template
Loads a template specified by path. If :template
or :format
is
specified in the #options hash, they are prepended and appended
to the path respectively.
# File 'lib/yard/templates/template.rb', line 204
def T(*path) # rubocop:disable Style/MethodName path.unshift( .template) if .template path.push( .format) if .format self.class.T(*path) end
#with_section (private)
[ GitHub ]#yieldall(opts = nil, &block)
Yields all subsections with any extra options
# File 'lib/yard/templates/template.rb', line 278
def yieldall(opts = nil, &block) with_section { run(opts, section, &block) } end