paru 0.2.5f → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d36825678ac43efb4419d37a42033055a161fe8
4
- data.tar.gz: c639c47e276492541231e67cc3a683b4b68cc3eb
3
+ metadata.gz: 185b75188642b10c09282a90bfc79cf3668881f7
4
+ data.tar.gz: a6ad0e6180196916b569e92c855bb6d0f5bca2c7
5
5
  SHA512:
6
- metadata.gz: 6384ccf86bd98e102fe068f6ceee9221a54758396f96e1f3787b16d8474530d6f683df255f845de565ffd98f80cea11a6df8b11155eccd758471758845291558
7
- data.tar.gz: 2220576d8c4c53fd8dfe0dd9bf5855ed82f55064ca9c87d3810dafdd418f15c7280ac54ef6b407015096319e9cb3aa00c783af5b8ccd9b90ed14df4a2c6dc150
6
+ metadata.gz: 6e6140070d0709f90fbd9c23ef86c01685fe1e2f7421203cb8c732f00ece343fe8bdf0c87eee5d4d302697c54153fa37321d2e515b6a077c2354fe6dfb365d30
7
+ data.tar.gz: 19606ebcbbf9bed59c9c2cbaa694f56d9b2998dd07c8d1b6067f5715d5fde07e3c145fbad059c3458ffc757e7193acfa97e50e08fd915a028ba671fbb874898e
@@ -18,5 +18,5 @@
18
18
  #++
19
19
  module Paru
20
20
  # Paru's current version
21
- VERSION = [0, 2, 4, 9]
21
+ VERSION = [0, 2, 5]
22
22
  end
@@ -199,9 +199,20 @@ module Paru
199
199
  # Booleans, Arrays, and Hashes. You can manipulate it like any other Ruby
200
200
  # Hash.
201
201
  #
202
+ # @!attribute metadata
203
+ # @return [Hash] The metadata of the document being filtered as a Ruby
204
+ # Hash
205
+ #
206
+ # @!attribute document
207
+ # @return [Document] The document being filtered
208
+ #
209
+ # @!attribute current_node
210
+ # @return [Node] The node in the AST of the document being filtered that
211
+ # is currently being inspected by the filter.
212
+ #
202
213
  class Filter
203
214
 
204
- attr_reader :metadata, :document
215
+ attr_reader :metadata, :document, :current_node
205
216
 
206
217
  # Create a new Filter instance. For convenience, {run} creates a new
207
218
  # {Filter} and runs it immediately. Use this constructor if you want
@@ -261,26 +272,38 @@ module Paru
261
272
 
262
273
  @metadata = PandocFilter::Metadata.new @document.meta
263
274
 
264
- @running = true
265
- @document.each_depth_first do |node|
266
- @filtered_nodes.push node
267
- instance_eval(&block)
268
- break unless @running
275
+ nodes_to_filter = Enumerator.new do |node_list|
276
+ @document.each_depth_first do |node|
277
+ node_list << node
278
+ end
279
+ end
280
+
281
+ @running = true # used to be able to stop filtering immediately
282
+ @current_node = @document
283
+
284
+ while @running do
285
+ begin
286
+ if @current_node.has_been_replaced?
287
+ @current_node = @current_node.get_replacement
288
+ @filtered_nodes.pop
289
+ else
290
+ @current_node = nodes_to_filter.next
291
+ end
292
+
293
+ @filtered_nodes.push @current_node
294
+
295
+ instance_eval(&block) # run the actual filter code
296
+ rescue StopIteration
297
+ @running = false
298
+ end
269
299
  end
300
+
270
301
  @running = false
271
302
 
272
303
  @document.meta = @metadata.to_meta
273
304
  @output.write @document.to_JSON
274
305
  end
275
306
 
276
- # +current_node+ points to the node that is *now* being processed while
277
- # running this filter.
278
- #
279
- # @return [Node] the node that is currently being processed
280
- def current_node()
281
- @filtered_nodes.last
282
- end
283
-
284
307
  # Specify what nodes to filter with a +selector+. If the +current_node+
285
308
  # matches that selector, it is passed to the block to this +with+ method.
286
309
  #
@@ -288,7 +311,7 @@ module Paru
288
311
  # @yield [Node] the current node if it matches the selector
289
312
  def with(selector)
290
313
  @selectors[selector] = Selector.new selector unless @selectors.has_key? selector
291
- yield current_node if @selectors[selector].matches? current_node, @filtered_nodes
314
+ yield @current_node if @selectors[selector].matches? @current_node, @filtered_nodes
292
315
  end
293
316
 
294
317
  # Stop processing the document any further and output it as it is now.
@@ -32,6 +32,16 @@ module Paru
32
32
  @children.find_index child
33
33
  end
34
34
 
35
+
36
+ # Get the child node at index
37
+ #
38
+ # @param index [Number] the index of the child to get
39
+ #
40
+ # @return [Node] the child at index
41
+ def get(index)
42
+ @children[index]
43
+ end
44
+
35
45
  # Insert child node among this node's children at position index.
36
46
  #
37
47
  # @param index [Integer] the position to insert the child
@@ -95,10 +105,17 @@ module Paru
95
105
  # @param block [Proc] the block to apply to each node in this node
96
106
  # tree
97
107
  #
98
- # @yield node
99
- def each_depth_first(&block)
108
+ # @yield [Node]
109
+ def each_depth_first(&block)
100
110
  yield self
101
- each {|child| child.each_depth_first(&block)} if has_children?
111
+
112
+ if has_been_replaced?
113
+ node = get_replacement
114
+ else
115
+ node = self
116
+ end
117
+
118
+ node.each {|child| child.each_depth_first(&block)} if node.has_children?
102
119
  end
103
120
 
104
121
  end
@@ -383,23 +383,43 @@ module Paru
383
383
  temp_doc = temp_doc.children.first
384
384
 
385
385
  if not temp_doc.children.all? {|node| node.is_inline?}
386
- raise Error.new "Cannot replace the inline level node represented by '#{outer_markdown}' with markdown that converts to block level nodes: '#{markdown}'."
386
+ raise Error.new "Cannot replace the inline level node represented by '#{self.markdown}' with markdown that converts to block level nodes: '#{markdown}'."
387
387
  end
388
-
388
+ else
389
+ replacement = temp_doc.children.first
390
+ @replacement = replacement unless replacement.nil? or to_ast == replacement.to_ast
389
391
  end
390
-
392
+
391
393
  index = current_index
392
394
  temp_doc.each do |child|
393
395
  index += 1
394
396
  parent.insert index, child
395
397
  end
398
+
399
+
396
400
  # Remove the original node
397
401
  parent.remove_at current_index
398
402
  end
399
-
400
403
  end
401
404
 
402
405
  alias outer_markdown= markdown=
406
+
407
+ # Has this node been replaced by using the {markdown} method? If
408
+ # so, return true.
409
+ #
410
+ # @return [Boolean]
411
+ def has_been_replaced?
412
+ not @replacement.nil?
413
+ end
414
+
415
+ # Get this node's replacemnt. Nil if it has not been replaced by
416
+ # the {markdown} method
417
+ #
418
+ # @return [Node] This node's replacement or nil if there is none.
419
+ def get_replacement
420
+ @replacement
421
+ end
422
+
403
423
  end
404
424
  end
405
425
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5f
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Huub de Beer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-11 00:00:00.000000000 Z
11
+ date: 2017-06-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Use Pandoc (http://www.pandoc.org) with ruby
14
14
  email: Huub@heerdebeer.org
@@ -101,12 +101,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
101
  version: '0'
102
102
  required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  requirements:
104
- - - ">"
104
+ - - ">="
105
105
  - !ruby/object:Gem::Version
106
- version: 1.3.1
106
+ version: '0'
107
107
  requirements: []
108
108
  rubyforge_project:
109
- rubygems_version: 2.5.2
109
+ rubygems_version: 2.6.11
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: Paru is a ruby wrapper around pandoc