Class: Deplate::Regions::Img

Inherits:
Deplate::Region show all
Defined in:
lib/deplate/regions.rb,
lib/deplate/mod/pstoedit.rb

Constant Summary collapse

@@ImgAutoN =
0
@@ImgSuffix =
{}

Instance Method Summary collapse

Methods inherited from Deplate::Region

check_file, clean_strings, #deprecated_regnote, deprecated_regnote, #finish_accum, #format_compound, regions, register_as, set_line_cont, #setup

Methods inherited from Element

#join_lines, #join_lines_re_zh_cn

Instance Method Details

#finishObject



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/deplate/regions.rb', line 467

def finish
    finish_accum
    nt = (@args['cmd'] || (!@regNote.empty? && @regNote) || @deplate.variables['imgCmd'] || '').split(/ /)
    @prg = nt[0]
    @cmdLineArgs = nt[1..-1]
    unless @prg
        log("No programm name given!", :error)
    else
        begin
            @args["sfx"] ||= @deplate.variables["imgSfx"]
            imgClass = Deplate::Regions.const_get("Img_" + @prg)
            self.extend(imgClass)
            i = img
            i.args.update(@args)
            i.update_args
            return i
        rescue StandardError => e
            log(["Program call failed", e, e.backtrace[0..10]], :error)
        end
    end
    return nil
end

#imgObject



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/deplate/regions.rb', line 490

def img
    formatter_handler = "img_#@prg"
    if @deplate.formatter.respond_to?(formatter_handler)

        rv = Deplate::Element::PseudoElement.new(@deplate, @source, self) do |invoker|
            @deplate.formatter.send(formatter_handler, self, @accum)
        end

        class << rv
            def register_caption
                @container.register_caption
            end

            def set_caption(*args)
                @container.set_caption(*args)
            end
        end

    else

        id  = @args["id"]
        unless id
            @@ImgAutoN += 1
            id = @deplate.auxiliary_auto_filename('img', @@ImgAutoN, @accum)
        end
        sfx = @args["sfx"] || suffix
        src = "#{id}.#{@prg}"
        out = "#{id}.#{sfx}"
        pwd = Dir.pwd
        d   = @deplate.dest ? File.dirname(@deplate.dest) : "."
        rv  = nil
        @deplate.in_working_dir do
            accum = prepare(@accum, out, sfx)
            if Deplate::Region.check_file(self, out, src, accum)
                log(["Files exist! Using", out], :anyway)
            else
                unless @deplate.options.force
                    for f in [src]
                        if File.exist?(f)
                            raise "Please delete '#{f}' or change the id before proceeding"
                        end
                    end
                end
                Deplate::External.write_file(self, src) {|io| io.puts(accum.join("\n"))}
                run(src, out, sfx)
            end
            if block_given?
                rv = yield(id, out)
            else
                rv = Deplate::Command::IMG.new(@deplate, @source, out, @match, @args, "IMG")
                rv.finish
            end
        end
    end
    return rv

end

#img_re_pstoeditObject



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
# File 'lib/deplate/mod/pstoedit.rb', line 61

def img
    formatter_handler = "img_#@prg"
    if @deplate.formatter.respond_to?(formatter_handler)

        rv = Deplate::Element::PseudoElement.new(@deplate, @source, self) do |invoker|
            @deplate.formatter.send(formatter_handler, self, @accum)
        end

        class << rv
            def register_caption
                @container.register_caption
            end

            def set_caption(*args)
                @container.set_caption(*args)
            end
        end

    else

        id  = @args["id"]
        unless id
            @@ImgAutoN += 1
            id = @deplate.auxiliary_auto_filename('img', @@ImgAutoN, @accum)
        end
        sfx = @args["sfx"] || suffix
        src = "#{id}.#{@prg}"
        out = "#{id}.#{sfx}"
        pwd = Dir.pwd
        d   = @deplate.dest ? File.dirname(@deplate.dest) : "."
        rv  = nil
        @deplate.in_working_dir do
            accum = prepare(@accum, out, sfx)
            if Deplate::Region.check_file(self, out, src, accum)
                log(["Files exist! Using", out], :anyway)
            else
                unless @deplate.options.force
                    for f in [src]
                        if File.exist?(f)
                            raise "Please delete '#{f}' or change the id before proceeding"
                        end
                    end
                end
                Deplate::External.write_file(self, src) {|io| io.puts(accum.join("\n"))}
                run(src, out, sfx)
            end
            if block_given?
                rv = yield(id, out)
            else
                rv = Deplate::Command::IMG.new(@deplate, @source, out, @match, @args, "IMG")
                rv.finish
            end
        end
    end
    return rv

end

#suffixObject



548
549
550
551
552
553
554
555
# File 'lib/deplate/regions.rb', line 548

def suffix
    block = @@ImgSuffix[@deplate.formatter.class]
    if block
        return block.call(self)
    else
        return "png"
    end
end