Class: ReVIEW::ImgMath

Inherits:
Object show all
Includes:
Loggable
Defined in:
lib/review/img_math.rb

Instance Attribute Summary

Attributes included from Loggable

#logger

Instance Method Summary collapse

Methods included from Loggable

#app_error, #debug, #error, #error!, #warn

Constructor Details

#initialize(config, path_name: '_review_math') ⇒ ImgMath

Returns a new instance of ImgMath.



16
17
18
19
20
21
# File 'lib/review/img_math.rb', line 16

def initialize(config, path_name: '_review_math')
  @config = config
  @logger = ReVIEW.logger
  @math_dir = File.join(@config['imagedir'], path_name)
  @math_maps = {}
end

Instance Method Details

#cleanup_mathimgObject



23
24
25
26
27
# File 'lib/review/img_math.rb', line 23

def cleanup_mathimg
  if @config['math_format'] == 'imgmath' && Dir.exist?(@math_dir)
    FileUtils.rm_rf(@math_dir)
  end
end

#defer_math_image(str, key) ⇒ Object



29
30
31
32
33
# File 'lib/review/img_math.rb', line 29

def defer_math_image(str, key)
  # for Re:VIEW >3
  @math_maps[key] = str
  File.join(@math_dir, "_gen_#{key}.#{@config['imgmath_options']['format']}")
end

#make_math_image(str, key, fontsize = 12) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/review/img_math.rb', line 35

def make_math_image(str, key, fontsize = 12)
  # Re:VIEW 2 compatibility

  img_path = File.join(@math_dir, "_gen_#{key}.#{@config['imgmath_options']['format']}")
  FileUtils.mkdir_p(@math_dir)
  fontsize2 = (fontsize * 1.2).round.to_i
  texsrc = <<-EOB
\\documentclass[12pt]{article}
\\usepackage[utf8]{inputenc}
\\usepackage{amsmath}
\\usepackage{amsthm}
\\usepackage{amssymb}
\\usepackage{amsfonts}
\\usepackage{anyfontsize}
\\usepackage{bm}
\\pagestyle{empty}

\\begin{document}
\\fontsize{#{fontsize}}{#{fontsize2}}\\selectfont #{str}
\\end{document}
  EOB
  Dir.mktmpdir do |tmpdir|
    tex_path = File.join(tmpdir, 'tmpmath.tex')
    dvi_path = File.join(tmpdir, 'tmpmath.dvi')
    File.write(tex_path, texsrc)
    cmd = "latex --interaction=nonstopmode --output-directory=#{tmpdir} #{tex_path} && dvipng -T tight -z9 -o #{img_path} #{dvi_path}"
    out, status = Open3.capture2e(cmd)
    unless status.success?
      raise ApplicationError, "latex compile error\n\nError log:\n#{out}"
    end

    img_path
  end
end

#make_math_imagesObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/review/img_math.rb', line 70

def make_math_images
  return if @math_maps.empty?

  FileUtils.mkdir_p(@math_dir)
  fontsize = @config['imgmath_options']['fontsize'].to_f
  lineheight = @config['imgmath_options']['lineheight'].to_f

  texsrc = default_imgmath_preamble
  if @config['imgmath_options']['preamble_file'] && File.readable?(@config['imgmath_options']['preamble_file'])
    texsrc = File.read(@config['imgmath_options']['preamble_file'])
  end

  texsrc << <<-EOB
\\begin{document}
\\fontsize{#{fontsize}}{#{lineheight}}\\selectfont
\\input{__IMGMATH_BODY__}
\\end{document}
  EOB

  math_real_dir = File.realpath(@math_dir)
  Dir.mktmpdir do |tmpdir|
    File.open(File.join(tmpdir, '__IMGMATH_BODY__.tex'), 'w') do |f|
      @math_maps.keys.sort.each do |key|
        f.puts "% #{key}"
        f.puts @math_maps[key]
        f.puts '\\clearpage'
        f.puts
      end
    end

    tex_path = File.join(tmpdir, '__IMGMATH__.tex')
    File.write(tex_path, texsrc)

    begin
      case @config['imgmath_options']['converter']
      when 'pdfcrop'
        make_math_images_pdfcrop(tmpdir, tex_path, math_real_dir)
      when 'dvipng'
        make_math_images_dvipng(tmpdir, tex_path, math_real_dir)
      else
        error! "unknown math converter error. imgmath_options/converter parameter should be 'pdfcrop' or 'dvipng'."
      end
    rescue CompileError
      FileUtils.cp([tex_path,
                    File.join(File.dirname(tex_path), '__IMGMATH_BODY__.tex'),
                    File.join(File.dirname(tex_path), '__IMGMATH__.log')],
                   math_real_dir)
      error! "LaTeX math compile error. See #{math_real_dir}/__IMGMATH__.log for details."
    end
  end
  @math_maps.clear
end