Class: Paru::Info

Inherits:
Object
  • Object
show all
Defined in:
lib/paru/info.rb

Overview

Information about pandoc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "pandoc") ⇒ Info

Create a new Info object

assumes it's on the environment's path.

Parameters:

  • path (String) (defaults to: "pandoc")

    the path to pandoc. Defaults to 'pandoc', i.e.,



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/paru/info.rb', line 39

def initialize(path = "pandoc")
  begin
    # Get pandoc's version information
    version_string = ''
    IO.popen("#{path} --version", 'r+') do |p|
      p.close_write
      version_string << p.read
    end

    # Extract the version as an array of integers, like SemVer.
    @version = version_string
      .match(/pandoc.* (\d+\.\d+.*)$/)[1]
      .split(".")
      .map {|s| s.to_i}

    # Extract the data directory
    @data_dir = version_string.match(/User data directory: (.+)$/)[1]

    # Extract scripting engine
    @scripting_engine = version_string.match(/Scripting engine: (.+)$/)[1]
  rescue StandardError => err
    warn "Error extracting pandoc's information: #{err.message}"
    warn "Using made up values instead."

    @version = @version || [2, 18]
    @data_dir = @data_dir || "."
    @scripting_engine = @scripting_engine || "Lua 5.4"
  end
end

Instance Attribute Details

#data_dirString

Returns Pandoc's default data directory.

Returns:

  • (String)

    Pandoc's default data directory



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
# File 'lib/paru/info.rb', line 32

class Info
  attr_reader :version, :data_dir, :scripting_engine

  # Create a new Info object
  #
  # @param path [String] the path to pandoc. Defaults to 'pandoc', i.e.,
  # assumes it's on the environment's path.
  def initialize(path = "pandoc")
    begin
      # Get pandoc's version information
      version_string = ''
      IO.popen("#{path} --version", 'r+') do |p|
        p.close_write
        version_string << p.read
      end

      # Extract the version as an array of integers, like SemVer.
      @version = version_string
        .match(/pandoc.* (\d+\.\d+.*)$/)[1]
        .split(".")
        .map {|s| s.to_i}

      # Extract the data directory
      @data_dir = version_string.match(/User data directory: (.+)$/)[1]

      # Extract scripting engine
      @scripting_engine = version_string.match(/Scripting engine: (.+)$/)[1]
    rescue StandardError => err
      warn "Error extracting pandoc's information: #{err.message}"
      warn "Using made up values instead."

      @version = @version || [2, 18]
      @data_dir = @data_dir || "."
      @scripting_engine = @scripting_engine || "Lua 5.4"
    end
  end

  # Get pandoc's info by key like a Hash for backwards compatability.
  #
  # @deprecated Use Info's getters instead.
  # 
  # @param key [String|Symbol] the key for the information to look up.
  # Info only supports keys 'version' and 'data_dir'.
  # @return [Any] Information associated with the key.
  # @raise [Error] for an unknown key.
  def [](key)
    case key
    when "verion", :version
      version
    when "data_dir", :data_dir
      data_dir
    when "scripting_engine", :scripting_engine
      scripting_engine
    else
      throw Error.new "Info does not know key '#{key}'"
    end
  end
end

#scripting_engineString

Returns Pandoc's internal scripting engine, like “Lua 5.4”.

Returns:

  • (String)

    Pandoc's internal scripting engine, like “Lua 5.4”



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
# File 'lib/paru/info.rb', line 32

class Info
  attr_reader :version, :data_dir, :scripting_engine

  # Create a new Info object
  #
  # @param path [String] the path to pandoc. Defaults to 'pandoc', i.e.,
  # assumes it's on the environment's path.
  def initialize(path = "pandoc")
    begin
      # Get pandoc's version information
      version_string = ''
      IO.popen("#{path} --version", 'r+') do |p|
        p.close_write
        version_string << p.read
      end

      # Extract the version as an array of integers, like SemVer.
      @version = version_string
        .match(/pandoc.* (\d+\.\d+.*)$/)[1]
        .split(".")
        .map {|s| s.to_i}

      # Extract the data directory
      @data_dir = version_string.match(/User data directory: (.+)$/)[1]

      # Extract scripting engine
      @scripting_engine = version_string.match(/Scripting engine: (.+)$/)[1]
    rescue StandardError => err
      warn "Error extracting pandoc's information: #{err.message}"
      warn "Using made up values instead."

      @version = @version || [2, 18]
      @data_dir = @data_dir || "."
      @scripting_engine = @scripting_engine || "Lua 5.4"
    end
  end

  # Get pandoc's info by key like a Hash for backwards compatability.
  #
  # @deprecated Use Info's getters instead.
  # 
  # @param key [String|Symbol] the key for the information to look up.
  # Info only supports keys 'version' and 'data_dir'.
  # @return [Any] Information associated with the key.
  # @raise [Error] for an unknown key.
  def [](key)
    case key
    when "verion", :version
      version
    when "data_dir", :data_dir
      data_dir
    when "scripting_engine", :scripting_engine
      scripting_engine
    else
      throw Error.new "Info does not know key '#{key}'"
    end
  end
end

#versionArray<Integer>

Returns Pandoc's version, like [2, 18, 1].

Returns:

  • (Array<Integer>)

    Pandoc's version, like [2, 18, 1]



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
# File 'lib/paru/info.rb', line 32

class Info
  attr_reader :version, :data_dir, :scripting_engine

  # Create a new Info object
  #
  # @param path [String] the path to pandoc. Defaults to 'pandoc', i.e.,
  # assumes it's on the environment's path.
  def initialize(path = "pandoc")
    begin
      # Get pandoc's version information
      version_string = ''
      IO.popen("#{path} --version", 'r+') do |p|
        p.close_write
        version_string << p.read
      end

      # Extract the version as an array of integers, like SemVer.
      @version = version_string
        .match(/pandoc.* (\d+\.\d+.*)$/)[1]
        .split(".")
        .map {|s| s.to_i}

      # Extract the data directory
      @data_dir = version_string.match(/User data directory: (.+)$/)[1]

      # Extract scripting engine
      @scripting_engine = version_string.match(/Scripting engine: (.+)$/)[1]
    rescue StandardError => err
      warn "Error extracting pandoc's information: #{err.message}"
      warn "Using made up values instead."

      @version = @version || [2, 18]
      @data_dir = @data_dir || "."
      @scripting_engine = @scripting_engine || "Lua 5.4"
    end
  end

  # Get pandoc's info by key like a Hash for backwards compatability.
  #
  # @deprecated Use Info's getters instead.
  # 
  # @param key [String|Symbol] the key for the information to look up.
  # Info only supports keys 'version' and 'data_dir'.
  # @return [Any] Information associated with the key.
  # @raise [Error] for an unknown key.
  def [](key)
    case key
    when "verion", :version
      version
    when "data_dir", :data_dir
      data_dir
    when "scripting_engine", :scripting_engine
      scripting_engine
    else
      throw Error.new "Info does not know key '#{key}'"
    end
  end
end

Instance Method Details

#[](key) ⇒ Any

Deprecated.

Use Info's getters instead.

Get pandoc's info by key like a Hash for backwards compatability.

Info only supports keys 'version' and 'data_dir'.

Parameters:

  • key (String|Symbol)

    the key for the information to look up.

Returns:

  • (Any)

    Information associated with the key.

Raises:

  • (Error)

    for an unknown key.



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/paru/info.rb', line 77

def [](key)
  case key
  when "verion", :version
    version
  when "data_dir", :data_dir
    data_dir
  when "scripting_engine", :scripting_engine
    scripting_engine
  else
    throw Error.new "Info does not know key '#{key}'"
  end
end