Class: Tex2png::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/tex2png/converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formula, density: 155) ⇒ Converter

Returns a new instance of Converter.



7
8
9
10
11
12
# File 'lib/tex2png/converter.rb', line 7

def initialize(formula, density: 155)
  @formula = formula
  @density = density
  @hash    = SecureRandom.hex(16)
  @base    = File.join TEMP_DIR, hash
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



5
6
7
# File 'lib/tex2png/converter.rb', line 5

def base
  @base
end

#densityObject (readonly)

Returns the value of attribute density.



5
6
7
# File 'lib/tex2png/converter.rb', line 5

def density
  @density
end

#dirObject (readonly)

Returns the value of attribute dir.



5
6
7
# File 'lib/tex2png/converter.rb', line 5

def dir
  @dir
end

#formulaObject (readonly)

Returns the value of attribute formula.



5
6
7
# File 'lib/tex2png/converter.rb', line 5

def formula
  @formula
end

#hashObject (readonly)

Returns the value of attribute hash.



5
6
7
# File 'lib/tex2png/converter.rb', line 5

def hash
  @hash
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/tex2png/converter.rb', line 5

def name
  @name
end

Instance Method Details

#contentObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tex2png/converter.rb', line 14

def content
  @content ||= "    \\\\documentclass[12pt]{article}\n    \\\\usepackage[utf8]{inputenc}\n    \\\\usepackage{\#{LATEX_PACKAGES.join(\", \")}}\n    \\\\begin{document}\n    \\\\pagestyle{empty}\n    \\\\begin{displaymath}\n    \#{formula}\n    \\\\end{displaymath}\n    \\\\end{document}\n  END\nend\n".gsub(/^ {6}/, "")

#dataObject



28
29
30
# File 'lib/tex2png/converter.rb', line 28

def data
  png {|io| "data:image/png;base64, #{io.read}"}
end

#png(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tex2png/converter.rb', line 32

def png(&block)
  @png ||= proc do
    args = [
     "-q -T tight -D #{density}", "-o #{base}.png", "#{base}.dvi"
    ]

    tex
    dvi
    Tex2png::dvipng.run(*args).raise!
    clean!

    path(:png)
  end.call

  file(:png, &block)
end