paru 0.2.5c → 0.2.5f
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/do-pandoc.rb +5 -5
- data/bin/pandoc2yaml.rb +23 -58
- data/lib/paru.rb +1 -5
- data/lib/paru/error.rb +0 -2
- data/lib/paru/filter.rb +46 -30
- data/lib/paru/filter/ast_manipulation.rb +0 -1
- data/lib/paru/filter/attr.rb +0 -1
- data/lib/paru/filter/block.rb +2 -2
- data/lib/paru/filter/block_quote.rb +2 -2
- data/lib/paru/filter/bullet_list.rb +2 -2
- data/lib/paru/filter/citation.rb +2 -2
- data/lib/paru/filter/cite.rb +8 -4
- data/lib/paru/filter/code.rb +11 -3
- data/lib/paru/filter/code_block.rb +5 -3
- data/lib/paru/filter/definition_list.rb +2 -2
- data/lib/paru/filter/definition_list_item.rb +4 -4
- data/lib/paru/filter/div.rb +5 -3
- data/lib/paru/filter/document.rb +47 -16
- data/lib/paru/filter/emph.rb +2 -2
- data/lib/paru/filter/empty_block.rb +3 -3
- data/lib/paru/filter/empty_inline.rb +2 -2
- data/lib/paru/filter/header.rb +6 -3
- data/lib/paru/filter/horizontal_rule.rb +6 -6
- data/lib/paru/filter/image.rb +2 -3
- data/lib/paru/filter/inline.rb +5 -2
- data/lib/paru/filter/inner_markdown.rb +85 -0
- data/lib/paru/filter/line_block.rb +8 -2
- data/lib/paru/filter/line_break.rb +2 -2
- data/lib/paru/filter/link.rb +4 -5
- data/lib/paru/filter/list.rb +8 -6
- data/lib/paru/filter/math.rb +2 -2
- data/lib/paru/filter/meta.rb +15 -4
- data/lib/paru/filter/meta_blocks.rb +2 -2
- data/lib/paru/filter/meta_bool.rb +2 -3
- data/lib/paru/filter/meta_inlines.rb +4 -2
- data/lib/paru/filter/meta_list.rb +2 -3
- data/lib/paru/filter/meta_map.rb +10 -142
- data/lib/paru/filter/meta_string.rb +1 -1
- data/lib/paru/filter/meta_value.rb +5 -4
- data/lib/paru/filter/metadata.rb +114 -0
- data/lib/paru/filter/node.rb +130 -11
- data/lib/paru/filter/note.rb +3 -4
- data/lib/paru/filter/null.rb +2 -2
- data/lib/paru/filter/ordered_list.rb +5 -5
- data/lib/paru/filter/para.rb +4 -2
- data/lib/paru/filter/plain.rb +4 -2
- data/lib/paru/filter/quoted.rb +2 -2
- data/lib/paru/filter/raw_block.rb +5 -3
- data/lib/paru/filter/raw_inline.rb +2 -3
- data/lib/paru/filter/small_caps.rb +2 -2
- data/lib/paru/filter/soft_break.rb +2 -2
- data/lib/paru/filter/space.rb +2 -2
- data/lib/paru/filter/span.rb +3 -4
- data/lib/paru/filter/str.rb +2 -2
- data/lib/paru/filter/strikeout.rb +2 -2
- data/lib/paru/filter/strong.rb +2 -2
- data/lib/paru/filter/subscript.rb +2 -2
- data/lib/paru/filter/superscript.rb +2 -2
- data/lib/paru/filter/table.rb +3 -3
- data/lib/paru/filter/table_row.rb +2 -2
- data/lib/paru/filter/target.rb +0 -1
- data/lib/paru/filter/version.rb +2 -2
- data/lib/paru/filter_error.rb +25 -0
- data/lib/paru/pandoc.rb +3 -4
- data/lib/paru/pandoc2yaml.rb +71 -0
- data/lib/paru/selector.rb +2 -4
- metadata +6 -3
- data/lib/paru/filter/markdown.rb +0 -150
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d36825678ac43efb4419d37a42033055a161fe8
|
4
|
+
data.tar.gz: c639c47e276492541231e67cc3a683b4b68cc3eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6384ccf86bd98e102fe068f6ceee9221a54758396f96e1f3787b16d8474530d6f683df255f845de565ffd98f80cea11a6df8b11155eccd758471758845291558
|
7
|
+
data.tar.gz: 2220576d8c4c53fd8dfe0dd9bf5855ed82f55064ca9c87d3810dafdd418f15c7280ac54ef6b407015096319e9cb3aa00c783af5b8ccd9b90ed14df4a2c6dc150
|
data/bin/do-pandoc.rb
CHANGED
@@ -2,9 +2,7 @@
|
|
2
2
|
require "yaml"
|
3
3
|
require 'optparse'
|
4
4
|
require "paru/pandoc"
|
5
|
-
|
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
|
-
|
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 #{
|
68
|
+
warn "Unsure what to do: no pandoc options in #{input_document}"
|
69
69
|
end
|
data/bin/pandoc2yaml.rb
CHANGED
@@ -9,86 +9,51 @@
|
|
9
9
|
# pandoc2yaml.rb input_file
|
10
10
|
#
|
11
11
|
##
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
require "json"
|
13
|
+
require 'optparse'
|
14
|
+
require 'paru/pandoc2yaml'
|
15
15
|
|
16
|
-
|
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
|
-
|
60
|
-
|
23
|
+
puts opts
|
24
|
+
exit
|
61
25
|
end
|
62
26
|
|
63
27
|
opts.on("-v", "--version", "Show version") do
|
64
|
-
|
65
|
-
|
28
|
+
puts "pandoc2yaml.rb is part of paru version 0.2.3"
|
29
|
+
exit
|
66
30
|
end
|
67
|
-
|
31
|
+
end
|
68
32
|
|
69
|
-
|
33
|
+
parser.parse! ARGV
|
70
34
|
|
71
|
-
|
35
|
+
input_document = ARGV.pop
|
72
36
|
|
73
|
-
|
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
|
-
|
42
|
+
end
|
79
43
|
|
80
|
-
|
81
|
-
|
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
|
-
|
48
|
+
end
|
85
49
|
|
86
|
-
|
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
|
data/lib/paru.rb
CHANGED
data/lib/paru/error.rb
CHANGED
@@ -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
|
data/lib/paru/filter.rb
CHANGED
@@ -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
|
-
|
20
|
-
|
21
|
-
|
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::
|
108
|
-
# {PandocFilter::Node#
|
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::
|
173
|
-
# {PandocFilter::Node#
|
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.
|
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
|
-
@
|
260
|
+
@document = read_document
|
261
|
+
|
262
|
+
@metadata = PandocFilter::Metadata.new @document.meta
|
265
263
|
|
266
|
-
@
|
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
|
-
@
|
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
|
-
#
|
293
|
-
#
|
294
|
-
#
|
295
|
-
#
|
296
|
-
def
|
297
|
-
|
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
|
data/lib/paru/filter/attr.rb
CHANGED
data/lib/paru/filter/block.rb
CHANGED
@@ -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
|
data/lib/paru/filter/citation.rb
CHANGED
@@ -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
|
#
|
data/lib/paru/filter/cite.rb
CHANGED
@@ -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
|
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
|
data/lib/paru/filter/code.rb
CHANGED
@@ -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
|