paru 0.2.4.8 → 0.2.5b
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.
- checksums.yaml +4 -4
- data/bin/do-pandoc.rb +5 -5
- data/bin/pandoc2yaml.rb +58 -23
- data/lib/paru/filter/document.rb +6 -35
- data/lib/paru/filter/meta_map.rb +13 -15
- data/lib/paru/filter/meta_value.rb +1 -1
- data/lib/paru/filter/raw_block.rb +1 -1
- data/lib/paru/filter.rb +13 -44
- data/lib/paru/selector.rb +1 -0
- data/lib/paru.rb +2 -4
- metadata +4 -6
- data/lib/paru/filter_error.rb +0 -24
- data/lib/paru/pandoc2yaml.rb +0 -71
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87a2a350eeeb33246aa3d841ca14062935c29806
|
4
|
+
data.tar.gz: 92e889e2dd7b06f6966327406cb8fed6d3b2f3dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 616a84c6f967318840a95e4c4f993a63e48cc461e95bab75981dc92b35a8b32db473a1878dc46371893a2b875eddbc964d2974a616ebc0f37c1f5c8b092f5015
|
7
|
+
data.tar.gz: 4ce02c9ad9a3983c2751bda198efcbdab1701ad9e5a5f181d03e5cf440ebc7a98f2390723c635fffb327b65e44a7856c4564e8e67e8ed26c60d2273760178c8d
|
data/bin/do-pandoc.rb
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
require "yaml"
|
3
3
|
require 'optparse'
|
4
4
|
require "paru/pandoc"
|
5
|
-
|
5
|
+
require_relative "./pandoc2yaml.rb"
|
6
|
+
|
7
|
+
include Pandoc2Yaml
|
6
8
|
|
7
9
|
parser = OptionParser.new do |opts|
|
8
10
|
opts.banner = "do-pandoc.rb runs pandoc on an input file using the pandoc configuration specified in that input file."
|
@@ -42,9 +44,7 @@ if !File.readable? document
|
|
42
44
|
warn "Cannot read file: #{input_document}"
|
43
45
|
exit
|
44
46
|
end
|
45
|
-
|
46
|
-
yaml = Paru::Pandoc2Yaml.extract_metadata(document)
|
47
|
-
metadata = YAML.load yaml
|
47
|
+
metadata = YAML.load Pandoc2Yaml.extract_metadata(document)
|
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}"
|
69
69
|
end
|
data/bin/pandoc2yaml.rb
CHANGED
@@ -9,51 +9,86 @@
|
|
9
9
|
# pandoc2yaml.rb input_file
|
10
10
|
#
|
11
11
|
##
|
12
|
-
|
13
|
-
require
|
14
|
-
require
|
12
|
+
module Pandoc2Yaml
|
13
|
+
require "json"
|
14
|
+
require "paru/pandoc"
|
15
15
|
|
16
|
-
|
16
|
+
# Paru converters:
|
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|
|
17
53
|
opts.banner = "pandoc2yaml.rb mines a pandoc markdown file for its YAML metadata"
|
18
54
|
opts.banner << "\n\nUsage: pandoc2yaml.rb some-pandoc-markdownfile.md"
|
19
55
|
opts.separator ""
|
20
56
|
opts.separator "Common options"
|
21
57
|
|
22
58
|
opts.on_tail("-h", "--help", "Show this message") do
|
23
|
-
|
24
|
-
|
59
|
+
puts opts
|
60
|
+
exit
|
25
61
|
end
|
26
62
|
|
27
63
|
opts.on("-v", "--version", "Show version") do
|
28
|
-
|
29
|
-
|
64
|
+
puts "pandoc2yaml.rb is part of paru version 0.2.3"
|
65
|
+
exit
|
30
66
|
end
|
31
|
-
end
|
67
|
+
end
|
32
68
|
|
33
|
-
parser.parse! ARGV
|
69
|
+
parser.parse! ARGV
|
34
70
|
|
35
|
-
input_document = ARGV.pop
|
71
|
+
input_document = ARGV.pop
|
36
72
|
|
37
|
-
if ARGV.size != 0 then
|
73
|
+
if ARGV.size != 0 then
|
38
74
|
warn "Expecting exactly one argument: the pandoc file to strip for metadata"
|
39
75
|
puts ""
|
40
76
|
puts parser
|
41
77
|
exit
|
42
|
-
end
|
78
|
+
end
|
43
79
|
|
44
|
-
document = File.expand_path input_document
|
45
|
-
if not File.exist? document
|
80
|
+
document = File.expand_path input_document
|
81
|
+
if not File.exist? document
|
46
82
|
warn "Cannot find file: #{input_document}"
|
47
83
|
exit
|
48
|
-
end
|
84
|
+
end
|
49
85
|
|
50
|
-
if !File.readable? document
|
86
|
+
if !File.readable? document
|
51
87
|
warn "Cannot read file: #{input_document}"
|
52
88
|
exit
|
53
|
-
end
|
54
|
-
|
55
|
-
yaml = Paru::Pandoc2Yaml.extract_metadata(document)
|
89
|
+
end
|
56
90
|
|
57
|
-
|
58
|
-
|
59
|
-
puts
|
91
|
+
output_metadata = Pandoc2Yaml.extract_metadata document
|
92
|
+
|
93
|
+
puts output_metadata
|
94
|
+
end
|
data/lib/paru/filter/document.rb
CHANGED
@@ -20,11 +20,10 @@ module Paru
|
|
20
20
|
module PandocFilter
|
21
21
|
|
22
22
|
require "json"
|
23
|
-
require_relative "./node
|
24
|
-
require_relative "./plain
|
25
|
-
require_relative "./meta
|
26
|
-
require_relative "./version
|
27
|
-
require_relative "../filter_error.rb"
|
23
|
+
require_relative "./node"
|
24
|
+
require_relative "./plain"
|
25
|
+
require_relative "./meta"
|
26
|
+
require_relative "./version"
|
28
27
|
|
29
28
|
# Pandoc type version key
|
30
29
|
VERSION = "pandoc-api-version"
|
@@ -50,37 +49,9 @@ module Paru
|
|
50
49
|
#
|
51
50
|
# @param json [String] a JSON string representation of the AST of a document
|
52
51
|
# @return [Document] the newly created document
|
53
|
-
#
|
54
|
-
# @raise [ParuFilterError] when parsing JSON AST from pandoc fails
|
55
|
-
# or the parsed results do not make sense.
|
56
52
|
def self.from_JSON(json)
|
57
|
-
|
58
|
-
|
59
|
-
version, metadata, contents = doc.values_at(VERSION, META, BLOCKS)
|
60
|
-
rescue Exception => e
|
61
|
-
raise FilterError.new <<WARNING
|
62
|
-
Unable to read document.
|
63
|
-
|
64
|
-
Most likely cause: Paru expects a pandoc installation that has been
|
65
|
-
compiled with pandoc-types >= #{CURRENT_PANDOC_VERSION.join('.')}. You can
|
66
|
-
check which pandoc-types have been compiled with your pandoc installation by
|
67
|
-
running `pandoc -v`.
|
68
|
-
|
69
|
-
Original error message: #{e.message}
|
70
|
-
WARNING
|
71
|
-
end
|
72
|
-
|
73
|
-
if -1 == (version <=> CURRENT_PANDOC_VERSION)
|
74
|
-
if metadata.has_key?('_debug')
|
75
|
-
warn <<WARNING
|
76
|
-
pandoc-types API version used in document (version = #{version.join('.')}) is
|
77
|
-
lower than the version of pandoc-types used by paru
|
78
|
-
(#{CURRENT_PANDOC_VERSION.join('.')}. If you experience unexpected results,
|
79
|
-
please try updating pandoc or downgrading paru.
|
80
|
-
WARNING
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
53
|
+
doc = JSON.parse json
|
54
|
+
version, metadata, contents = doc.values_at(VERSION, META, BLOCKS)
|
84
55
|
PandocFilter::Document.new version, metadata, contents
|
85
56
|
end
|
86
57
|
|
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
|
-
#
|
45
|
+
# and {delete} methods to manipulate metadata.
|
46
46
|
#
|
47
47
|
# @param key [String] the key
|
48
48
|
#
|
@@ -56,7 +56,9 @@ module Paru
|
|
56
56
|
# to set the metadata.
|
57
57
|
#
|
58
58
|
# @param key [String] the key to set
|
59
|
-
# @param value
|
59
|
+
# @param value
|
60
|
+
# [MetaBlocks|MetaBool|MetaInline|MetaList|MetaMap|MetaString|MetaValue]
|
61
|
+
# the value to set
|
60
62
|
def []=(key, value)
|
61
63
|
@children[key] = value
|
62
64
|
end
|
@@ -71,7 +73,6 @@ module Paru
|
|
71
73
|
# Mixin the YAML code into this metadata object
|
72
74
|
#
|
73
75
|
# @param yaml_string [YAML] A string with YAML data
|
74
|
-
# @return [MetaMap] this MetaMap object
|
75
76
|
#
|
76
77
|
# @example Set some properties in the metadata
|
77
78
|
# #!/usr/bin/env ruby
|
@@ -81,7 +82,7 @@ module Paru
|
|
81
82
|
# Paru::Filter.run do
|
82
83
|
# metadata.yaml <<~YAML
|
83
84
|
# ---
|
84
|
-
# date:
|
85
|
+
# date: Date.today.to_s
|
85
86
|
# title: This **is** the title
|
86
87
|
# pandoc_options:
|
87
88
|
# from: markdown
|
@@ -98,7 +99,6 @@ module Paru
|
|
98
99
|
meta_from_yaml(yaml_string).each do |key, value|
|
99
100
|
self[key] = value
|
100
101
|
end
|
101
|
-
self
|
102
102
|
end
|
103
103
|
|
104
104
|
# Replace the property in this MetaMap matching the selector with
|
@@ -108,7 +108,8 @@ 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
|
111
|
+
# @param value
|
112
|
+
# [MetaBlocks|MetaBool|MetaInline|MetaList|MetaMap|MetaString|MetaValue|String]
|
112
113
|
# if value is a String, it is treated as a yaml string
|
113
114
|
def replace(selector, value)
|
114
115
|
parent = select(selector, true)
|
@@ -123,7 +124,7 @@ module Paru
|
|
123
124
|
# @param selector [String] a dot-separated sequence of property
|
124
125
|
# names denoting a sequence of descendants.
|
125
126
|
#
|
126
|
-
# @return [MetaBlocks|MetaBool|
|
127
|
+
# @return [MetaBlocks|MetaBool|MetaInline|MetaList|MetaMap|MetaString|MetaValue] the value matching the selected property, nil if it cannot be found
|
127
128
|
def get(selector)
|
128
129
|
select(selector)
|
129
130
|
end
|
@@ -144,17 +145,14 @@ module Paru
|
|
144
145
|
# @param selector [String] a dot-separated sequence of property
|
145
146
|
# names denoting a sequence of descendants
|
146
147
|
#
|
147
|
-
# @return [MetaBlocks|MetaBool|
|
148
|
+
# @return [MetaBlocks|MetaBool|MetaInline|MetaList|MetaMap|MetaString|MetaValue] the value of the deleted property, nil if it cannot be found
|
148
149
|
#
|
149
150
|
def delete(selector)
|
150
|
-
|
151
|
-
|
152
|
-
parent.children.delete(last_key(selector))
|
153
|
-
else
|
154
|
-
nil
|
155
|
-
end
|
151
|
+
parent = select(selector, true)
|
152
|
+
parent.children.delete(last_key(selector))
|
156
153
|
end
|
157
154
|
|
155
|
+
|
158
156
|
# The AST contents
|
159
157
|
def ast_contents()
|
160
158
|
ast = Hash.new
|
@@ -175,7 +173,7 @@ module Paru
|
|
175
173
|
# @param get_parent [Boolean = false] Get the parent MetaMap
|
176
174
|
# of the selected property instead of its value
|
177
175
|
#
|
178
|
-
# @return [MetaBlocks|MetaBool|
|
176
|
+
# @return [MetaBlocks|MetaBool|MetaInline|MetaList|MetaMap|MetaString|MetaValue] the value of the deleted property, nil if it cannot be found
|
179
177
|
def select(selector, get_parent = false)
|
180
178
|
keys = selector.split(".")
|
181
179
|
level = self
|
data/lib/paru/filter.rb
CHANGED
@@ -19,7 +19,7 @@
|
|
19
19
|
module Paru
|
20
20
|
|
21
21
|
require_relative "./selector"
|
22
|
-
require_relative "
|
22
|
+
require_relative "filter/document"
|
23
23
|
|
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].
|
@@ -198,23 +198,11 @@ module Paru
|
|
198
198
|
#
|
199
199
|
class Filter
|
200
200
|
|
201
|
-
#
|
202
|
-
#
|
203
|
-
#
|
204
|
-
#
|
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.
|
201
|
+
# Run the filter specified by block. In the block you specify
|
202
|
+
# selectors and actions to be performed on selected nodes. In the
|
203
|
+
# example below, the selector is "Image", which selects all image
|
204
|
+
# nodes. The action is to prepend the contents of the image's caption
|
205
|
+
# by the string "Figure. ".
|
218
206
|
#
|
219
207
|
# @param block [Proc] the filter specification
|
220
208
|
#
|
@@ -225,39 +213,23 @@ module Paru
|
|
225
213
|
# end
|
226
214
|
# end
|
227
215
|
def self.run(&block)
|
228
|
-
Filter.new(
|
216
|
+
Filter.new().filter(&block)
|
229
217
|
end
|
230
218
|
|
219
|
+
|
231
220
|
# The Document node from JSON formatted pandoc document structure
|
232
221
|
# on STDIN that is being filtered
|
233
222
|
#
|
234
223
|
# @return [Document] create a new Document node from a pandoc AST from
|
235
224
|
# JSON from STDIN
|
236
225
|
def document()
|
237
|
-
PandocFilter::Document.from_JSON
|
226
|
+
PandocFilter::Document.from_JSON $stdin.read
|
238
227
|
end
|
239
228
|
|
240
|
-
# Create a filter using +block+.
|
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
|
229
|
+
# Create a filter using +block+.
|
247
230
|
#
|
231
|
+
# @param block [Proc] a block specifying selectors and actions
|
248
232
|
# @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
|
261
233
|
def filter(&block)
|
262
234
|
@selectors = Hash.new
|
263
235
|
@filtered_nodes = []
|
@@ -268,9 +240,10 @@ module Paru
|
|
268
240
|
instance_eval(&block)
|
269
241
|
end
|
270
242
|
|
271
|
-
|
243
|
+
puts @doc.to_JSON
|
272
244
|
end
|
273
245
|
|
246
|
+
|
274
247
|
# +current_node+ points to the node that is *now* being processed while
|
275
248
|
# running this filter.
|
276
249
|
#
|
@@ -298,8 +271,4 @@ module Paru
|
|
298
271
|
end
|
299
272
|
|
300
273
|
end
|
301
|
-
|
302
|
-
# FilterError is thrown when there is an error during fitlering
|
303
|
-
class FilterError < Error
|
304
|
-
end
|
305
274
|
end
|
data/lib/paru/selector.rb
CHANGED
@@ -25,6 +25,7 @@ module Paru
|
|
25
25
|
class SelectorParseError < Error
|
26
26
|
end
|
27
27
|
|
28
|
+
|
28
29
|
# A Selector models a relationship between Pandoc AST nodes, such as
|
29
30
|
# parent-child or sibling. Selectors in paru are like CSS selectors, but
|
30
31
|
# more limited because the Pandoc AST is quite simple.
|
data/lib/paru.rb
CHANGED
@@ -18,11 +18,9 @@
|
|
18
18
|
#++
|
19
19
|
module Paru
|
20
20
|
require "paru/pandoc"
|
21
|
-
require "paru/filter"
|
22
|
-
require "paru/filter_error"
|
23
21
|
require "paru/error"
|
24
|
-
require "paru/
|
22
|
+
require "paru/filter"
|
25
23
|
|
26
24
|
# Paru's current version
|
27
|
-
VERSION = [0, 2, 4,
|
25
|
+
VERSION = [0, 2, 4, 4]
|
28
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5b
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Huub de Beer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Use Pandoc (http://www.pandoc.org) with ruby
|
14
14
|
email: Huub@heerdebeer.org
|
@@ -80,9 +80,7 @@ files:
|
|
80
80
|
- lib/paru/filter/table_row.rb
|
81
81
|
- lib/paru/filter/target.rb
|
82
82
|
- lib/paru/filter/version.rb
|
83
|
-
- lib/paru/filter_error.rb
|
84
83
|
- lib/paru/pandoc.rb
|
85
|
-
- lib/paru/pandoc2yaml.rb
|
86
84
|
- lib/paru/pandoc_options.yaml
|
87
85
|
- lib/paru/selector.rb
|
88
86
|
homepage: https://heerdebeer.org/Software/markdown/paru/
|
@@ -100,9 +98,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
98
|
version: '0'
|
101
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
100
|
requirements:
|
103
|
-
- - "
|
101
|
+
- - ">"
|
104
102
|
- !ruby/object:Gem::Version
|
105
|
-
version:
|
103
|
+
version: 1.3.1
|
106
104
|
requirements: []
|
107
105
|
rubyforge_project:
|
108
106
|
rubygems_version: 2.5.2
|
data/lib/paru/filter_error.rb
DELETED
@@ -1,24 +0,0 @@
|
|
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
|
-
require_relative "./error.rb"
|
20
|
-
module Paru
|
21
|
-
# A FilterError raised when there is an error while running a filter.
|
22
|
-
class FilterError < Error
|
23
|
-
end
|
24
|
-
end
|
data/lib/paru/pandoc2yaml.rb
DELETED
@@ -1,71 +0,0 @@
|
|
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
|
-
require "json"
|
21
|
-
require_relative "./pandoc.rb"
|
22
|
-
|
23
|
-
# Utility class to extract YAML metadata form a markdown file in pandoc's
|
24
|
-
# own markdown format.
|
25
|
-
class Pandoc2Yaml
|
26
|
-
# Paru converters:
|
27
|
-
# Note. When converting metadata back to the pandoc markdown format, you have
|
28
|
-
# to use the option "standalone", otherwise the metadata is skipped
|
29
|
-
|
30
|
-
# Converter from pandoc's markdown to pandoc's AST JSON
|
31
|
-
PANDOC_2_JSON = Paru::Pandoc.new {from "markdown"; to "json"}
|
32
|
-
|
33
|
-
# Converter from pandoc's AST JSON back to pandoc. Note the
|
34
|
-
# 'standalone' property, which is needed to output the metadata as
|
35
|
-
# well.
|
36
|
-
JSON_2_PANDOC = Paru::Pandoc.new {from "json"; to "markdown"; standalone}
|
37
|
-
|
38
|
-
# When converting a pandoc document to JSON, or vice versa, the JSON object
|
39
|
-
# has the following three properties:
|
40
|
-
|
41
|
-
# Pandoc-type API version key
|
42
|
-
VERSION = "pandoc-api-version"
|
43
|
-
# Meta block key
|
44
|
-
META = "meta"
|
45
|
-
# Content's blocks key
|
46
|
-
BLOCKS = "blocks"
|
47
|
-
|
48
|
-
# Extract the YAML metadata from input document
|
49
|
-
#
|
50
|
-
# @param input_document [String] path to input document
|
51
|
-
# @return [String] YAML metadata from input document on STDOUT
|
52
|
-
def self.extract_metadata input_document
|
53
|
-
json = JSON.parse(PANDOC_2_JSON << File.read(input_document))
|
54
|
-
yaml = ""
|
55
|
-
|
56
|
-
version, metadata = json.values_at(VERSION, META)
|
57
|
-
|
58
|
-
if not metadata.empty? then
|
59
|
-
metadata_document = {
|
60
|
-
VERSION => version,
|
61
|
-
META => metadata,
|
62
|
-
BLOCKS => []
|
63
|
-
}
|
64
|
-
|
65
|
-
yaml = JSON_2_PANDOC << JSON.generate(metadata_document)
|
66
|
-
end
|
67
|
-
|
68
|
-
yaml
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|