Class: SiSU_TeX::Source::LaTeXtoPdf

Inherits:
Object
  • Object
show all
Defined in:
lib/sisu/texpdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(md, env) ⇒ LaTeXtoPdf

Returns a new instance of LaTeXtoPdf.



183
184
185
186
# File 'lib/sisu/texpdf.rb', line 183

def initialize(md,env)
  @md,@env=md,env
  @f=SiSU_Env::FileOp.new(@md).base_filename
end

Instance Method Details

#latex_do(texfilename, papersize) ⇒ Object



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
217
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/sisu/texpdf.rb', line 187

def latex_do(texfilename,papersize)
  @texfilename=texfilename
  @@n_lpdf=@@n_lpdf+1
  tex_fn_base=@texfilename.gsub(/\.tex$/,'')
  tell=SiSU_Screen::Ansi.new(@md.opt.selections.str)
  if @md.opt.act[:pdf_p][:set]==:on
    if (@md.opt.act[:verbose][:set]==:on \
    || @md.opt.act[:verbose_plus][:set]==:on \
    || @md.opt.act[:maintenance][:set]==:on)
      SiSU_Screen::Ansi.new(
        @md.opt.act[:color_state][:set],
        "#{papersize} portrait ->"
      ).dark_grey_title_hi
    end
    cmd=SiSU_Env::SystemCall.new("#{tex_fn_base}.tex",'',@md.opt.selections.str)
    if @md.opt.act[:verbose][:set]==:on \
    || @md.opt.act[:verbose_plus][:set]==:on \
    || @md.opt.act[:maintenance][:set]==:on
      tell.grey_open
    end
    if "#{tex_fn_base}" =~/\w+/ \
    and "#{papersize}" =~/\w+/
      2.times { |i| cmd.latex2pdf(@md,papersize) } #comment out to skip processing of latex portrait
    end
    if @md.opt.act[:verbose][:set]==:on \
    || @md.opt.act[:verbose_plus][:set]==:on \
    || @md.opt.act[:maintenance][:set]==:on
      tell.p_off
    end
  end
  if @md.opt.act[:pdf_l][:set]==:on
    if (@md.opt.act[:verbose][:set]==:on \
    || @md.opt.act[:verbose_plus][:set]==:on \
    || @md.opt.act[:maintenance][:set]==:on)
      SiSU_Screen::Ansi.new(
        @md.opt.act[:color_state][:set],
        "#{papersize} landscape ->"
      ).dark_grey_title_hi
    end
    cmd=SiSU_Env::SystemCall.new("#{tex_fn_base}.landscape.tex",'',@md.opt.selections.str)
    if (@md.opt.act[:verbose][:set]==:on \
    || @md.opt.act[:verbose_plus][:set]==:on \
    || @md.opt.act[:maintenance][:set]==:on)
      tell.grey_open
    end
    if "#{tex_fn_base}" =~/\w+/ \
    and "#{papersize}" =~/\w+/
      2.times { |i| cmd.latex2pdf(@md,papersize) } #comment out to skip processing of latex landscape
    end
    if (@md.opt.act[:verbose][:set]==:on \
    || @md.opt.act[:verbose_plus][:set]==:on \
    || @md.opt.act[:maintenance][:set]==:on)
      tell.p_off
    end
  end
  pwd=Dir.pwd
  if @md.opt.act[:pdf_p][:set]==:on
    portrait_pdf="#{pwd}/#{tex_fn_base}.pdf"
  end
  if @md.opt.act[:pdf_l][:set]==:on
    landscape_pdf="#{pwd}/#{tex_fn_base}.landscape.pdf"
  end
  case papersize
  when /a4/     then pdf_p=@f.pdf_p_a4;     pdf_l=@f.pdf_l_a4
  when /a5/     then pdf_p=@f.pdf_p_a5;     pdf_l=@f.pdf_l_a5
  when /b5/     then pdf_p=@f.pdf_p_b5;     pdf_l=@f.pdf_l_b5
  when /letter/ then pdf_p=@f.pdf_p_letter; pdf_l=@f.pdf_l_letter
  when /legal/  then pdf_p=@f.pdf_p_legal;  pdf_l=@f.pdf_l_legal
  else               pdf_p=@f.pdf_p_a4;     pdf_l=@f.pdf_l_a4
  end
  FileUtils::mkdir_p(@md.file.output_path.pdf.dir) unless FileTest.directory?(@md.file.output_path.pdf.dir)
  cX=SiSU_Screen::Ansi.new(@md.opt.act[:color_state][:set]).cX
  if @md.opt.act[:pdf_p][:set]==:on
    if FileTest.file?(portrait_pdf)
      FileUtils::cp(portrait_pdf,"#{@md.file.output_path.pdf.dir}/#{pdf_p}")
      FileUtils::rm(portrait_pdf)
    else
      STDERR.puts "#{__FILE__}:#{__LINE__} NOT FOUND: #{portrait_pdf}" if @md.opt.act[:maintenance][:set]==:on
      errmsg="pdf file not generated #{portrait_pdf.gsub(/.+?([^\/]+?\.pdf)$/,'\1')} (check texlive dependencies)"
      if @md.opt.act[:no_stop][:set]==:on
        SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
          error("#{errmsg}, proceeding without pdf output (as requested)")
      else
        SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
          error("#{errmsg}, STOPPING")
        exit
      end
    end
  end
  if @md.opt.act[:pdf_l][:set]==:on
    if FileTest.file?(landscape_pdf)
      FileUtils::cp(landscape_pdf,"#{@md.file.output_path.pdf.dir}/#{pdf_l}")
      FileUtils::rm(landscape_pdf)
    else
      STDERR.puts "#{__FILE__}:#{__LINE__} NOT FOUND: #{landscape_pdf}" if @md.opt.act[:maintenance][:set]==:on
      errmsg="pdf file not generated #{landscape_pdf.gsub(/.+?([^\/]+?\.pdf)$/,'\1')} (check texlive dependencies)"
      if @md.opt.act[:no_stop][:set]==:on
        SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
          error("#{errmsg}, proceeding without pdf output (as requested)")
      else
        SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
          error("#{errmsg}, STOPPING")
        exit
      end
    end
  end
  if (@md.opt.act[:verbose][:set]==:on \
  || @md.opt.act[:verbose_plus][:set]==:on \
  || @md.opt.act[:maintenance][:set]==:on)
    SiSU_Screen::Ansi.new(
      @md.opt.act[:color_state][:set],
      @@n_lpdf,
      'processed (SiSU LaTeX to pdf - using pdfetex aka. pdftex or pdflatex)'
    ).generic_number
  end
end

#latexrun_selectiveObject



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/sisu/texpdf.rb', line 303

def latexrun_selective
  begin
    pwd=Dir.pwd
    Dir.chdir(pwd) #watch
    @tex_f_no=0
    if FileTest.file?(@env.source_file_with_path)
      @md.papersize_array.each do |ps|
        if @md.fns =~/\.(?:-|ssm\.)?sst$/
          case @md.fns
          when /\.(?:-|ssm\.)?sst$/
            if FileTest.directory?(@env.processing_path.tex)==true
              Dir.chdir(@env.processing_path.tex)
              texfile=@md.fns.gsub(/$/,".#{ps}.tex").
                gsub(/~/,'-')
              if @md.opt.act[:pdf_p][:set]==:on \
              or @md.opt.act[:pdf_l][:set]==:on
                latex_do(texfile,ps)
                if @md.opt.act[:pdf_p][:set]==:on
                  if File.exist?(texfile) \
                  and File.size(texfile) > 0
                    #@tex_f_no+=1
                  else
                    errmsg="zero file size #{@env.processing_path.tex}/#{texfile}"
                    if @md.opt.act[:no_stop][:set]==:on
                      SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
                        error("#{errmsg}, proceeding without pdf output (as requested)")
                    else
                      SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
                        error("#{errmsg}, STOPPING")
                      exit
                    end
                  end
                end
              end
            end
          end
        end
      end
      case @md.papersize_array[0] #default pdf
      when /a4/     then pdf_p=@f.pdf_p_a4;     pdf_l=@f.pdf_l_a4
      when /a5/     then pdf_p=@f.pdf_p_a5;     pdf_l=@f.pdf_l_a5
      when /b5/     then pdf_p=@f.pdf_p_b5;     pdf_l=@f.pdf_l_b5
      when /letter/ then pdf_p=@f.pdf_p_letter; pdf_l=@f.pdf_l_letter
      when /legal/  then pdf_p=@f.pdf_p_legal;  pdf_l=@f.pdf_l_legal
      else               pdf_p=@f.pdf_p_a4;     pdf_l=@f.pdf_l_a4
      end
      if @md.opt.act[:pdf_p][:set]==:on
        if FileTest.file?("#{@md.file.output_path.pdf.dir}/#{pdf_p}")
          mklnk=((@md.file.output_dir_structure.by_language_code?) \
          || (@md.file.output_dir_structure.by_filetype?)) \
          ? "#{@md.fnb}.portrait.pdf"
          : 'portrait.pdf'
          if FileTest.directory?(@md.file.output_path.pdf.dir)
            pwd=Dir.pwd
            Dir.chdir(@md.file.output_path.pdf.dir)
            FileUtils::rm_f(mklnk)
            FileUtils::ln_s(pdf_p, mklnk)
            Dir.chdir(pwd)
          end
        end
      end
      if @md.opt.act[:pdf_l][:set]==:on
        if FileTest.file?("#{@md.file.output_path.pdf.dir}/#{pdf_l}")
          mklnk=((@md.file.output_dir_structure.by_language_code?) \
          || (@md.file.output_dir_structure.by_filetype?)) \
          ? "#{@md.fnb}.landscape.pdf"
          : 'landscape.pdf'
          pwd_set=Dir.pwd
          Dir.chdir(@md.file.output_path.pdf.dir)
          FileUtils::rm_f(mklnk)
          FileUtils::ln_s(pdf_l, mklnk)
          Dir.chdir(pwd_set)
        end
      end
    else
      SiSU_Screen::Ansi.new(
        @md.opt.act[:color_state][:set],
        "*WARN* FILE NOT FOUND: << #{@md.fns} >> - requested latex system processing skipped"
      ).warn
    end
    lst=Dir["*.{aux,log,out}"]
    lst.each {|file| File.unlink(file)} if lst
  rescue
    SiSU_Errors::Rescued.new($!,$@,@md.opt.selections.str,@md.fns).location do
      __LINE__.to_s + ':' + __FILE__
    end
  end
end