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.



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

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.



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

def base
  @base
end

#densityObject (readonly)

Returns the value of attribute density.



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

def density
  @density
end

#dirObject (readonly)

Returns the value of attribute dir.



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

def dir
  @dir
end

#formulaObject (readonly)

Returns the value of attribute formula.



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

def formula
  @formula
end

#hashObject (readonly)

Returns the value of attribute hash.



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

def hash
  @hash
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#contentObject



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

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

#dataObject



29
30
31
32
33
# File 'lib/tex2png/converter.rb', line 29

def data
  @data ||= png do |io|
    "data:image/png;base64, #{Base64.encode64(io.read)}"
  end
end

#png(&block) ⇒ Object



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

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