module YARD::Templates::Helpers::HtmlHelper

The helper module for HTML templates.

Constants

URLMATCH

@private

Public Class Methods

urlencode(text) click to toggle source

Escapes a URL

@param [String] text the URL @return [String] the escaped URL

# File lib/yard/templates/helpers/html_helper.rb, line 27
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

Public Instance Methods

h(text) click to toggle source

Escapes HTML entities

@param [String] text the text to escape @return [String] the HTML with escaped entities

# File lib/yard/templates/helpers/html_helper.rb, line 19
def h(text)
  CGI.escapeHTML(text.to_s)
end
html_markup_asciidoc(text) click to toggle source

Converts Asciidoc to HTML @param [String] text input Asciidoc text @return [String] output HTML

# File lib/yard/templates/helpers/html_helper.rb, line 91
def html_markup_asciidoc(text)
  markup_class(:asciidoc).render(text)
end
html_markup_html(text) click to toggle source

Converts HTML to HTML @param [String] text input html @return [String] output HTML @since 0.6.0

# File lib/yard/templates/helpers/html_helper.rb, line 149
def html_markup_html(text)
  text
end
html_markup_markdown(text) click to toggle source

Converts Markdown to HTML @param [String] text input Markdown text @return [String] output HTML @since 0.6.0

# File lib/yard/templates/helpers/html_helper.rb, line 74
def html_markup_markdown(text)
  # TODO: other libraries might be more complex
  provider = markup_class(:markdown)
  if provider.to_s == 'RDiscount'
    provider.new(text, :autolink).to_html
  elsif provider.to_s == 'RedcarpetCompat'
    provider.new(text, :no_intraemphasis, :gh_blockcode,
                       :fenced_code, :autolink, :tables,
                       :lax_spacing).to_html
  else
    provider.new(text).to_html
  end
end
html_markup_none(text) click to toggle source

@return [String] the same text with no markup @since 0.6.6

# File lib/yard/templates/helpers/html_helper.rb, line 141
def html_markup_none(text)
  h(text)
end
html_markup_pre(text) click to toggle source

Converts plaintext to pre-formatted HTML @param [String] text the input text @return [String] the output HTML @since 0.6.0

# File lib/yard/templates/helpers/html_helper.rb, line 127
def html_markup_pre(text)
  "<pre>" + h(text) + "</pre>"
end
html_markup_rdoc(text) click to toggle source

Converts RDoc formatting (SimpleMarkup) to HTML @param [String] text the input RDoc formatted text @return [String] output HTML @since 0.6.0

# File lib/yard/templates/helpers/html_helper.rb, line 117
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) click to toggle source

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

@param [String] source the Ruby source @return [String] the highlighted HTML @since 0.7.0

# File lib/yard/templates/helpers/html_helper.rb, line 160
def html_markup_ruby(source)
  '<pre class="code ruby">' + html_syntax_highlight(source, :ruby) + '</pre>'
end
html_markup_text(text) click to toggle source

Converts plaintext to regular HTML @param [String] text the input text @return [String] the output HTML @since 0.6.0

# File lib/yard/templates/helpers/html_helper.rb, line 135
def html_markup_text(text)
  h(text).gsub(/\r?\n/, '<br/>')
end
html_markup_textile(text) click to toggle source

Converts Textile to HTML @param [String] text the input Textile text @return [String] output HTML @since 0.6.0

# File lib/yard/templates/helpers/html_helper.rb, line 99
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) click to toggle source

Converts plaintext to strict Textile (hard breaks) @param [String] text the input textile data @return [String] the output HTML @since 0.6.0

# File lib/yard/templates/helpers/html_helper.rb, line 109
def html_markup_textile_strict(text)
  markup_class(:textile).new(text).to_html
end
html_syntax_highlight(source, type = nil) click to toggle source

Syntax highlights source in language type.

@note To support a specific language type, implement the method

+html_syntax_highlight_TYPE+ in this class.

@param [String] source the source code to highlight @param [Symbol, String] type the language type (:ruby, :plain, etc). Use

:plain for no syntax highlighting.

@return [String] the highlighted source

# File lib/yard/templates/helpers/html_helper.rb, line 180
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) click to toggle source

@return [String] unhighlighted source

# File lib/yard/templates/helpers/html_helper.rb, line 191
def html_syntax_highlight_plain(source)
  h(source)
end
htmlify(text, markup = options.markup) click to toggle source

Turns text into HTML using markup style formatting.

@param [String] text the text to format @param [Symbol] markup examples are :markdown, :textile, :rdoc.

To add a custom markup type, see {MarkupHelper}

@return [String] the HTML

# File lib/yard/templates/helpers/html_helper.rb, line 53
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].include?(markup)
    html = parse_codeblocks(html)
  end
  html
end
htmlify_line(*args) click to toggle source

@return [String] HTMLified text as a single line (paragraphs removed)

# File lib/yard/templates/helpers/html_helper.rb, line 165
def htmlify_line(*args)
  "<div class='inline'>" + htmlify(*args) + "</div>"
end

Private Instance Methods

urlencode(text) click to toggle source

Escapes a URL

@param [String] text the URL @return [String] the escaped URL

# File lib/yard/templates/helpers/html_helper.rb, line 27
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