Class: RSpecDocumentation::HtmlElement

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_documentation/html_element.rb

Overview

Receives a spec manifest and generates a ‘Kramdown::Document` contaning an HTML node with a tabbed view displaying the example’s code, its output and, if format is ‘:html`, the rendered HTML of the output. Injected into the parsed `Kramdown::Document` for the root Markdown file, replacing the code block it was produced from.

Instance Method Summary collapse

Constructor Details

#initialize(spec:) ⇒ HtmlElement

Returns a new instance of HtmlElement.



9
10
11
# File 'lib/rspec_documentation/html_element.rb', line 9

def initialize(spec:)
  @spec = spec
end

Instance Method Details

#code_sourceObject



17
18
19
20
21
# File 'lib/rspec_documentation/html_element.rb', line 17

def code_source
  formatter = Rouge::Formatters::HTML.new
  lexer = Rouge::Lexers::Ruby.new
  Formatters.with_translated_html_entities(formatter.format(lexer.lex(spec.source)))
end

#elementObject



13
14
15
# File 'lib/rspec_documentation/html_element.rb', line 13

def element
  Kramdown::Document.new(tabbed_spec, input: 'html').root
end

#element_idObject



37
38
39
# File 'lib/rspec_documentation/html_element.rb', line 37

def element_id
  @element_id ||= SecureRandom.uuid
end

#prettified_outputObject



23
24
25
# File 'lib/rspec_documentation/html_element.rb', line 23

def prettified_output
  Formatters.with_translated_html_entities(formatter.prettified_output)
end

#render_raw?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/rspec_documentation/html_element.rb', line 33

def render_raw?
  formatter.render_raw?
end

#rendered_outputObject



27
28
29
30
31
# File 'lib/rspec_documentation/html_element.rb', line 27

def rendered_output
  return formatter.rendered_output if render_raw?

  Formatters.with_translated_html_entities(formatter.rendered_output)
end