Class: RubyPandoc::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-pandoc/converter.rb

Constant Summary collapse

READERS =

The available readers and their corresponding names. The keys are used to generate methods and specify options to Pandoc.

{
  'native'   => 'pandoc native',
  'json'     => 'pandoc JSON',
  'markdown' => 'markdown',
  'rst'      => 'reStructuredText',
  'textile'  => 'textile',
  'html'     => 'HTML',
  'latex'    => 'LaTeX'
}.freeze
STRING_WRITERS =

The available string writers and their corresponding names. The keys are used to generate methods and specify options to Pandoc.

{
  'native'        => 'pandoc native',
  'json'          => 'pandoc JSON',
  'html'          => 'HTML',
  'html5'         => 'HTML5',
  's5'            => 'S5 HTML slideshow',
  'slidy'         => 'Slidy HTML slideshow',
  'dzslides'      => 'Dzslides HTML slideshow',
  'docbook'       => 'DocBook XML',
  'opendocument'  => 'OpenDocument XML',
  'latex'         => 'LaTeX',
  'beamer'        => 'Beamer PDF slideshow',
  'context'       => 'ConTeXt',
  'texinfo'       => 'GNU Texinfo',
  'man'           => 'groff man',
  'markdown'      => 'markdown',
  'plain'         => 'plain',
  'rst'           => 'reStructuredText',
  'mediawiki'     => 'MediaWiki markup',
  'textile'       => 'textile',
  'rtf'           => 'rich text format',
  'org'           => 'emacs org mode',
  'asciidoc'      => 'asciidoc'
}.freeze
BINARY_WRITERS =

The available binary writers and their corresponding names. The keys are used to generate methods and specify options to Pandoc.

{
  'odt'   => 'OpenDocument',
  'docx'  => 'Word docx',
  'epub'  => 'EPUB V2',
  'epub3' => 'EPUB V3'
}.freeze
WRITERS =

All of the available Writers.

STRING_WRITERS.merge(BINARY_WRITERS)
@@pandoc_path =
'pandoc'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Converter

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)


74
75
76
77
78
79
80
81
82
83
84
85
86
# 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

Class Method Details

.pandoc_path=(path) ⇒ Object

To use run the pandoc command with a custom executable path, the path to the pandoc executable can be set here.



61
62
63
# File 'lib/ruby-pandoc/converter.rb', line 61

def self.pandoc_path=(path)
  @@pandoc_path = path
end

Instance Method Details

#convert(*args) ⇒ Object

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"


99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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