Class: NoraMark::Html::TagWriter
- Inherits:
-
Object
- Object
- NoraMark::Html::TagWriter
show all
- Includes:
- Util, NodeUtil
- Defined in:
- lib/nora_mark/html/tag_writer.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from NodeUtil
#_node, #block, #inline, #text
Methods included from Util
#escape_html
Constructor Details
#initialize(tag_name, generator, **param) ⇒ TagWriter
Returns a new instance of TagWriter.
17
18
19
20
21
22
23
24
25
|
# File 'lib/nora_mark/html/tag_writer.rb', line 17
def initialize(tag_name, generator, **param)
@tag_name = tag_name
@generator = generator
@context = generator.context
@trailer = trailer
@node_preprocessors = []
@write_body_preprocessors = []
@param = param
end
|
Instance Attribute Details
#node_preprocessors ⇒ Object
Returns the value of attribute node_preprocessors.
6
7
8
|
# File 'lib/nora_mark/html/tag_writer.rb', line 6
def node_preprocessors
@node_preprocessors
end
|
#trailer ⇒ Object
Returns the value of attribute trailer.
6
7
8
|
# File 'lib/nora_mark/html/tag_writer.rb', line 6
def trailer
@trailer
end
|
#write_body_preprocessors ⇒ Object
Returns the value of attribute write_body_preprocessors.
6
7
8
|
# File 'lib/nora_mark/html/tag_writer.rb', line 6
def write_body_preprocessors
@write_body_preprocessors
end
|
Class Method Details
.create(tag_name, generator, node_preprocessor: nil, write_body_preprocessor: nil, trailer: "\n", chop_last_space: false) {|instance| ... } ⇒ Object
8
9
10
11
12
13
14
15
|
# File 'lib/nora_mark/html/tag_writer.rb', line 8
def self.create(tag_name, generator, node_preprocessor: nil, write_body_preprocessor: nil, trailer: "\n", chop_last_space: false)
instance = TagWriter.new(tag_name, generator, chop_last_space: chop_last_space)
instance.node_preprocessors << node_preprocessor unless node_preprocessor.nil?
instance.write_body_preprocessors << write_body_preprocessor unless write_body_preprocessor.nil?
instance.trailer = trailer
yield instance if block_given?
instance
end
|
Instance Method Details
#add_class(node, cls) ⇒ Object
51
52
53
|
# File 'lib/nora_mark/html/tag_writer.rb', line 51
def add_class(node, cls)
(node.classes ||= []) << cls
end
|
#attr_string(attrs) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/nora_mark/html/tag_writer.rb', line 27
def attr_string(attrs)
return '' if attrs.nil?
attrs.map do |name, vals|
if vals.nil?
''
elsif !vals.is_a? Array
" #{name}='#{escape_html(name)}'"
elsif vals.size == 0
''
else
" #{name}='#{escape_html(vals.join(' '))}'"
end
end.join('')
end
|
#class_string(cls_array) ⇒ Object
43
44
45
|
# File 'lib/nora_mark/html/tag_writer.rb', line 43
def class_string(cls_array)
attr_string({ class: cls_array })
end
|
#ids_string(ids_array) ⇒ Object
47
48
49
|
# File 'lib/nora_mark/html/tag_writer.rb', line 47
def ids_string(ids_array)
attr_string({ id: ids_array })
end
|
#output(string) ⇒ Object
75
76
77
|
# File 'lib/nora_mark/html/tag_writer.rb', line 75
def output(string)
@context << string
end
|
#tag_end(node) ⇒ Object
79
80
81
82
83
84
|
# File 'lib/nora_mark/html/tag_writer.rb', line 79
def tag_end(node)
return if node.no_tag
tag_name = @tag_name || node.name
@context << "</#{tag_name}>#{@trailer}"
end
|
#tag_start(node) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/nora_mark/html/tag_writer.rb', line 55
def tag_start(node)
return if node.no_tag
ids = node.ids || []
classes = node.classes || []
attr = node.attrs || {}
node.n.each { |k, v|
if k.to_s.start_with? 'data-'
attr.merge!({ k => [v] })
end
}
tag_name = @tag_name || node.name
@context << "<#{tag_name}#{ids_string(ids)}#{class_string(classes)}#{attr_string(attr)}"
if node.body_empty
@context << " />"
else
@context << ">"
end
end
|
#write(node) ⇒ Object
86
87
88
89
90
91
92
93
|
# File 'lib/nora_mark/html/tag_writer.rb', line 86
def write(node)
@node_preprocessors.each { |x| node = instance_exec node.dup, &x }
@context.enable_pgroup, saved_ep = !(node.params.map(&:text).include?('wo-pgroup') || !@context.enable_pgroup), @context.enable_pgroup
tag_start node
write_body node unless node.body_empty
tag_end node unless node.body_empty
@context.enable_pgroup = saved_ep
end
|
#write_body(node) ⇒ Object
95
96
97
98
99
100
101
|
# File 'lib/nora_mark/html/tag_writer.rb', line 95
def write_body(node)
@write_body_preprocessors.each { |x|
return if instance_exec(node, &x) == :done
}
write_children node
@generator.context.chop_last_space if node.n[:chop_last_space]
end
|
#write_children(node) ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/nora_mark/html/tag_writer.rb', line 103
def write_children(node)
if node.raw_text?
output escape_html(node.content.join "\n")
else
write_nodeset(node.children)
end
end
|
#write_nodeset(nodeset) ⇒ Object
111
112
113
114
115
116
|
# File 'lib/nora_mark/html/tag_writer.rb', line 111
def write_nodeset(nodeset)
return if nodeset.nil? || nodeset.size == 0
nodeset.each { |x| @generator.to_html x }
@generator.context.chop_last_space if (@param[:chop_last_space])
end
|