Module: YARD::Templates::Helpers::BaseHelper
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/yard/templates/helpers/base_helper.rb |
Overview
The base helper module included in all templates.
Managing Global Template State
-
#globals ⇒ OpenStruct
An object that keeps track of global state throughout the entire template rendering process (including any sub-templates).
Running the Verifier
-
#run_verifier(list) ⇒ Array<CodeObjects::Base>
Runs a list of objects against the
::YARD::Verifier
object passed into the template and returns the subset of verified objects.
Escaping Text
-
#h(text)
Escapes text.
Linking Objects and URLs
-
#link_file(filename, title = nil, anchor = nil) ⇒ String
Links to an extra file.
-
#link_include_file(file) ⇒ String
Include a file as a docstring in output.
-
#link_include_object(obj) ⇒ String
Includes an object's docstring into output.
-
#link_object(obj, title = nil) ⇒ String
Links to an object with an optional title.
-
#link_url(url, title = nil, params = nil) ⇒ String
Links to a URL.
-
#linkify(*args)
Links objects or URLs.
Formatting Object Attributes
- #format_object_title(object) ⇒ String
- #format_object_type(object) ⇒ String
-
#format_source(value) ⇒ String
Indents and formats source code.
-
#format_types(list, brackets = true) ⇒ String
Formats a list of return types for output and links each type.
Instance Attribute Summary
- #object rw
- #owner ⇒ CodeObjects::Base readonly
- #serializer rw
Instance Attribute Details
#object (rw)
[ GitHub ]# File 'lib/yard/templates/helpers/base_helper.rb', line 5
attr_accessor :object, :serializer
#owner ⇒ CodeObjects::Base (readonly)
# File 'lib/yard/templates/helpers/base_helper.rb', line 11
attr_reader :owner
#serializer (rw)
[ GitHub ]# File 'lib/yard/templates/helpers/base_helper.rb', line 5
attr_accessor :object, :serializer
Instance Method Details
#format_object_title(object) ⇒ String
# File 'lib/yard/templates/helpers/base_helper.rb', line 196
def format_object_title(object) case object when YARD::CodeObjects::RootObject "Top Level Namespace" else format_object_type(object) + ": " + object.title end end
#format_object_type(object) ⇒ String
# File 'lib/yard/templates/helpers/base_helper.rb', line 182
def format_object_type(object) case object when YARD::CodeObjects::ClassObject object.is_exception? ? "Exception" : "Class" else object.type.to_s.capitalize end end
#format_source(value) ⇒ String
Indents and formats source code
# File 'lib/yard/templates/helpers/base_helper.rb', line 209
def format_source(value) sp = value.split("\n").last[/^(\s+)/, 1] num = sp ? sp.size : 0 value.gsub(/^\s{#{num}}/, '') end
#format_types(list, brackets = true) ⇒ String
Formats a list of return types for output and links each type.
# File 'lib/yard/templates/helpers/base_helper.rb', line 168
def format_types(list, brackets = true) list.nil? || list.empty? ? "" : (brackets ? "(#{list.join(", ")})" : list.join(", ")) end
#globals ⇒ OpenStruct
An object that keeps track of global state throughout the entire template rendering process (including any sub-templates).
# File 'lib/yard/templates/helpers/base_helper.rb', line 20
def globals; .globals end
#h(text)
Escapes text. This is used a lot by the HtmlHelper
and there should
be some helper to "clean up" text for whatever, this is it.
# File 'lib/yard/templates/helpers/base_helper.rb', line 38
def h(text) text end
#link_file(filename, title = nil, anchor = nil) ⇒ String
Links to an extra file
# File 'lib/yard/templates/helpers/base_helper.rb', line 152
def link_file(filename, title = nil, anchor = nil) # rubocop:disable Lint/UnusedMethodArgument return filename.filename if CodeObjects::ExtraFileObject === filename filename end
#link_include_file(file) ⇒ String
Include a file as a docstring in output
# File 'lib/yard/templates/helpers/base_helper.rb', line 113
def link_include_file(file) File.read(file) end
#link_include_object(obj) ⇒ String
Includes an object's docstring into output.
# File 'lib/yard/templates/helpers/base_helper.rb', line 105
def link_include_object(obj) obj.docstring end
#link_object(obj, title = nil) ⇒ String
Links to an object with an optional title
# File 'lib/yard/templates/helpers/base_helper.rb', line 122
def link_object(obj, title = nil) return title if title case obj when YARD::CodeObjects::Base, YARD::CodeObjects::Proxy obj.title when String, Symbol P(obj).title else obj end end
#link_url(url, title = nil, params = nil) ⇒ String
Links to a URL
# File 'lib/yard/templates/helpers/base_helper.rb', line 141
def link_url(url, title = nil, params = nil) # rubocop:disable Lint/UnusedMethodArgument url end
#linkify(*args)
Links objects or URLs. This method will delegate to the correct link_
method depending on the arguments passed in.
# File 'lib/yard/templates/helpers/base_helper.rb', line 55
def linkify(*args) if args.first.is_a?(String) case args.first when %r{://}, /^mailto:/ link_url(args[0], args[1], {:target => '_parent'}.merge(args[2] || {})) when /^include:file:(\S+)/ file = $1 relpath = File.relative_path(Dir.pwd, File. (file)) if relpath =~ /^\.\./ log.warn "Cannot include file from path `#{file}'" "" elsif File.file?(file) link_include_file(file) else log.warn "Cannot find file at `#{file}' for inclusion" "" end when /^include:(\S+)/ path = $1 obj = YARD::Registry.resolve(object.namespace, path) if obj link_include_object(obj) else log.warn "Cannot find object at `#{path}' for inclusion" "" end when /^render:(\S+)/ path = $1 obj = YARD::Registry.resolve(object, path) if obj opts = .dup opts.delete(:serializer) obj.format(opts) else '' end when /^file:(\S+?)(?:#(\S+))?$/ link_file($1, args[1] ? args[1] : nil, $2) else link_object(*args) end else link_object(*args) end end
#run_verifier(list) ⇒ Array<CodeObjects::Base>
Runs a list of objects against the ::YARD::Verifier
object passed into the
template and returns the subset of verified objects.
# File 'lib/yard/templates/helpers/base_helper.rb', line 30
def run_verifier(list) .verifier ? .verifier.run(list) : list end