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,11 +16,12 @@
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 "./attr.rb"
21
+ require_relative "./inner_markdown.rb"
22
+
19
23
  module Paru
20
24
  module PandocFilter
21
- require_relative "./block"
22
- require_relative "./attr"
23
-
24
25
  # A CodeBlock is a Block level node with an attribute object and the
25
26
  # code as a string
26
27
  #
@@ -30,6 +31,7 @@ module Paru
30
31
  # @!attribute string
31
32
  # @return [String]
32
33
  class CodeBlock < Block
34
+ include InnerMarkdown
33
35
  attr_accessor :attr, :string
34
36
 
35
37
  # Create a new CodeBlock based on the contents
@@ -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 DefinitionList is a list of term-definition pairs, respecitively an Inline list and a Block list.
24
24
  class DefinitionList < Block
25
25
  # Create a new DefinitionList node
@@ -16,12 +16,12 @@
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 "./list.rb"
21
+ require_relative "./inline.rb"
22
+
19
23
  module Paru
20
24
  module PandocFilter
21
- require_relative "./block"
22
- require_relative "./list"
23
- require_relative "./inline"
24
-
25
25
  # A DefinitionListItem is a helper node to represent the pair of a term
26
26
  # and its definition in a DefinitionList
27
27
  #
@@ -16,13 +16,15 @@
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 "./attr.rb"
21
+ require_relative "./inner_markdown.rb"
22
+
19
23
  module Paru
20
24
  module PandocFilter
21
- require_relative "./block"
22
- require_relative "./attr"
23
-
24
25
  # A Div node consisting of an attribute object and a list of Block nodes.
25
26
  class Div < Block
27
+ include InnerMarkdown
26
28
 
27
29
  # Create a new Div node based on the contents
28
30
  #
@@ -16,15 +16,17 @@
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
- module PandocFilter
19
+ require "json"
21
20
 
22
- require "json"
23
- require_relative "./node"
24
- require_relative "./plain"
25
- require_relative "./meta"
26
- require_relative "./version"
21
+ require_relative "./node.rb"
22
+ require_relative "./plain.rb"
23
+ require_relative "./meta.rb"
24
+ require_relative "./version.rb"
27
25
 
26
+ require_relative "../filter_error.rb"
27
+
28
+ module Paru
29
+ module PandocFilter
28
30
  # Pandoc type version key
29
31
  VERSION = "pandoc-api-version"
30
32
  # Pandoc type meta key
@@ -43,28 +45,56 @@ module Paru
43
45
  # @return [Meta] the metadata of this document
44
46
  class Document < Node
45
47
 
46
- attr_reader :meta
48
+ attr_accessor :meta
47
49
 
48
50
  # Create a new Document from a JSON representation of the AST
49
51
  #
50
52
  # @param json [String] a JSON string representation of the AST of a document
51
53
  # @return [Document] the newly created document
54
+ #
55
+ # @raise [ParuFilterError] when parsing JSON AST from pandoc fails
56
+ # or the parsed results do not make sense.
52
57
  def self.from_JSON(json)
53
- doc = JSON.parse json
54
- version, metadata, contents = doc.values_at(VERSION, META, BLOCKS)
58
+ begin
59
+ doc = JSON.parse json
60
+ version, metadata, contents = doc.values_at(VERSION, META, BLOCKS)
61
+ rescue Exception => e
62
+ raise FilterError.new <<WARNING
63
+ Unable to read document.
64
+
65
+ Most likely cause: Paru expects a pandoc installation that has been
66
+ compiled with pandoc-types >= #{CURRENT_PANDOC_VERSION.join('.')}. You can
67
+ check which pandoc-types have been compiled with your pandoc installation by
68
+ running `pandoc -v`.
69
+
70
+ Original error message: #{e.message}
71
+ WARNING
72
+ end
73
+
74
+ if -1 == (version <=> CURRENT_PANDOC_VERSION)
75
+ if metadata.has_key?('debug_')
76
+ warn <<WARNING
77
+ pandoc-types API version used in document (version = #{version.join('.')}) is
78
+ lower than the version of pandoc-types used by paru
79
+ (#{CURRENT_PANDOC_VERSION.join('.')}. If you experience unexpected results,
80
+ please try updating pandoc or downgrading paru.
81
+ WARNING
82
+ end
83
+ end
84
+
55
85
  PandocFilter::Document.new version, metadata, contents
56
86
  end
57
87
 
58
88
  # Create a new Document fragment from a list of Node elements
59
89
  #
60
- # @param node_list [Array<Node>] a list of nodes to create a Document
90
+ # @param node_list [Node[]] a list of nodes to create a Document
61
91
  # fragment from
62
92
  #
63
93
  # @return [Document] the document containing nodes in node_list
64
94
  def self.fragment(node_list)
65
95
  meta = Hash.new
66
96
 
67
- if node_list.any? {|n| n.is_block?}
97
+ if node_list.nil? or node_list.any? {|n| n.is_block?}
68
98
  new_doc = Document.new CURRENT_PANDOC_VERSION, meta, []
69
99
  new_doc.children = node_list
70
100
  else
@@ -79,15 +109,16 @@ module Paru
79
109
  # Create a new Document node based on the pandoc type version,
80
110
  # metadata, and the contents of the document
81
111
  #
82
- # @param version [Array<Integer>] the version of pandoc types
83
- # @param meta [Array] metadata
84
- # @param contents [Array] contents
85
- def initialize(version, meta, contents)
112
+ # @param version [Integer = CURRENT_PANDOC_VERSION] the version of pandoc types
113
+ # @param meta [Array = []] metadata
114
+ # @param contents [Array = []] contents
115
+ def initialize(version = CURRENT_PANDOC_VERSION, meta = [], contents = [])
86
116
  @version = Version.new version
87
117
  @meta = Meta.new meta
88
118
  super contents
89
119
  end
90
120
 
121
+
91
122
  # Create an AST representation of this Document
92
123
  def to_ast()
93
124
  {
@@ -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
  # An Emph, emphasized Inline
24
24
  class Emph < Inline
25
25
  end
@@ -16,15 +16,15 @@
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
  # An EmptyBlock, has not contents
24
24
  class EmptyBlock < Block
25
25
 
26
26
  # Create an empty block
27
- def initialize _
27
+ def initialize(contents = [])
28
28
  super []
29
29
  end
30
30
 
@@ -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
  # An EmptyInline node, has no content
24
24
  class EmptyInline < Inline
25
25
 
@@ -16,11 +16,12 @@
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 "./attr.rb"
21
+ require_relative "./inner_markdown.rb"
22
+
19
23
  module Paru
20
24
  module PandocFilter
21
- require_relative "./block"
22
- require_relative "./attr"
23
-
24
25
  # A Header node has a level, an attribute object and the contents of
25
26
  # the header as a list on Inline nodes.
26
27
  #
@@ -30,6 +31,8 @@ module Paru
30
31
  # @!attribute attr
31
32
  # @return [Attr]
32
33
  class Header < Block
34
+ include InnerMarkdown
35
+
33
36
  attr_accessor :level, :attr
34
37
 
35
38
  # Create a new Header node
@@ -16,12 +16,12 @@
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
- module PandocFilter
21
- require_relative "./empty_block"
19
+ require_relative "./empty_block.rb"
22
20
 
23
- # HorizontalRule
24
- class HorizontalRule < EmptyBlock
21
+ module Paru
22
+ module PandocFilter
23
+ # HorizontalRule
24
+ class HorizontalRule < EmptyBlock
25
+ end
25
26
  end
26
- end
27
27
  end
@@ -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 "./link.rb"
20
+
19
21
  module Paru
20
22
  module PandocFilter
21
-
22
- require_relative "./link"
23
-
24
23
  # An Image has an attribute object, a caption, and a target
25
24
  class Image < Link
26
25
 
@@ -16,13 +16,15 @@
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
+ require_relative "./inner_markdown.rb"
21
+
19
22
  module Paru
20
23
  module PandocFilter
21
- require_relative "./node"
22
-
23
24
  # An Inline node
24
25
  # @see http://hackage.haskell.org/package/pandoc-types-1.17.0.4/docs/Text-Pandoc-Definition.html#t:Inline
25
26
  class Inline < Node
27
+ include InnerMarkdown
26
28
 
27
29
  # Create a new Inline node with contents
28
30
  # @param contents [Array] the contents of this inline node
@@ -43,6 +45,7 @@ module Paru
43
45
  def has_inline?()
44
46
  true
45
47
  end
48
+
46
49
  end
47
50
  end
48
51
  end
@@ -0,0 +1,85 @@
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
+ #++
19
+ module Paru
20
+ module PandocFilter
21
+
22
+ # A mixin to add inner_markdown properties to Nodes for which it makes
23
+ # sense to have an inner_markdown. Only those nodes that have a clear
24
+ # identifiable Inline level content, have the {#inner_markdown}
25
+ # method. This are almost all Inline nodes (except Cite) and Block
26
+ # level nodes with Inline contents like {Para} or {Header}.
27
+ module InnerMarkdown
28
+
29
+ # Get the markdown representation of this Node's children.
30
+ #
31
+ # @return [String] the inner markdown representation of this Node
32
+ #
33
+ # @example Replace all occurrences of "hello" by "world" in all paragraphs
34
+ # Paru::Filter.run do
35
+ # with "Para" do |p|
36
+ # p.inner_markdown = p.inner_markdown.gsub "hello", "world"
37
+ # end
38
+ # end
39
+ #
40
+ def inner_markdown()
41
+ if has_children?
42
+ temp_doc = PandocFilter::Document.fragment @children
43
+ AST2MARKDOWN << temp_doc.to_JSON
44
+ elsif has_string?
45
+ @string
46
+ end
47
+ end
48
+
49
+ # Replace this Node's children with the Nodes represented by the
50
+ # markdown string
51
+ #
52
+ # @param markdown [String] the markdown string to replace this
53
+ # Node's children
54
+ #
55
+ # @example Replace all occurrences of "hello" by "world" in all paragraphs
56
+ # Paru::Filter.run do
57
+ # with "Para" do |p|
58
+ # p.inner_markdown = p.inner_markdown.gsub "hello", "world"
59
+ # end
60
+ # end
61
+ #
62
+ def inner_markdown=(markdown)
63
+ if has_string?
64
+ @string = markdown
65
+ else
66
+ if markdown.nil? or markdown.empty?
67
+ @children = []
68
+ else
69
+ json = MARKDOWN2JSON << markdown
70
+ temp_doc = PandocFilter::Document.from_JSON json
71
+ temp_doc.children.each {|c| c.parent = @parent}
72
+
73
+ if has_inline?
74
+ @children = temp_doc.children.first.children
75
+ elsif has_block?
76
+ @children = temp_doc.children
77
+ else
78
+ # Unknown; what to do here?
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -16,12 +16,18 @@
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.rb"
22
-
23
23
  # A LineBlock is a List of Lists of Inline nodes
24
24
  class LineBlock < List
25
+ # Create a new LineBlock node based on contents
26
+ #
27
+ # @param contents [Array] the contents of the LineBlock
28
+ def initialize(contents)
29
+ super(contents, Para)
30
+ end
25
31
  end
26
32
  end
27
33
  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 LineBreak is an empty inline element
24
24
  class LineBreak < EmptyInline
25
25
  end
@@ -16,13 +16,12 @@
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
+ require_relative "./target.rb"
22
+
19
23
  module Paru
20
24
  module PandocFilter
21
-
22
- require_relative "./inline"
23
- require_relative "./attr"
24
- require_relative "./target"
25
-
26
25
  # A Link node has an attribute object, an Inline description and a
27
26
  # target
28
27
  #