Module: Texvc

Defined in:
lib/texvc.rb

Defined Under Namespace

Classes: LexingError, SyntaxError, UnknownError, UnknownFunctionError

Constant Summary collapse

VERSION =
'0.1.1.1'

Class Method Summary collapse

Class Method Details

.parse(latex, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/texvc.rb', line 14

def parse(latex, options = {})
  image = nil
  latex = "{}_{#{latex}}" if options[:inline]

  TempDir.create :basename => 'texvc' do |tmp|
    cmd = Escape.shell_command(['texvc', tmp, tmp, latex, 'utf8'])
    result = nil
    IO.popen(%Q{#{cmd.to_s} 2>/dev/null}, 'r+') do |io|
      io.puts latex rescue nil
      result = io.gets
    end
  
    path = File.join(tmp, "#{result[1, 32]}.png")

    image = case result[0]
    when ?+; read_image(path)
    when ?c; read_image(path)
    when ?m; read_image(path)
    when ?l; read_image(path)
    when ?C; read_image(path)
    when ?M; read_image(path)
    when ?L; read_image(path)
    when ?X; read_image(path)
    when ?S; raise SyntaxError.new
    when ?E; raise LexingError.new
    when ?F; raise UnknownFunctionError.new(result[1..-1])
    else raise UnknownError.new(result)
    end
  end

  image
end