Module: PandocRb

Defined in:
lib/pandoc_rb.rb,
lib/pandoc_rb/error.rb,
lib/pandoc_rb/readers.rb,
lib/pandoc_rb/version.rb,
lib/pandoc_rb/writers.rb,
lib/pandoc_rb/parsec_error.rb,
lib/pandoc_rb/parse_failure.rb,
ext/pandoc_rb/pandoc_rb.c

Defined Under Namespace

Classes: Error, ParseFailure, ParsecError

Constant Summary collapse

Readers =
[ "commonmark",
  "docbook",
  "docx",
  "epub",
  "haddock",
  "html",
  "json",
  "latex",
  "markdown",
  "markdown_github",
  "markdown_mmd",
  "markdown_phpextra",
  "markdown_strict",
  "mediawiki",
  "native",
  "odt",
  "opml",
  "org",
  "rst",
  "t2t",
  "textile",
  "twiki"
]
VERSION =
"0.2.2"
Writers =
[ "asciidoc",
  "beamer",
  "commonmark",
  "context",
  "docbook",
  "docbook5",
  "docx",
  "dokuwiki",
  "dzslides",
  "epub",
  "epub3",
  "fb2",
  "haddock",
  "html",
  "html5",
  "icml",
  "json",
  "latex",
  "man",
  "markdown",
  "markdown_github",
  "markdown_mmd",
  "markdown_phpextra",
  "markdown_strict",
  "mediawiki",
  "native",
  "odt",
  "opendocument",
  "opml",
  "org",
  "plain",
  "revealjs",
  "rst",
  "rtf",
  "s5",
  "slideous",
  "slidy",
  "tei",
  "texinfo",
  "textile",
  "zimwiki"
]

Class Method Summary collapse

Class Method Details

.convert(in_format_str, out_format_str, input_str, extract_media_path = '') ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pandoc_rb.rb', line 33

def self.convert(in_format_str, out_format_str, input_str, extract_media_path='')
  unless @PandocRb_loaded
    self.convert_init
    Kernel.at_exit do
      PandocRb.convert_exit
    end
    @PandocRb_loaded = true
  end

  if in_format_str.to_s == 'docx'
    input_str.force_encoding "UTF-8"
  end

  begin
    in_format          = in_format_str.to_s.encode "UTF-8"
    out_format         = out_format_str.to_s.encode "UTF-8"
    input              = input_str.to_s.encode "UTF-8"
    extract_media_path = extract_media_path.to_s.encode "UTF-8"
    success, result    = PandocRb.convert_raw in_format, out_format, input, extract_media_path
    self.raise_exception result if (success == false)
    result
  end
end

.convert_exitObject



14
# File 'ext/pandoc_rb/pandoc_rb.c', line 14

VALUE method_convert_exit(VALUE self);

.convert_initObject



13
# File 'ext/pandoc_rb/pandoc_rb.c', line 13

VALUE method_convert_init(VALUE self);

.convert_rawObject



15
# File 'ext/pandoc_rb/pandoc_rb.c', line 15

VALUE method_convert_raw(VALUE self, VALUE in_format, VALUE out_format, VALUE input, VALUE extract_media_path);

.raise_exception(result) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pandoc_rb.rb', line 13

def self.raise_exception(result)
  if    /^Unknown reader: / === result
    raise ArgumentError, result
  elsif /^Unknown writer: / === result
    raise ArgumentError, result
  elsif /^Pandoc timed out/ === result
    raise Timeout::Error
  elsif /^Pandoc internal / === result
    raise PandocRb::Error, result.sub(/^Pandoc internal error: /, '')
  end
  result = JSON.parse result
  if    result['tag'] == 'ParseFailure'
    raise PandocRb::ParseFailure.new(result['contents'])
  elsif result['tag'] == 'ParsecError'
    raise PandocRb::ParsecError.new( result['contents'])
  else
    raise "Unknown error type returned from pandoc: #{result}"
  end
end

.reader_from_ext(extension) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pandoc_rb.rb', line 57

def self.reader_from_ext(extension)
  {
    ""          => "markdown",
    ".tex"      => "latex",
    ".latex"    => "latex",
    ".ltx"      => "latex",
    ".context"  => "context",
    ".ctx"      => "context",
    ".rtf"      => "rtf",
    ".rst"      => "rst",
    ".s5"       => "s5",
    ".native"   => "native",
    ".json"     => "json",
    ".txt"      => "markdown",
    ".text"     => "markdown",
    ".md"       => "markdown",
    ".markdown" => "markdown",
    ".textile"  => "textile",
    ".lhs"      => "markdown+lhs",
    ".texi"     => "texinfo",
    ".texinfo"  => "texinfo",
    ".db"       => "docbook",
    ".odt"      => "odt",
    ".docx"     => "docx",
    ".epub"     => "epub",
    ".org"      => "org",
    ".asciidoc" => "asciidoc",
    ".adoc"     => "asciidoc",
    ".pdf"      => "latex",
    ".fb2"      => "fb2",
    ".opml"     => "opml",
    ".icml"     => "icml",
    ".tei.xml"  => "tei",
    ".tei"      => "tei",
  }[extension&.downcase] || !!extension&.downcase&.match(/\.\d$/)
end