module YARD::Templates::Helpers::HtmlHelper
The helper module for HTML templates.
Constants
- URLMATCH
@private
Public Class Methods
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
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
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
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
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
@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
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
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
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
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
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
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
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
@return [String] unhighlighted source
# File lib/yard/templates/helpers/html_helper.rb, line 191 def html_syntax_highlight_plain(source) h(source) end
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
@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
Resolves any text in the form of +{Name}+ to the object specified by Name. Also supports link titles in the form +{Name title}+.
@example Linking to an instance method
resolve_links("{MyClass#method}") # => "<a href='...'>MyClass#method</a>"
@example Linking to a class with a title
resolve_links("{A::B::C the C class}") # => "<a href='...'>the c class</a>"
@param [String] text the text to resolve links in @return [String] HTML with linkified references
# File lib/yard/templates/helpers/html_helper.rb, line 206 def resolve_links(text) code_tags = 0 text.gsub(%r{<(/)?(pre|code|tt)|(\|!)?\{(?!\})(\S+?)(?:\s([^\}]*?\S))?\}(?=[\W<]|.+</|$)}m) do |str| closed = $1 tag = $2 escape = $3 name = $4 title = $5 match = $& if tag code_tags += (closed ? -1 : 1) next str end next str unless code_tags == 0 next(match[1..-1]) if escape next(match) if name[0, 1] == '|' if name == '<a' && title =~ %r{href=["'](.+?)["'].*>.*</a>\s*(.*)\Z} name = $1 title = $2 title = nil if title.empty? end
Private Instance Methods
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