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
@@ -16,14 +16,24 @@
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 "../pandoc.rb"
20
+
21
+ require_relative './ast_manipulation.rb'
22
+
19
23
  module Paru
20
24
  # PandocFilter is a module containig the paru's Filter functionality
21
25
  module PandocFilter
26
+ # A Paru::Pandoc converter from JSON to markdown
27
+ AST2MARKDOWN = Paru::Pandoc.new do
28
+ from "json"
29
+ to "markdown"
30
+ end
22
31
 
23
- require_relative "./ast_manipulation"
24
- require_relative "./markdown"
25
- require_relative "../pandoc"
26
- require_relative "./document"
32
+ # A Paru::Pandoc converter from markdown to JSON
33
+ MARKDOWN2JSON = Paru::Pandoc.new do
34
+ from "markdown"
35
+ to "json"
36
+ end
27
37
 
28
38
  # Every node in a Pandoc AST is mapped to Node. Filters are all about
29
39
  # manipulating Nodes.
@@ -33,22 +43,68 @@ module Paru
33
43
  class Node
34
44
  include Enumerable
35
45
  include ASTManipulation
36
- include Markdown
37
46
 
38
47
  attr_accessor :parent
39
48
 
40
- Dir[File.dirname(__FILE__) + '/*.rb'].each do |file|
41
- require_relative file
42
- end
49
+ # Block level nodes
50
+ require_relative './block_quote.rb'
51
+ require_relative './block.rb'
52
+ require_relative './bullet_list.rb'
53
+ require_relative './code_block.rb'
54
+ require_relative './definition_list_item.rb'
55
+ require_relative './definition_list.rb'
56
+ require_relative './div.rb'
57
+ require_relative './empty_block.rb'
58
+ require_relative './header.rb'
59
+ require_relative './horizontal_rule.rb'
60
+ require_relative './line_block.rb'
61
+ require_relative './null.rb'
62
+ require_relative './ordered_list.rb'
63
+ require_relative './para.rb'
64
+ require_relative './plain.rb'
65
+ require_relative './raw_block.rb'
66
+ require_relative './table.rb'
67
+ require_relative './table_row.rb'
68
+
69
+ # Inline level nodes
70
+ require_relative './cite.rb'
71
+ require_relative './code.rb'
72
+ require_relative './emph.rb'
73
+ require_relative './empty_inline.rb'
74
+ require_relative './image.rb'
75
+ require_relative './inline.rb'
76
+ require_relative './line_break.rb'
77
+ require_relative './link.rb'
78
+ require_relative './math.rb'
79
+ require_relative './note.rb'
80
+ require_relative './quoted.rb'
81
+ require_relative './raw_inline.rb'
82
+ require_relative './small_caps.rb'
83
+ require_relative './soft_break.rb'
84
+ require_relative './space.rb'
85
+ require_relative './span.rb'
86
+ require_relative './strikeout.rb'
87
+ require_relative './strong.rb'
88
+ require_relative './str.rb'
89
+ require_relative './subscript.rb'
90
+ require_relative './superscript.rb'
91
+
92
+ # Metadata level nodes
93
+ require_relative './meta_blocks.rb'
94
+ require_relative './meta_bool.rb'
95
+ require_relative './meta_inlines.rb'
96
+ require_relative './meta_list.rb'
97
+ require_relative './meta_map.rb'
98
+ require_relative './meta_string.rb'
43
99
 
44
100
  # Create a new Node with contents. Also indicate if this node has
45
101
  # inline children or block children.
46
102
  #
47
- # @param contents [Array<pandoc node in JSON>] the contents of
103
+ # @param contents [Array<pandoc node in JSON> = []] the contents of
48
104
  # this node
49
105
  # @param inline_children [Boolean] does this node have
50
106
  # inline children (true) or block children (false).
51
- def initialize(contents, inline_children = false)
107
+ def initialize(contents = [], inline_children = false)
52
108
  @children = []
53
109
  @parent = nil
54
110
 
@@ -110,7 +166,7 @@ module Paru
110
166
  # @return [Boolean] True if this node has any children, false
111
167
  # otherwise.
112
168
  def has_children?()
113
- defined? @children and @children.size > 0
169
+ defined? @children and not @children.nil? and @children.size > 0
114
170
  end
115
171
 
116
172
  # Get this node's children,
@@ -281,6 +337,69 @@ module Paru
281
337
  }
282
338
  end
283
339
 
340
+ # Get the markdown representation of this Node, including the Node
341
+ # itself.
342
+ #
343
+ # @return [String] the outer markdown representation of this Node
344
+ def markdown()
345
+ temp_doc = PandocFilter::Document.fragment [self]
346
+ markdown = AST2MARKDOWN << temp_doc.to_JSON
347
+ markdown
348
+ end
349
+
350
+ alias outer_markdown markdown
351
+
352
+ # Set the markdown representation of this Node: replace this Node
353
+ # by the Node represented by the markdown string. If an inline
354
+ # node is being replaced and the replacement has more than one
355
+ # paragraph, only the contents of the first paragraph is used
356
+ #
357
+ # @param markdown [String] the markdown string to replace this
358
+ # Node
359
+ #
360
+ # @example Replacing all horizontal lines by a Plain node saying "hi"
361
+ # Paru::Filter.run do
362
+ # with "HorizontalLine" do |line|
363
+ # line.markdown = "hi"
364
+ # end
365
+ # end
366
+ #
367
+ def markdown=(markdown)
368
+ json = MARKDOWN2JSON << markdown
369
+ temp_doc = PandocFilter::Document.from_JSON json
370
+
371
+ if not has_parent? or is_root?
372
+ @children = temp_doc.children
373
+ else
374
+ # replace current node by new nodes
375
+ # There is a difference between inline and block nodes
376
+ current_index = parent.find_index self
377
+
378
+ # By default, pandoc creates a Block level node when
379
+ # converting a string. However, if the original is a
380
+ # inline level node, so should its replacement node(s) be.
381
+ # Only using first block node (paragraph?)
382
+ if is_inline?
383
+ temp_doc = temp_doc.children.first
384
+
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}'."
387
+ end
388
+
389
+ end
390
+
391
+ index = current_index
392
+ temp_doc.each do |child|
393
+ index += 1
394
+ parent.insert index, child
395
+ end
396
+ # Remove the original node
397
+ parent.remove_at current_index
398
+ end
399
+
400
+ end
401
+
402
+ alias outer_markdown= markdown=
284
403
  end
285
404
  end
286
405
  end
@@ -16,12 +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 "./block.rb"
21
+
19
22
  module Paru
20
23
  module PandocFilter
21
-
22
- require_relative "./inline"
23
- require_relative "./block"
24
-
25
24
  # A Note node like a foot note or end note. It is a special node in
26
25
  # the sense that itself is an Inline level node, but its contents are
27
26
  # Block level.
@@ -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 "./empty_block.rb"
20
+
19
21
  module Paru
20
22
  module PandocFilter
21
- require_relative "./empty_block"
22
-
23
23
  # A Null node is an EmptyBlock node
24
24
  class Null < EmptyBlock
25
25
  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 "./list.rb"
20
+ require_relative "./list_attributes.rb"
21
+
19
22
  module Paru
20
23
  module PandocFilter
21
- require_relative "./list"
22
- require_relative "./list_attributes"
23
-
24
24
  # An OrderedList Node
25
25
  #
26
26
  # @example In markdown an ordered list looks like
@@ -39,14 +39,14 @@ module Paru
39
39
  #
40
40
  # @param contents [Array]
41
41
  def initialize(contents)
42
- @list_attributes = ListAttributes.new contents[0]
43
42
  super contents[1]
43
+ @list_attributes = ListAttributes.new contents[0]
44
44
  end
45
45
 
46
46
  # The AST contents
47
47
  #
48
48
  # @return [Array]
49
- def ast_contents
49
+ def ast_contents()
50
50
  [
51
51
  @list_attributes.to_ast,
52
52
  super
@@ -16,12 +16,14 @@
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
+ require_relative "./inner_markdown.rb"
21
+
19
22
  module Paru
20
23
  module PandocFilter
21
- require_relative "./block"
22
-
23
24
  # A Para is a paragraph: a list of Inline nodes
24
25
  class Para < Block
26
+ include InnerMarkdown
25
27
 
26
28
  # Create a new Para node based on the contents
27
29
  #
@@ -16,14 +16,16 @@
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
+ require_relative "./inner_markdown.rb"
21
+
19
22
  module Paru
20
23
  module PandocFilter
21
- require_relative "./block"
22
-
23
24
  # A Plain node is a basic {Block} level node with {Inline} child nodes. Not
24
25
  # to be confused with {Para}, which represents a paragraph. A Plain
25
26
  # is a more general type of block level node.
26
27
  class Plain < Block
28
+ include InnerMarkdown
27
29
 
28
30
  # Create a new Plain node based on contents
29
31
  #
@@ -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 Quoted node represents a quote with a type and the contents of the
24
24
  # quote
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 "./block.rb"
20
+ require_relative "./inner_markdown.rb"
21
+
19
22
  module Paru
20
23
  module PandocFilter
21
- require_relative "./block"
22
-
23
24
  # A RawBlock node has a format and a string contents
24
25
  #
25
26
  # @!attribute format
@@ -28,6 +29,7 @@ module Paru
28
29
  # @!attribute string
29
30
  # @return [String]
30
31
  class RawBlock < Block
32
+ include InnerMarkdown
31
33
  attr_accessor :format, :string
32
34
 
33
35
  # Create a new RawBlock node based on the contents
@@ -38,7 +40,7 @@ module Paru
38
40
  end
39
41
 
40
42
  # Create an AST representation of this RawBlock node
41
- def to_ast()
43
+ def ast_contents()
42
44
  [
43
45
  @format,
44
46
  @string
@@ -16,11 +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
-
22
- require_relative "./inline"
23
-
24
23
  # A RawInline node has a format and a string value
25
24
  #
26
25
  # @!attribute format
@@ -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 SmallCaps node is an inline level node representing SMALL CAPS
24
24
  # text
25
25
  class SmallCaps < Inline
@@ -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 "./empty_inline.rb"
20
+
19
21
  module Paru
20
22
  module PandocFilter
21
- require_relative "./empty_inline"
22
-
23
23
  # A SoftBreak node
24
24
  class SoftBreak < EmptyInline
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 "./empty_inline.rb"
20
+
19
21
  module Paru
20
22
  module PandocFilter
21
- require_relative "./empty_inline"
22
-
23
23
  # A Space node
24
24
  class Space < EmptyInline
25
25
  end
@@ -16,12 +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
-
22
- require_relative "./inline"
23
- require_relative "./attr"
24
-
25
24
  # A Span node is a general Inline level node with attributes and
26
25
  # contens
27
26
  #
@@ -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 Str node represents a string
24
24
  #
25
25
  # @!attribute string
@@ -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 Strikeout inline node
24
24
  class Strikeout < Inline
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 Strong inline node
24
24
  class Strong < Inline
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 Subscript inline node
24
24
  class Subscript < Inline
25
25
  end