Class: NoraMark::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nora_mark/node.rb,
lib/nora_mark/parser.kpeg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



6
7
8
# File 'lib/nora_mark/node.rb', line 6

def attrs
  @attrs
end

#body_emptyObject

Returns the value of attribute body_empty.



6
7
8
# File 'lib/nora_mark/node.rb', line 6

def body_empty
  @body_empty
end

#classesObject

Returns the value of attribute classes.



6
7
8
# File 'lib/nora_mark/node.rb', line 6

def classes
  @classes
end

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/nora_mark/node.rb', line 6

def content
  @content
end

#first_childObject

Returns the value of attribute first_child.



7
8
9
# File 'lib/nora_mark/node.rb', line 7

def first_child
  @first_child
end

#holdersObject

Returns the value of attribute holders.



7
8
9
# File 'lib/nora_mark/node.rb', line 7

def holders
  @holders
end

#idsObject

Returns the value of attribute ids.



6
7
8
# File 'lib/nora_mark/node.rb', line 6

def ids
  @ids
end

#last_childObject

Returns the value of attribute last_child.



7
8
9
# File 'lib/nora_mark/node.rb', line 7

def last_child
  @last_child
end

#line_noObject

Returns the value of attribute line_no.



6
7
8
# File 'lib/nora_mark/node.rb', line 6

def line_no
  @line_no
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/nora_mark/node.rb', line 6

def name
  @name
end

#nextObject

Returns the value of attribute next.



7
8
9
# File 'lib/nora_mark/node.rb', line 7

def next
  @next
end

#no_tagObject

Returns the value of attribute no_tag.



6
7
8
# File 'lib/nora_mark/node.rb', line 6

def no_tag
  @no_tag
end

#parentObject

Returns the value of attribute parent.



7
8
9
# File 'lib/nora_mark/node.rb', line 7

def parent
  @parent
end

#prevObject

Returns the value of attribute prev.



7
8
9
# File 'lib/nora_mark/node.rb', line 7

def prev
  @prev
end

#raw_contentObject

Returns the value of attribute raw_content.



6
7
8
# File 'lib/nora_mark/node.rb', line 6

def raw_content
  @raw_content
end

#raw_textObject

Returns the value of attribute raw_text.



6
7
8
# File 'lib/nora_mark/node.rb', line 6

def raw_text
  @raw_text
end

Instance Method Details

#_find_node(raw_selector) ⇒ Object



295
296
297
298
299
300
301
302
303
# File 'lib/nora_mark/node.rb', line 295

def _find_node raw_selector
  return self if _match? raw_selector
  return nil unless @first_child

  return (@first_child.find { |n| n._match? raw_selector } ||
          @first_child.inject(nil) do |r, n|
            r or n._find_node raw_selector
          end)
end

#_match?(raw_selector) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/nora_mark/node.rb', line 48

def _match?(raw_selector)
  raw_selector.inject(true) { |result, s|
    result && s.call(self)
  }
end

#_remove_internalObject



152
153
154
155
156
157
# File 'lib/nora_mark/node.rb', line 152

def _remove_internal
  @parent.first_child = @next if !@parent.nil? && @parent.first_child == self
  @parent.last_child = @prev if !@parent.nil? && @parent.last_child == self
  @next.prev = @prev unless @next.nil?
  @prev.next = @next unless @prev.nil?
end

#add_attr(attr) ⇒ Object



32
33
34
# File 'lib/nora_mark/node.rb', line 32

def add_attr attr
  (@attrs ||= {}).merge! attr
end

#after(node) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/nora_mark/node.rb', line 177

def after(node)
  node.remove
  node.parent = @parent
  node.prev = self
  node.next = @next
  @next.prev = node unless @next.nil?
  @next = node
  if !@parent.nil? && @parent.last_child == self
    @parent.last_child = node
  end
  node.reparent
  @parent.children_replaced unless @parent.nil?
end

#all_nodesObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/nora_mark/node.rb', line 272

def all_nodes
  r = []
  if !@params.nil?
    @params.each do |node_array|
      r = node_array[0].inject([]) do |result, node|
        result << node
        result + node.all_nodes
      end
    end
  end
  if !@first_child.nil?
    r = @first_child.inject(r) do |result, node|
      result << node
      result + node.all_nodes
    end
  end
  r
end

#ancestors(selector = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/nora_mark/node.rb', line 82

def ancestors(selector = {})
  result = []
  raw_selector = build_selector selector
  node = parent
  while !node.nil?
    result << node if node._match? raw_selector
    node = node.parent
  end
  result
end

#append_child(node) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/nora_mark/node.rb', line 257

def append_child(node)
  node.remove
  node.reparent
  if self.children.size == 0
    @raw_content = [node]
    reparent
  else
    @last_child.next = node
    node.prev = @last_child
    node.parent = self
    @last_child = node
    children_replaced
  end
end

#before(node) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/nora_mark/node.rb', line 191

def before(node)
  node.remove
  node.parent = @parent
  node.next = self
  node.prev = @prev
  @prev.next = node unless @prev.nil?
  @prev = node
  if !@parent.nil? && @parent.first_child == self
    @parent.first_child = node
  end
  node.reparent
  @parent.children_replaced unless @parent.nil?
end

#build_selector(selector) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/nora_mark/node.rb', line 71

def build_selector(selector)
  original_selector = selector
  case selector
  when String
    selector = { name: original_selector }
  when Regexp
    selector = { proc: proc { |node| original_selector =~ node.name } }
  end
  selector.map { |k, v| modify_selector(k, v) }
end

#childrenObject



131
132
133
134
135
# File 'lib/nora_mark/node.rb', line 131

def children
  return [] if @first_child.nil?

  @children ||= rebuild_children
end

#children=(x) ⇒ Object



137
138
139
140
# File 'lib/nora_mark/node.rb', line 137

def children=(x)
  @raw_content = x.to_ary
  reparent
end

#children_empty?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/nora_mark/node.rb', line 93

def children_empty?
  children.nil? || children.size == 0 || children.reject { |x| x.nil? }.size == 0
end

#children_replacedObject



142
143
144
# File 'lib/nora_mark/node.rb', line 142

def children_replaced
  rebuild_children
end

#cloneObject



305
306
307
308
309
# File 'lib/nora_mark/node.rb', line 305

def clone
  @raw_content = nil
  all_nodes.each { |node| node.instance_eval { @raw_content = nil } }
  Marshal.restore Marshal.dump self
end

#eachObject



36
37
38
39
40
41
42
# File 'lib/nora_mark/node.rb', line 36

def each
  node = self
  while !node.nil?
    node, node_old = node.next, node
    yield node_old
  end
end

#find_node(selector) ⇒ Object



291
292
293
# File 'lib/nora_mark/node.rb', line 291

def find_node selector
  _find_node(build_selector selector)
end

#match?(selector) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/nora_mark/node.rb', line 44

def match?(selector)
  _match? build_selector(selector)
end

#modify_selector(k, v) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nora_mark/node.rb', line 54

def modify_selector(k, v)
  case k
  when :type
    proc { |node| node.kind_of? NoraMark.const_get(v) }
  when :name
    proc { |node| node.name ==  v }
  when :id
    proc { |node| (node.ids || []).contain? v }
  when :class
    proc { |node| (node.class || []).contain? v }
  when :proc
    v
  else
    raise 'no selector'
  end
end

#named_paramsObject Also known as: n



17
18
19
# File 'lib/nora_mark/node.rb', line 17

def named_params
  @named_params ||= {}
end

#named_params=(named_params) ⇒ Object



13
14
15
# File 'lib/nora_mark/node.rb', line 13

def named_params=(named_params)
  @named_params = named_params
end

#paramsObject Also known as: p



25
26
27
# File 'lib/nora_mark/node.rb', line 25

def params
  @params
end

#params=(params) ⇒ Object



21
22
23
# File 'lib/nora_mark/node.rb', line 21

def params=(params)
  @params = params.map { |param| NodeSet.new param }
end

#prepend_child(node) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/nora_mark/node.rb', line 242

def prepend_child(node)
  node.remove
  node.reparent
  if self.children.size == 0
    @raw_content = [node]
    reparent
  else
    @first_child.prev = node
    node.next = @first_child
    node.parent = self
    @first_child = node
    children_replaced
  end
end

#raw_text?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/nora_mark/node.rb', line 9

def raw_text?
  @raw_text
end

#rebuild_childrenObject



127
128
129
# File 'lib/nora_mark/node.rb', line 127

def rebuild_children
  @children = @first_child.nil? ? [] : NodeSet.new(@first_child.collect { |node| node })
end

#removeObject



159
160
161
162
163
164
# File 'lib/nora_mark/node.rb', line 159

def remove
  _remove_internal
  @parent.children_replaced unless @parent.nil?
  unlink
  self
end

#remove_followingObject



166
167
168
169
170
171
172
173
174
175
# File 'lib/nora_mark/node.rb', line 166

def remove_following
  parent = @parent
  r = self.map do |node|
    node._remove_internal
    node.unlink
    node
  end
  parent.children_replaced if parent
  r
end

#reparentObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nora_mark/node.rb', line 97

def reparent
  @params ||= []
  @params = @params.map do |node_array|
    node_array.inject(nil) do |prev, child_node|
      child_node.prev = prev
      prev.next = child_node if !prev.nil?
      child_node.parent = self
      child_node.reparent
      child_node
    end
    NodeSet.new node_array
  end

  return if @raw_content.nil? || raw_text

  @raw_content.each { |node| node.remove }
  @first_child = @raw_content.first
  @last_child = @raw_content.last
  @raw_content.inject(nil) do |prev, child_node|
    child_node.prev = prev
    prev.next = child_node if !prev.nil?
    child_node.parent = self
    child_node.reparent
    child_node
  end
  @raw_content = nil
  @children = nil
  rebuild_children
end

#replace(node) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/nora_mark/node.rb', line 205

def replace(node)
  node = [node] if !node.is_a? Array

  first_node = node.shift
  rest_nodes = node

  first_node.parent = @parent
  if !@parent.nil?
    @parent.first_child = first_node if (@parent.first_child == self)
    @parent.last_child = first_node if (@parent.last_child == self)
  end

  first_node.prev = @prev
  first_node.next = @next

  @prev.next = first_node unless @prev.nil?
  @next.prev = first_node unless @next.nil?

  first_node.reparent
  first_node.parent.children_replaced unless first_node.parent.nil?
  unlink
  rest_nodes.inject(first_node) do |prev, rest_node|
    prev.after rest_node
    rest_node
  end
end

#textObject



311
312
313
314
315
# File 'lib/nora_mark/node.rb', line 311

def text
  children.inject("") do |result, node|
    result << node.text
  end
end


146
147
148
149
150
# File 'lib/nora_mark/node.rb', line 146

def unlink
  @parent = nil
  @prev = nil
  @next = nil
end

#wrap(node, method = :prepend) ⇒ Object



232
233
234
235
236
237
238
239
240
# File 'lib/nora_mark/node.rb', line 232

def wrap(node, method = :prepend)
  replace(node)
  if (method == :prepend)
    node.prepend_child(self)
  else
    node.append_child(self)
  end
  node
end