Class: LatexFlow::CLI::Build

Inherits:
Generic
  • Object
show all
Defined in:
lib/latex-flow/cli/build.rb

Instance Method Summary collapse

Methods inherited from Generic

#initialize

Constructor Details

This class inherits a constructor from LatexFlow::CLI::Generic

Instance Method Details

#auto_buildObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/latex-flow/cli/build.rb', line 14

def auto_build
  require 'watchr'
  require 'digest/md5'

  @executor = Class.new {
    def submit(path, &task)
      current_hash = Digest::MD5.file(path).to_s
      @hashes ||= {}
      if @hashes[path] != current_hash
        task.call()
        @hashes[path] = current_hash
      end
    end
  }.new

  script = Watchr::Script.new
  script.watch('.*\.tex|.*\.bib') do |m|
    @executor.submit(m[0]) do
      task
    end

  end
  Watchr::Controller.new(script, Watchr.handler.new).run
end

#bibtexObject



109
110
111
112
113
114
115
# File 'lib/latex-flow/cli/build.rb', line 109

def bibtex
  require 'latex-flow/tools/bibtex'
  bibtex = Tools::BibTeX.new("#{ENV.fetch('BIBTEX', 'bibtex')} #{@options[:target]}")
  bibtex.run
  $stderr.puts bibtex.messages(color:@options[:color]) if @options[:verbose]
  bibtex.success?
end

#buildObject



10
11
12
# File 'lib/latex-flow/cli/build.rb', line 10

def build
  task
end

#dvipdfmObject



117
118
119
120
121
122
123
# File 'lib/latex-flow/cli/build.rb', line 117

def dvipdfm
  require 'latex-flow/tools/dvipdfm'
  dvipdfm = Tools::Dvipdfm.new("#{ENV.fetch('DVIPDFM', 'dvipdfm')} #{@options[:target]}")
  dvipdfm.run
  $stderr.puts dvipdfm.messages(color:@options[:color]) if @options[:verbose]
  dvipdfm.success?
end

#latexObject



101
102
103
104
105
106
107
# File 'lib/latex-flow/cli/build.rb', line 101

def latex
  require 'latex-flow/tools/latex'
  latex = Tools::LaTeX.new("#{ENV.fetch('LATEX', 'latex -halt-on-error')} #{@options[:target]}")
  latex.run
  $stderr.puts latex.messages(color:@options[:color]) if @options[:verbose]
  latex.success?
end

#runObject



6
7
8
# File 'lib/latex-flow/cli/build.rb', line 6

def run
  @options[:auto] ? auto_build : build
end

#taskObject



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
69
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
# File 'lib/latex-flow/cli/build.rb', line 39

def task
  (@options['latex-max-count']).times do
    # latex
    require 'latex-flow/tools/latex'
    latex = Tools::LaTeX.new("#{ENV.fetch('LATEX', 'latex -halt-on-error')} #{@options[:target]}")
    latex.run
    $stderr.puts latex.messages(color:@options[:color]) if @options[:verbose]
    return if !latex.success?
    # bibtex
    if use_bibtex? && latex.need_bibtex?
      require 'latex-flow/tools/bibtex'
      bibtex = Tools::BibTeX.new("#{ENV.fetch('BIBTEX', 'bibtex')} #{@options[:target]}")
      bibtex.run
      $stderr.puts bibtex.messages(color:@options[:color]) if @options[:verbose]
      return if !bibtex.success?
      next
    else
      break if !latex.next?
    end
  end

  # dvipdfm
  require 'latex-flow/tools/dvipdfm'
  dvipdfm = Tools::Dvipdfm.new("#{ENV.fetch('DVIPDFM', 'dvipdfm')} #{@options[:target]}")
  dvipdfm.run
  $stderr.puts dvipdfm.messages(color:@options[:color]) if @options[:verbose]
  return if !dvipdfm.success?


  # # latex
  # require 'latex-flow/tools/latex'
  # latex = Tools::LaTeX.new("#{ENV.fetch('LATEX', 'latex -halt-on-error')} #{@options[:target]}")
  # latex.run
  # $stderr.puts latex.messages(color:@options[:color]) if @options[:verbose]
  # return if !latex.success?


  # # bibtex
  # if use_bibtex?
  #   require 'latex-flow/tools/bibtex'
  #   bibtex = Tools::BibTeX.new("#{ENV.fetch('BIBTEX', 'bibtex')} #{@options[:target]}")
  #   bibtex.run
  #   $stderr.puts bibtex.messages(color:@options[:color]) if @options[:verbose]
  #   return if !bibtex.success?
  # end

  # # latex(loop)
  # if latex.next?
  #   (@options['latex-max-count']-2).times do
  #     require 'latex-flow/tools/latex'
  #     latex = Tools::LaTeX.new("#{ENV.fetch('LATEX', 'latex -halt-on-error')} #{@options[:target]}")
  #     latex.run
  #     $stderr.puts latex.messages(color:@options[:color]) if @options[:verbose]
  #     return if !latex.success?
  #     latex.next? ? next : break
  #   end
  # end

  # dvipdfm

end

#use_bibtex?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/latex-flow/cli/build.rb', line 125

def use_bibtex?
  @use_bibtex ||= !File.read(@options[:target] + '.tex').scan(/(^[^%\n]*?\\bibliography\{.+?\}.*$)/).flatten.empty?
end