Class: PandocRuby

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

Constant Summary

EXECUTABLES =

The executable options. The `pandoc` executable is used by default.

pandoc
markdown2pdf
html2markdown
hsmarkdown
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',
}
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'
}
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'
}
WRITERS =

All of the available Writers.

STRING_WRITERS.merge(BINARY_WRITERS)
@@bin_path =
nil
@@allow_file_paths =
false

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 should be the string that will be converted or, if `.allow_file_paths` has been set to `true`, this can also be a path to a file. The executable name can be used as the second argument, but will default to `pandoc` if the second argument is omitted or anything other than an executable name. Any other arguments will be converted to pandoc options.



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

def initialize(*args)
  target = args.shift
  @target = if @@allow_file_paths && File.exists?(target)
    File.read(target)
  else
    target rescue target
  end
  @executable = args.shift if EXECUTABLES.include?(args[0])
  self.options = args
end

Instance Attribute Details

#binary_outputObject

Returns the value of attribute binary_output



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

def binary_output
  @binary_output
end

#option_stringObject

Returns the value of attribute option_string



92
93
94
# File 'lib/pandoc-ruby.rb', line 92

def option_string
  @option_string
end

#optionsObject

Returns the value of attribute options



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

def options
  @options
end

#writerObject

Returns the value of attribute writer



98
99
100
# File 'lib/pandoc-ruby.rb', line 98

def writer
  @writer
end

Class Method Details

.allow_file_paths=(value) ⇒ Object

Pandoc can also be used with a file path as the first argument. For security reasons, this is disabled by default, but it can be enabled by setting this to `true`.



77
78
79
# File 'lib/pandoc-ruby.rb', line 77

def self.allow_file_paths=(value)
  @@allow_file_paths = value
end

.bin_path=(path) ⇒ Object

If the pandoc executables are not in the PATH, bin_path can be set to the directory they are contained in.



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

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

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



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

def self.convert(*args)
  new(*args).convert
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"


129
130
131
132
133
134
135
136
137
# File 'lib/pandoc-ruby.rb', line 129

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