Class: PanHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/pan_handler/source.rb,
lib/pan_handler/version.rb,
lib/pan_handler/middleware.rb,
lib/pan_handler/pan_handler.rb,
lib/pan_handler/configuration.rb

Defined Under Namespace

Classes: Configuration, ImproperSourceError, Middleware, NoExecutableError, Source

Constant Summary collapse

VERSION =
"0.0.2"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_file_or_html, options = {}) ⇒ PanHandler

Returns a new instance of PanHandler.

Raises:



22
23
24
25
26
27
28
# File 'lib/pan_handler/pan_handler.rb', line 22

def initialize(url_file_or_html, options = {})
  @source = Source.new(url_file_or_html)
  @options = PanHandler.configuration.default_options.merge(options)
  @options = normalize_options(@options)

  raise NoExecutableError.new unless File.exists?(PanHandler.configuration.pandoc)
end

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



20
21
22
# File 'lib/pan_handler/configuration.rb', line 20

def configuration
  @configuration
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/pan_handler/pan_handler.rb', line 20

def options
  @options
end

#sourceObject

Returns the value of attribute source.



19
20
21
# File 'lib/pan_handler/pan_handler.rb', line 19

def source
  @source
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



27
28
29
# File 'lib/pan_handler/configuration.rb', line 27

def self.configure
  yield(configuration)
end

Instance Method Details

#command(path = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pan_handler/pan_handler.rb', line 30

def command(path = nil)
  args = [executable]
  args += @options.to_a.flatten.compact

  args << '--output'
  args << (path || '-') # Write to file or stdout

  unless @source.html?
    args << @source.to_s
  end

  args.map {|arg| %Q{"#{arg.gsub('"', '\"')}"}}
end

#executableObject



44
45
46
47
48
49
50
51
52
# File 'lib/pan_handler/pan_handler.rb', line 44

def executable
  default = PanHandler.configuration.pandoc
  return default if default !~ /^\// # its not a path, so nothing we can do
  if File.exist?(default)
    default
  else
    default.split('/').last
  end
end

#to_file(path) ⇒ Object



74
75
76
77
# File 'lib/pan_handler/pan_handler.rb', line 74

def to_file(path)
  self.to_odt(path)
  File.new(path)
end

#to_odt(path = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pan_handler/pan_handler.rb', line 54

def to_odt(path=nil)
  if path.nil?
    tempfile = Tempfile.new('panhandler')
    path = tempfile.path
  end
  args = command(path)
  invoke = args.join(' ')

  result = IO.popen(invoke, "wb+") do |odt|
    odt.puts(@source.to_s) if @source.html?
    odt.close_write
    odt.gets(nil)
  end
  result = File.read(path) if path
  tempfile.unlink if tempfile

  raise "command failed: #{invoke}" if result.empty?
  return result
end