paru 0.2.5c → 0.2.5f

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/bin/do-pandoc.rb +5 -5
  3. data/bin/pandoc2yaml.rb +23 -58
  4. data/lib/paru.rb +1 -5
  5. data/lib/paru/error.rb +0 -2
  6. data/lib/paru/filter.rb +46 -30
  7. data/lib/paru/filter/ast_manipulation.rb +0 -1
  8. data/lib/paru/filter/attr.rb +0 -1
  9. data/lib/paru/filter/block.rb +2 -2
  10. data/lib/paru/filter/block_quote.rb +2 -2
  11. data/lib/paru/filter/bullet_list.rb +2 -2
  12. data/lib/paru/filter/citation.rb +2 -2
  13. data/lib/paru/filter/cite.rb +8 -4
  14. data/lib/paru/filter/code.rb +11 -3
  15. data/lib/paru/filter/code_block.rb +5 -3
  16. data/lib/paru/filter/definition_list.rb +2 -2
  17. data/lib/paru/filter/definition_list_item.rb +4 -4
  18. data/lib/paru/filter/div.rb +5 -3
  19. data/lib/paru/filter/document.rb +47 -16
  20. data/lib/paru/filter/emph.rb +2 -2
  21. data/lib/paru/filter/empty_block.rb +3 -3
  22. data/lib/paru/filter/empty_inline.rb +2 -2
  23. data/lib/paru/filter/header.rb +6 -3
  24. data/lib/paru/filter/horizontal_rule.rb +6 -6
  25. data/lib/paru/filter/image.rb +2 -3
  26. data/lib/paru/filter/inline.rb +5 -2
  27. data/lib/paru/filter/inner_markdown.rb +85 -0
  28. data/lib/paru/filter/line_block.rb +8 -2
  29. data/lib/paru/filter/line_break.rb +2 -2
  30. data/lib/paru/filter/link.rb +4 -5
  31. data/lib/paru/filter/list.rb +8 -6
  32. data/lib/paru/filter/math.rb +2 -2
  33. data/lib/paru/filter/meta.rb +15 -4
  34. data/lib/paru/filter/meta_blocks.rb +2 -2
  35. data/lib/paru/filter/meta_bool.rb +2 -3
  36. data/lib/paru/filter/meta_inlines.rb +4 -2
  37. data/lib/paru/filter/meta_list.rb +2 -3
  38. data/lib/paru/filter/meta_map.rb +10 -142
  39. data/lib/paru/filter/meta_string.rb +1 -1
  40. data/lib/paru/filter/meta_value.rb +5 -4
  41. data/lib/paru/filter/metadata.rb +114 -0
  42. data/lib/paru/filter/node.rb +130 -11
  43. data/lib/paru/filter/note.rb +3 -4
  44. data/lib/paru/filter/null.rb +2 -2
  45. data/lib/paru/filter/ordered_list.rb +5 -5
  46. data/lib/paru/filter/para.rb +4 -2
  47. data/lib/paru/filter/plain.rb +4 -2
  48. data/lib/paru/filter/quoted.rb +2 -2
  49. data/lib/paru/filter/raw_block.rb +5 -3
  50. data/lib/paru/filter/raw_inline.rb +2 -3
  51. data/lib/paru/filter/small_caps.rb +2 -2
  52. data/lib/paru/filter/soft_break.rb +2 -2
  53. data/lib/paru/filter/space.rb +2 -2
  54. data/lib/paru/filter/span.rb +3 -4
  55. data/lib/paru/filter/str.rb +2 -2
  56. data/lib/paru/filter/strikeout.rb +2 -2
  57. data/lib/paru/filter/strong.rb +2 -2
  58. data/lib/paru/filter/subscript.rb +2 -2
  59. data/lib/paru/filter/superscript.rb +2 -2
  60. data/lib/paru/filter/table.rb +3 -3
  61. data/lib/paru/filter/table_row.rb +2 -2
  62. data/lib/paru/filter/target.rb +0 -1
  63. data/lib/paru/filter/version.rb +2 -2
  64. data/lib/paru/filter_error.rb +25 -0
  65. data/lib/paru/pandoc.rb +3 -4
  66. data/lib/paru/pandoc2yaml.rb +71 -0
  67. data/lib/paru/selector.rb +2 -4
  68. metadata +6 -3
  69. data/lib/paru/filter/markdown.rb +0 -150
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e39519f32865af110131f673e16b6e4a99f567d0
4
- data.tar.gz: bfc70e18a105a8488cc4ba06b2f3cc393e7bb9b6
3
+ metadata.gz: 9d36825678ac43efb4419d37a42033055a161fe8
4
+ data.tar.gz: c639c47e276492541231e67cc3a683b4b68cc3eb
5
5
  SHA512:
6
- metadata.gz: d1169e0f995d1722251fcfbb0107c386aaf6d5f0df98ead73fac6adf64276bf7715e1f4003c512186cd41d228a38f007fa26cc18cb62c033f3c69e863a570268
7
- data.tar.gz: 12f8bbb1532f99347ac624b56215b2932bf56db09ac2d4a8f3583601fe5aadbc2df071ba4b183b07d1165c3e29f3e111743c677854cc967677341e62c24bcb44
6
+ metadata.gz: 6384ccf86bd98e102fe068f6ceee9221a54758396f96e1f3787b16d8474530d6f683df255f845de565ffd98f80cea11a6df8b11155eccd758471758845291558
7
+ data.tar.gz: 2220576d8c4c53fd8dfe0dd9bf5855ed82f55064ca9c87d3810dafdd418f15c7280ac54ef6b407015096319e9cb3aa00c783af5b8ccd9b90ed14df4a2c6dc150
@@ -2,9 +2,7 @@
2
2
  require "yaml"
3
3
  require 'optparse'
4
4
  require "paru/pandoc"
5
- require_relative "./pandoc2yaml.rb"
6
-
7
- include Pandoc2Yaml
5
+ require "paru/pandoc2yaml"
8
6
 
9
7
  parser = OptionParser.new do |opts|
10
8
  opts.banner = "do-pandoc.rb runs pandoc on an input file using the pandoc configuration specified in that input file."
@@ -44,7 +42,9 @@ if !File.readable? document
44
42
  warn "Cannot read file: #{input_document}"
45
43
  exit
46
44
  end
47
- metadata = YAML.load Pandoc2Yaml.extract_metadata(document)
45
+
46
+ yaml = Paru::Pandoc2Yaml.extract_metadata(document)
47
+ metadata = YAML.load yaml
48
48
 
49
49
  if metadata.has_key? "pandoc" then
50
50
  begin
@@ -65,5 +65,5 @@ if metadata.has_key? "pandoc" then
65
65
  warn "Something went wrong while using pandoc:\n\n#{e.message}"
66
66
  end
67
67
  else
68
- warn "Unsure what to do: no pandoc options in #{input}"
68
+ warn "Unsure what to do: no pandoc options in #{input_document}"
69
69
  end
@@ -9,86 +9,51 @@
9
9
  # pandoc2yaml.rb input_file
10
10
  #
11
11
  ##
12
- module Pandoc2Yaml
13
- require "json"
14
- require "paru/pandoc"
12
+ require "json"
13
+ require 'optparse'
14
+ require 'paru/pandoc2yaml'
15
15
 
16
- # Paru converters:
17
- # Note. When converting metadata back to the pandoc markdown format, you have
18
- # to use the option "standalone", otherwise the metadata is skipped
19
- PANDOC_2_JSON = Paru::Pandoc.new {from "markdown"; to "json"}
20
- JSON_2_PANDOC = Paru::Pandoc.new {from "json"; to "markdown"; standalone}
21
-
22
- # When converting a pandoc document to JSON, or vice versa, the JSON object
23
- # has the following three properties:
24
- VERSION = "pandoc-api-version"
25
- META = "meta"
26
- BLOCKS = "blocks"
27
-
28
- def extract_metadata input_document
29
- json = JSON.parse(PANDOC_2_JSON << File.read(input_document))
30
- yaml = ""
31
-
32
- version, metadata = json.values_at(VERSION, META)
33
-
34
- if not metadata.empty? then
35
- metadata_document = {
36
- VERSION => version,
37
- META => metadata,
38
- BLOCKS => []
39
- }
40
-
41
- yaml = JSON_2_PANDOC << JSON.generate(metadata_document)
42
- end
43
-
44
- yaml
45
- end
46
- end
47
-
48
- if __FILE__ == $0
49
- include Pandoc2Yaml
50
- require 'optparse'
51
-
52
- parser = OptionParser.new do |opts|
16
+ parser = OptionParser.new do |opts|
53
17
  opts.banner = "pandoc2yaml.rb mines a pandoc markdown file for its YAML metadata"
54
18
  opts.banner << "\n\nUsage: pandoc2yaml.rb some-pandoc-markdownfile.md"
55
19
  opts.separator ""
56
20
  opts.separator "Common options"
57
21
 
58
22
  opts.on_tail("-h", "--help", "Show this message") do
59
- puts opts
60
- exit
23
+ puts opts
24
+ exit
61
25
  end
62
26
 
63
27
  opts.on("-v", "--version", "Show version") do
64
- puts "pandoc2yaml.rb is part of paru version 0.2.3"
65
- exit
28
+ puts "pandoc2yaml.rb is part of paru version 0.2.3"
29
+ exit
66
30
  end
67
- end
31
+ end
68
32
 
69
- parser.parse! ARGV
33
+ parser.parse! ARGV
70
34
 
71
- input_document = ARGV.pop
35
+ input_document = ARGV.pop
72
36
 
73
- if ARGV.size != 0 then
37
+ if ARGV.size != 0 or input_document.nil? or input_document.empty? then
74
38
  warn "Expecting exactly one argument: the pandoc file to strip for metadata"
75
39
  puts ""
76
40
  puts parser
77
41
  exit
78
- end
42
+ end
79
43
 
80
- document = File.expand_path input_document
81
- if not File.exist? document
44
+ document = File.expand_path input_document
45
+ if not File.exist? document
82
46
  warn "Cannot find file: #{input_document}"
83
47
  exit
84
- end
48
+ end
85
49
 
86
- if !File.readable? document
50
+ if !File.readable? document
87
51
  warn "Cannot read file: #{input_document}"
88
52
  exit
89
- end
90
-
91
- output_metadata = Pandoc2Yaml.extract_metadata document
92
-
93
- puts output_metadata
94
53
  end
54
+
55
+ yaml = Paru::Pandoc2Yaml.extract_metadata(document)
56
+
57
+ yaml = "---\n..." if yaml.empty?
58
+
59
+ puts yaml
@@ -17,10 +17,6 @@
17
17
  # Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
  module Paru
20
- require "paru/pandoc"
21
- require "paru/error"
22
- require "paru/filter"
23
-
24
20
  # Paru's current version
25
- VERSION = [0, 2, 4, 4]
21
+ VERSION = [0, 2, 4, 9]
26
22
  end
@@ -16,9 +16,7 @@
16
16
  # You should have received a copy of the GNU General Public License
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
-
20
19
  module Paru
21
-
22
20
  # An error class to use as a basis for paru specific errors.
23
21
  class Error < RuntimeError
24
22
  end
@@ -16,11 +16,11 @@
16
16
  # You should have received a copy of the GNU General Public License
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
- module Paru
20
-
21
- require_relative "./selector"
22
- require_relative "filter/document"
19
+ require_relative "./selector.rb"
20
+ require_relative "./filter/document.rb"
21
+ require_relative "./filter/metadata.rb"
23
22
 
23
+ module Paru
24
24
  # Paru filter is a wrapper around pandoc's JSON api, which is based on
25
25
  # {pandoc-types}[https://hackage.haskell.org/package/pandoc-types-1.17.0.4/docs/Text-Pandoc-Definition.html].
26
26
  # Pandoc treats block elements and inline elements differently.
@@ -54,6 +54,7 @@ module Paru
54
54
  "SmallCaps",
55
55
  "Quoted",
56
56
  "Cite",
57
+ "Code",
57
58
  "Space",
58
59
  "SoftBreak",
59
60
  "LineBreak",
@@ -104,8 +105,8 @@ module Paru
104
105
  #
105
106
  # ![Figure 2. My second image](img/rabbit.jpeg)
106
107
  #
107
- # The method {PandocFilter::Node#inner_markdown} and its counterpart
108
- # {PandocFilter::Node#outer_markdown} are a great way to manipulate the
108
+ # The method {PandocFilter::InnerMarkdown#inner_markdown} and its counterpart
109
+ # {PandocFilter::Node#markdown} are a great way to manipulate the
109
110
  # contents of a selected {PandocFilter::Node}. No messing about creating
110
111
  # and filling {PandocFilter::Node}s, you can just use pandoc's own
111
112
  # markdown format!
@@ -169,8 +170,8 @@ module Paru
169
170
  #
170
171
  # == Manipulating nodes: Removing horizontal lines
171
172
  #
172
- # Although the {PandocFilter::Node#inner_markdown} and
173
- # {PandocFilter::Node#outer_markdown} work in most situations, sometimes
173
+ # Although the {PandocFilter::InnerMarkdown#inner_markdown} and
174
+ # {PandocFilter::Node#markdown} work in most situations, sometimes
174
175
  # direct manipulation of the pandoc document AST is useful. These
175
176
  # {PandocFilter::ASTManipulation} methods are mixed in
176
177
  # {PandocFilter::Node} and can be used on any node in your filter. For
@@ -181,9 +182,7 @@ module Paru
181
182
  #
182
183
  # Note that you could have arrived at the same effect by using:
183
184
  #
184
- # rule.outer_markdown = ""
185
- #
186
- #
185
+ # rule.markdown = ""
187
186
  #
188
187
  # == Manipulating metadata:
189
188
  #
@@ -196,8 +195,14 @@ module Paru
196
195
  #
197
196
  # {include:file:examples/filters/add_today.rb}
198
197
  #
198
+ # In a filter, the +metadata+ property is a Ruby Hash of Strings, Numbers,
199
+ # Booleans, Arrays, and Hashes. You can manipulate it like any other Ruby
200
+ # Hash.
201
+ #
199
202
  class Filter
200
203
 
204
+ attr_reader :metadata, :document
205
+
201
206
  # Create a new Filter instance. For convenience, {run} creates a new
202
207
  # {Filter} and runs it immediately. Use this constructor if you want
203
208
  # to run a filter on different input and output streams that STDIN and
@@ -210,6 +215,7 @@ module Paru
210
215
  def initialize(input = $stdin, output = $stdout)
211
216
  @input = input
212
217
  @output = output
218
+ @running = false
213
219
  end
214
220
 
215
221
  # Run the filter specified by block. This is a convenience method that
@@ -228,15 +234,6 @@ module Paru
228
234
  Filter.new($stdin, $stdout).filter(&block)
229
235
  end
230
236
 
231
- # The Document node from JSON formatted pandoc document structure
232
- # on STDIN that is being filtered
233
- #
234
- # @return [Document] create a new Document node from a pandoc AST from
235
- # JSON from STDIN
236
- def document()
237
- PandocFilter::Document.from_JSON @input.read
238
- end
239
-
240
237
  # Create a filter using +block+. In the block you specify
241
238
  # selectors and actions to be performed on selected nodes. In the
242
239
  # example below, the selector is "Image", which selects all image
@@ -257,18 +254,23 @@ module Paru
257
254
  # end
258
255
  # end
259
256
  #
260
- # # do something with output.string
261
257
  def filter(&block)
262
258
  @selectors = Hash.new
263
259
  @filtered_nodes = []
264
- @doc = document
260
+ @document = read_document
261
+
262
+ @metadata = PandocFilter::Metadata.new @document.meta
265
263
 
266
- @doc.each_depth_first do |node|
264
+ @running = true
265
+ @document.each_depth_first do |node|
267
266
  @filtered_nodes.push node
268
267
  instance_eval(&block)
268
+ break unless @running
269
269
  end
270
+ @running = false
270
271
 
271
- @output.write @doc.to_JSON
272
+ @document.meta = @metadata.to_meta
273
+ @output.write @document.to_JSON
272
274
  end
273
275
 
274
276
  # +current_node+ points to the node that is *now* being processed while
@@ -289,13 +291,27 @@ module Paru
289
291
  yield current_node if @selectors[selector].matches? current_node, @filtered_nodes
290
292
  end
291
293
 
292
- # While running a filter you can access the document's metadata through
293
- # the +metadata+ method.
294
- #
295
- # @return [Meta] the filtered document's metadata
296
- def metadata()
297
- @doc.meta
294
+ # Stop processing the document any further and output it as it is now.
295
+ # This is a great timesaver for filters that only act on a small
296
+ # number of nodes in a large document, or when you only want to set
297
+ # the metadata.
298
+ def stop!()
299
+ @running = false
298
300
  end
299
301
 
302
+ private
303
+
304
+ # The Document node from JSON formatted pandoc document structure
305
+ # on STDIN that is being filtered
306
+ #
307
+ # @return [Document] create a new Document node from a pandoc AST from
308
+ # JSON from STDIN
309
+ def read_document()
310
+ PandocFilter::Document.from_JSON @input.read
311
+ end
312
+ end
313
+
314
+ # FilterError is thrown when there is an error during filtering
315
+ class FilterError < Error
300
316
  end
301
317
  end
@@ -18,7 +18,6 @@
18
18
  #++
19
19
  module Paru
20
20
  module PandocFilter
21
-
22
21
  # ASTManipulation is a mixin for Node with some standard tree
23
22
  # manipulation methods such as inserting or removing nodes, replacing
24
23
  # nodes, and so on.
@@ -18,7 +18,6 @@
18
18
  #++
19
19
  module Paru
20
20
  module PandocFilter
21
-
22
21
  # Attr represents an attribute object for a node. It contains of an id, a
23
22
  # list of class names and a list of key-value pairs.
24
23
  #
@@ -16,10 +16,10 @@
16
16
  # You should have received a copy of the GNU General Public License
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
+ require_relative "./node.rb"
20
+
19
21
  module Paru
20
22
  module PandocFilter
21
- require_relative "./node"
22
-
23
23
  # A Block level node.
24
24
  # @see https://hackage.haskell.org/package/pandoc-types-1.17.0.5/docs/Text-Pandoc-Definition.html#t:Block
25
25
  class Block < Node
@@ -16,10 +16,10 @@
16
16
  # You should have received a copy of the GNU General Public License
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
+ require_relative "./block.rb"
20
+
19
21
  module Paru
20
22
  module PandocFilter
21
- require_relative "./block"
22
-
23
23
  # A BlockQuote node. Contains a list of Blocks
24
24
  class BlockQuote < Block
25
25
  # Has this node a block?
@@ -16,10 +16,10 @@
16
16
  # You should have received a copy of the GNU General Public License
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
+ require_relative "./list.rb"
20
+
19
21
  module Paru
20
22
  module PandocFilter
21
- require_relative "./list"
22
-
23
23
  # BulletList, contains a list of list of Block nodes.
24
24
  class BulletList < List
25
25
  end
@@ -16,10 +16,10 @@
16
16
  # You should have received a copy of the GNU General Public License
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #--
19
+ require_relative "./inline.rb"
20
+
19
21
  module Paru
20
22
  module PandocFilter
21
- require_relative "./inline"
22
-
23
23
  # A Citation consists of an id, a prefix, a suffix, a mode, a note
24
24
  # number, and integer hash. All of which are optional.
25
25
  #
@@ -16,10 +16,11 @@
16
16
  # You should have received a copy of the GNU General Public License
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
+ require_relative "./inline.rb"
20
+ require_relative "./citation.rb"
21
+
19
22
  module Paru
20
23
  module PandocFilter
21
- require_relative "./inline"
22
-
23
24
  # A Cite node, consisting of a list of Citation nodes, and a list of
24
25
  # Inline nodes
25
26
  #
@@ -32,12 +33,12 @@ module Paru
32
33
  #
33
34
  # @param contents [Array] an array containing a list of citations
34
35
  # and a list of inline nodes
35
- def initialize contents
36
+ def initialize(contents)
37
+ super contents[1]
36
38
  @citations = []
37
39
  contents[0].each do |citation|
38
40
  @citations.push Citation.new(citation)
39
41
  end
40
- super contents[1]
41
42
  end
42
43
 
43
44
  # Create an AST representation of this Cite node.
@@ -47,6 +48,9 @@ module Paru
47
48
  super
48
49
  ]
49
50
  end
51
+
52
+ undef_method :inner_markdown
53
+ undef_method :inner_markdown=
50
54
  end
51
55
  end
52
56
  end
@@ -16,11 +16,11 @@
16
16
  # You should have received a copy of the GNU General Public License
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
+ require_relative "./inline.rb"
20
+ require_relative "./attr.rb"
21
+
19
22
  module Paru
20
23
  module PandocFilter
21
- require_relative "./inline"
22
- require_relative "./attr"
23
-
24
24
  # A Code node, with an attribute object and the code itself as a
25
25
  # string.
26
26
  #
@@ -61,6 +61,14 @@ module Paru
61
61
  def has_inline?()
62
62
  false
63
63
  end
64
+
65
+ # Get the markdown representation of this Node, including the Node
66
+ # itself.
67
+ #
68
+ # @return [String] the outer markdown representation of this Node
69
+ def markdown()
70
+ super.strip
71
+ end
64
72
  end
65
73
  end
66
74
  end