pandoc-ruby 0.0.3 → 0.0.4

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.
@@ -19,35 +19,38 @@ Next, install PandocRuby from gemcutter.
19
19
  @converter = PandocRuby.new('/some/file.md', :from => :markdown, :to => :rst)
20
20
  puts @converter.convert
21
21
 
22
- This takes the Markdown formatted file and converts it to reStructuredText.
22
+ This takes the Markdown formatted file and converts it to reStructuredText. The first argument can be either a file or a string.
23
23
 
24
- You can use the `#convert` class method:
24
+ You can also use the `#convert` class method:
25
25
 
26
26
  puts PandocRuby.convert('/some/file.md', :from => :markdown, :to => :html)
27
27
 
28
- Included is a `#to_s` method for somewhat nicer use in Rails views.
28
+ When no options are passed, pandoc's default behavior converts markdown to html. To specify options, simply pass options as a hash to the initializer. Pandoc's wrapper executables can also be used by passing the executable name as the second argument. For example,
29
29
 
30
- ... helper file ...
31
- def format(text)
32
- PandocRuby.convert(text)
33
- end
30
+ PandocRuby.new('/some/file.html', 'html2markdown')
34
31
 
35
- ... view file ...
36
- <%= format text %>
32
+ will use Pandoc's `html2markdown` wrapper.
37
33
 
38
- The default behavior follows the pandoc executable's behavior, converting markdown to html. To specify options, simply pass options as a hash to the initializer. Pandoc's wrapper executables can also be used by passing the executable name as the second argument. For example,
34
+ Other arguments are simply converted into command line options, accepting symbols or strings for options without arguments and hashes of strings or symbols for options with arguments.
39
35
 
40
- PandocRuby.new('/some/file.html', 'html2markdown')
36
+ PandocRuby.convert('/some/file.html', :s, {:to => :rst, :f => :markdown}, 'no-wrap')
41
37
 
42
- will use Pandoc's `html2markdown` wrapper.
38
+ becomes
43
39
 
44
- Assumes pandoc executables are in the path. If not, set their location
40
+ pandoc -s --to=rst -f markdown --no-wrap /some/file.html
41
+
42
+ PandocRuby assumes the pandoc executables are in the path. If not, set their location
45
43
  with `PandocRuby.bin_path = '/path/to/bin'`
46
44
 
47
45
  For more information on Pandoc, see the [Pandoc documentation](http://johnmacfarlane.net/pandoc/) or run `man pandoc`.
48
46
 
49
47
  Pretty much everything in the gem was derived directly from [Albino](http://github.com/github/albino).
50
48
 
49
+ ## Caveats
50
+
51
+ * This has only been tested on *nix systems.
52
+ * Some conversions may still not work and/or require additional dependencies.
53
+
51
54
  ## Note on Patches/Pull Requests
52
55
 
53
56
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -17,16 +17,25 @@ class PandocRuby
17
17
  new(*args).convert
18
18
  end
19
19
 
20
- def initialize(target, *args)
20
+ def initialize(*args)
21
+ target = args.shift
21
22
  @target = File.exists?(target) ? File.read(target) : target rescue target
22
23
  if args[0] && !args[0].respond_to?(:merge) && EXECUTABLES.include?(args[0])
23
- @executable = args[0]
24
+ @executable = args.shift
24
25
  else
25
26
  @executable = 'pandoc'
26
27
  end
27
- @options = args.last.respond_to?(:merge) ? args.last : {}
28
+ @options = args.empty? ? [] : args
28
29
  end
29
30
 
31
+ def convert
32
+ executable = @@bin_path ? File.join(@@bin_path, @executable) : @executable
33
+ execute executable + convert_options
34
+ end
35
+ alias_method :to_s, :convert
36
+
37
+ private
38
+
30
39
  def execute(command)
31
40
  pid, stdin, stdout, stderr = Open4.popen4(command)
32
41
  stdin.puts @target
@@ -34,19 +43,16 @@ class PandocRuby
34
43
  stdout.read.strip
35
44
  end
36
45
 
37
- def convert
38
- executable = @@bin_path ? File.join(@@bin_path, @executable) : @executable
39
- execute executable + convert_options
40
- end
41
- alias_method :to_s, :convert
42
46
 
43
47
  def convert_options
44
- @options.inject('') do |string, (flag, value)|
45
- string + if flag.to_s.length == 1
46
- " -#{flag} #{value}"
48
+ @options.inject('') do |string, opt|
49
+ string + if opt.respond_to?(:each_pair)
50
+ opt.inject('') do |s, (flag, val)|
51
+ s + (flag.to_s.length == 1 ? " -#{flag} #{val}" : " --#{flag}=#{val}")
52
+ end
47
53
  else
48
- " --#{flag}=#{value}"
54
+ opt.to_s.length == 1 ? " -#{opt}" : " --#{opt}"
49
55
  end
50
56
  end
51
57
  end
52
- end
58
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{pandoc-ruby}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
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 = %q{2009-10-12}
12
+ s.date = %q{2009-10-13}
13
13
  s.description = %q{Ruby wrapper for Pandoc}
14
14
  s.email = %q{wmelody@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -37,18 +37,24 @@ class PandocRubyTest < Test::Unit::TestCase
37
37
  assert converter.convert
38
38
  end
39
39
 
40
+ should "accept a variety of options" do
41
+ converter = PandocRuby.new(@file, :s, {:to => :rst, :f => :markdown}, 'no-wrap')
42
+ converter.expects(:execute).with('pandoc -s --to=rst -f markdown --no-wrap').returns(true)
43
+ assert converter.convert
44
+ end
45
+
40
46
  should "accept optional executable" do
41
47
  converter = PandocRuby.new(@file, 'html2markdown')
42
48
  converter.expects(:execute).with('html2markdown').returns(true)
43
49
  assert converter.convert
44
50
  end
45
51
 
46
- should "not accept non-pandoc optional executable" do
47
- converter = PandocRuby.new(@file, 'ls')
48
- converter.expects(:execute).with('pandoc').returns(true)
52
+ should "use non-executable second arg as option" do
53
+ converter = PandocRuby.new(@file, 'toc')
54
+ converter.expects(:execute).with('pandoc --toc').returns(true)
49
55
  assert converter.convert
50
56
  end
51
-
57
+
52
58
  should "work with strings" do
53
59
  converter = PandocRuby.new('## this is a title')
54
60
  assert_match %r(h2), converter.convert
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pandoc-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - William Melody
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-12 00:00:00 -05:00
12
+ date: 2009-10-13 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency