Class: PandocRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/pandoc-ruby.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'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ PandocRuby

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)


103
104
105
106
107
108
109
110
# File 'lib/pandoc-ruby.rb', line 103

def initialize(*args)
  if args[0].is_a?(String)
    @input_string = args.shift
  elsif args[0].is_a?(Array)
    @input_files = args.shift.join(' ')
  end
  self.options = args
end

Instance Attribute Details

#binary_outputObject

Returns the value of attribute binary_output.



84
85
86
# File 'lib/pandoc-ruby.rb', line 84

def binary_output
  @binary_output
end

#option_stringObject

Returns the value of attribute option_string.



79
80
81
# File 'lib/pandoc-ruby.rb', line 79

def option_string
  @option_string
end

#optionsObject

Returns the value of attribute options.



74
75
76
# File 'lib/pandoc-ruby.rb', line 74

def options
  @options
end

#writerObject

Returns the value of attribute writer.



89
90
91
# File 'lib/pandoc-ruby.rb', line 89

def writer
  @writer
end

Class Method Details

.convert(*args) ⇒ Object

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.



70
71
72
# File 'lib/pandoc-ruby.rb', line 70

def self.convert(*args)
  new(*args).convert
end

.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.



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

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

Instance Method Details

#convert(*args) ⇒ Object Also known as: to_s

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"


123
124
125
126
127
128
129
130
131
# File 'lib/pandoc-ruby.rb', line 123

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