Module: ReVIEW::MakerHelper

Included in:
EPUBMaker, WEBMaker
Defined in:
lib/review/makerhelper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bindirObject

Return review/bin directory



16
17
18
# File 'lib/review/makerhelper.rb', line 16

def bindir
  Pathname.new("#{Pathname.new(__FILE__).realpath.dirname}/../../bin").realpath
end

.copy_images_to_dir(from_dir, to_dir, options = {}) ⇒ Object

Copy image files under from_dir to to_dir recursively

Args

from_dir

path to the directory which has image files to be copied

to_dir

path to the directory to which the image files are copied

options

used to specify optional operations during copy

Returns

list of image files

Options

:convert

Conversion rule

Examples

copy_images_to_dir("/path/to/foo", "/path/to/bar", :convert => {:eps => :png})

Image files are copied recursively, and each ‘.eps’ file is converted into ‘.eps.png’



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
# File 'lib/review/makerhelper.rb', line 37

def copy_images_to_dir(from_dir, to_dir, options = {})
  image_files = []

  Dir.open(from_dir) do |dir|
    dir.each do |fname|
      next if fname =~ /^\./
      if FileTest.directory?("#{from_dir}/#{fname}")
        image_files += copy_images_to_dir("#{from_dir}/#{fname}", "#{to_dir}/#{fname}", options)
      else
        FileUtils.mkdir_p(to_dir) unless File.exist?(to_dir)

        is_converted = false
        (options[:convert] || {}).each do |orig_type, conv_type|
          next unless /\.#{orig_type}$/ =~ fname
          is_converted = system("convert #{from_dir}/#{fname} #{to_dir}/#{fname}.#{conv_type}")
          image_files << "#{from_dir}/#{fname}.#{conv_type}"
        end

        exts = options[:exts] || %w[png gif jpg jpeg svg pdf eps ai tif psd]
        exts_str = exts.join('|')
        if !is_converted && fname =~ /\.(#{exts_str})$/i
          FileUtils.cp "#{from_dir}/#{fname}", to_dir
          image_files << "#{from_dir}/#{fname}"
        end
      end
    end
  end

  image_files
end

Instance Method Details

#cleanup_mathimgObject



69
70
71
72
73
74
# File 'lib/review/makerhelper.rb', line 69

def cleanup_mathimg
  math_dir = "./#{@config['imagedir']}/_review_math"
  if @config['imgmath'] && Dir.exist?(math_dir)
    FileUtils.rm_rf(math_dir)
  end
end

#default_imgmath_preambleObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/review/makerhelper.rb', line 76

def default_imgmath_preamble
  <<-EOB
\\documentclass[uplatex,a3paper,landscape]{jsarticle}
\\usepackage[deluxe,uplatex]{otf}
\\usepackage[T1]{fontenc}
\\usepackage{textcomp}
\\usepackage{lmodern}
\\usepackage[dvipdfmx]{graphicx}
\\usepackage[dvipdfmx,table]{xcolor}
\\usepackage[utf8]{inputenc}
\\usepackage{ascmac}
\\usepackage{float}
\\usepackage{alltt}
\\usepackage{amsmath}
\\usepackage{amssymb}
\\usepackage{amsfonts}
\\usepackage{anyfontsize}
\\usepackage{bm}
\\pagestyle{empty}
% \\setpaperwidth{1000mm}
EOB
end

#make_math_images(math_dir) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/review/makerhelper.rb', line 99

def make_math_images(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_dir = File.realpath(math_dir)
  Dir.mktmpdir do |tmpdir|
    FileUtils.cp([File.join(math_dir, '__IMGMATH_BODY__.tex'),
                  File.join(math_dir, '__IMGMATH_BODY__.map')],
                 tmpdir)
    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_dir)
      when 'dvipng'
        make_math_images_dvipng(tmpdir, tex_path, math_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__.log')],
                   math_dir)
      error "LaTeX math compile error. See #{math_dir}/__IMGMATH__.log for details."
    end
  end
  FileUtils.rm_f([File.join(math_dir, '__IMGMATH_BODY__.tex'),
                  File.join(math_dir, '__IMGMATH_BODY__.map')])
end

#make_math_images_dvipng(dir, tex_path, math_dir) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/review/makerhelper.rb', line 218

def make_math_images_dvipng(dir, tex_path, math_dir)
  Dir.chdir(dir) do
    dvi_path = '__IMGMATH__.dvi'
    out, status = Open3.capture2e(*[@config['texcommand'], @config['texoptions'].shellsplit, tex_path].flatten.compact)
    if !status.success? || !File.exist?(dvi_path)
      raise CompileError
    end

    File.open('__IMGMATH_BODY__.map') do |f|
      page = 0
      f.each_line do |key|
        page += 1
        key.chomp!
        args = @config['imgmath_options']['dvipng_cmd'].shellsplit
        args.map! do |m|
          m.sub('%i', dvi_path).
            sub('%o', File.join(math_dir, "_gen_#{key}.#{@config['imgmath_options']['format']}")).
            sub('%O', File.join(math_dir, "_gen_#{key}")).
            sub('%p', page.to_s)
        end
        out, status = Open3.capture2e(*args)
        unless status.success?
          warn "error in dvipng. Error log:\n#{out}"
          raise CompileError
        end
      end
    end
  end
end

#make_math_images_pdfcrop(dir, tex_path, math_dir) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/review/makerhelper.rb', line 143

def make_math_images_pdfcrop(dir, tex_path, math_dir)
  Dir.chdir(dir) do
    dvi_path = '__IMGMATH__.dvi'
    pdf_path = '__IMGMATH__.pdf'
    out, status = Open3.capture2e(*[@config['texcommand'], @config['texoptions'].shellsplit, tex_path].flatten.compact)
    if !status.success? || (!File.exist?(dvi_path) && !File.exist?(pdf_path))
      raise CompileError
    end
    if File.exist?(dvi_path)
      out, status = Open3.capture2e(*[@config['dvicommand'], @config['dvioptions'].shellsplit, dvi_path].flatten.compact)
      if !status.success? || !File.exist?(pdf_path)
        warn "error in #{@config['dvicommand']}. Error log:\n#{out}"
        raise CompileError
      end
    end

    args = @config['imgmath_options']['pdfcrop_cmd'].shellsplit
    args.map! do |m|
      m.sub('%i', pdf_path).
        sub('%o', '__IMGMATH__pdfcrop.pdf')
    end
    out, status = Open3.capture2e(*args)
    unless status.success?
      warn "error in pdfcrop. Error log:\n#{out}"
      raise CompileError
    end
    pdf_path = '__IMGMATH__pdfcrop.pdf'
    pdf_path2 = pdf_path

    File.open('__IMGMATH_BODY__.map') do |f|
      page = 0
      f.each_line do |key|
        page += 1
        key.chomp!
        if File.exist?(File.join(math_dir, "_gen_#{key}.#{@config['imgmath_options']['format']}"))
          # made already
          next
        end

        if @config['imgmath_options']['extract_singlepage']
          # if extract_singlepage = true, split each page
          args = @config['imgmath_options']['pdfextract_cmd'].shellsplit

          args.map! do |m|
            m.sub('%i', pdf_path).
              sub('%o', "__IMGMATH__pdfcrop_p#{page}.pdf").
              sub('%O', "__IMGMATH__pdfcrop_p#{page}").
              sub('%p', page.to_s)
          end
          out, status = Open3.capture2e(*args)
          unless status.success?
            warn "error in pdf extracting. Error log:\n#{out}"
            raise CompileError
          end

          pdf_path2 = "__IMGMATH__pdfcrop_p#{page}.pdf"
        end

        args = @config['imgmath_options']['pdfcrop_pixelize_cmd'].shellsplit
        args.map! do |m|
          m.sub('%i', pdf_path2).
            sub('%o', File.join(math_dir, "_gen_#{key}.#{@config['imgmath_options']['format']}")).
            sub('%O', File.join(math_dir, "_gen_#{key}")).
            sub('%p', page.to_s)
        end
        out, status = Open3.capture2e(*args)
        unless status.success?
          warn "error in pdf pixelizing. Error log:\n#{out}"
          raise CompileError
        end
      end
    end
  end
end