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

Defined in:
lib/yard/templates/helpers/html_helper.rb

Overview

Monkey into YARD’s HtmlHelper module to add common methods we want available in HTML templates.

Instance Method Summary collapse

Instance Method Details

#htmlify_cucumber_line(text, markup = nil, strip: true) ⇒ String

Like #htmlify_cucumber_text but operates on single lines via #h for ‘:none`.

Parameters:

  • text (String)

    The text to HTML-ify.

  • markup (nil | #to_sym) (defaults to: nil)

    What markup to use; see #htmlify_cucumber_text for details.

  • strip (Boolean) (defaults to: true)

    Should ‘text` have String#strip called on it first when rendering markup?

Returns:

  • (String)

    HTML.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/yard/templates/helpers/html_helper.rb', line 131

def htmlify_cucumber_line(text, markup=nil, strip: true)
  # Resolve the `markup` {Symbol}
  markup = resolve_cucumber_markup markup
  
  # Switch off to the appropriate method.
  case markup
  when :none
    # Do what it was before: call {#h}
    h(text)
  when :default
    # Do whatever the rest of YARD is doing.
    htmlify_line(strip ? text.strip : text)
  else
    htmlify_line(strip ? text.strip : text, markup)
  end
end

#htmlify_cucumber_text(text, markup = nil, strip: true) ⇒ String

Wrapper for HTML-ifying text from Cucumber features and scenarios that takes into account the ‘yard-cucumber.markup` config value.

Parameters:

  • text (String)

    String to be converted to HTML markup.

  • markup (nil | #to_sym) (defaults to: nil)

    The markup with which to parse the text:

    1. ‘nil` consults the `yard-cucumber.markup` config setting, then proceeds with that value as if it was passed in.

      If the setting is missing or has a ‘nil` value, then `:none` is used to maintain the same behavior as before this method was introduced.

    2. ‘:none` uses #htmlify_with_newlines, which is what the call sites for this method used to use beforehand.

    3. ‘:default` uses #htmlify with no `markup` argument, causing it to use whatever markup is configured in the #options.

    4. Anything else is passed as the ‘markup` argument to #htmlify.

  • strip (Boolean) (defaults to: true)

    When ‘true`, String#strip will be called on `text` before HTML-ifying.

    Texts strings from features and scenarios seem to sometimes have at least leading whitespace that does not seem intended given the ‘.feature` file.

    This can trigger code-formatting on the text when processing with Markdown, and I might imagine cause weirdness given other markups as well.

    NOTE To preserve legacy behavior ‘text` is never stripped when `markup` is resolved to `:none`.

Returns:

  • (String)

    HTML markup.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/yard/templates/helpers/html_helper.rb', line 97

def htmlify_cucumber_text(text, markup=nil, strip: true)
  # Resolve the `markup` {Symbol}
  markup = resolve_cucumber_markup markup
  
  # Switch off to the appropriate method.
  case markup
  when :none
    # Do what it was before: call {#htmlify_with_newlines}, escapes entities 
    # and replaces "\n" with `<br/>` tags.
    htmlify_with_newlines(text)
  when :default
    # Do whatever the rest of YARD is doing.
    htmlify(strip ? text.strip : text)
  else
    htmlify(strip ? text.strip : text, markup)
  end
end

#htmlify_with_newlines(text) ⇒ String

Note:

2019.01.10 nrser This method used to be duplicated in ‘lib/templates/default/`…

  1. feature/html/setup.rb

  2. featuredirectory/html/setup.rb

but de-duplicated it to here after creating this file for #htmlify_cucumber_text.

Original HTML-ification method, that is now referred to as ‘:none` markup. Escapes HTML entities and replaces newlines with `<br/>`.

Parameters:

  • text (String)

    String to be converted to HTML markup.

Returns:

  • (String)

    HTML markup.

See Also:



32
33
34
# File 'lib/yard/templates/helpers/html_helper.rb', line 32

def htmlify_with_newlines(text)
  text.split("\n").collect {|c| h(c).gsub(/\s/,'&nbsp;') }.join("<br/>")
end