Module: Gollum::Tex

Extended by:
POSIX::Spawn
Defined in:
lib/gollum/tex.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

Template =
<<-EOS
\\documentclass[12pt]{article}
\\usepackage{color}
\\usepackage[dvips]{graphicx}
\\pagestyle{empty}
\\pagecolor{white}
\\begin{document}
{\\color{black}
\\begin{eqnarray*}
%s
\\end{eqnarray*}}
\\end{document}
EOS

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.convert_pathObject

Returns the value of attribute convert_path.



27
28
29
# File 'lib/gollum/tex.rb', line 27

def convert_path
  @convert_path
end

.dvips_pathObject

Returns the value of attribute dvips_path.



27
28
29
# File 'lib/gollum/tex.rb', line 27

def dvips_path
  @dvips_path
end

.latex_pathObject

Returns the value of attribute latex_path.



27
28
29
# File 'lib/gollum/tex.rb', line 27

def latex_path
  @latex_path
end

Class Method Details

.check_dependencies!Object



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

def self.check_dependencies!
  return if @dependencies_available

  if `which latex` == ""
    raise Error, "`latex` command not found"
  end

  if `which dvips` == ""
    raise Error, "`dvips` command not found"
  end

  if `which convert` == ""
    raise Error, "`convert` command not found"
  end

  if `which gs` == ""
    raise Error, "`gs` command not found"
  end

  @dependencies_available = true
end

.render_formula(formula) ⇒ Object



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
# File 'lib/gollum/tex.rb', line 56

def self.render_formula(formula)
  check_dependencies!

  Dir.mktmpdir('tex') do |path|
    tex_path = ::File.join(path, 'formula.tex')
    dvi_path = ::File.join(path, 'formula.dvi')
    eps_path = ::File.join(path, 'formula.eps')
    png_path = ::File.join(path, 'formula.png')

    ::File.open(tex_path, 'w') { |f| f.write(Template % formula) }

    result = sh latex_path, '-interaction=batchmode', 'formula.tex', :chdir => path
    raise Error, "`latex` command failed: #{result}" unless ::File.exist?(dvi_path)

    result = sh dvips_path, '-o', eps_path, '-E', dvi_path
    raise Error, "`dvips` command failed: #{result}" unless ::File.exist?(eps_path)
    result = sh convert_path, '+adjoin',
      '-antialias',
      '-transparent', 'white',
      '-density', '150x150',
      eps_path, png_path
    raise Error, "`convert` command failed: #{result}" unless ::File.exist?(png_path)

    ::File.read(png_path)
  end
end