darcsden :: lyro -> texzip -> blob

Small tool to bundle TeX projects

root / lib / texzip / Project.rb

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
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
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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
require 'pathname'
require 'set'
require 'fileutils'
require 'bibtex'
require 'highline'

class TeXzip::Error < Exception; end

class TeXzip::Project < HighLine

    PACKAGE_EXTENSIONS = %w(.sty .cls)
    IMAGE_EXTENSIONS = %w(.jpg .pdf .eps .pstex)
    TEXIMAGE_EXTENSIONS = %w(.pspdftex .pdf_t .pstex_t)

    class Quit < Exception; end

    attr_accessor :overwrite_all

    class FilePath
        def initialize(root_dir, file)
            @root_dir = Pathname.new(root_dir).expand_path
            @file = Pathname.new(file)
            @file = @root_dir.join(file).expand_path.relative_path_from(@root_dir)
        end

        def set_output_directory dir
            @out_dir = Pathname.new(dir).expand_path
        end

        def file
            @file
        end

        def output_path
            @out_dir.join(@file).expand_path
        end

        def path
            @root_dir.join(@file).expand_path
        end

        def extname
            @file.extname
        end

        def hash
            path.to_s.hash
        end

        def eql?(file_path)
            path.to_s.eql? file_path.path.to_s
        end
    end

    def initialize( master_file )
        super()
        @tex_master_file = Pathname.new(master_file).expand_path

        # All possible include-paths for TeX
        @tex_dirs = [@tex_master_file.dirname]
        @tex_dirs.concat((ENV["TEXINPUTS"] || "").split(':').map{|d| Pathname.new(d)})
        @tex_dirs.map! &:expand_path
        @tex_dirs.uniq!

        @overwrite_all = false

        parse_files
    end

    def parse_files
        # The hash of all files, including the whole text.
        @tex_files = {}
        @image_files = Set.new
        @bib_files = Set.new
        @cites = Set.new

        # Read all files recursively
        unparsed_files = [@tex_master_file]
        until unparsed_files.empty?
            fname = unparsed_files.pop
            file = find_file( fname )
            if file.nil? then
                if PACKAGE_EXTENSIONS.include? File.extname(fname)
                    next
                else
                    raise TeXzip::Error, "Can't find file: #{fname}"
                end
            end

            unless @tex_files.has_key? file
                included_files = parse_file file
                unparsed_files.concat included_files
            end
        end

        unless @bib_files.empty?
            @bib = BibTeX::Bibliography.new
            @bib_files.each do |bib_file|
                bib = BibTeX.open(bib_file.path)
                bib.replace_strings
                @bib.add(bib.data)
            end
        else
            @bib = nil
        end
    end

    # Returns the master-file's path.
    def master_file
        @tex_master_file
    end

    # Returns a list of included tex and sty files.
    # @return [Array<Pathname>] Included tex files.
    def tex_files
        @tex_files.keys
    end

    # Returns a list of included image-files.
    # @return [Array<Pathname>] Included image files.
    def image_files
        @image_files.to_a
    end

    # Returns a list of included BibTeX-files.
    # @return [Array<Pathname>] Included BibTeX files.
    def bib_files
        @bib_files.to_a
    end

    # Returns a list of citations.
    # @return [Array<String>] Citations.
    def cites
        @cites.to_a
    end

    # Returns the full path for a certain file.
    #
    # The file is searched in the current directory as well as all
    # directories given by the environment variable +TEXINPUTS+
    #
    # @param [String] file The (relative) path of the file.
    # @param [Array<String>] extensions The (possible) file extensions.
    # @return [Pathname,nil] The path to the file if exist.
    def find_file( file, extensions = [] )
        extensions.unshift "" # the empty extension
        extensions.uniq!

        @tex_dirs.each do |d|
            extensions.each do |ext|
                file_path = d.join(file + ext).expand_path
                if File.file? file_path
                    return FilePath.new(d, file + ext)
                end
            end
        end
        return nil
    end

    # Returns the full paths for all variants of a certain file.
    #
    # The files are searched in the current directory as well as all
    # directories given by the environment variable +TEXINPUTS+
    #
    # @param [String] file The base file-name.
    # @param [Array<String>] extensions The possible file-extensions.
    # @return [Array<Pathname>] All found files.
    def find_files( file, extensions )
        extensions.uniq!

        files = []

        extensions.each do |ext|
            @tex_dirs.each do |d|
                file_path = d.join(file + ext).expand_path
                if file_path.file?
                    files << FilePath.new(d, file + ext)
                    break
                end
            end
        end

        files
    end

    # Load and parse a single tex-file.
    #
    # The file is parsed for commands including images, BibTeX-files
    # and citations. The command along with the command's argument is
    # passed to the block. The block is assumed to return a list of
    # further tex-files to be parsed.
    #
    # @param [Pathname,String] file_name The name of the TeX-file to parse
    # @return [Array<String>] A list of included TeX-files.
    def parse_file file_name, &block
        text = nil
        File.open(file_name.path, "rb") do |f|
            text = f.read
        end
        @tex_files[file_name] = text

        block = method(:handle_command) unless block

        included_files = []
        text_without_comments = ""
        text.each_line do |line|
            comment_match = line.match /(?:\\)*%/
            if comment_match and (comment_match.end(0) - comment_match.begin(0)).odd?
                line = line[0...comment_match.end(0)]
            end
            text_without_comments.concat line
        end

        text_without_comments.scan(/\\(documentclass|usepackage|include|input|includegraphics|bibliography|cite)(?:\[[^\]]+\])?\{([^}]+)\}/) do |cmd, arg|
            new_files = block.call cmd, arg
            included_files.concat new_files if new_files
        end
        included_files
    end

    # Handles parsed commands.
    def handle_command command, argument
        case command
        when "includegraphics"
            add_image argument
        when  "bibliography"
            argument.split(',').uniq.each{|f| add_bib f.strip}
        when "usepackage"
            return [argument + ".sty"]
        when "documentclass"
            return [argument + ".cls"]
        when "cite"
            @cites.merge argument.split(',').map(&:strip)
        else
            ext = File.extname(argument)
            if TEXIMAGE_EXTENSIONS.include?(ext)
                file = find_file(argument)
                unless file
                    puts "WARNING: Can't find tex-image file #{argument}"
                    return nil
                end
                dir = File.dirname(argument)
                parse_file file do |command, arg|
                    if command == "includegraphics"
                        add_image File.join(dir, arg)
                    else
                        raise TeXzip::Error, "Unexpected command '\\#{command}' in tex-image file: \\#{argument}"
                    end
                    nil
                end
            elsif ext != ".tex"
                argument += ".tex"
            end
            return [argument]
        end
        return nil
    end

    # Adds an image to the list of included images.
    # @param [String] image_file_name The path of the image-file
    def add_image image_file_name
        ext = File.extname(image_file_name)
        if ext == ""
            image_files = find_files( image_file_name, IMAGE_EXTENSIONS )
        else
            image_files = [find_file( image_file_name )].compact
        end

        if image_files.empty?
            puts "WARNING: Can't find included image #{image_file_name}"
        else
            @image_files.merge image_files
        end
    end

    # Adds a BibTeX-file to the list of included BibTeX-files.
    # @param [String] image_file_name The path of the BibTeX-file.
    def add_bib bib_file_name
        bib_file = find_file( bib_file_name, [".bib"] )

        if bib_file.nil?
            puts "WARNING: Can't find included BibTeX file #{bib_file_name}"
        else
            @bib_files.add bib_file
        end
    end

    def modify_files outdir, image_dir, bibtex_file
        @output_directory = Pathname.new(outdir).expand_path
        @output_image_directory = @output_directory.join(Pathname.new(image_dir)).expand_path
        @output_bibtex = @output_directory.join(Pathname.new(bibtex_file)).expand_path
        @modified_files = {}

        @tex_files.each_key do |file|
            if TEXIMAGE_EXTENSIONS.include? file.extname
                file.set_output_directory @output_image_directory
            else
                file.set_output_directory @output_directory
            end
        end
        @tex_files.each_pair do |file, text|
            @modified_files[file] = update_file file, text
        end

        @image_files.each do |file|
            file.set_output_directory @output_image_directory
        end

        filter_bibtex
    end

    def update_file tex_file, text
        ext = tex_file.path.extname

        new_text = ""
        text.each_line do |line|
            comment_match = line.match /(?:\\)*%/
            if comment_match and (comment_match.end(0) - comment_match.begin(0)).odd?
                comment = line[comment_match.end(0) .. -1]
                line = line[0...comment_match.end(0)]
            else
                comment = ""
            end
            new_line = line.gsub(/(\\(include|input|includegraphics|bibliography)(?:\[[^\]]+\])?)\{([^}]+)\}/) { |m|
                start = $1
                cmd = $2
                file = $3
                if cmd == "includegraphics"
                    if TEXIMAGE_EXTENSIONS.include? ext
                        file = File.join(tex_file.file.dirname, file)
                    end
                    new_file = @output_image_directory.join(Pathname.new(file)).relative_path_from(@output_directory)
                elsif cmd == "bibliography"
                    new_file = @output_bibtex.basename.to_s.gsub(/\.bib$/, '')
                else
                    if TEXIMAGE_EXTENSIONS.include? File.extname(file)
                        new_file = @output_image_directory.join(Pathname.new(file)).relative_path_from(@output_directory)
                    else
                        new_file = @output_directory.join(Pathname.new(file)).relative_path_from(@output_directory)
                    end
                end
                "#{start}{#{new_file}}"
            }
            new_text.concat new_line
            new_text.concat comment
        end

        return new_text
    end

    def filter_bibtex
        if @bib
            cites = @cites.to_a.map{|c| c.to_s}
            seen_cites = cites.to_set
            until cites.empty?
                cite = cites.pop
                entries = @bib[cite]
                if entries.nil?
                    puts "WARNING: Can't find BibTeX-entry #{cite}"
                else
                    entries = [entries] unless entries.kind_of? Array
                    entries.each do |entry|
                        crossref = entry["crossref"]
                        if crossref
                            crossref.split(',').map(&:strip).each do |ref|
                                ref = ref.to_s
                                cites << ref if seen_cites.add? ref
                            end
                        end
                    end
                end
            end

            @bib = BibTeX::Bibliography.new.add(@bib.data.select{|entry| seen_cites.include? entry.key.to_s})
        end
    end

    def write_files( force = false )
        cwd = Pathname.getwd.expand_path
        write_data do |path, data|
            puts "Write file #{path.relative_path_from(cwd)}"
            FileUtils.mkdir_p path.dirname unless path.dirname.exist?
            if data.kind_of? Pathname
                FileUtils.copy data, path
            else
                File.open(path, "wb") do |f|
                    f.write data
                end
            end
        end
    end

    def write_archive( archive_file, force = false )
        require 'ffi-libarchive'

        archive_file = Pathname.new(archive_file).expand_path
        return unless ask_overwrite(archive_file)

        compression = case File.basename(archive_file.to_s)
                      when /\.tgz$/, /\.tar\.gz$/
                          :gzip
                      when /\.tbz2$/, /\.tar\.bz2$/
                          :bzip2
                      when /\.txz$/, /\.tar\.xz$/
                          :xz
                      when /\.tlzma$/, /\.tar\.lzma$/
                          :lzma
                      when /\.tZ$/, /\.tar\.Z$/
                          :Z
                      when /\.tar$/
                          :none
                      else
                          raise TeXzip::Error, "Can't derive archive-type from file name #{archive_file}"
                      end

        puts "Write archive #{archive_file.relative_path_from(Pathname.getwd)}"
        Archive.write_open_filename archive_file.to_s, compression, :tar do |ar|
            write_data true do |path, data|
                ar.add_entry do |e|
                    e.pathname = path.relative_path_from(@output_directory).to_s
                    if data.kind_of? Pathname
                        e.copy_stat(data.to_s)
                        File.open(data, "rb", &:read)
                    else
                        e.mode = 0644
                        e.atime = Time.now
                        e.mtime = Time.now
                        e.filetype = :file
                        data
                    end
                end
            end
        end
    end

    def write_data( force = false, &block )
        raise ArgumentError, "Block required" unless block

        overwrite_all = force
        commands = []

        @modified_files.each_pair do |file, text|
            if force or ask_overwrite(file.output_path)
                commands << [file.output_path, text]
            end
        end

        @image_files.each do |file|
            if force or ask_overwrite(file.output_path)
                commands << [file.output_path, file.path]
            end
        end

        if @bib and (force or ask_overwrite(@output_bibtex))
            commands << [@output_bibtex, @bib.to_s]
        end

        commands.each do |path, data|
            block.call path, data
        end
    end

    def ask_overwrite file
        if !@overwrite_all and File.exist?(file)
            ask("File #{file.relative_path_from(Pathname.getwd)} exists. Overwrite? [Ynaq]") do |q|
                q.character = true
                q.validate = /[ynaq\r ]/
                q.case = :down
                q.overwrite = false
                q.answer_type = lambda{ |c|
                    case c
                    when "q"; raise Quit
                    when "y"; true
                    when "n"; false
                    when "a"; @overwrite_all = true; true
                    end
                }
            end
        else
            true
        end
    end
end