paru 0.4.3 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b56730c82e1a52321e635423e8bf5dcd5679cc4902a44c6bdcb9ea5f7eb02644
4
- data.tar.gz: 3368084001dd7f8a3d16b93c6b57a2a671762b8c853f90116a4ff8575b3562c5
3
+ metadata.gz: dc1a2d8d47cd75c3bc4d34a4fbbbecf8eca9d475e8e3f2aa1260f02e39f8885f
4
+ data.tar.gz: df6b6ea223c9e48067ddd342a51715fb6fbab95d08e94c5f26f263b789dd63fe
5
5
  SHA512:
6
- metadata.gz: ddaeba8c423ca4c44284b448019161f6a287d11e3bd721b006e2c499ac096d9901be16f43511a9a6a337a3ad7b79a3cb727ff4e4a1d25f43c3e141201ef6c6ea
7
- data.tar.gz: f20c5628b950ac5f4f5188a71ec714b5662b1340ac331d6bc073f78247f1df42edcde89f58823642a9dc75bb358499e202fc78842f3761d362ba8e4cd1e5c094
6
+ metadata.gz: ceaa544a0e8963d59c70700f3208bac827a2c2274dd4be86673ccfc26977c59914c8b8d69521d8b2629ec745bda85c316a55ac47d5b0e69fa177d88e4fef2c47
7
+ data.tar.gz: 9113b9aaae1d0f4fe3e4188a76556984542b8281154faba088dd38a97630d3a66031d8d0b49b029069bd719178b355be6e240bcdd7bedfa3ad754083143668bf
@@ -61,6 +61,9 @@ module Paru
61
61
  ]
62
62
  end
63
63
 
64
+ # Create an AST representation of this Node
65
+ #
66
+ # @return [Hash]
64
67
  def to_ast()
65
68
  ast_contents()
66
69
  end
@@ -64,6 +64,9 @@ module Paru
64
64
  ]
65
65
  end
66
66
 
67
+ # Create an AST representation of this Node
68
+ #
69
+ # @return [Hash]
67
70
  def to_ast()
68
71
  ast_contents()
69
72
  end
@@ -30,7 +30,10 @@ module Paru
30
30
  def initialize(contents)
31
31
  super []
32
32
  contents.each do |item|
33
- @children.push DefinitionListItem.new item
33
+ child = DefinitionListItem.new item
34
+ child.parent = self
35
+
36
+ @children.push child
34
37
  end
35
38
  end
36
39
 
@@ -37,8 +37,15 @@ module Paru
37
37
  #
38
38
  # @param item [Array] the [term, definition]
39
39
  def initialize(item)
40
+ super []
41
+
40
42
  @term = Block.new item[0]
43
+ @term.parent = self
44
+ @children << @term
45
+
41
46
  @definition = List.new item[1]
47
+ @definition.parent = self
48
+ @children << @definition
42
49
  end
43
50
 
44
51
  # Create an AST representation of this DefinitionListItem
@@ -31,6 +31,9 @@ module Paru
31
31
  @value = value
32
32
  end
33
33
 
34
+ # Create an AST representation of this Node
35
+ #
36
+ # @return [Hash]
34
37
  def to_ast()
35
38
  @value
36
39
  end
@@ -31,7 +31,10 @@ module Paru
31
31
  def initialize(contents, node_class = Block)
32
32
  super []
33
33
  contents.each do |item|
34
- @children.push node_class.new item
34
+ child = node_class.new(item)
35
+ child.parent = self
36
+
37
+ @children.push child
35
38
  end
36
39
  end
37
40
 
@@ -61,6 +61,9 @@ module Paru
61
61
  ]
62
62
  end
63
63
 
64
+ # Create an AST representation of this Node
65
+ #
66
+ # @return [Hash]
64
67
  def to_ast()
65
68
  ast_contents()
66
69
  end
@@ -72,6 +72,9 @@ module Paru
72
72
  ]
73
73
  end
74
74
 
75
+ # Create an AST representation of this Node
76
+ #
77
+ # @return [Hash]
75
78
  def to_ast()
76
79
  ast_contents()
77
80
  end
@@ -58,6 +58,9 @@ module Paru
58
58
  ]
59
59
  end
60
60
 
61
+ # Create an AST representation of this Node
62
+ #
63
+ # @return [Hash]
61
64
  def to_ast()
62
65
  ast_contents()
63
66
  end
data/lib/paru/filter.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017, 2022 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -284,6 +284,11 @@ module Paru
284
284
  end
285
285
  end
286
286
 
287
+ @ran_before = false
288
+ @ran_after = false
289
+ instance_eval(&block) # run filter with before block
290
+ @ran_before = true
291
+
287
292
  @current_node = @document
288
293
 
289
294
  nodes_to_filter.each do |node|
@@ -299,6 +304,9 @@ module Paru
299
304
  instance_eval(&block) # run the actual filter code
300
305
  end
301
306
 
307
+ @ran_after = true
308
+ instance_eval(&block) # run filter with after block
309
+
302
310
  write_document
303
311
  end
304
312
 
@@ -308,8 +316,26 @@ module Paru
308
316
  # @param selector [String] a selector string
309
317
  # @yield [Node] the current node if it matches the selector
310
318
  def with(selector)
311
- @selectors[selector] = Selector.new selector unless @selectors.has_key? selector
312
- yield @current_node if @selectors[selector].matches? @current_node, @filtered_nodes
319
+ if @ran_before and !@ran_after
320
+ @selectors[selector] = Selector.new selector unless @selectors.has_key? selector
321
+ yield @current_node if @selectors[selector].matches? @current_node, @filtered_nodes
322
+ end
323
+ end
324
+
325
+ # Before running the filter on all nodes, the +document+ is passed to
326
+ # the block to this +before+ method. This method is run exactly once.
327
+ #
328
+ # @yield [Document] the document
329
+ def before()
330
+ yield @document unless @ran_before
331
+ end
332
+
333
+ # After running the filter on all nodes, the +document+ is passed to
334
+ # the block to this +after+ method. This method is run exactly once.
335
+ #
336
+ # @yield [Document] the document
337
+ def after()
338
+ yield @document if @ran_after
313
339
  end
314
340
 
315
341
  # Stop processing the document any further and output it as it is now.
@@ -324,7 +350,6 @@ module Paru
324
350
  exit true
325
351
  end
326
352
 
327
-
328
353
  private
329
354
 
330
355
  # The Document node from JSON formatted pandoc document structure
data/lib/paru/info.rb ADDED
@@ -0,0 +1,90 @@
1
+ #--
2
+ # Copyright 2022 Huub de Beer <Huub@heerdebeer.org>
3
+ #
4
+ # This file is part of Paru
5
+ #
6
+ # Paru is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Paru is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ require_relative "error.rb"
20
+
21
+ module Paru
22
+ # Information about pandoc
23
+ #
24
+ # @!attribute version
25
+ # @return [Array<Integer>] Pandoc's version, like [2, 18, 1]
26
+ #
27
+ # @!attribute data_dir
28
+ # @return [String] Pandoc's default data directory
29
+ #
30
+ # @!attribute scripting_engine
31
+ # @return [String] Pandoc's internal scripting engine, like "Lua 5.4"
32
+ class Info
33
+ attr_reader :version, :data_dir, :scripting_engine
34
+
35
+ # Create a new Info object
36
+ #
37
+ # @param path [String] the path to pandoc. Defaults to 'pandoc', i.e.,
38
+ # assumes it's on the environment's path.
39
+ def initialize(path = "pandoc")
40
+ begin
41
+ # Get pandoc's version information
42
+ version_string = ''
43
+ IO.popen("#{path} --version", 'r+') do |p|
44
+ p.close_write
45
+ version_string << p.read
46
+ end
47
+
48
+ # Extract the version as an array of integers, like SemVer.
49
+ @version = version_string
50
+ .match(/pandoc.* (\d+\.\d+.*)$/)[1]
51
+ .split(".")
52
+ .map {|s| s.to_i}
53
+
54
+ # Extract the data directory
55
+ @data_dir = version_string.match(/User data directory: (.+)$/)[1]
56
+
57
+ # Extract scripting engine
58
+ @scripting_engine = version_string.match(/Scripting engine: (.+)$/)[1]
59
+ rescue StandardError => err
60
+ warn "Error extracting pandoc's information: #{err.message}"
61
+ warn "Using made up values instead."
62
+
63
+ @version = @version || [2, 18]
64
+ @data_dir = @data_dir || "."
65
+ @scripting_engine = @scripting_engine || "Lua 5.4"
66
+ end
67
+ end
68
+
69
+ # Get pandoc's info by key like a Hash for backwards compatability.
70
+ #
71
+ # @deprecated Use Info's getters instead.
72
+ #
73
+ # @param key [String|Symbol] the key for the information to look up.
74
+ # Info only supports keys 'version' and 'data_dir'.
75
+ # @return [Any] Information associated with the key.
76
+ # @raise [Error] for an unknown key.
77
+ def [](key)
78
+ case key
79
+ when "verion", :version
80
+ version
81
+ when "data_dir", :data_dir
82
+ data_dir
83
+ when "scripting_engine", :scripting_engine
84
+ scripting_engine
85
+ else
86
+ throw Error.new "Info does not know key '#{key}'"
87
+ end
88
+ end
89
+ end
90
+ end
data/lib/paru/pandoc.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017, 2022 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -21,6 +21,7 @@ require "shellwords"
21
21
  require "yaml"
22
22
 
23
23
  require_relative "error.rb"
24
+ require_relative "info.rb"
24
25
 
25
26
  module Paru
26
27
  # Pandoc is a wrapper around the pandoc document converter. See
@@ -92,8 +93,7 @@ module Paru
92
93
  # directory. This method is typically used in scripts that use Paru to
93
94
  # automate the use of pandoc.
94
95
  #
95
- # @return [Hash{:version => Array<Integer>, :data_dir => String}] Pandoc's
96
- # version, such as "[2.10.1]" and the data directory, such as "/home/huub/.pandoc".
96
+ # @return [Info] Pandoc's version, such as "[2.10.1]" and the data directory, such as "/home/huub/.pandoc".
97
97
  def self.info()
98
98
  @@info
99
99
  end
@@ -216,43 +216,7 @@ module Paru
216
216
  "pandoc"
217
217
  end
218
218
 
219
- begin
220
- version_string = ''
221
- IO.popen("#{@@pandoc_exec} --version", 'r+') do |p|
222
- p.close_write
223
- version_string << p.read
224
- end
225
- rescue StandardError => err
226
- throw Error.new "Unable to run pandoc via command '#{@@pandoc_exec} --version': #{err.message}"
227
- end
228
-
229
- version = version_string
230
- .match(/pandoc.* (\d+\.\d+.*)$/)[1]
231
- .split(".")
232
- .map {|s| s.to_i}
233
- major_version = version[0]
234
-
235
- # Pandoc version 2.7 introduced a new default data dir to comply
236
- # with XDG Base Directory Specification
237
-
238
- xdg_data_dir, old_data_dir = version_string.match(/User data directory: (.+)$/)[1].split(" or ")
239
-
240
- if File.directory? xdg_data_dir then
241
- data_dir = xdg_data_dir
242
- elsif not old_data_dir.nil? and File.directory? old_data_dir then
243
- # The new-style data directory does not exist, but the
244
- # old-style does, so use the old-style data directory for
245
- # backwards compatibility
246
- data_dir = old_data_dir
247
- else
248
- # Neither data directory exists, default to the new default
249
- data_dir = xdg_data_dir
250
- end
251
-
252
- @@info = {
253
- :version => version,
254
- :data_dir => data_dir
255
- }
219
+ @@info = Info.new(@@pandoc_exec)
256
220
 
257
221
  # For each pandoc command line option a method is defined as follows:
258
222
  OPTIONS = YAML.load_file File.join(__dir__, "pandoc_options.yaml")
@@ -58,13 +58,14 @@ tab_stop: 4
58
58
  track_changes: "accept"
59
59
  extract_media: ""
60
60
  abbreviations: ""
61
+ trace: true
61
62
  #####
62
63
  # General writing options:
63
64
  #####
64
- sandbox: false
65
65
  standalone: true
66
66
  template: ""
67
67
  variable: [""]
68
+ sandbox: true
68
69
  print_default_template: ""
69
70
  print_default_data_file: ""
70
71
  eol: "native"
@@ -84,7 +85,7 @@ include_before_body: [""]
84
85
  include_after_body: [""]
85
86
  resource_path: ""
86
87
  request_header: ""
87
- no_check_certificate: false
88
+ no_check_certificate: true
88
89
  #####
89
90
  # Options affecting specific writers
90
91
  #####
@@ -94,7 +95,7 @@ ascii: true
94
95
  reference_links: true
95
96
  reference_location: "block"
96
97
  markdown_headings: "atx"
97
- atx_headers: false
98
+ atx_headers: true
98
99
  top_level_division: "section"
99
100
  number_sections: true
100
101
  number_offset: 2
@@ -137,4 +138,3 @@ gladtex: true
137
138
  #####
138
139
  dump_args: true
139
140
  ignore_args: true
140
- trace: true
data/lib/paru/selector.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017, 2022 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -32,6 +32,12 @@ module Paru
32
32
  # that selector expression or not.
33
33
  class Selector
34
34
 
35
+ # Pseudo selector to select any inline and block node
36
+ ANY_SELECTOR = "*"
37
+
38
+ # All pseudo selectors
39
+ PSEUDO_SELECTORS = [ANY_SELECTOR]
40
+
35
41
  # Create a new Selector based on the selector string
36
42
  #
37
43
  # @param selector [String] the selector string
@@ -51,9 +57,14 @@ module Paru
51
57
  # @return [Boolean] True if the node in the context of the
52
58
  # filtered_nodes is selected by this Selector
53
59
  def matches? node, filtered_nodes
54
- node.type == @type and
55
- @classes.all? {|c| node.has_class? c } and
56
- @relations.all? {|r| r.matches? node, filtered_nodes}
60
+ case @type
61
+ when ANY_SELECTOR
62
+ Paru::PANDOC_TYPES.include? node.type
63
+ else
64
+ node.type == @type and
65
+ @classes.all? {|c| node.has_class? c } and
66
+ @relations.all? {|r| r.matches? node, filtered_nodes}
67
+ end
57
68
  end
58
69
 
59
70
  private
@@ -61,7 +72,7 @@ module Paru
61
72
  S = /\s*/
62
73
  # Improved CSS class selector taken from https://stackoverflow.com/questions/448981/which-characters-are-valid-in-css-class-names-selectors/449000#449000
63
74
  CLASS = /(\.-?[_a-zA-Z]+[_a-zA-Z0-9-]*)/
64
- TYPE = /(?<type>(?<name>[A-Z][a-zA-Z]*)(?<classes>#{CLASS}*))/
75
+ TYPE = /(?<type>(?<name>[A-Z][a-zA-Z]*|\*)(?<classes>#{CLASS}*))/
65
76
  OTHER_TYPE = /(?<other_type>(?<other_name>[A-Z][a-zA-Z]*)(?<other_classes>#{CLASS}*))/
66
77
  OPERATOR = /(?<operator>\+|-|>)/
67
78
  DISTANCE = /(?<distance>[1-9][0-9]*)/
@@ -87,7 +98,7 @@ module Paru
87
98
 
88
99
  # Is type actually a Pandoc AST node type?
89
100
  def is_pandoc_type(type)
90
- Paru::PANDOC_TYPES.include? type
101
+ Paru::PANDOC_TYPES.concat(PSEUDO_SELECTORS).include? type
91
102
  end
92
103
 
93
104
  def expect(parts, part)
@@ -176,8 +187,10 @@ module Paru
176
187
 
177
188
  def is_descendant?(node)
178
189
  distance = 0
190
+ parent = nil
179
191
  begin
180
192
  distance += 1 if @distance > 0
193
+ node = parent unless parent.nil?
181
194
  parent = node.parent
182
195
  ancestry = parent.type == @type and @classes.all? {|c| parent.has_class? c}
183
196
  end while not ancestry and not parent.is_root? and distance <= @distance
data/lib/paru.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016, 2017, 2018, 2019, 2020 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017, 2018, 2019, 2020, 2022 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -17,6 +17,6 @@
17
17
  # Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
  module Paru
20
- # Paru's current version
21
- VERSION = [0, 4, 3]
20
+ # Paru's current version
21
+ VERSION = [1, 0, 2].freeze
22
22
  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.4.3
4
+ version: 1.0.2
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: 2021-11-20 00:00:00.000000000 Z
11
+ date: 2022-06-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Control pandoc with Ruby and write pandoc filters in Ruby
14
14
  email: Huub@heerdebeer.org
@@ -93,6 +93,7 @@ files:
93
93
  - lib/paru/filter/value.rb
94
94
  - lib/paru/filter/version.rb
95
95
  - lib/paru/filter_error.rb
96
+ - lib/paru/info.rb
96
97
  - lib/paru/pandoc.rb
97
98
  - lib/paru/pandoc2yaml.rb
98
99
  - lib/paru/pandoc_options.yaml
@@ -109,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
110
  requirements:
110
111
  - - ">="
111
112
  - !ruby/object:Gem::Version
112
- version: 2.6.8
113
+ version: 2.7.6
113
114
  required_rubygems_version: !ruby/object:Gem::Requirement
114
115
  requirements:
115
116
  - - ">="