pandoc-ruby 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +1 -1
- data/lib/pandoc-ruby.rb +150 -42
- data/pandoc-ruby.gemspec +2 -2
- data/test/test_pandoc-ruby.rb +12 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17b3f81bb4b27fe9caf0f61c14043084c0f02776
|
4
|
+
data.tar.gz: aa1251098ed96b13d1690f17055f230aa50bbc29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9dbf61c66d2e5e4f2bf6c7f16d587b36f99d90fea3c7a3d4d5ea84aac57b2b54da11066edd88f159a71f28c00d3827fa8a6a3e21105ac876c0688bb1ada53bae
|
7
|
+
data.tar.gz: 7dc6c106893b19897971b80a217d3ca4fe8f897ef5e627d4ee52d65b1e7cc07c332c60c83dabc4ab5fb4fe8bed2a4142a3445fb25cb2f222e9ed2199820204b7
|
data/README.markdown
CHANGED
@@ -67,7 +67,7 @@ If you'd prefer a pure-Ruby extended markdown interpreter that can output a few
|
|
67
67
|
|
68
68
|
This gem was inspired by [Albino](http://github.com/github/albino). For a slightly different approach to using Pandoc with Ruby, see [Pandoku](http://github.com/dahlia/pandoku).
|
69
69
|
|
70
|
-
## Pandoc
|
70
|
+
## Pandoc Tip
|
71
71
|
|
72
72
|
If you are trying to generate a standalone file with full file headers rather than just a marked up fragment, remember to pass the `:standalone` option so the correct header and footer are added.
|
73
73
|
|
data/lib/pandoc-ruby.rb
CHANGED
@@ -2,9 +2,11 @@ require 'open3'
|
|
2
2
|
require 'tempfile'
|
3
3
|
|
4
4
|
class PandocRuby
|
5
|
+
|
5
6
|
@@bin_path = nil
|
6
7
|
@@allow_file_paths = false
|
7
8
|
|
9
|
+
# The executable options. The `pandoc` executable is used by default.
|
8
10
|
EXECUTABLES = %W[
|
9
11
|
pandoc
|
10
12
|
markdown2pdf
|
@@ -12,6 +14,8 @@ class PandocRuby
|
|
12
14
|
hsmarkdown
|
13
15
|
]
|
14
16
|
|
17
|
+
# The available readers and their corresponding names. The keys are used to
|
18
|
+
# generate methods and specify options to Pandoc.
|
15
19
|
READERS = {
|
16
20
|
'native' => 'pandoc native',
|
17
21
|
'json' => 'pandoc JSON',
|
@@ -22,7 +26,9 @@ class PandocRuby
|
|
22
26
|
'latex' => 'LaTeX',
|
23
27
|
}
|
24
28
|
|
25
|
-
|
29
|
+
# The available string writers and their corresponding names. The keys are
|
30
|
+
# used to generate methods and specify options to Pandoc.
|
31
|
+
STRING_WRITERS = {
|
26
32
|
'native' => 'pandoc native',
|
27
33
|
'json' => 'pandoc JSON',
|
28
34
|
'html' => 'HTML',
|
@@ -47,25 +53,57 @@ class PandocRuby
|
|
47
53
|
'asciidoc' => 'asciidoc'
|
48
54
|
}
|
49
55
|
|
56
|
+
# The available binary writers and their corresponding names. The keys are
|
57
|
+
# used to generate methods and specify options to Pandoc.
|
50
58
|
BINARY_WRITERS = {
|
51
59
|
'odt' => 'OpenDocument',
|
52
60
|
'docx' => 'Word docx',
|
53
61
|
'epub' => 'EPUB V2',
|
54
62
|
'epub3' => 'EPUB V3'
|
55
63
|
}
|
64
|
+
|
65
|
+
# All of the available Writers.
|
66
|
+
WRITERS = STRING_WRITERS.merge(BINARY_WRITERS)
|
56
67
|
|
68
|
+
# If the pandoc executables are not in the PATH, bin_path can be set to
|
69
|
+
# the directory they are contained in.
|
57
70
|
def self.bin_path=(path)
|
58
71
|
@@bin_path = path
|
59
72
|
end
|
60
73
|
|
74
|
+
# Pandoc can also be used with a file path as the first argument. For
|
75
|
+
# security reasons, this is disabled by default, but it can be enabled by
|
76
|
+
# setting this to `true`.
|
61
77
|
def self.allow_file_paths=(value)
|
62
78
|
@@allow_file_paths = value
|
63
79
|
end
|
64
|
-
|
80
|
+
|
81
|
+
# A shortcut method that creates a new PandocRuby object and immediately
|
82
|
+
# calls `#convert`. Options passed to this method are passed directly to
|
83
|
+
# `#new` and treated the same as if they were passed directly to the
|
84
|
+
# initializer.
|
65
85
|
def self.convert(*args)
|
66
86
|
new(*args).convert
|
67
87
|
end
|
68
88
|
|
89
|
+
attr_accessor :options
|
90
|
+
def options; @options || [] end
|
91
|
+
|
92
|
+
attr_accessor :option_string
|
93
|
+
def options_string; @option_string || '' end
|
94
|
+
|
95
|
+
attr_accessor :binary_output
|
96
|
+
def binary_output; @binary_output || false end
|
97
|
+
|
98
|
+
attr_accessor :writer
|
99
|
+
def writer; @writer || 'html' end
|
100
|
+
|
101
|
+
# Create a new PandocRuby converter object. The first argument should be
|
102
|
+
# the string that will be converted or, if `.allow_file_paths` has been set
|
103
|
+
# to `true`, this can also be a path to a file. The executable name can
|
104
|
+
# be used as the second argument, but will default to `pandoc` if the second
|
105
|
+
# argument is omitted or anything other than an executable name. Any other
|
106
|
+
# arguments will be converted to pandoc options.
|
69
107
|
def initialize(*args)
|
70
108
|
target = args.shift
|
71
109
|
@target = if @@allow_file_paths && File.exists?(target)
|
@@ -73,31 +111,40 @@ class PandocRuby
|
|
73
111
|
else
|
74
112
|
target rescue target
|
75
113
|
end
|
76
|
-
@executable = EXECUTABLES.include?(args[0])
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
def convert_binary(executable, *args)
|
81
|
-
tmp_file = Tempfile.new('pandoc-conversion')
|
82
|
-
begin
|
83
|
-
args += [{:output => tmp_file.path}]
|
84
|
-
execute executable + convert_options(args)
|
85
|
-
return IO.binread(tmp_file)
|
86
|
-
ensure
|
87
|
-
tmp_file.unlink
|
88
|
-
end
|
114
|
+
@executable = args.shift if EXECUTABLES.include?(args[0])
|
115
|
+
self.options = args
|
89
116
|
end
|
90
117
|
|
118
|
+
# Run the conversion. The convert method can take any number of arguments,
|
119
|
+
# which will be converted to pandoc options. If options were already
|
120
|
+
# specified in an initializer or reader method, they will be combined with
|
121
|
+
# any that are passed to this method.
|
122
|
+
#
|
123
|
+
# Returns a string with the converted content.
|
124
|
+
#
|
125
|
+
# Example:
|
126
|
+
#
|
127
|
+
# PandocRuby.new("# text").convert
|
128
|
+
# # => "<h1 id=\"text\">text</h1>\n"
|
91
129
|
def convert(*args)
|
92
|
-
|
93
|
-
|
94
|
-
|
130
|
+
self.options += args if args
|
131
|
+
self.option_string = prepare_options(self.options)
|
132
|
+
if self.binary_output
|
133
|
+
convert_binary
|
95
134
|
else
|
96
|
-
|
135
|
+
convert_string
|
97
136
|
end
|
98
137
|
end
|
99
138
|
alias_method :to_s, :convert
|
100
139
|
|
140
|
+
# Generate class methods for each of the readers in PandocRuby::READERS.
|
141
|
+
# When one of these methods is called, it simply calls the initializer
|
142
|
+
# with the `from` option set to the reader key, and returns the object.
|
143
|
+
#
|
144
|
+
# Example:
|
145
|
+
#
|
146
|
+
# PandocRuby.markdown("# text")
|
147
|
+
# # => #<PandocRuby:0x007 @target="# text", @options=[{:from=>"markdown"}]
|
101
148
|
class << self
|
102
149
|
READERS.each_key do |r|
|
103
150
|
define_method(r) do |*args|
|
@@ -107,7 +154,15 @@ class PandocRuby
|
|
107
154
|
end
|
108
155
|
end
|
109
156
|
|
110
|
-
WRITERS.
|
157
|
+
# Generate instance methods for each of the writers in PandocRuby::WRITERS.
|
158
|
+
# When one of these methods is called, it simply calls the `#convert` method
|
159
|
+
# with the `to` option set to the writer key, thereby returning the
|
160
|
+
# converted string.
|
161
|
+
#
|
162
|
+
# Example:
|
163
|
+
# PandocRuby.new("# text").to_html
|
164
|
+
# # => "<h1 id=\"text\">text</h1>\n"
|
165
|
+
WRITERS.each_key do |w|
|
111
166
|
define_method(:"to_#{w}") do |*args|
|
112
167
|
args += [{:to => w.to_sym}]
|
113
168
|
convert(*args)
|
@@ -116,6 +171,41 @@ class PandocRuby
|
|
116
171
|
|
117
172
|
private
|
118
173
|
|
174
|
+
# Sets the executable, which by default is `pandoc`. The `@executable`
|
175
|
+
# variable can be set in the initializer, so testing for its presence first.
|
176
|
+
# Finally, checking to see if the bin_path was set and, if so, using that.
|
177
|
+
def executable
|
178
|
+
@executable ||= 'pandoc'
|
179
|
+
@@bin_path ? File.join(@@bin_path, @executable) : @executable
|
180
|
+
end
|
181
|
+
|
182
|
+
# Executes the pandoc command for binary writers. A temp file is created
|
183
|
+
# and written to, then read back into the program as a string, then the
|
184
|
+
# temp file is closed and unlinked.
|
185
|
+
def convert_binary
|
186
|
+
begin
|
187
|
+
tmp_file = Tempfile.new('pandoc-conversion')
|
188
|
+
self.options += [{:output => tmp_file.path}]
|
189
|
+
self.option_string = "#{self.option_string} --output #{tmp_file.path}"
|
190
|
+
execute(command_with_options)
|
191
|
+
return IO.binread(tmp_file)
|
192
|
+
ensure
|
193
|
+
tmp_file.close
|
194
|
+
tmp_file.unlink
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Executes the pandoc command for btring writers.
|
199
|
+
def convert_string
|
200
|
+
execute(command_with_options)
|
201
|
+
end
|
202
|
+
|
203
|
+
# Combines the executable string with the option string.
|
204
|
+
def command_with_options
|
205
|
+
executable + self.option_string
|
206
|
+
end
|
207
|
+
|
208
|
+
# Runs the command and returns the output.
|
119
209
|
def execute(command)
|
120
210
|
output = ''
|
121
211
|
Open3::popen3(command) do |stdin, stdout, stderr|
|
@@ -126,34 +216,52 @@ private
|
|
126
216
|
output
|
127
217
|
end
|
128
218
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
219
|
+
# Builds the option string to be passed to pandoc by iterating over the
|
220
|
+
# opts passed in. Recursively calls itself in order to handle hash options.
|
221
|
+
def prepare_options(opts = [])
|
222
|
+
opts.inject('') do |string, (option, value)|
|
223
|
+
string += case
|
224
|
+
when value != nil
|
225
|
+
create_option(option, value)
|
226
|
+
when option.respond_to?(:each_pair)
|
227
|
+
prepare_options(option)
|
228
|
+
else
|
229
|
+
create_option(option)
|
230
|
+
end
|
136
231
|
end
|
137
232
|
end
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
233
|
+
|
234
|
+
# Takes a flag and optional argument, uses it to set any relevant options
|
235
|
+
# used by the library, and returns string with the option formatted as a
|
236
|
+
# command line options. If the option has an argument, it is also included.
|
237
|
+
def create_option(flag, argument = nil)
|
238
|
+
return if !flag
|
239
|
+
set_pandoc_ruby_options(flag, argument)
|
240
|
+
if !!argument
|
241
|
+
"#{option_flag(flag)} #{argument}"
|
242
|
+
else
|
243
|
+
option_flag(flag)
|
143
244
|
end
|
144
245
|
end
|
145
246
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
247
|
+
# Formats an option flag in order to be used with the pandoc command line
|
248
|
+
# tool.
|
249
|
+
def option_flag(flag)
|
250
|
+
if flag.length == 1
|
251
|
+
" -#{flag}"
|
252
|
+
else
|
253
|
+
" --#{flag.to_s.gsub(/_/, '-')}"
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
# Takes an option and optional argument and uses them to set any flags
|
258
|
+
# used by PandocRuby.
|
259
|
+
def set_pandoc_ruby_options(flag, argument = nil)
|
260
|
+
case flag.to_sym
|
261
|
+
when :t, :to
|
262
|
+
self.writer = argument.to_s
|
263
|
+
self.binary_output = true if BINARY_WRITERS.keys.include?(self.writer)
|
155
264
|
end
|
156
|
-
false
|
157
265
|
end
|
158
266
|
|
159
267
|
end
|
data/pandoc-ruby.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "pandoc-ruby"
|
8
|
-
s.version = "0.7.
|
8
|
+
s.version = "0.7.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["William Melody"]
|
12
|
-
s.date = "2013-
|
12
|
+
s.date = "2013-06-21"
|
13
13
|
s.description = "Ruby wrapper for Pandoc"
|
14
14
|
s.email = "hi@williammelody.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/test/test_pandoc-ruby.rb
CHANGED
@@ -43,7 +43,7 @@ class TestPandocRuby < Test::Unit::TestCase
|
|
43
43
|
|
44
44
|
should "accept long options" do
|
45
45
|
converter = PandocRuby.new(@file, :to => :rst)
|
46
|
-
converter.expects(:execute).with('pandoc --to
|
46
|
+
converter.expects(:execute).with('pandoc --to rst').returns(true)
|
47
47
|
assert converter.convert
|
48
48
|
end
|
49
49
|
|
@@ -53,7 +53,7 @@ class TestPandocRuby < Test::Unit::TestCase
|
|
53
53
|
}, 'no-wrap')
|
54
54
|
converter \
|
55
55
|
.expects(:execute) \
|
56
|
-
.with('pandoc -s -f markdown --to
|
56
|
+
.with('pandoc -s -f markdown --to rst --no-wrap') \
|
57
57
|
.returns(true)
|
58
58
|
assert converter.convert
|
59
59
|
end
|
@@ -62,7 +62,7 @@ class TestPandocRuby < Test::Unit::TestCase
|
|
62
62
|
converter = PandocRuby.new(@file)
|
63
63
|
converter \
|
64
64
|
.expects(:execute) \
|
65
|
-
.with('pandoc -s -f markdown --to
|
65
|
+
.with('pandoc -s -f markdown --to rst --no-wrap') \
|
66
66
|
.returns(true)
|
67
67
|
assert converter.convert(:s, {:f => :markdown, :to => :rst}, 'no-wrap')
|
68
68
|
end
|
@@ -73,7 +73,7 @@ class TestPandocRuby < Test::Unit::TestCase
|
|
73
73
|
}, :table_of_contents)
|
74
74
|
converter \
|
75
75
|
.expects(:execute) \
|
76
|
-
.with('pandoc --email-obfuscation
|
76
|
+
.with('pandoc --email-obfuscation javascript --table-of-contents') \
|
77
77
|
.returns(true)
|
78
78
|
assert converter.convert
|
79
79
|
end
|
@@ -93,17 +93,17 @@ class TestPandocRuby < Test::Unit::TestCase
|
|
93
93
|
PandocRuby::READERS.each_key do |r|
|
94
94
|
should "convert from #{r} with PandocRuby.#{r}" do
|
95
95
|
converter = PandocRuby.send(r, @file)
|
96
|
-
converter.expects(:execute).with("pandoc --from
|
96
|
+
converter.expects(:execute).with("pandoc --from #{r}").returns(true)
|
97
97
|
assert converter.convert
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
PandocRuby::
|
101
|
+
PandocRuby::STRING_WRITERS.each_key do |w|
|
102
102
|
should "convert to #{w} with to_#{w}" do
|
103
103
|
converter = PandocRuby.new(@file)
|
104
104
|
converter \
|
105
105
|
.expects(:execute) \
|
106
|
-
.with("pandoc --no-wrap --to
|
106
|
+
.with("pandoc --no-wrap --to #{w}") \
|
107
107
|
.returns(true)
|
108
108
|
assert converter.send("to_#{w}", :no_wrap)
|
109
109
|
end
|
@@ -114,7 +114,7 @@ class TestPandocRuby < Test::Unit::TestCase
|
|
114
114
|
converter = PandocRuby.new(@file)
|
115
115
|
converter \
|
116
116
|
.expects(:execute) \
|
117
|
-
.with(regexp_matches(/^pandoc --no-wrap --to
|
117
|
+
.with(regexp_matches(/^pandoc --no-wrap --to #{w} --output /)) \
|
118
118
|
.returns(true)
|
119
119
|
assert converter.send("to_#{w}", :no_wrap)
|
120
120
|
end
|
@@ -155,7 +155,7 @@ class TestPandocRuby < Test::Unit::TestCase
|
|
155
155
|
"rst" => "reStructuredText"
|
156
156
|
}
|
157
157
|
|
158
|
-
assert_equal PandocRuby::
|
158
|
+
assert_equal PandocRuby::STRING_WRITERS, {
|
159
159
|
"mediawiki" => "MediaWiki markup",
|
160
160
|
"html" => "HTML",
|
161
161
|
"plain" => "plain",
|
@@ -187,5 +187,8 @@ class TestPandocRuby < Test::Unit::TestCase
|
|
187
187
|
"epub3" => "EPUB V3"
|
188
188
|
}
|
189
189
|
|
190
|
+
assert_equal PandocRuby::WRITERS, (
|
191
|
+
PandocRuby::STRING_WRITERS.merge(PandocRuby::BINARY_WRITERS)
|
192
|
+
)
|
190
193
|
end
|
191
194
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pandoc-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- William Melody
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mocha
|