class PandocRuby
Constants
- BINARY_WRITERS
The available binary writers and their corresponding names. The keys are used to generate methods and specify options to Pandoc.
- READERS
The available readers and their corresponding names. The keys are used to generate methods and specify options to Pandoc.
- STRING_WRITERS
The available string writers and their corresponding names. The keys are used to generate methods and specify options to Pandoc.
- WRITERS
All of the available Writers.
Attributes
Public Class Methods
A shortcut method that creates a new PandocRuby
object and immediately calls ‘#convert`. Options passed to this method are passed directly to `#new` and treated the same as if they were passed directly to the initializer.
# File lib/pandoc-ruby.rb, line 137 def self.convert(*args) new(*args).convert end
Create a new PandocRuby
converter object. The first argument contains the input either as string or as an array of filenames.
Any other arguments will be converted to pandoc options.
Usage:
new("# A String", :option1 => :value, :option2) new(["/path/to/file.md"], :option1 => :value, :option2) new(["/to/file1.html", "/to/file2.html"], :option1 => :value)
# File lib/pandoc-ruby.rb, line 177 def initialize(*args) case args[0] when String self.input_string = args.shift when Array self.input_files = args.shift.map { |f| "'#{f}'" }.join(' ') end self.options = args end
Public Instance Methods
# File lib/pandoc-ruby.rb, line 143 def binary_output @binary_output ||= false end
Run the conversion. The convert method can take any number of arguments, which will be converted to pandoc options. If options were already specified in an initializer or reader method, they will be combined with any that are passed to this method.
Returns a string with the converted content.
Example:
PandocRuby.new("# text").convert # => "<h1 id=\"text\">text</h1>\n"
# File lib/pandoc-ruby.rb, line 199 def convert(*args) self.options += args if args self.option_string = prepare_options(self.options) if self.binary_output convert_binary else convert_string end end
# File lib/pandoc-ruby.rb, line 155 def option_string @option_string ||= '' end
# File lib/pandoc-ruby.rb, line 149 def options @options ||= [] end
# File lib/pandoc-ruby.rb, line 161 def writer @writer ||= 'html' end
Private Instance Methods
Execute the pandoc command for binary writers. A temp file is created and written to, then read back into the program as a string, then the temp file is closed and unlinked.
# File lib/pandoc-ruby.rb, line 251 def convert_binary tmp_file = Tempfile.new('pandoc-conversion') begin self.options += [{ :output => tmp_file.path }] self.option_string = "#{self.option_string} --output \"#{tmp_file.path}\"" execute_pandoc return IO.binread(tmp_file) ensure tmp_file.close tmp_file.unlink end end
Execute the pandoc command for string writers.
# File lib/pandoc-ruby.rb, line 269 def convert_string execute_pandoc end
Takes a flag and optional argument, uses it to set any relevant options used by the library, and returns string with the option formatted as a command line options. If the option has an argument, it is also included.
# File lib/pandoc-ruby.rb, line 329 def create_option(flag, argument = nil) return '' unless flag flag = flag.to_s set_pandoc_ruby_options(flag, argument) return '' if flag == 'timeout' # pandoc doesn't accept timeouts yet if argument.nil? format_flag(flag) else "#{format_flag(flag)} \"#{argument}\"" end end
Run the command and returns the output.
# File lib/pandoc-ruby.rb, line 283 def execute(command) output = error = exit_status = nil @timeout ||= 31_557_600 Open3.popen3(command) do |stdin, stdout, stderr, wait_thr| begin Timeout.timeout(@timeout) do stdin.puts self.input_string stdin.close output = stdout.read error = stderr.read exit_status = wait_thr.value end rescue Timeout::Error => ex Process.kill 9, wait_thr.pid maybe_ex = "\n#{ex}" if ex error = "Pandoc timed out after #{@timeout} seconds.#{maybe_ex}" end end raise error unless exit_status && exit_status.success? output end
Wrapper to run pandoc in a consistent, DRY way
# File lib/pandoc-ruby.rb, line 274 def execute_pandoc if !self.input_files.nil? execute("#{PandocRuby.pandoc_path} #{self.input_files}#{self.option_string}") else execute("#{PandocRuby.pandoc_path}#{self.option_string}") end end
Formats an option flag in order to be used with the pandoc command line tool.
# File lib/pandoc-ruby.rb, line 345 def format_flag(flag) if flag.length == 1 " -#{flag}" elsif flag =~ /^-|\+/ " #{flag}" else " --#{flag.to_s.tr('_', '-')}" end end
Builds the option string to be passed to pandoc by iterating over the opts passed in. Recursively calls itself in order to handle hash options.
# File lib/pandoc-ruby.rb, line 314 def prepare_options(opts = []) opts.inject('') do |string, (option, value)| string + if value create_option(option, value) elsif option.respond_to?(:each_pair) prepare_options(option) else create_option(option) end end end
Takes an option and optional argument and uses them to set any flags used by PandocRuby
.
# File lib/pandoc-ruby.rb, line 357 def set_pandoc_ruby_options(flag, argument = nil) case flag when 't', 'to' self.writer = argument.to_s self.binary_output = true if BINARY_WRITERS.key?(self.writer) when 'timeout' @timeout = argument end end