123456789_123456789_123456789_123456789_123456789_

Module: YARD::Templates::Helpers::HtmlHelper

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Defined in: lib/yard/templates/helpers/html_helper.rb

Overview

The helper module for HTML templates.

Constant Summary

MarkupHelper - Included

MARKUP_EXTENSIONS, MARKUP_FILE_SHEBANG, MARKUP_PROVIDERS

Escaping Template Data

Converting Markup to HTML

Syntax Highlighting Source Code

Linking Objects and URLs

URL Helpers

Formatting Objects and Attributes

Getting the Character Encoding

Instance Method Summary

HtmlSyntaxHighlightHelper - Included

ModuleHelper - Included

#prune_method_listing

Prunes the method listing by running the verifier and removing attributes/aliases.

MarkupHelper - Included

#load_markup_provider

Attempts to load the first valid markup provider in MARKUP_PROVIDERS.

#markup_class

Gets the markup provider class/module constant for a markup type Call #load_markup_provider before using this method.

#markup_file_contents

Strips any shebang lines on the file contents that pertain to markup or preprocessing data.

#markup_for_file

Checks for a shebang or looks at the file extension to determine the markup type for the file contents.

#markup_provider

Gets the markup provider name for a markup type Call #load_markup_provider before using this method.

Class Method Details

.urlencode(text) ⇒ String (mod_func)

Escapes a URL

Parameters:

Returns:

  • (String)

    the escaped URL

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 31

def urlencode(text)
  text = text.dup
  enc = nil
  if text.respond_to?(:force_encoding)
    enc = text.encoding
    text = text.force_encoding('binary')
  end

  text = text.gsub(/%[a-z0-9]{2}|#{URLMATCH}/i) do
    $&.size > 1 ? $& : "%" + $&.ord.to_s(16).upcase
  end.tr(' ', '+')

  text = text.force_encoding(enc) if enc
  text
end

Instance Method Details

#anchor_for(object) ⇒ String

Parameters:

Returns:

  • (String)

    the anchor for a specific object

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 347

def anchor_for(object)
  case object
  when CodeObjects::MethodObject
    "#{object.name}-#{object.scope}_#{object.type}"
  when CodeObjects::ClassVariableObject
    "#{object.name.to_s.gsub('@@', '')}-#{object.type}"
  when CodeObjects::Base
    "#{object.name}-#{object.type}"
  when CodeObjects::Proxy
    object.path
  else
    object.to_s
  end
end

#charsetString

Returns the current character set. The default value can be overridden by setting the LANG environment variable or by overriding this method. In Ruby 1.9 you can also modify this value by setting Encoding.default_external.

Returns:

  • (String)

    the current character set

Since:

  • 0.5.4

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 574

def charset
  has_encoding = defined?(::Encoding)
  if defined?(@file) && @file && has_encoding
    lang = @file.contents.encoding.to_s
  else
    return 'utf-8' unless has_encoding || ENV['LANG']
    lang =
      if has_encoding
        ::Encoding.default_external.name.downcase
      else
        ENV['LANG'].downcase.split('.').last
      end
  end

  case lang
  when "ascii-8bit", "us-ascii", "ascii-7bit"; 'iso-8859-1'
  when "utf8"; 'utf-8'
  else; lang
  end
end

#convert_method_to_overload(meth) (private)

Converts a ::YARD::CodeObjects::MethodObject into an overload object

Since:

  • 0.5.3

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 609

def convert_method_to_overload(meth)
  # use first overload tag if it has a return type and method itself does not
  if !meth.tag(:return) && meth.tags(:overload).size == 1 && meth.tag(:overload).tag(:return)
    return meth.tag(:overload)
  end
  meth
end

#detect_lang_in_codeblock_attributes(pre_html_attrs, code_html_attrs) ⇒ String? (private)

Parses code block's HTML attributes in order to detect the programming language of what's enclosed in that code block.

Parameters:

  • pre_html_attrs (String, nil)

    HTML attribute list of pre element

  • code_html_attrs (String, nil)

    HTML attribute list of code element

Returns:

  • (String, nil)

    detected programming language

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 664

def detect_lang_in_codeblock_attributes(pre_html_attrs, code_html_attrs)
  detected = nil
  detected ||= (/\bdata-lang="(.+?)"/ =~ code_html_attrs && $1)
  detected ||= (/\blang="(.+?)"/ =~ pre_html_attrs && $1)
  detected ||= (/\bclass="(.+?)"/ =~ code_html_attrs && $1)
  detected
end

#format_object_name_list(objects) ⇒ String

Formats a list of objects and links them

Returns:

  • (String)

    a formatted list of objects

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 458

def format_object_name_list(objects)
  objects.sort_by {|o| o.name.to_s.downcase }.map do |o|
    "<span class='name'>" + linkify(o, o.name) + "</span>"
  end.join(", ")
end

#format_types(typelist, brackets = true) ⇒ String

Formats a list of types from a tag.

Parameters:

  • typelist (Array<String>, FalseClass)

    the list of types to be formatted.

  • brackets (Boolean) (defaults to: true)

    omits the surrounding brackets if brackets is set to false.

Returns:

  • (String)

    the list of types formatted as [Type1, Type2, ...] with the types linked to their respective descriptions.

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 476

def format_types(typelist, brackets = true)
  return unless typelist.is_a?(Array)
  list = typelist.map do |type|
    type = type.gsub(/([<>])/) { h($1) }
    type = type.gsub(/([\w:]+)/) { $1 == "lt" || $1 == "gt" ? $1 : linkify($1, $1) }
    "<tt>" + type + "</tt>"
  end
  list.empty? ? "" : (brackets ? "(#{list.join(", ")})" : list.join(", "))
end

#h(text) ⇒ String

Escapes HTML entities

Parameters:

  • text (String)

    the text to escape

Returns:

  • (String)

    the HTML with escaped entities

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 23

def h(text)
  CGI.escapeHTML(text.to_s)
end

#html_markup_asciidoc(text) ⇒ String

Converts Asciidoc to HTML

Parameters:

  • text (String)

    input Asciidoc text

Returns:

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 109

def html_markup_asciidoc(text)
  options = {:attributes => ASCIIDOC_ATTRIBUTES}
  markup_class(:asciidoc).convert(text, options)
end

#html_markup_html(text) ⇒ String

Converts HTML to HTML

Parameters:

  • text (String)

    input html

Returns:

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 168

def html_markup_html(text)
  text
end

#html_markup_markdown(text) ⇒ String

Converts Markdown to HTML

Parameters:

  • text (String)

    input Markdown text

Returns:

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 78

def html_markup_markdown(text)
  # TODO: other libraries might be more complex
  provider = markup_class(:markdown)
  case provider.to_s
  when  'RDiscount'
    provider.new(text, :autolink).to_html
  when 'RedcarpetCompat'
    provider.new(text, :autolink,
                       :fenced_code,
                       :gh_blockcode,
                       :lax_spacing,
                       :tables,
                       :with_toc_data,
                       :no_intraemphasis).to_html
  when 'CommonMarker'
    CommonMarker.render_html(text, %i[DEFAULT GITHUB_PRE_LANG], %i[autolink table])
  else
    provider.new(text).to_html
  end
end

#html_markup_none(text) ⇒ String

Returns:

  • (String)

    the same text with no markup

Since:

  • 0.6.6

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 160

def html_markup_none(text)
  h(text)
end

#html_markup_org(text) ⇒ String

Converts org-mode to HTML

Parameters:

  • text (String)

    input org-mode text

Returns:

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 102

def html_markup_org(text)
  markup_class(:org).new(text).to_html
end

#html_markup_pre(text) ⇒ String

Converts plaintext to pre-formatted HTML

Parameters:

  • text (String)

    the input text

Returns:

  • (String)

    the output HTML

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 146

def html_markup_pre(text)
  "<pre>" + h(text) + "</pre>"
end

#html_markup_rdoc(text) ⇒ String

Converts RDoc formatting (SimpleMarkup) to HTML

Parameters:

  • text (String)

    the input RDoc formatted text

Returns:

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 136

def html_markup_rdoc(text)
  doc = markup_class(:rdoc).new(text)
  doc.from_path = url_for(object) if doc.respond_to?(:from_path=)
  doc.to_html
end

#html_markup_ruby(source) ⇒ String

Highlights Ruby source. Similar to #html_syntax_highlight, but this method is meant to be called from #htmlify when markup is set to "ruby".

Parameters:

  • source (String)

    the Ruby source

Returns:

  • (String)

    the highlighted HTML

Since:

  • 0.7.0

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 179

def html_markup_ruby(source)
  '<pre class="code ruby">' + html_syntax_highlight(source, :ruby) + '</pre>'
end

#html_markup_text(text) ⇒ String

Converts plaintext to regular HTML

Parameters:

  • text (String)

    the input text

Returns:

  • (String)

    the output HTML

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 154

def html_markup_text(text)
  h(text).gsub(/\r?\n/, '<br/>')
end

#html_markup_textile(text) ⇒ String

Converts Textile to HTML

Parameters:

  • text (String)

    the input Textile text

Returns:

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 118

def html_markup_textile(text)
  doc = markup_class(:textile).new(text)
  doc.hard_breaks = false if doc.respond_to?(:hard_breaks=)
  doc.to_html
end

#html_markup_textile_strict(text) ⇒ String

Converts plaintext to strict Textile (hard breaks)

Parameters:

  • text (String)

    the input textile data

Returns:

  • (String)

    the output HTML

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 128

def html_markup_textile_strict(text)
  markup_class(:textile).new(text).to_html
end

#html_syntax_highlight(source, type = nil) ⇒ String

Note:

To support a specific language type, implement the method html_syntax_highlight_TYPE in this class.

Syntax highlights source in language type.

Parameters:

  • source (String)

    the source code to highlight

  • type (Symbol, String) (defaults to: nil)

    the language type (:ruby, :plain, etc). Use :plain for no syntax highlighting.

Returns:

  • (String)

    the highlighted source

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 199

def html_syntax_highlight(source, type = nil)
  return "" unless source
  return h(source) unless options.highlight

  new_type, source = parse_lang_for_codeblock(source)
  type ||= new_type || :ruby
  meth = "html_syntax_highlight_#{type}"
  respond_to?(meth) ? send(meth, source) : h(source)
end

#html_syntax_highlight_plain(source) ⇒ String

Returns:

  • (String)

    unhighlighted source

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 210

def html_syntax_highlight_plain(source)
  h(source)
end

#htmlify(text, markup = options.markup) ⇒ String

Turns text into HTML using markup style formatting.

Parameters:

  • text (String)

    the text to format

  • markup (Symbol) (defaults to: options.markup)

    examples are :markdown, :textile, :rdoc. To add a custom markup type, see MarkupHelper

Returns:

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 57

def htmlify(text, markup = options.markup)
  markup_meth = "html_markup_#{markup}"
  return text unless respond_to?(markup_meth)
  return "" unless text
  return text unless markup
  html = send(markup_meth, text).dup
  if html.respond_to?(:encode)
    html = html.force_encoding(text.encoding) # for libs that mess with encoding
    html = html.encode(:invalid => :replace, :replace => '?')
  end
  html = resolve_links(html)
  unless [:text, :none, :pre, :ruby].include?(markup)
    html = parse_codeblocks(html)
  end
  html
end

#htmlify_line(*args) ⇒ String

Returns:

  • (String)

    HTMLified text as a single line (paragraphs removed)

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 184

def htmlify_line(*args)
  "<div class='inline'>" + htmlify(*args) + "</div>"
end

#insert_include(text, markup = options.markup)

Inserts an include link while respecting inlining

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 296

def insert_include(text, markup = options.markup)
  htmlify(text, markup).gsub(%r{\A\s*<p>|</p>\s*\Z}, '')
end

#mtime(_file)

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 400

def mtime(_file) nil end

#mtime_url(obj, anchor = nil, relative = true)

Alias for #url_for.

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 399

alias mtime_url url_for

#parse_codeblocks(html) ⇒ String (private)

Parses code blocks out of html and performs syntax highlighting on code inside of the blocks.

Parameters:

  • html (String)

    the html to search for code in

Returns:

  • (String)

    highlighted html

See Also:

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 640

def parse_codeblocks(html)
  html.gsub(%r{<pre((?:\s+\w+="(?:.+?)")*)\s*>(?:\s*<code((?:\s+\w+="(?:.+?)")*)\s*>)?(.+?)(?:</code>\s*)?</pre>}m) do
    string = $3

    # handle !!!LANG prefix to send to html_syntax_highlight_LANG
    language, = parse_lang_for_codeblock(string)
    language ||= detect_lang_in_codeblock_attributes($1, $2)
    language ||= object.source_type

    if options.highlight
      string = html_syntax_highlight(CGI.unescapeHTML(string), language)
    end
    classes = ['code', language].compact.join(' ')
    %(<pre class="#{classes}"><code class="#{language}">#{string}</code></pre>)
  end
end

#parse_lang_for_codeblock(source) ⇒ Array(String, String) (private)

Parses !!!lang out of codeblock, returning the codeblock language followed by the source code.

Parameters:

  • source (String)

    the source code whose language to determine

Returns:

Since:

  • 0.7.5

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 624

def parse_lang_for_codeblock(source)
  type = nil
  if source =~ /\A(?:[ \t]*\r?\n)?[ \t]*!!!([\w.+-]+)[ \t]*\r?\n/
    type = $1
    source = $'
  end

  [type, source]
end

#signature(meth, link = true, show_extras = true, full_attr_name = true) ⇒ String

Formats the signature of method meth.

Parameters:

  • meth (CodeObjects::MethodObject)

    the method object to list the signature of

  • link (Boolean) (defaults to: true)

    whether to link the method signature to the details view

  • show_extras (Boolean) (defaults to: true)

    whether to show extra meta-data (visibility, attribute info)

  • full_attr_name (Boolean) (defaults to: true)

    whether to show the full attribute name ("name=" instead of "name")

Returns:

  • (String)

    the formatted method signature

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 529

def signature(meth, link = true, show_extras = true, full_attr_name = true)
  meth = convert_method_to_overload(meth)

  type = signature_types(meth, link)
  type = "&#x21d2; #{type}" if type && !type.empty?
  scope = meth.scope == :class ? "." : "#"
  name = full_attr_name ? meth.name : meth.name.to_s.gsub(/^(\w+)=$/, '\1')
  blk = format_block(meth)
  args = !full_attr_name && meth.writer? ? "" : format_args(meth)
  extras = []
  extras_text = ''
  if show_extras
    rw = meth.attr_info
    if rw
      attname = [rw[:read] ? 'read' : nil, rw[:write] ? 'write' : nil].compact
      attname = attname.size == 1 ? attname.join('') + 'only' : nil
      extras << attname if attname
    end
    extras << meth.visibility if meth.visibility != :public
    extras_text = ' <span class="extras">(' + extras.join(", ") + ')</span>' unless extras.empty?
  end
  title = "%s<strong>%s</strong>%s %s %s" % [scope, h(name), args, blk, type]
  if link
    if meth.is_a?(YARD::CodeObjects::MethodObject)
      link_title = "#{h meth.name(true)} (#{meth.scope} #{meth.type})"
    else
      link_title = "#{h name} (#{meth.type})"
    end
    obj = meth.respond_to?(:object) ? meth.object : meth
    url = url_for(object, obj)
    link_url(url, title, :title => link_title) + extras_text
  else
    title + extras_text
  end
end

#signature_types(meth, link = true) ⇒ String

Get the return types for a method signature.

Parameters:

Returns:

  • (String)

    the signature types

Since:

  • 0.5.3

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 492

def signature_types(meth, link = true)
  meth = convert_method_to_overload(meth)
  if meth.respond_to?(:object) && !meth.has_tag?(:return)
    meth = meth.object
  end

  type = options.default_return || ""
  if meth.tag(:return) && meth.tag(:return).types
    types = meth.tags(:return).map {|t| t.types ? t.types : [] }.flatten.uniq
    first = link ? h(types.first) : format_types([types.first], false)
    if types.size == 2 && types.last == 'nil'
      type = first + '<sup>?</sup>'
    elsif types.size == 2 && types.last =~ /^(Array)?<#{Regexp.quote types.first}>$/
      type = first + '<sup>+</sup>'
    elsif types.size > 2
      type = [first, '...'].join(', ')
    elsif types == ['void'] && options.hide_void_return
      type = ""
    else
      type = link ? h(types.join(", ")) : format_types(types, false)
    end
  elsif !type.empty?
    type = link ? h(type) : format_types([type], false)
  end
  type = "#{type} " unless type.empty?
  type
end

#tag_attrs(opts = {}) ⇒ String (private)

Converts a set of hash options into HTML attributes for a tag

Parameters:

Returns:

  • (String)

    the tag attributes of an HTML tag

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 603

def tag_attrs(opts = {})
  opts.sort_by {|k, _v| k.to_s }.map {|k, v| "#{k}=#{v.to_s.inspect}" if v }.join(" ")
end

#url_for(obj, anchor = nil, relative = true) ⇒ String Also known as: #mtime_url

Returns the URL for an object.

Parameters:

  • obj (String, CodeObjects::Base)

    the object (or object path) to link to

  • anchor (String) (defaults to: nil)

    the anchor to link to

  • relative (Boolean) (defaults to: true)

    use a relative or absolute link

Returns:

  • (String)

    the URL location of the object

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 368

def url_for(obj, anchor = nil, relative = true)
  link = nil
  return link unless serializer
  return link if obj.is_a?(CodeObjects::Base) && run_verifier([obj]).empty?

  if obj.is_a?(CodeObjects::Base) && !obj.is_a?(CodeObjects::NamespaceObject)
    # If the obj is not a namespace obj make it the anchor.
    anchor = obj
    obj = obj.namespace
  end

  objpath = serializer.serialized_path(obj)
  return link unless objpath

  relative = false if object == Registry.root
  if relative
    fromobj = object
    if object.is_a?(CodeObjects::Base) &&
       !object.is_a?(CodeObjects::NamespaceObject)
      fromobj = owner
    end

    from = serializer.serialized_path(fromobj)
    link = File.relative_path(from, objpath)
  else
    link = objpath
  end

  link + (anchor ? '#' + urlencode(anchor_for(anchor)) : '')
end

#url_for_file(filename, anchor = nil) ⇒ String

Returns the URL for a specific file

Parameters:

Returns:

  • (String)

    the URL pointing to the file

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 407

def url_for_file(filename, anchor = nil)
  return '' unless serializer
  fromobj = object
  if CodeObjects::Base === fromobj && !fromobj.is_a?(CodeObjects::NamespaceObject)
    fromobj = fromobj.namespace
  end
  from = serializer.serialized_path(fromobj)
  path = filename == options.readme ?
    'index.html' : serializer.serialized_path(filename)
  link = File.relative_path(from, path)
  link += (anchor ? '#' + urlencode(anchor) : '')
  link
end

#url_for_framesetString

Returns the URL for the frameset page

Returns:

  • (String)

    the URL pointing to the frames page

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 434

def url_for_frameset
  url_for_file("frames.html")
end

#url_for_indexString

Returns the URL for the alphabetic index page

Returns:

  • (String)

    the URL pointing to the first main page the user should see.

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 450

def url_for_index
  url_for_file("_index.html")
end

#url_for_list(type) ⇒ String

Returns the URL for a list type

Parameters:

  • type (String, Symbol)

    the list type to generate a URL for

Returns:

  • (String)

    the URL pointing to the list

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 426

def url_for_list(type)
  url_for_file("#{type}_list.html")
end

#url_for_mainString

Returns the URL for the main page (README or alphabetic index)

Returns:

  • (String)

    the URL pointing to the first main page the user should see.

[ GitHub ]

  
# File 'lib/yard/templates/helpers/html_helper.rb', line 442

def url_for_main
  url_for_file("index.html")
end