Module: Yaml::Converter::Renderer::PandocShell

Defined in:
lib/yaml/converter/renderer/pandoc_shell.rb

Overview

Shells out to pandoc to convert markdown to target format.
Provides lightweight detection of pandoc and execution with arbitrary arguments.

Examples:

Convert markdown to PDF

Yaml::Converter::Renderer::PandocShell.render(
  md_path: 'doc.md', out_path: 'doc.pdf', args: ['-N', '--toc']
)

Class Method Summary collapse

Class Method Details

.render(md_path:, out_path:, pandoc_path: nil, args: []) ⇒ Boolean

Invoke pandoc to convert markdown file to another format.

Examples:

Basic HTML conversion

Yaml::Converter::Renderer::PandocShell.render(
  md_path: 'in.md', out_path: 'out.html'
)

Parameters:

  • md_path (String)

    path to markdown file

  • out_path (String)

    desired output file path with extension

  • pandoc_path (String, nil) (defaults to: nil)

    override path to pandoc binary (auto-detected if nil)

  • args (Array<String>) (defaults to: [])

    extra pandoc arguments (e.g. [‘-N’,’–toc’])

Returns:

  • (Boolean)

    true if successful, false otherwise



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yaml/converter/renderer/pandoc_shell.rb', line 40

def render(md_path:, out_path:, pandoc_path: nil, args: [])
  bin = pandoc_path || which("pandoc")
  return false unless bin
  cmd = [bin] + args + ["-o", out_path, md_path]
  _stdout, stderr, status = Open3.capture3(*cmd)
  unless status.success?
    warn("pandoc failed: #{stderr}")
    return false
  end
  true
end

.which(cmd) ⇒ String?

Locate an executable in the current PATH.

Parameters:

  • cmd (String)

    command name, e.g. ‘pandoc’

Returns:

  • (String, nil)

    full path if found, otherwise nil



21
22
23
24
25
26
27
# File 'lib/yaml/converter/renderer/pandoc_shell.rb', line 21

def which(cmd)
  ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
    exe = File.join(path, cmd)
    return exe if File.executable?(exe)
  end
  nil
end