Class: Coradoc::Html::Converters::Example
- Inherits:
-
Base
- Object
- Base
- Coradoc::Html::Converters::Example
show all
- Defined in:
- lib/coradoc/html/converters/example.rb
Overview
Converter for CoreModel::Block (example) to HTML <div class=“example”>
Class Method Summary
collapse
Methods inherited from Base
build_class_attribute, build_element, build_html_attributes, convert_content_to_html, convert_element_to_core, convert_node_to_core, escape_attribute, escape_html, extract_model_attributes, extract_node_attributes, extract_text_fallback, find_converter_class_by_name, find_converter_for_model, handle_unknown_content, render_core_abbreviation, render_core_annotation_block, render_core_bibliography, render_core_bibliography_entry, render_core_block, render_core_block_image, render_core_definition_item, render_core_definition_list, render_core_footnote, render_core_footnote_reference, render_core_inline_element, render_core_inline_image, render_core_list_block, render_core_list_item, render_core_span, render_core_structural_element, render_core_table_cell, render_core_table_row, render_core_term, render_core_toc, render_core_toc_entry, resolve_block_semantic_type, resolve_format_specific_semantic, transform_to_coremodel, treat_children
Class Method Details
.build_attributes(example) ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/coradoc/html/converters/example.rb', line 57
def self.build_attributes(example)
attrs = [%( class="example")]
attrs << %( id="#{escape_attribute(example.id)}") if example.id
attrs.join
end
|
.build_title(example) ⇒ Object
66
67
68
69
70
71
72
73
|
# File 'lib/coradoc/html/converters/example.rb', line 66
def self.build_title(example)
return nil unless example.title
title_text = example.title.to_s
return nil if title_text.empty?
%(<div class="example-title">#{escape_html(title_text)}</div>)
end
|
.convert_item(item) ⇒ Object
87
88
89
90
91
92
93
94
|
# File 'lib/coradoc/html/converters/example.rb', line 87
def self.convert_item(item)
case item
when String
"<p>#{escape_html(item)}</p>"
else
convert_content_to_html(item)
end
end
|
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/coradoc/html/converters/example.rb', line 96
def self.(nodes)
nodes.map do |node|
if node.text? && !node.text.strip.empty?
node.text.strip
elsif node.element?
case node.name
when 'p'
Paragraph.to_coradoc(node)
else
node.text.strip
end
end
end.compact
end
|
.process_content(content) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/coradoc/html/converters/example.rb', line 75
def self.process_content(content)
return '' if content.nil?
if content.is_a?(Array)
content.map { |item| convert_item(item) }.join("\n")
elsif content.is_a?(String)
"<p>#{escape_html(content)}</p>"
else
convert_item(content)
end
end
|
.to_coradoc(element, _options = {}) ⇒ Object
Convert HTML <div class=“example”> to CoreModel::Block (example)
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/coradoc/html/converters/example.rb', line 30
def self.to_coradoc(element, _options = {})
return nil unless element.name == 'div'
return nil unless element['class']&.include?('example')
title_elem = element.at_css('.example-title')
title = title_elem&.text&.strip
content_nodes = if title_elem
element.children.reject { |node| node == title_elem }
else
element.children
end
content = (content_nodes)
id = element['id']
Coradoc::CoreModel::ExampleBlock.new(
content: content,
title: title,
id: id
)
end
|
.to_html(example, _options = {}) ⇒ Object
Convert CoreModel::Block (example) to HTML <div class=“example”>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/coradoc/html/converters/example.rb', line 9
def self.to_html(example, _options = {})
return '' unless example
attrs = build_attributes(example)
title_html = build_title(example)
content = process_content(example.content)
example_html = ''
example_html += "#{title_html}\n" if title_html
example_html += content
%(<div#{attrs}>\n#{example_html}\n</div>)
end
|