paru 0.2.5b → 0.2.5c
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/paru/filter.rb +39 -12
- data/lib/paru/filter/meta_map.rb +9 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e39519f32865af110131f673e16b6e4a99f567d0
|
4
|
+
data.tar.gz: bfc70e18a105a8488cc4ba06b2f3cc393e7bb9b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1169e0f995d1722251fcfbb0107c386aaf6d5f0df98ead73fac6adf64276bf7715e1f4003c512186cd41d228a38f007fa26cc18cb62c033f3c69e863a570268
|
7
|
+
data.tar.gz: 12f8bbb1532f99347ac624b56215b2932bf56db09ac2d4a8f3583601fe5aadbc2df071ba4b183b07d1165c3e29f3e111743c677854cc967677341e62c24bcb44
|
data/lib/paru/filter.rb
CHANGED
@@ -198,11 +198,23 @@ module Paru
|
|
198
198
|
#
|
199
199
|
class Filter
|
200
200
|
|
201
|
-
#
|
202
|
-
#
|
203
|
-
#
|
204
|
-
#
|
205
|
-
#
|
201
|
+
# Create a new Filter instance. For convenience, {run} creates a new
|
202
|
+
# {Filter} and runs it immediately. Use this constructor if you want
|
203
|
+
# to run a filter on different input and output streams that STDIN and
|
204
|
+
# STDOUT respectively.
|
205
|
+
#
|
206
|
+
# @param input [IO = $stdin] the input stream to read, defaults to
|
207
|
+
# STDIN
|
208
|
+
# @param output [IO = $stdout] the output stream to write, defaults to
|
209
|
+
# STDOUT
|
210
|
+
def initialize(input = $stdin, output = $stdout)
|
211
|
+
@input = input
|
212
|
+
@output = output
|
213
|
+
end
|
214
|
+
|
215
|
+
# Run the filter specified by block. This is a convenience method that
|
216
|
+
# creates a new {Filter} using input stream STDIN and output stream
|
217
|
+
# STDOUT and immediately runs {filter} with the block supplied.
|
206
218
|
#
|
207
219
|
# @param block [Proc] the filter specification
|
208
220
|
#
|
@@ -213,23 +225,39 @@ module Paru
|
|
213
225
|
# end
|
214
226
|
# end
|
215
227
|
def self.run(&block)
|
216
|
-
Filter.new().filter(&block)
|
228
|
+
Filter.new($stdin, $stdout).filter(&block)
|
217
229
|
end
|
218
230
|
|
219
|
-
|
220
231
|
# The Document node from JSON formatted pandoc document structure
|
221
232
|
# on STDIN that is being filtered
|
222
233
|
#
|
223
234
|
# @return [Document] create a new Document node from a pandoc AST from
|
224
235
|
# JSON from STDIN
|
225
236
|
def document()
|
226
|
-
PandocFilter::Document.from_JSON
|
237
|
+
PandocFilter::Document.from_JSON @input.read
|
227
238
|
end
|
228
239
|
|
229
|
-
# Create a filter using +block+.
|
240
|
+
# Create a filter using +block+. In the block you specify
|
241
|
+
# selectors and actions to be performed on selected nodes. In the
|
242
|
+
# example below, the selector is "Image", which selects all image
|
243
|
+
# nodes. The action is to prepend the contents of the image's caption
|
244
|
+
# by the string "Figure. ".
|
245
|
+
#
|
246
|
+
# @param block [Proc] the filter specification
|
230
247
|
#
|
231
|
-
# @param block [Proc] a block specifying selectors and actions
|
232
248
|
# @return [JSON] a JSON string with the filtered pandoc AST
|
249
|
+
#
|
250
|
+
# @example Add 'Figure' to each image's caption
|
251
|
+
# input = IOString.new(File.read("my_report.md")
|
252
|
+
# output = IOString.new
|
253
|
+
#
|
254
|
+
# Paru::Filter.new(input, output).filter do
|
255
|
+
# with "Image" do |image|
|
256
|
+
# image.inner_markdown = "Figure. #{image.inner_markdown}"
|
257
|
+
# end
|
258
|
+
# end
|
259
|
+
#
|
260
|
+
# # do something with output.string
|
233
261
|
def filter(&block)
|
234
262
|
@selectors = Hash.new
|
235
263
|
@filtered_nodes = []
|
@@ -240,10 +268,9 @@ module Paru
|
|
240
268
|
instance_eval(&block)
|
241
269
|
end
|
242
270
|
|
243
|
-
|
271
|
+
@output.write @doc.to_JSON
|
244
272
|
end
|
245
273
|
|
246
|
-
|
247
274
|
# +current_node+ points to the node that is *now* being processed while
|
248
275
|
# running this filter.
|
249
276
|
#
|
data/lib/paru/filter/meta_map.rb
CHANGED
@@ -42,7 +42,7 @@ module Paru
|
|
42
42
|
end
|
43
43
|
|
44
44
|
# Get the value belonging to key. Prefer to use the {has?}, {get},
|
45
|
-
# and {delete} methods to manipulate metadata.
|
45
|
+
# {replace} and {delete} methods to manipulate metadata.
|
46
46
|
#
|
47
47
|
# @param key [String] the key
|
48
48
|
#
|
@@ -56,9 +56,7 @@ module Paru
|
|
56
56
|
# to set the metadata.
|
57
57
|
#
|
58
58
|
# @param key [String] the key to set
|
59
|
-
# @param value
|
60
|
-
# [MetaBlocks|MetaBool|MetaInline|MetaList|MetaMap|MetaString|MetaValue]
|
61
|
-
# the value to set
|
59
|
+
# @param value [MetaBlocks|MetaBool|MetaInlines|MetaList|MetaMap|MetaString|MetaValue] the value to set
|
62
60
|
def []=(key, value)
|
63
61
|
@children[key] = value
|
64
62
|
end
|
@@ -73,6 +71,7 @@ module Paru
|
|
73
71
|
# Mixin the YAML code into this metadata object
|
74
72
|
#
|
75
73
|
# @param yaml_string [YAML] A string with YAML data
|
74
|
+
# @return [MetaMap] this MetaMap object
|
76
75
|
#
|
77
76
|
# @example Set some properties in the metadata
|
78
77
|
# #!/usr/bin/env ruby
|
@@ -82,7 +81,7 @@ module Paru
|
|
82
81
|
# Paru::Filter.run do
|
83
82
|
# metadata.yaml <<~YAML
|
84
83
|
# ---
|
85
|
-
# date: Date.today.to_s
|
84
|
+
# date: #{Date.today.to_s}
|
86
85
|
# title: This **is** the title
|
87
86
|
# pandoc_options:
|
88
87
|
# from: markdown
|
@@ -99,6 +98,7 @@ module Paru
|
|
99
98
|
meta_from_yaml(yaml_string).each do |key, value|
|
100
99
|
self[key] = value
|
101
100
|
end
|
101
|
+
self
|
102
102
|
end
|
103
103
|
|
104
104
|
# Replace the property in this MetaMap matching the selector with
|
@@ -108,8 +108,7 @@ module Paru
|
|
108
108
|
# @param selector [String] a dot-separated sequence of property
|
109
109
|
# names denoting a sequence of descendants.
|
110
110
|
#
|
111
|
-
# @param value
|
112
|
-
# [MetaBlocks|MetaBool|MetaInline|MetaList|MetaMap|MetaString|MetaValue|String]
|
111
|
+
# @param value [MetaBlocks|MetaBool|MetaInlines|MetaList|MetaMap|MetaString|MetaValue|String]
|
113
112
|
# if value is a String, it is treated as a yaml string
|
114
113
|
def replace(selector, value)
|
115
114
|
parent = select(selector, true)
|
@@ -124,7 +123,7 @@ module Paru
|
|
124
123
|
# @param selector [String] a dot-separated sequence of property
|
125
124
|
# names denoting a sequence of descendants.
|
126
125
|
#
|
127
|
-
# @return [MetaBlocks|MetaBool|
|
126
|
+
# @return [MetaBlocks|MetaBool|MetaInlines|MetaList|MetaMap|MetaString|MetaValue] the value matching the selected property, nil if it cannot be found
|
128
127
|
def get(selector)
|
129
128
|
select(selector)
|
130
129
|
end
|
@@ -145,14 +144,13 @@ module Paru
|
|
145
144
|
# @param selector [String] a dot-separated sequence of property
|
146
145
|
# names denoting a sequence of descendants
|
147
146
|
#
|
148
|
-
# @return [MetaBlocks|MetaBool|
|
147
|
+
# @return [MetaBlocks|MetaBool|MetaInlines|MetaList|MetaMap|MetaString|MetaValue] the value of the deleted property, nil if it cannot be found
|
149
148
|
#
|
150
149
|
def delete(selector)
|
151
150
|
parent = select(selector, true)
|
152
151
|
parent.children.delete(last_key(selector))
|
153
152
|
end
|
154
153
|
|
155
|
-
|
156
154
|
# The AST contents
|
157
155
|
def ast_contents()
|
158
156
|
ast = Hash.new
|
@@ -173,7 +171,7 @@ module Paru
|
|
173
171
|
# @param get_parent [Boolean = false] Get the parent MetaMap
|
174
172
|
# of the selected property instead of its value
|
175
173
|
#
|
176
|
-
# @return [MetaBlocks|MetaBool|
|
174
|
+
# @return [MetaBlocks|MetaBool|MetaInlines|MetaList|MetaMap|MetaString|MetaValue] the value of the deleted property, nil if it cannot be found
|
177
175
|
def select(selector, get_parent = false)
|
178
176
|
keys = selector.split(".")
|
179
177
|
level = self
|