pandoc-ruby 2.1.4 → 2.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ec374a42893d6b60b7b22ab158a0c1aaa5eaad560523ff4f9fb57272587f20d
4
- data.tar.gz: f6c841353fa08a7182276fff06eb5bcef5e36d95a3560ca3ae7114048ac48156
3
+ metadata.gz: cae685a045d8dbe0031568787e914fc4a3a6ac2228abf9cb5447ee41cebddfe4
4
+ data.tar.gz: e603e4caa7038e4b299457c8ad9e48464d6283ecaa02ac5dc383d27e7f768b2f
5
5
  SHA512:
6
- metadata.gz: a81a3373767b06a74a75bc0c15edb546071d7ccbbd588f5ca5a5f075065b7ad02acbe71c90fb111e1cebf5d666798ca11b426f5222faef4bf5c33523bee14127
7
- data.tar.gz: 65b26a50284f935ded2d61b895fc492d8fa04a1fd51bd45d13aa7da2aba657a8c96b4f4e7cd2595cde6ecaaa49bbf94a6c76c3c6b42c559ebd477604f20e5ce9
6
+ metadata.gz: b066e046f9f55034567f483b037481c933b4c754cfe776133c002f15968999f27a924ebb2ce6d726637741e734893439b6f4ec3ae1157647641be274e11273c3
7
+ data.tar.gz: ded6cbe85511048fc67b07aba3b6ed83610bfce88ac391e820c80ad341c3fe886e4109695dab660b6fe58dad6aaeb2460d6b882a324169d582d64c3c9ade7352
data/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- minitest (5.14.0)
5
- mocha (1.11.2)
6
- rake (13.0.1)
7
- rdoc (6.2.1)
4
+ minitest (5.14.4)
5
+ mocha (1.13.0)
6
+ rake (13.0.6)
7
+ rdoc (6.3.3)
8
8
 
9
9
  PLATFORMS
10
10
  ruby
@@ -16,4 +16,4 @@ DEPENDENCIES
16
16
  rdoc
17
17
 
18
18
  BUNDLED WITH
19
- 2.1.4
19
+ 2.2.28
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # PandocRuby
2
2
 
3
+ [![Build Status](https://travis-ci.org/xwmx/pandoc-ruby.svg?branch=master)](https://travis-ci.org/xwmx/pandoc-ruby)
4
+ [![Gem Version](https://img.shields.io/gem/v/pandoc-ruby)](http://rubygems.org/gems/pandoc-ruby)
5
+ [![Gem Downloads](https://img.shields.io/gem/dt/pandoc-ruby)](http://rubygems.org/gems/pandoc-ruby)
6
+
3
7
  PandocRuby is a wrapper for [Pandoc](http://johnmacfarlane.net/pandoc/), a
4
8
  Haskell library with command line tools for converting one markup format to
5
9
  another.
@@ -62,11 +66,12 @@ Also provided are `#to_[writer]` instance methods for each of the writers,
62
66
  and these can also accept options:
63
67
 
64
68
  ```ruby
65
- PandocRuby.new("# Some title").to_html(:no_wrap)
66
- # => "<div id=\"some-title\"><h1>Some title</h1></div>"
69
+ PandocRuby.new('# Example').to_html(:ascii)
70
+ # => "<h1 id="example">Example</h1>"
67
71
  # or
68
- PandocRuby.new("# Some title").to_rst
69
- # => "Some title\n=========="
72
+ PandocRuby.new("# Example").to_rst
73
+ # => "Example
74
+ # ======="
70
75
  ```
71
76
 
72
77
  Similarly, there are class methods for each of the readers, so readers
data/lib/pandoc-ruby.rb CHANGED
@@ -3,7 +3,11 @@ require 'tempfile'
3
3
  require 'timeout'
4
4
 
5
5
  class PandocRuby
6
- @@pandoc_path = 'pandoc'
6
+ # Use the pandoc command with a custom executable path.
7
+ @pandoc_path = 'pandoc'
8
+ class << self
9
+ attr_accessor :pandoc_path
10
+ end
7
11
 
8
12
  # The available readers and their corresponding names. The keys are used to
9
13
  # generate methods and specify options to Pandoc.
@@ -112,12 +116,6 @@ class PandocRuby
112
116
  # All of the available Writers.
113
117
  WRITERS = STRING_WRITERS.merge(BINARY_WRITERS)
114
118
 
115
- # To use run the pandoc command with a custom executable path, the path
116
- # to the pandoc executable can be set here.
117
- def self.pandoc_path=(path)
118
- @@pandoc_path = path
119
- end
120
-
121
119
  # A shortcut method that creates a new PandocRuby object and immediately
122
120
  # calls `#convert`. Options passed to this method are passed directly to
123
121
  # `#new` and treated the same as if they were passed directly to the
@@ -127,23 +125,27 @@ class PandocRuby
127
125
  end
128
126
 
129
127
  attr_writer :binary_output
128
+
130
129
  def binary_output
131
- @binary_output ||= false
130
+ @binary_output ||= false
132
131
  end
133
132
 
134
133
  attr_writer :options
134
+
135
135
  def options
136
- @options ||= []
136
+ @options ||= []
137
137
  end
138
138
 
139
139
  attr_writer :option_string
140
+
140
141
  def option_string
141
- @option_string ||= ''
142
+ @option_string ||= ''
142
143
  end
143
144
 
144
145
  attr_writer :writer
146
+
145
147
  def writer
146
- @writer ||= 'html'
148
+ @writer ||= 'html'
147
149
  end
148
150
 
149
151
  attr_accessor :input_files
@@ -159,11 +161,13 @@ class PandocRuby
159
161
  # new(["/path/to/file.md"], :option1 => :value, :option2)
160
162
  # new(["/to/file1.html", "/to/file2.html"], :option1 => :value)
161
163
  def initialize(*args)
162
- if args[0].is_a?(String)
164
+ case args[0]
165
+ when String
163
166
  self.input_string = args.shift
164
- elsif args[0].is_a?(Array)
165
- self.input_files = args.shift.join(' ')
167
+ when Array
168
+ self.input_files = args.shift.join(' ')
166
169
  end
170
+
167
171
  self.options = args
168
172
  end
169
173
 
@@ -179,8 +183,9 @@ class PandocRuby
179
183
  # PandocRuby.new("# text").convert
180
184
  # # => "<h1 id=\"text\">text</h1>\n"
181
185
  def convert(*args)
182
- self.options += args if args
183
- self.option_string = prepare_options(self.options)
186
+ self.options += args if args
187
+ self.option_string = prepare_options(self.options)
188
+
184
189
  if self.binary_output
185
190
  convert_binary
186
191
  else
@@ -201,6 +206,7 @@ class PandocRuby
201
206
  READERS.each_key do |r|
202
207
  define_method(r) do |*args|
203
208
  args += [{ :from => r }]
209
+
204
210
  new(*args)
205
211
  end
206
212
  end
@@ -218,6 +224,7 @@ class PandocRuby
218
224
  WRITERS.each_key do |w|
219
225
  define_method(:"to_#{w}") do |*args|
220
226
  args += [{ :to => w.to_sym }]
227
+
221
228
  convert(*args)
222
229
  end
223
230
  end
@@ -229,13 +236,17 @@ class PandocRuby
229
236
  # temp file is closed and unlinked.
230
237
  def convert_binary
231
238
  tmp_file = Tempfile.new('pandoc-conversion')
239
+
232
240
  begin
233
- self.options += [{ :output => tmp_file.path }]
234
- self.option_string = "#{self.option_string} --output #{tmp_file.path}"
241
+ self.options += [{ :output => tmp_file.path }]
242
+ self.option_string = "#{self.option_string} --output \"#{tmp_file.path}\""
243
+
235
244
  execute_pandoc
245
+
236
246
  return IO.binread(tmp_file)
237
247
  ensure
238
248
  tmp_file.close
249
+
239
250
  tmp_file.unlink
240
251
  end
241
252
  end
@@ -248,30 +259,33 @@ class PandocRuby
248
259
  # Wrapper to run pandoc in a consistent, DRY way
249
260
  def execute_pandoc
250
261
  if !self.input_files.nil?
251
- execute("#{@@pandoc_path} #{self.input_files}#{self.option_string}")
262
+ execute("#{PandocRuby.pandoc_path} #{self.input_files}#{self.option_string}")
252
263
  else
253
- execute("#{@@pandoc_path}#{self.option_string}")
264
+ execute("#{PandocRuby.pandoc_path}#{self.option_string}")
254
265
  end
255
266
  end
256
267
 
257
268
  # Run the command and returns the output.
258
269
  def execute(command)
259
270
  output = error = exit_status = nil
260
- @timeout ||= 31_557_600 # A year should be enough?
271
+
272
+ @timeout ||= 31_557_600
273
+
261
274
  Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
262
- begin
263
- Timeout.timeout(@timeout) do
264
- stdin.puts self.input_string
265
- stdin.close
266
- output = stdout.read
267
- error = stderr.read
268
- exit_status = wait_thr.value
269
- end
270
- rescue Timeout::Error => ex
271
- Process.kill 9, wait_thr.pid
272
- maybe_ex = "\n#{ex}" if ex
273
- error = "Pandoc timed out after #{@timeout} seconds.#{maybe_ex}"
275
+ Timeout.timeout(@timeout) do
276
+ stdin.puts self.input_string
277
+
278
+ stdin.close
279
+
280
+ output = stdout.read
281
+ error = stderr.read
282
+ exit_status = wait_thr.value
274
283
  end
284
+ rescue Timeout::Error => ex
285
+ Process.kill 9, wait_thr.pid
286
+
287
+ maybe_ex = "\n#{ex}" if ex
288
+ error = "Pandoc timed out after #{@timeout} seconds.#{maybe_ex}"
275
289
  end
276
290
 
277
291
  raise error unless exit_status && exit_status.success?
@@ -305,10 +319,8 @@ class PandocRuby
305
319
 
306
320
  if argument.nil?
307
321
  format_flag(flag)
308
- elsif argument.to_s =~ /\s/
309
- "#{format_flag(flag)} \"#{argument}\""
310
322
  else
311
- "#{format_flag(flag)} #{argument}"
323
+ "#{format_flag(flag)} \"#{argument}\""
312
324
  end
313
325
  end
314
326
 
@@ -329,8 +341,8 @@ class PandocRuby
329
341
  def set_pandoc_ruby_options(flag, argument = nil)
330
342
  case flag
331
343
  when 't', 'to'
332
- self.writer = argument.to_s
333
- self.binary_output = true if BINARY_WRITERS.key?(self.writer)
344
+ self.writer = argument.to_s
345
+ self.binary_output = true if BINARY_WRITERS.key?(self.writer)
334
346
  when 'timeout'
335
347
  @timeout = argument
336
348
  end
data/pandoc-ruby.gemspec CHANGED
@@ -5,9 +5,9 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = 'pandoc-ruby'
8
- s.version = '2.1.4'
8
+ s.version = '2.1.5'
9
9
  s.authors = ['William Melody']
10
- s.date = '2020-03-25'
10
+ s.date = '2021-11-13'
11
11
  s.description = 'Ruby wrapper for Pandoc'
12
12
  s.email = 'hi@williammelody.com'
13
13
  s.extra_rdoc_files = [
@@ -23,7 +23,8 @@ describe 'Conversions' do
23
23
  :from => from,
24
24
  :to => to
25
25
  )
26
- assert_equal(converted_content.strip, to_content.strip)
26
+
27
+ assert_equal(to_content.strip, converted_content.strip)
27
28
  end
28
29
  end
29
30
  end
@@ -47,7 +48,7 @@ describe 'Conversions' do
47
48
  )
48
49
  end
49
50
 
50
- assert_match(/couldn't parse docx file/, error.message)
51
+ assert_match(/couldn't unpack docx container/, error.message)
51
52
  end
52
53
 
53
54
  it "raises an error when attempting to convert doc with doc format" do
@@ -2,10 +2,10 @@ require 'helper'
2
2
 
3
3
  describe PandocRuby do
4
4
  before do
5
- @file = File.join(File.dirname(__FILE__), 'files', 'test.md')
6
- @file2 = File.join(File.dirname(__FILE__), 'files', 'test2.md')
7
- @string = '# Test String'
8
- @converter = PandocRuby.new(@string, :t => :rst)
5
+ @file = File.join(File.dirname(__FILE__), 'files', 'test.md')
6
+ @file2 = File.join(File.dirname(__FILE__), 'files', 'test2.md')
7
+ @string = '# Test String'
8
+ @converter = PandocRuby.new(@string, :t => :rst)
9
9
  end
10
10
 
11
11
  after do
@@ -14,15 +14,20 @@ describe PandocRuby do
14
14
 
15
15
  it 'calls bare pandoc when passed no options' do
16
16
  converter = PandocRuby.new(@string)
17
+
17
18
  converter.expects(:execute).with('pandoc').returns(true)
19
+
18
20
  assert converter.convert
19
21
  end
20
22
 
21
23
  it 'converts with altered pandoc_path' do
22
24
  path = '/usr/bin/env pandoc'
23
25
  PandocRuby.pandoc_path = path
26
+
24
27
  converter = PandocRuby.new(@string)
28
+
25
29
  converter.expects(:execute).with(path).returns(true)
30
+
26
31
  assert converter.convert
27
32
  end
28
33
 
@@ -45,50 +50,66 @@ describe PandocRuby do
45
50
  end
46
51
 
47
52
  it 'accepts short options' do
48
- @converter.expects(:execute).with('pandoc -t rst').returns(true)
53
+ @converter.expects(:execute).with('pandoc -t "rst"').returns(true)
54
+
49
55
  assert @converter.convert
50
56
  end
51
57
 
52
58
  it 'accepts long options' do
53
59
  converter = PandocRuby.new(@string, :to => :rst)
54
- converter.expects(:execute).with('pandoc --to rst').returns(true)
60
+
61
+ converter.expects(:execute).with('pandoc --to "rst"').returns(true)
62
+
55
63
  assert converter.convert
56
64
  end
57
65
 
58
66
  it 'accepts a variety of options in initializer' do
59
- converter = PandocRuby.new(@string, :s, {
60
- :f => :markdown, :to => :rst
61
- }, 'no-wrap')
62
- converter \
63
- .expects(:execute) \
64
- .with('pandoc -s -f markdown --to rst --no-wrap') \
67
+ converter = PandocRuby.new(
68
+ @string,
69
+ :s,
70
+ { :f => :markdown, :to => :rst },
71
+ 'no-wrap'
72
+ )
73
+
74
+ converter \
75
+ .expects(:execute) \
76
+ .with('pandoc -s -f "markdown" --to "rst" --no-wrap') \
65
77
  .returns(true)
78
+
66
79
  assert converter.convert
67
80
  end
68
81
 
69
82
  it 'accepts a variety of options in convert' do
70
83
  converter = PandocRuby.new(@string)
71
- converter \
72
- .expects(:execute) \
73
- .with('pandoc -s -f markdown --to rst --no-wrap') \
84
+
85
+ converter \
86
+ .expects(:execute) \
87
+ .with('pandoc -s -f "markdown" --to "rst" --no-wrap') \
74
88
  .returns(true)
89
+
75
90
  assert converter.convert(:s, { :f => :markdown, :to => :rst }, 'no-wrap')
76
91
  end
77
92
 
78
93
  it 'converts underscore symbol args to hyphenated long options' do
79
- converter = PandocRuby.new(@string, {
80
- :email_obfuscation => :javascript
81
- }, :table_of_contents)
82
- converter \
83
- .expects(:execute) \
84
- .with('pandoc --email-obfuscation javascript --table-of-contents') \
94
+ converter = PandocRuby.new(
95
+ @string,
96
+ { :email_obfuscation => :javascript },
97
+ :table_of_contents
98
+ )
99
+
100
+ converter \
101
+ .expects(:execute) \
102
+ .with('pandoc --email-obfuscation "javascript" --table-of-contents') \
85
103
  .returns(true)
104
+
86
105
  assert converter.convert
87
106
  end
88
107
 
89
108
  it 'uses second arg as option' do
90
109
  converter = PandocRuby.new(@string, 'toc')
110
+
91
111
  converter.expects(:execute).with('pandoc --toc').returns(true)
112
+
92
113
  assert converter.convert
93
114
  end
94
115
 
@@ -97,9 +118,11 @@ describe PandocRuby do
97
118
  @string,
98
119
  '+RTS', '-M512M', '-RTS', '--to=markdown', '--no-wrap'
99
120
  )
121
+
100
122
  converter.expects(:execute).with(
101
123
  'pandoc +RTS -M512M -RTS --to=markdown --no-wrap'
102
124
  ).returns(true)
125
+
103
126
  assert converter.convert
104
127
  end
105
128
 
@@ -112,6 +135,7 @@ describe PandocRuby do
112
135
  ),
113
136
  "<p>Line 1</p>\n<h1>Heading</h1>\n"
114
137
  )
138
+
115
139
  assert_equal(
116
140
  PandocRuby.convert(
117
141
  "Line 1\n# Heading",
@@ -131,6 +155,7 @@ describe PandocRuby do
131
155
  ),
132
156
  "~example~\n"
133
157
  )
158
+
134
159
  assert_equal(
135
160
  PandocRuby.convert(
136
161
  "<sub>example</sub>\n",
@@ -149,7 +174,9 @@ describe PandocRuby do
149
174
  :to => 'html',
150
175
  :output => file.path
151
176
  )
177
+
152
178
  file.rewind
179
+
153
180
  assert_equal("<h1 id=\"example\">Example</h1>\n", file.read)
154
181
  end
155
182
  end
@@ -162,10 +189,13 @@ describe PandocRuby do
162
189
  :to => 'html',
163
190
  :output => file.path
164
191
  )
165
- converter \
166
- .expects(:execute) \
167
- .with("pandoc --from markdown --to html --output \"#{file.path}\"") \
168
- .returns(true)
192
+
193
+ converter \
194
+ .expects(:execute) \
195
+ .with(
196
+ "pandoc --from \"markdown\" --to \"html\" --output \"#{file.path}\""
197
+ ).returns(true)
198
+
169
199
  assert converter.convert
170
200
  end
171
201
  end
@@ -180,6 +210,7 @@ describe PandocRuby do
180
210
  )
181
211
 
182
212
  file.rewind
213
+
183
214
  assert_equal("<h1 id=\"example\">Example</h1>\n", file.read)
184
215
  end
185
216
  end
@@ -192,10 +223,13 @@ describe PandocRuby do
192
223
  :to => 'html',
193
224
  :output => Pathname.new(file.path)
194
225
  )
195
- converter \
196
- .expects(:execute) \
197
- .with("pandoc --from markdown --to html --output \"#{file.path}\"") \
198
- .returns(true)
226
+
227
+ converter \
228
+ .expects(:execute) \
229
+ .with(
230
+ "pandoc --from \"markdown\" --to \"html\" --output \"#{file.path}\""
231
+ ).returns(true)
232
+
199
233
  assert converter.convert
200
234
  end
201
235
  end
@@ -208,7 +242,9 @@ describe PandocRuby do
208
242
  :to => 'html',
209
243
  :output => Pathname.new(file.path)
210
244
  )
245
+
211
246
  file.rewind
247
+
212
248
  assert_equal("<h1 id=\"example\">Example</h1>\n", file.read)
213
249
  end
214
250
  end
@@ -222,7 +258,9 @@ describe PandocRuby do
222
258
  PandocRuby::READERS.each_key do |r|
223
259
  it "converts from #{r} with PandocRuby.#{r}" do
224
260
  converter = PandocRuby.send(r, @string)
225
- converter.expects(:execute).with("pandoc --from #{r}").returns(true)
261
+
262
+ converter.expects(:execute).with("pandoc --from \"#{r}\"").returns(true)
263
+
226
264
  assert converter.convert
227
265
  end
228
266
  end
@@ -230,10 +268,12 @@ describe PandocRuby do
230
268
  PandocRuby::STRING_WRITERS.each_key do |w|
231
269
  it "converts to #{w} with to_#{w}" do
232
270
  converter = PandocRuby.new(@string)
233
- converter \
234
- .expects(:execute) \
235
- .with("pandoc --no-wrap --to #{w}") \
271
+
272
+ converter \
273
+ .expects(:execute) \
274
+ .with("pandoc --no-wrap --to \"#{w}\"") \
236
275
  .returns(true)
276
+
237
277
  assert converter.send("to_#{w}", :no_wrap)
238
278
  end
239
279
  end
@@ -241,26 +281,31 @@ describe PandocRuby do
241
281
  PandocRuby::BINARY_WRITERS.each_key do |w|
242
282
  it "converts to #{w} with to_#{w}" do
243
283
  converter = PandocRuby.new(@string)
244
- converter \
245
- .expects(:execute) \
246
- .with(regexp_matches(/^pandoc --no-wrap --to #{w} --output /)) \
284
+
285
+ converter \
286
+ .expects(:execute) \
287
+ .with(regexp_matches(/^pandoc --no-wrap --to "#{w}" --output /)) \
247
288
  .returns(true)
289
+
248
290
  assert converter.send("to_#{w}", :no_wrap)
249
291
  end
250
292
  end
251
293
 
252
294
  it 'works with strings' do
253
295
  converter = PandocRuby.new('## this is a title')
296
+
254
297
  assert_match(/h2/, converter.convert)
255
298
  end
256
299
 
257
300
  it 'accepts blank strings' do
258
301
  converter = PandocRuby.new('')
302
+
259
303
  assert_match("\n", converter.convert)
260
304
  end
261
305
 
262
306
  it 'accepts nil and treats like a blank string' do
263
307
  converter = PandocRuby.new(nil)
308
+
264
309
  assert_match("\n", converter.convert)
265
310
  end
266
311
 
@@ -273,21 +318,20 @@ describe PandocRuby do
273
318
  end
274
319
 
275
320
  it 'runs more than 400 times without error' do
276
- begin
277
- 400.times do
278
- PandocRuby.convert(@string)
279
- end
280
- assert true
281
- rescue Errno::EMFILE, Errno::EAGAIN => e
282
- flunk e
321
+ 400.times do
322
+ PandocRuby.convert(@string)
283
323
  end
324
+
325
+ assert true
326
+ rescue Errno::EMFILE, Errno::EAGAIN => e
327
+ flunk e
284
328
  end
285
329
 
286
330
  it 'gracefully times out when pandoc hangs due to malformed input' do
287
- skip(%(
288
- Pandoc no longer times out with test file. Determine how to test.
289
- ))
331
+ skip('Pandoc no longer times out with test file. Determine how to test.')
332
+
290
333
  file = File.join(File.dirname(__FILE__), 'files', 'bomb.tex')
334
+
291
335
  contents = File.read(file)
292
336
 
293
337
  assert_raises(RuntimeError) do
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: 2.1.4
4
+ version: 2.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - William Melody
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-25 00:00:00.000000000 Z
11
+ date: 2021-11-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby wrapper for Pandoc
14
14
  email: hi@williammelody.com
@@ -36,7 +36,7 @@ homepage: http://github.com/xwmx/pandoc-ruby
36
36
  licenses:
37
37
  - MIT
38
38
  metadata: {}
39
- post_install_message:
39
+ post_install_message:
40
40
  rdoc_options: []
41
41
  require_paths:
42
42
  - lib
@@ -51,8 +51,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  requirements: []
54
- rubygems_version: 3.1.2
55
- signing_key:
54
+ rubygems_version: 3.2.22
55
+ signing_key:
56
56
  specification_version: 4
57
57
  summary: PandocRuby
58
58
  test_files: []