paru 0.4.2.4 → 1.0.0

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: 90995074101bc9a2a14e72b9e32e319ab836cf545568927f8f554939e8a7f50e
4
- data.tar.gz: 2f2d91f0e45c27cae4df32267a0a98511ccfbcf28cec29f1ed0b9318de9ee5c2
3
+ metadata.gz: d31c824836527183bdaa118455039dcb428dfaf24b48fda1415ddc09d39cb1c0
4
+ data.tar.gz: 7ce8dbcb28c40c56821352df0168952535e14682d744a039ab8e281cde529aeb
5
5
  SHA512:
6
- metadata.gz: 02ec89f731646faf0e0731b0fc903a5ffa6145a75b24ba01a45d13670b33709e23ee86ba571abcf813f0d9201ccdb4fccf40cc619c393832770c72c7b67b35cf
7
- data.tar.gz: 4e168c11e2209cee4d108db0be97f0c24e5dcc7a59da1fe5643654b7879ad43eb33929ac33dbe1cfd10b19cde9a196b812009495b0700c0df5b7656e09b9525e
6
+ metadata.gz: 9866df62f18f6f929546dcd6b697b4ed8dfc6cc9c2a3a4f9d2f0623a2f79e4fff524aaf4558c185e24527987474a7e737b4ce288662e2169a6e417f7bd931de5
7
+ data.tar.gz: 8e18948789ae07df6ee6ee0d97c33fef28db223a5dc3e37872dc443baede147ed06858c2c980b71b367b8d64da3e770b3bd608ccfefd393972a6c5937d08b998
@@ -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
@@ -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
@@ -49,7 +49,7 @@ module Paru
49
49
  if yaml_string.empty?
50
50
  contents = {}
51
51
  else
52
- contents = YAML.load yaml_string, permitted_classes: [Date]
52
+ contents = YAML.safe_load yaml_string, permitted_classes: [Date]
53
53
  end
54
54
 
55
55
  if not contents
@@ -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,63 +216,20 @@ 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
- }
256
-
257
- # Load the options for the appropriate major version of pandoc
258
- if not [1, 2].include? major_version
259
- throw Error.new "Unknown major pandoc version: '#{major_version}'. Expected the major version to be '1' or '2'. Please check the pandoc path: '#{@@pandoc_exec}'."
260
- # defaults to version 1
261
- major_version = 2
262
- end
219
+ @@info = Info.new(@@pandoc_exec)
263
220
 
264
221
  # For each pandoc command line option a method is defined as follows:
265
- OPTIONS = YAML.load_file File.join(__dir__, "pandoc_options_version_#{major_version}.yaml")
222
+ OPTIONS = YAML.load_file File.join(__dir__, "pandoc_options.yaml")
266
223
 
267
- OPTIONS.keys.each do |option|
224
+ OPTIONS.each_pair do |option, default|
268
225
  if OPTIONS[option].is_a? Array then
269
226
 
270
227
  # option can be set multiple times, for example adding multiple css
271
228
  # files
272
229
 
273
- default = OPTIONS[option][0]
274
-
275
230
  define_method(option) do |value = default|
231
+ value = [] if value.nil?
232
+
276
233
  if @options[option].nil? then
277
234
  @options[option] = []
278
235
  end
@@ -291,6 +248,8 @@ module Paru
291
248
 
292
249
  default = OPTIONS[option]
293
250
  define_method(option) do |value = default|
251
+ value = default if value.nil?
252
+
294
253
  @options[option] = value
295
254
  self
296
255
  end
@@ -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)
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, 2, 4]
20
+ # Paru's current version
21
+ VERSION = [1, 0, 0].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.2.4
4
+ version: 1.0.0
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-04 00:00:00.000000000 Z
11
+ date: 2022-05-29 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,10 +93,10 @@ 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
- - lib/paru/pandoc_options_version_1.yaml
99
- - lib/paru/pandoc_options_version_2.yaml
99
+ - lib/paru/pandoc_options.yaml
100
100
  - lib/paru/selector.rb
101
101
  homepage: https://heerdebeer.org/Software/markdown/paru/
102
102
  licenses:
@@ -110,14 +110,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
110
  requirements:
111
111
  - - ">="
112
112
  - !ruby/object:Gem::Version
113
- version: 2.6.8
113
+ version: 2.7.6
114
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  requirements:
116
116
  - - ">="
117
117
  - !ruby/object:Gem::Version
118
118
  version: '0'
119
119
  requirements: []
120
- rubygems_version: 3.2.22
120
+ rubygems_version: 3.2.3
121
121
  signing_key:
122
122
  specification_version: 4
123
123
  summary: Paru is a ruby wrapper around pandoc
@@ -1,112 +0,0 @@
1
- #--
2
- # Copyright 2015, 2016, 2017 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
- # See http://pandoc.org/README.html for an overview of all options
19
- #++
20
- # Options for pandoc version 1.x, in order of occurrence in the pandoc manual.
21
- # General options
22
- from: ""
23
- read: ""
24
- to: ""
25
- write: ""
26
- output: ""
27
- data_dir: ""
28
- list_input_formats: true
29
- list_output_formats: true
30
- list_extensions: true
31
- list_highlight_languages: true
32
- list_highlight_styles: true
33
- version: true
34
- # Reader options
35
- parse_raw: true
36
- smart: true
37
- old_dashes: true
38
- base_header_level: 1
39
- indented_code_classes: ""
40
- default_image_extension: ""
41
- file_scope: true
42
- filter: [""]
43
- metadata: [""]
44
- normalize: true
45
- preserve_tabs: true
46
- tab_stop: 4
47
- track_changes: "accept"
48
- extract_media: true
49
- # General writer options
50
- standalone: true
51
- template: ""
52
- variable: [""]
53
- print_default_template: ""
54
- print_default_data_file: ""
55
- dpi: 96
56
- wrap: "auto"
57
- no_wrap: true
58
- columns: 78
59
- toc: true
60
- table_of_contents: true
61
- toc_depth: 3
62
- no_highlight: true
63
- highlight_style: "pygments"
64
- include_in_header: [""]
65
- include_before_body: [""]
66
- include_after_body: [""]
67
- # Options affecting specific writers
68
- self_contained: true
69
- html_q_tags: true
70
- ascii: true
71
- reference_links: true
72
- reference_location: "document"
73
- atx_headers: true
74
- chapters: true
75
- top_level_division: "section"
76
- number_sections: true
77
- number_offset: "0"
78
- no_tex_ligatures: true
79
- listings: true
80
- incremental: true
81
- slide_level: 1
82
- section_divs: true
83
- email_obfuscation: "none"
84
- id_prefix: ""
85
- title_prefix: ""
86
- css: [""]
87
- reference_odt: ""
88
- reference_docx: ""
89
- epub_stylesheet: "epub.css"
90
- epub_cover_image: ""
91
- epub_metadata: ""
92
- epub_embed_font: ""
93
- epub_chapter_level: 1
94
- latex_engine: "pdflatex"
95
- latex_engine_opt: [""]
96
- # Citation rendering
97
- bibliography: ""
98
- csl: ""
99
- citation_abbreviations: ""
100
- natbib: true
101
- biblatex: true
102
- # Math rendering in HTML
103
- latexmathml: ""
104
- mathml: ""
105
- jsmath: ""
106
- mathjax: ""
107
- gladtex: true
108
- mimetex: ""
109
- webtex: ""
110
- katex: ""
111
- katex_stylesheet: ""
112
- #--