Class: TReX

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

Overview

TODO track changed files and only recompile then…

Constant Summary collapse

VERSION =
'1.0.8'
VALID_ACTIONS =

mapping of input command shortcuts and method names

{
    "view" =>    :view,
    "compile" => :compile,
    "tex" =>     :compile,
    "count" =>   :count,
    "clean" =>   :clean,
    "bibtex" =>  :bibtex,
    "bib" =>     :bibtex,
    "v" =>       :view,
    "spell" =>   :spell_check,
    "check" =>   :spell_check,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments, stdin) ⇒ TReX




89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/trex.rb', line 89

def initialize(arguments, stdin)
    @arguments           = arguments
    @stdin               = stdin
    @action              = :compile
    @opts                = nil
    # Set defaults
    @options             = OpenStruct.new
    @options.verbose     = false
    @options.quiet       = false
    @options.interaction = 'nonstopmode'
    @options.garbage     = GARBAGE
    @options.figures     = FIGURES
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



72
73
74
# File 'lib/trex.rb', line 72

def options
  @options
end

Instance Method Details

#bibtexObject




193
194
195
196
197
198
199
200
201
# File 'lib/trex.rb', line 193

def bibtex
    if not File.exist?(@options.sourceBase + '.aux')
        return self.compile
    end
    warning = BibtexWarning.new()
    warning.handle(`bibtex #{@options.sourceBase + '.aux'}`)
    puts warning.to_s unless warning.empty?
    return warning.empty?
end

#check_sourceObject




112
113
114
115
116
117
118
119
120
121
122
# File 'lib/trex.rb', line 112

def check_source
    return if File.exist?(@options.source)
    if not @options.source.end_with? "tex"
        @options.source += '.' if not @options.source.end_with? '.'
        @options.source += 'tex'
        self.source! @options.source
    end
    return if File.exist?(@options.source)
    print "File doesn't exist: ".red.bold, @options.source, "\n"
    exit 1
end

#cleanObject




147
148
149
150
151
# File 'lib/trex.rb', line 147

def clean
    @options.garbage.split.each { |f|
        `rm -f #{@options.sourceBase}.#{f}`
    }
end

#compileObject




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
# File 'lib/trex.rb', line 154

def compile
    self.check_commands
    self.check_source
    baseCommand = self.create_compile_baseCommand
    # 1/4 first run interactive... ignoring cite/ref errors
    formatter = TexOutputFormatter.new(`#{baseCommand}`,
                                       @options.verbose, @options.quiet,
                                       @options.source)
    if not formatter.hasCitationWarnings? \
       and not formatter.hasReferenceWarnings?
        formatter.print unless @options.quiet
        return true
    end
    # 2/4 bibtex on citation warnings
    if formatter.hasCitationWarnings? and not self.bibtex \
       and not @options.quiet
        # obviously something went wrong in either compiling bibtex or the
        # latex file itself. Print everything except for reference and
        # citation warnings
        errorGroups = formatter.errorGroups
        errorGroups.delete formatter.referenceWarnings
        errorGroups.delete formatter.citationWarnings
        formatter.print
    end
    # 3/4 second run
    if not system("#{baseCommand} > /dev/null 2>&1")
        formatter.print
        "\n! Could not create PDF !".error
        return false
    end
    # 4/4 looking for ref/cite errors
    formatter = TexOutputFormatter.new(`#{baseCommand}`,
                                       @options.verbose, @options.quiet,
                                       @options.source)
    formatter.print if not @options.quiet
    return true
end

#countObject




224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/trex.rb', line 224

def count
    @options.quiet = true
    self.compile
    puts 'Sources'.red + ":"
    self.print_wc_results `wc #{@options.source}`
    puts 'PDFtoText'.red + ":"
    #TODO create a tmp file instead of saving locally
    self.print_wc_results `pdftotext -enc UTF-8 -nopgbrk #{@options.output} - | wc`
    `rm -f #{@options.sourceBase}.txt`
    if self.has_command? 'texcount'
        puts 'texcount'.red + ":"
        puts `texcount -relaxed -sum #{@options.source}`
    end
end

#hackObject




139
140
141
142
143
144
# File 'lib/trex.rb', line 139

def hack
    editor = ENV['EDITOR'] || "vim"
    cmd = "sudo #{editor} #{__FILE__}"
    print cmd
    exec(cmd)
end


239
240
241
242
243
244
245
246
# File 'lib/trex.rb', line 239

def print_wc_results(result)
    result = result.split()[0..2]
    max    = result.map{|l| l.size}.max
    puts "    #{result[0].rjust(max).yellow} lines"
    puts "    #{result[1].rjust(max).yellow} words"
    puts "    #{result[2].rjust(max).yellow} characters"

end

#runObject




125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/trex.rb', line 125

def run
    begin
        if self.parsed_options? && self.arguments_valid?
            self.process_arguments
            self.process_command
        else
            self.output_usage
        end
    rescue Interrupt
        puts ''
    end
end

#source!(sourceFile) ⇒ Object




104
105
106
107
108
# File 'lib/trex.rb', line 104

def source!(sourceFile)
    @options.source      = sourceFile
    @options.sourceBase  = sourceFile.sub(/\.\w+$/,'')
    @options.output      = @options.sourceBase + '.pdf'
end

#spell_checkObject




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
# File 'lib/trex.rb', line 249

def spell_check
    require 'tempfile'
    self.check_source 

    detexed = Tempfile.new(@options.source)
    # strip away spell warnings occuring before the \begin{document}
    documentStartLine = self.detect_document_start_line
    # strip all the latex commands using detex
    `tail -n +#{documentStartLine} #{@options.source} | detex -e stcode,ccode,listing > #{detexed.path}`

    # run the spellchecker
    output = `atdtool #{detexed.path}`
    output = output.gsub(detexed.path, File.basename(@options.source))
    
    detexed.unlink
    output.lines do |line|
        if line.match(/^  /)
            print ' '
            print line.strip
        else
            /(?<file>[^:]+)\:(?<line>\d+)\:(?<col>\d+)\:(?<rest>[^"]+)(?<match> "[^"]+")(?<trailing>.*)/ =~ line
            print "\n"
            print file 
            print ':'
            print line.to_i + documentStartLine - 1
            print ':'
            print col
            print ':'
            print match
            print rest
            print trailing.strip
        end
    end
    puts ''
end

#viewObject




204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/trex.rb', line 204

def view
    self.compile
    open = nil
    if RUBY_PLATFORM.include? "linux"
        'gnome-open evince kpdf Xpdf'.split.each { |c|
            if self.has_command? c
                open = c
                break
            end
        }
    elsif RUBY_PLATFORM.include? "darwin"
        open = 'open'
    end
    if open.nil?
        "\n no command found to open #{ @options.output} !\n".error 1
    end
    system("#{open} #{@options.output}")
end