class RubyPandoc::Converter
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.
Public Class Methods
Create a new RubyPandoc
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/ruby-pandoc/converter.rb, line 74 def initialize(*args) @input_files = nil @input_string = nil if args[0].is_a?(String) @input_string = args.shift elsif args[0].is_a?(Array) @input_files = args.shift.join(' ') end @options = args || [] @option_string = nil @binary_output = false @writer = 'html' end
To use run the pandoc command with a custom executable path, the path to the pandoc executable can be set here.
# File lib/ruby-pandoc/converter.rb, line 61 def self.pandoc_path=(path) @@pandoc_path = path end
Public Instance Methods
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:
RubyPandoc.new("# text").convert # => "<h1 id=\"text\">text</h1>\n"
# File lib/ruby-pandoc/converter.rb, line 99 def convert(*args) @options += args if args outputfile = @options.map{ |x| x[:output] }.compact tmp_file = Tempfile.new('pandoc-conversion') @options += [{ output: tmp_file.path }] if outputfile.empty? @option_string = prepare_options(@options) begin run_pandoc IO.binread(tmp_file) ensure tmp_file.close tmp_file.unlink end 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/ruby-pandoc/converter.rb, line 152 def convert_binary 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/ruby-pandoc/converter.rb, line 188 def create_option(flag, argument = nil) return '' unless flag flag = flag.to_s return " #{argument}" if flag == 'extra' set_pandoc_ruby_options(flag, argument) if !argument.nil? "#{format_flag(flag)} #{argument}" else format_flag(flag) end end
Formats an option flag in order to be used with the pandoc command line tool.
# File lib/ruby-pandoc/converter.rb, line 202 def format_flag(flag) if flag.length == 1 " -#{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/ruby-pandoc/converter.rb, line 172 def prepare_options(opts = []) opts.inject('') do |string, (option, value)| string += case when value create_option(option, value) when option.respond_to?(:each_pair) prepare_options(option) else create_option(option) end end end
Wrapper to run pandoc in a consistent, DRY way
# File lib/ruby-pandoc/converter.rb, line 156 def run_pandoc command = unless @input_files.nil? || @input_files.empty? "#{@@pandoc_path} #{@input_files} #{@option_string}" else "#{@@pandoc_path} #{@option_string}" end output = error = exit_status = nil options = {} options[:stdin_data] = @input_string if @input_string output, error, exit_status = Open3.capture3(command, **options) raise error unless exit_status && exit_status.success? output end
Takes an option and optional argument and uses them to set any flags used by RubyPandoc
.
# File lib/ruby-pandoc/converter.rb, line 212 def set_pandoc_ruby_options(flag, argument = nil) case flag when 't', 'to' @writer = argument.to_s @binary_output = true if BINARY_WRITERS.keys.include?(@writer) end end