paru 0.2.4.2 → 0.2.4.3

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.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/lib/paru.rb +8 -20
  3. data/lib/paru/error.rb +4 -6
  4. data/lib/paru/filter.rb +144 -110
  5. data/lib/paru/filter/ast_manipulation.rb +75 -39
  6. data/lib/paru/filter/attr.rb +72 -36
  7. data/lib/paru/filter/block.rb +14 -8
  8. data/lib/paru/filter/block_quote.rb +12 -9
  9. data/lib/paru/filter/bullet_list.rb +6 -6
  10. data/lib/paru/filter/citation.rb +51 -25
  11. data/lib/paru/filter/cite.rb +29 -20
  12. data/lib/paru/filter/code.rb +41 -24
  13. data/lib/paru/filter/code_block.rb +36 -21
  14. data/lib/paru/filter/definition_list.rb +19 -15
  15. data/lib/paru/filter/definition_list_item.rb +30 -17
  16. data/lib/paru/filter/div.rb +29 -21
  17. data/lib/paru/filter/document.rb +73 -46
  18. data/lib/paru/filter/emph.rb +6 -6
  19. data/lib/paru/filter/empty_block.rb +17 -13
  20. data/lib/paru/filter/empty_inline.rb +24 -17
  21. data/lib/paru/filter/header.rb +38 -23
  22. data/lib/paru/filter/image.rb +13 -11
  23. data/lib/paru/filter/inline.rb +21 -10
  24. data/lib/paru/filter/line_block.rb +6 -6
  25. data/lib/paru/filter/line_break.rb +6 -6
  26. data/lib/paru/filter/link.rb +33 -21
  27. data/lib/paru/filter/list.rb +26 -17
  28. data/lib/paru/filter/list_attributes.rb +53 -32
  29. data/lib/paru/filter/markdown.rb +102 -59
  30. data/lib/paru/filter/math.rb +65 -38
  31. data/lib/paru/filter/meta.rb +26 -16
  32. data/lib/paru/filter/meta_blocks.rb +12 -9
  33. data/lib/paru/filter/meta_bool.rb +6 -6
  34. data/lib/paru/filter/meta_inlines.rb +12 -9
  35. data/lib/paru/filter/meta_list.rb +6 -6
  36. data/lib/paru/filter/meta_map.rb +49 -33
  37. data/lib/paru/filter/meta_string.rb +6 -6
  38. data/lib/paru/filter/meta_value.rb +22 -14
  39. data/lib/paru/filter/node.rb +204 -129
  40. data/lib/paru/filter/note.rb +31 -20
  41. data/lib/paru/filter/null.rb +6 -6
  42. data/lib/paru/filter/ordered_list.rb +34 -18
  43. data/lib/paru/filter/para.rb +20 -13
  44. data/lib/paru/filter/plain.rb +21 -12
  45. data/lib/paru/filter/quoted.rb +27 -18
  46. data/lib/paru/filter/raw_block.rb +32 -19
  47. data/lib/paru/filter/raw_inline.rb +40 -22
  48. data/lib/paru/filter/small_caps.rb +7 -6
  49. data/lib/paru/filter/soft_break.rb +6 -6
  50. data/lib/paru/filter/space.rb +6 -6
  51. data/lib/paru/filter/span.rb +28 -18
  52. data/lib/paru/filter/str.rb +29 -18
  53. data/lib/paru/filter/strikeout.rb +6 -6
  54. data/lib/paru/filter/strong.rb +6 -6
  55. data/lib/paru/filter/subscript.rb +6 -6
  56. data/lib/paru/filter/superscript.rb +6 -6
  57. data/lib/paru/filter/table.rb +51 -29
  58. data/lib/paru/filter/table_row.rb +21 -14
  59. data/lib/paru/filter/target.rb +29 -15
  60. data/lib/paru/filter/version.rb +23 -14
  61. data/lib/paru/pandoc.rb +165 -111
  62. data/lib/paru/pandoc_options.yaml +3 -3
  63. data/lib/paru/selector.rb +176 -153
  64. metadata +2 -3
  65. data/lib/paru/filter/alignment.rb +0 -30
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -17,17 +17,19 @@
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
  module Paru
20
- module PandocFilter
20
+ module PandocFilter
21
21
 
22
- require_relative "./link"
22
+ require_relative "./link"
23
23
 
24
- # Image Attr [Inline] Target
25
- class Image < Link
26
- # Although Image is defined to be inline, probably because in HTML it
27
- # can be an inline element, often it acts like a block element.
28
- def can_act_as_both_block_and_inline?
29
- true
30
- end
24
+ # An Image has an attribute object, a caption, and a target
25
+ class Image < Link
26
+
27
+ # Although Image is defined to be inline, probably because in HTML it
28
+ # can be an inline element, often it acts like a block element.
29
+ # @return [Boolean] true
30
+ def can_act_as_both_block_and_inline?
31
+ true
32
+ end
33
+ end
31
34
  end
32
- end
33
35
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -20,18 +20,29 @@ module Paru
20
20
  module PandocFilter
21
21
  require_relative "./node"
22
22
 
23
+ # An Inline node
24
+ # @see http://hackage.haskell.org/package/pandoc-types-1.17.0.4/docs/Text-Pandoc-Definition.html#t:Inline
23
25
  class Inline < Node
24
- def initialize contents
25
- super contents, true
26
- end
27
26
 
28
- def is_inline?
29
- true
30
- end
27
+ # Create a new Inline node with contents
28
+ # @param contents [Array] the contents of this inline node
29
+ def initialize(contents)
30
+ super contents, true
31
+ end
31
32
 
32
- def has_inline?
33
- true
34
- end
33
+ # Is this Inline node inline? Of course!
34
+ #
35
+ # @return [Boolean] true
36
+ def is_inline?()
37
+ true
38
+ end
39
+
40
+ # Has this Inline node inline contents? Of course!
41
+ #
42
+ # @return [Boolean] true
43
+ def has_inline?()
44
+ true
45
+ end
35
46
  end
36
47
  end
37
48
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -17,11 +17,11 @@
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #--
19
19
  module Paru
20
- module PandocFilter
21
- require_relative "./list.rb"
20
+ module PandocFilter
21
+ require_relative "./list.rb"
22
22
 
23
- # LineBlock [[Inline]]
24
- class LineBlock < List
23
+ # A LineBlock is a List of Lists of Inline nodes
24
+ class LineBlock < List
25
+ end
25
26
  end
26
- end
27
27
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -17,11 +17,11 @@
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
  module Paru
20
- module PandocFilter
21
- require_relative "./empty_inline"
20
+ module PandocFilter
21
+ require_relative "./empty_inline"
22
22
 
23
- # LineBreak
24
- class LineBreak < EmptyInline
23
+ # A LineBreak is an empty inline element
24
+ class LineBreak < EmptyInline
25
+ end
25
26
  end
26
- end
27
27
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -17,30 +17,42 @@
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
  module Paru
20
- module PandocFilter
20
+ module PandocFilter
21
21
 
22
- require_relative "./inline"
23
- require_relative "./attr"
24
- require_relative "./target"
22
+ require_relative "./inline"
23
+ require_relative "./attr"
24
+ require_relative "./target"
25
25
 
26
- # Link Attr [Inline] Target
27
- class Link < Inline
28
- attr_accessor :attr, :target
26
+ # A Link node has an attribute object, an Inline description and a
27
+ # target
28
+ #
29
+ # @!attribute attr
30
+ # @return [Attr]
31
+ #
32
+ # @!attribute targer
33
+ # @return [Target]
34
+ class Link < Inline
35
+ attr_accessor :attr, :target
29
36
 
30
- def initialize contents
31
- @attr = Attr.new contents[0]
32
- super contents[1]
33
- @target = Target.new contents[2]
34
- end
37
+ # Create a new Link node with contents
38
+ #
39
+ # @param contents [Array] an array with attributes, description,
40
+ # and target information
41
+ def initialize(contents)
42
+ @attr = Attr.new contents[0]
43
+ super contents[1]
44
+ @target = Target.new contents[2]
45
+ end
35
46
 
36
- def ast_contents
37
- [
38
- @attr.to_ast,
39
- super,
40
- @target.to_ast
41
- ]
42
- end
47
+ # Create an AST representation of this Link node
48
+ def ast_contents
49
+ [
50
+ @attr.to_ast,
51
+ super,
52
+ @target.to_ast
53
+ ]
54
+ end
43
55
 
56
+ end
44
57
  end
45
- end
46
58
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -17,24 +17,33 @@
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
  module Paru
20
- module PandocFilter
21
- require_relative "./block"
20
+ module PandocFilter
21
+ require_relative "./block"
22
22
 
23
- class List < Block
24
- def initialize contents
25
- super []
26
- contents.each do |item|
27
- @children.push Block.new item
28
- end
29
- end
23
+ # A List node is a base node for various List node types
24
+ class List < Block
25
+
26
+ # Create a new List node based on contents
27
+ #
28
+ # @param contents [Array] the contents of the list
29
+ def initialize contents
30
+ super []
31
+ contents.each do |item|
32
+ @children.push Block.new item
33
+ end
34
+ end
30
35
 
31
- def ast_contents
32
- @children.map {|child| child.ast_contents}
33
- end
36
+ # Create an AST representation of this List node
37
+ def ast_contents
38
+ @children.map {|child| child.ast_contents}
39
+ end
34
40
 
35
- def has_block?
36
- true
37
- end
41
+ # Has this List node block contents?
42
+ #
43
+ # @return [Boolean] true
44
+ def has_block?
45
+ true
46
+ end
47
+ end
38
48
  end
39
- end
40
49
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -17,39 +17,60 @@
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
  module Paru
20
- module PandocFilter
21
- class ListAttributes
20
+ module PandocFilter
21
+ # ListAttributes represent the attributes of a list.
22
+ #
23
+ # @see http://hackage.haskell.org/package/pandoc-types-1.17.0.4/docs/Text-Pandoc-Definition.html#t:ListAttributes
24
+ #
25
+ # @!attribute start
26
+ # @return [Integer]
27
+ #
28
+ # @!attribute number_style
29
+ # @return [STYLES]
30
+ #
31
+ # @!attribute number_delim
32
+ # @return [DELIMS]
33
+ class ListAttributes
34
+
35
+ # The various styles of list numbers
36
+ STYLES = [
37
+ "DefaultStyle",
38
+ "Example",
39
+ "Decimal",
40
+ "LowerRoman",
41
+ "UpperRoman",
42
+ "LowerAlpha",
43
+ "UpperAlpha"
44
+ ]
22
45
 
23
- STYLES = [
24
- "DefaultStyle",
25
- "Example",
26
- "Decimal",
27
- "LowerRoman",
28
- "UpperRoman",
29
- "LowerAlpha",
30
- "UpperAlpha"
31
- ]
32
- DELIMS = [
33
- "DefaultDelim",
34
- "Period",
35
- "OneParen",
36
- "TwoParens"
37
- ]
46
+ # The various delimeters of list numbers
47
+ DELIMS = [
48
+ "DefaultDelim",
49
+ "Period",
50
+ "OneParen",
51
+ "TwoParens"
52
+ ]
38
53
 
39
- attr_accessor :start, :number_style, :number_delim
40
- def initialize attributes
41
- @start = attributes[0]
42
- @number_style = attributes[1]
43
- @number_delim = attributes[2]
44
- end
54
+ attr_accessor :start, :number_style, :number_delim
45
55
 
46
- def to_ast
47
- [
48
- @start,
49
- @number_style,
50
- @number_delim
51
- ]
52
- end
56
+ # Create a new ListAttribute object with attributes
57
+ #
58
+ # @param attributes [Array] an array with start, number style, and
59
+ # number delimeter
60
+ def initialize(attributes)
61
+ @start = attributes[0]
62
+ @number_style = attributes[1]
63
+ @number_delim = attributes[2]
64
+ end
65
+
66
+ # Create an AST representation of this ListAttributes object
67
+ def to_ast()
68
+ [
69
+ @start,
70
+ @number_style,
71
+ @number_delim
72
+ ]
73
+ end
74
+ end
53
75
  end
54
- end
55
76
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright 2015, 2016 Huub de Beer <Huub@heerdebeer.org>
2
+ # Copyright 2015, 2016, 2017 Huub de Beer <Huub@heerdebeer.org>
3
3
  #
4
4
  # This file is part of Paru
5
5
  #
@@ -17,74 +17,117 @@
17
17
  # along with Paru. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
  module Paru
20
- module PandocFilter
21
- module Markdown
20
+ module PandocFilter
21
+ # A mixin to add inner_markdown and outer_markdown properties to Nodes
22
+ module Markdown
23
+ require_relative "../pandoc"
24
+ require_relative "./document"
22
25
 
23
- require_relative "../pandoc"
24
- require_relative "./document"
26
+ # A Paru::Pandoc converter from JSON to markdown
27
+ AST2MARKDOWN = Paru::Pandoc.new do
28
+ from "json"
29
+ to "markdown"
30
+ end
25
31
 
26
- AST2MARKDOWN = Paru::Pandoc.new do
27
- from "json"
28
- to "markdown"
29
- end
32
+ # A Paru::Pandoc converter from markdown to JSON
33
+ MARKDOWN2JSON = Paru::Pandoc.new do
34
+ from "markdown"
35
+ to "json"
36
+ end
30
37
 
31
- MARKDOWN2JSON = Paru::Pandoc.new do
32
- from "markdown"
33
- to "json"
34
- end
38
+ # Get the markdown representation of this Node, including the Node
39
+ # itself.
40
+ #
41
+ # @return [String] the outer markdown representation of this Node
42
+ def outer_markdown()
43
+ temp_doc = PandocFilter::Document.fragment [self]
44
+ AST2MARKDOWN << temp_doc.to_JSON
45
+ end
35
46
 
36
- def outer_markdown
37
- temp_doc = PandocFilter::Document.fragment [self]
38
- AST2MARKDOWN << temp_doc.to_JSON
39
- end
47
+ # Set the markdown representation of this Node: replace this Node
48
+ # by the Node represented by the markdown string
49
+ #
50
+ # @param markdown [String] the markdown string to replace this
51
+ # Node
52
+ #
53
+ # @example Replacing all horizontal lines by a Plain node saying "hi"
54
+ # Paru::Filter.run do
55
+ # with "HorizontalLine" do |line|
56
+ # line.outer_markdown = "hi"
57
+ # end
58
+ # end
59
+ #
60
+ def outer_markdown=(markdown)
61
+ json = MARKDOWN2JSON << markdown
62
+ temp_doc = PandocFilter::Document.from_JSON json
40
63
 
41
- def outer_markdown= markdown
42
- json = MARKDOWN2JSON << markdown
43
- temp_doc = PandocFilter::Document.from_JSON json
64
+ if not has_parent? or is_root?
65
+ @children = temp_doc.children
66
+ else
67
+ # replace current node by new nodes
68
+ # There is a difference between inline and block nodes
69
+ current_index = parent.find_index self
70
+ index = current_index
71
+ temp_doc.each do |child|
72
+ index += 1
73
+ parent.insert index, child
74
+ end
75
+ # Remove the original node
76
+ parent.remove_at current_index
77
+ end
44
78
 
45
- if not has_parent? or is_root?
46
- @children = temp_doc.children
47
- else
48
- # replace current node by new nodes
49
- # There is a difference between inline and block nodes
50
- current_index = parent.find_index self
51
- index = current_index
52
- temp_doc.each do |child|
53
- index += 1
54
- parent.insert index, child
55
- end
56
- # Remove the original node
57
- parent.remove_at current_index
58
- end
59
-
60
- end
79
+ end
61
80
 
62
- def inner_markdown
63
- temp_doc = PandocFilter::Document.fragment @children
64
- AST2MARKDOWN << temp_doc.to_JSON
65
- end
81
+ # Get the markdown representation of this Node's children
82
+ #
83
+ # @return [String] the inner markdown representation of this Node
84
+ #
85
+ # @example Replace all occurrences of "hello" by "world" in all paragraphs
86
+ # Paru::Filter.run do
87
+ # with "Para" do |p|
88
+ # p.inner_markdown = p.inner_markdown.gsub "hello", "world"
89
+ # end
90
+ # end
91
+ #
92
+ def inner_markdown()
93
+ temp_doc = PandocFilter::Document.fragment @children
94
+ AST2MARKDOWN << temp_doc.to_JSON
95
+ end
66
96
 
67
- def inner_markdown= markdown
68
- if has_string?
69
- @string = markdown
70
- else
71
- if markdown.empty?
72
- @children = []
73
- else
74
- json = MARKDOWN2JSON << markdown
75
- temp_doc = PandocFilter::Document.from_JSON json
76
- temp_doc.children.each {|c| c.parent = @parent}
97
+ # Replace this Node's children with the Nodes represented by the
98
+ # markdown string
99
+ #
100
+ # @param markdown [String] the markdown string to replace this
101
+ # Node's children
102
+ #
103
+ # @example Replace all occurrences of "hello" by "world" in all paragraphs
104
+ # Paru::Filter.run do
105
+ # with "Para" do |p|
106
+ # p.inner_markdown = p.inner_markdown.gsub "hello", "world"
107
+ # end
108
+ # end
109
+ #
110
+ def inner_markdown=(markdown)
111
+ if has_string?
112
+ @string = markdown
113
+ else
114
+ if markdown.empty?
115
+ @children = []
116
+ else
117
+ json = MARKDOWN2JSON << markdown
118
+ temp_doc = PandocFilter::Document.from_JSON json
119
+ temp_doc.children.each {|c| c.parent = @parent}
77
120
 
78
- if has_inline?
79
- @children = temp_doc.children[0].children
80
- elsif has_block?
81
- @children = temp_doc.children
82
- else
83
- # Unknown; what to do here?
121
+ if has_inline?
122
+ @children = temp_doc.children[0].children
123
+ elsif has_block?
124
+ @children = temp_doc.children
125
+ else
126
+ # Unknown; what to do here?
127
+ end
128
+ end
129
+ end
84
130
  end
85
- end
86
131
  end
87
- end
88
132
  end
89
- end
90
133
  end