summaryrefslogtreecommitdiff
path: root/support/rake4latex/call_rake4latex.rb
blob: a712f0a5817f1536c8c158fa3e5905a8d1dbfd16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
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
142
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
=begin rdoc
call_rake4latex.rb offers an easy and fast interface
to rake4latex.

Just call it from the shell:
  call_rake4latex.rb my_file

my_file.tex will be compiled to my_file.pdf.

For details, please use the help:
  call_rake4latex.rb -h

For complex documents you may want define your own rakefile
and use rake.

It is recommended to copy this file to a location in your 
search path for executable files.
=end

require 'rake4latex'
#~ Dir.chdir('lib'){ require 'rake4latex' }
require 'optparse'

#Anlegen des Parsers
opts = OptionParser.new()
opts.banner = "Usage: call_rake4latex.rb [options] filename"	#Usage-zeile
opts.separator "Call LaTeX as often as needed."
opts.separator ""
opts.separator "Examples:"
opts.separator "call_rake4latex testdocument.pdf"
opts.separator "  Create testdocument.pdf via pdflatex"
opts.separator "  Starts only if testdocument.tex is newer then testdocument.pdf."
opts.separator ""
opts.separator "call_rake4latex -e testdocument"
opts.separator "  Touch 'testdocument.tex' (set the modification date)"
opts.separator "  Create testdocument.pdf via pdflatex"
opts.separator ""
opts.separator "Specific options:"

#
#List of task to be done after the TeX-call.
#
TASK_BEFORE_TeX = []  #only task where we need the filenem
TASK_AFTER_TeX = []
PARSE_MESSAGES = true


#
#fixme: format
=begin
Shortcuts
=end
opts.on('-e', "--enforce", "Enforce an initial TeX-run") { |v|
  TASK_BEFORE_TeX << :enforce
}

opts.on('-f', "--format FORMAT", "Set the format for TeX-run (pdflatex, xelatex, lualatex)") { |v|
  Rake4LaTeX::DEFAULT_SETTINGS[:program] = v.to_sym
}

#
#fixme:
#Warum gibt es immer einen TeX-Lauf mit dieser Option?
#
opts.on('--dep', "--dependecies", "Build dependecies (rakefile recommended)") { |v|
  TASK_BEFORE_TeX << :dependecies
}
opts.on("-i", "--ignore", "--ignore-error", "Allow LaTeX errors") { |v|
  puts "Allow LaTeX errors"  if PARSE_MESSAGES
    Rake4LaTeX::DEFAULT_SETTINGS[:texerrors_allowed] = true
}
opts.on("-s", "--statistic", "Show the statistic after TeX-run") { |v|
  puts "Show the statistic after TeX-run"  if PARSE_MESSAGES
  TASK_BEFORE_TeX  << :statistic  #Set filename
  TASK_AFTER_TeX  << :statistic
}
opts.on("-o", "--overview", "Create a log-overview file") { |v|
  puts "Create a log-overview file after TeX-run"  if PARSE_MESSAGES
  TASK_BEFORE_TeX  << :statistic  #Set filename
  TASK_AFTER_TeX  << :log_overview_file
}
opts.on("-c", "--clean", "Clean after TeX-run (delete auxiliary files)") { |v|
  puts "Clean after TeX-run"  if PARSE_MESSAGES
  TASK_AFTER_TeX  << :clean
}

=begin
Main options
=end
# Soll der Parameter Optional sein, kann [] verwendet werden.
opts.on("--touch FILE", "Touch FILE") { |v|
  puts "Touch #{v.inspect}" if PARSE_MESSAGES
  task :touch => v
  task :default => :touch
}


#
opts.on("--task TASK", "Define a rake-task") { |v|
  puts "Set task #{v.inspect}" if PARSE_MESSAGES
  task :default => v
}

#
opts.on("--task_after TASK", "Define a rake-task after the TeX-run") { |v|
  puts "Set task #{v.inspect} after LaTeX" if PARSE_MESSAGES
  TASK_AFTER_TeX  << v
}

=begin
LaTeXRunner-options
=end
#
opts.on("--maxrun MAXRUN", "Maximum MAXRUN TeX calls") { |v|
  puts "Set maximum TeX-Runs #{v.inspect}"  if PARSE_MESSAGES
    Rake4LaTeX::DEFAULT_SETTINGS[:maxruns] = v.to_i
}

opts.on("-l", "--loglevel LEVEL", "Log Level for the logger (1=debug, 2=info(default), 3=warn 4=error)") { |v|
  puts "Set loglevel to #{v.inspect}"  if PARSE_MESSAGES
  Rake4LaTeX::DEFAULT_SETTINGS[:loglevel] = v.to_i
}

opts.on("-v", "--version", "Version") { |v|
  puts "Rake4LaTeX-Caller Version #{Rake4LaTeX::VERSION}" 
}

task :default #empty dummy task

#Parsen der Parameter mit Exception bei ungültigen Parametern
begin
	opts.parse!
  ARGV.each{|arg|
    target = arg.ext('pdf')
    puts "Define target #{target}"
    TASK_BEFORE_TeX.each{|task|
    case task
      when :enforce
        puts "Force an initial TeX-run (touch #{target.ext('tex')})" if PARSE_MESSAGES
        task (:touch => target.ext('tex'))
        task (:default => :touch)
      when :dependecies
        puts "Build dependecies for #{target.ext('tex')}" if PARSE_MESSAGES
        TeXfile.new(target.ext('tex')).includes(:recursive, :uniq).each{|dep|
          puts "     => #{dep.inspect}" if PARSE_MESSAGES
          task (target => dep)
        }
      when :statistic
        task (:statistic => target.ext('tex'))
      when :log_overview_file
        task (:log_overview_file => target.ext('tex'))
      else
        raise ArgumentError, "Undefined option #{task.inspect}"
      end
    }
    task( :default => target )
    puts "call_rake4latex tries to generate #{target}"
  }  
rescue OptionParser::MissingArgument, OptionParser::InvalidOption => err
	puts "Error:\t#{err}"
	#Ausgabe der Schnittstelle
	puts opts
end

#
#Set tasks after the LaTeX-call
TASK_AFTER_TeX.each{|task|
  task (:default => task)
}

app = Rake.application
app[:default].invoke

if $0 == __FILE__
  if STDIN.tty?
    puts "Finished, please enter any key"
    STDOUT.flush
    STDIN.getc 
  end
end