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)


106
107
108
109
110
111
112
113
# File 'lib/pandoc-ruby.rb', line 106

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

Instance Attribute Details

#binary_outputObject



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

def binary_output
  @binary_output ||= false
end

#input_filesObject

Returns the value of attribute input_files.



94
95
96
# File 'lib/pandoc-ruby.rb', line 94

def input_files
  @input_files
end

#input_stringObject

Returns the value of attribute input_string.



95
96
97
# File 'lib/pandoc-ruby.rb', line 95

def input_string
  @input_string
end

#option_stringObject



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

def option_string
  @option_string ||= ''
end

#optionsObject



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

def options
  @options ||= []
end

#writerObject



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

def writer
  @writer ||= 'html'
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"


126
127
128
129
130
131
132
133
134
# File 'lib/pandoc-ruby.rb', line 126

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