Class: Deplate::Regions::Code

Inherits:
Native show all
Defined in:
lib/deplate/regions.rb,
lib/deplate/mod/code-gvim.rb,
lib/deplate/mod/code-gvim71.rb,
lib/deplate/mod/code-coderay.rb

Constant Summary collapse

@@code_idx =
0

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Native

#expand_template, #finish, #format_special

Methods inherited from Deplate::Region

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

Methods inherited from Element

#join_lines, #join_lines_re_zh_cn

Class Attribute Details

.general_highlighterObject (readonly)

Returns the value of attribute general_highlighter.



289
290
291
# File 'lib/deplate/regions.rb', line 289

def general_highlighter
  @general_highlighter
end

.optionsObject (readonly)

Returns the value of attribute options.



289
290
291
# File 'lib/deplate/regions.rb', line 289

def options
  @options
end

.syntax_highlighterObject (readonly)

Returns the value of attribute syntax_highlighter.



289
290
291
# File 'lib/deplate/regions.rb', line 289

def syntax_highlighter
  @syntax_highlighter
end

Class Method Details

.add_highlighter(syntax, format, agent) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/deplate/regions.rb', line 295

def add_highlighter(syntax, format, agent)
    case syntax
    when Array
        syntax.each do |syn|
            add_highlighter(syn, format, agent)
        end
    else
        if syntax
            @syntax_highlighter[syntax] ||= []
            @syntax_highlighter[syntax] << [format, agent]
        else
            @general_highlighter[format] = agent
        end
    end
end

.highlighter_option(agent, options) ⇒ Object



291
292
293
# File 'lib/deplate/regions.rb', line 291

def highlighter_option(agent, options)
    @options[agent].merge!(options)
end

Instance Method Details

#coderay_to_html(syntax, style, text) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/deplate/mod/code-coderay.rb', line 27

def coderay_to_html(syntax, style, text)
    unless @deplate.allow_external
        return
    end
    tokens = CodeRay.scan text, syntax.intern
    args = {}
    # args[:css] = :class
    if @args['lineNumers'] or @deplate.variables['codeLineNumbers']
        args[:line_numbers] = :table
    end
    begin
        rv = [tokens.div(args)]
        return rv
    rescue Exception => e
        log(['Error in module', 'code-coderay', e], :error)
    end
    return nil
end

#gvim_to_html(syntax, style, text) ⇒ Object



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
# File 'lib/deplate/mod/code-gvim.rb', line 31

def gvim_to_html(syntax, style, text)
    unless @deplate.allow_external
        return
    end
    gvim = %{#{Deplate::External.get_app('gvim')} -f +"syn on" +"let use_xhtml = 1" +"set ft=#{syntax}" +"colorscheme #{style || "default"}" +"run! syntax/2html.vim" +"wq" +"q" deplateGvim}
    # p "DBG #{gvim}"
    @deplate.in_working_dir(Dir.tmpdir) do
        FileUtils.rm("deplateGvim.html") if File.exist?("deplateGvim.html")
        Deplate::External.write_file(self, "deplateGvim") {|io| io.puts(text)}
        IO.popen(gvim) {|io| puts io.gets until io.eof }
        if File.exist?("deplateGvim.html")
            rv = [%{<div class="code">}]
            File.open("deplateGvim.html") do |io|
                until io.eof?
                    line = io.gets
                    line.chomp!
                    if line =~ /^<pre>$/ .. line =~ /^<\/pre\>$/
                        line.gsub!(/<font (\w+)="(.+?)">(.*?)<\/font>/,
                                   '<span style="\\1: \\2;">\\3</span>')
                        rv << line
                    end
                end
            end
            rv << %{</div>}
            # return rv.join("\n")
            return rv
        else
            log("Error when running gvim", :error)
        end
    end
    return nil
end

#processObject



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
# File 'lib/deplate/regions.rb', line 318

def process
    process_etc
    text = @elt.join("\n")
    if (s = @args['syntax'])
        @syntax = s
    end
    if (s = @args['style'])
        @style = s
    end
    if @style
        @style = Deplate::Core.clean_name(@style)
        @deplate.call_methods_matching(@deplate.formatter, /^hook_code_process_/, @style)
    end
    if @syntax
        e = nil
        fmt_name = @deplate.formatter.formatter_name
        @deplate.in_working_dir do
            id       = @args['id']
            if id
                id.gsub!(/\W/, '00')
                fcode    = @deplate.auxiliary_filename("code_#{id}")
                fout     = @deplate.auxiliary_filename("code_#{id}.#{fmt_name}")
            else
                log('No ID given', :anyway)
                @@code_idx += 1
                fcode = @deplate.auxiliary_filename(@deplate.auxiliary_auto_filename('code', @@code_idx, @elt, @syntax))
                fout  = @deplate.auxiliary_filename(@deplate.auxiliary_auto_filename('code', @@code_idx, @elt, fmt_name))
            end

            # highlighter_agent = nil
            # specialized_highlighter = self.class.syntax_highlighter[@syntax]
            # if specialized_highlighter
            #     specialized_highlighter.each do |fmt, agent|
            #         if @deplate.formatter.matches?(fmt)
            #             highlighter_agent = agent
            #             break
            #         end
            #     end
            # end
            # unless highlighter_agent
            #     highlighter_agent = self.class.general_highlighter[fmt_name]
            # end
            # unless highlighter_agent
            #     log(['No highlighter defined', fmt_name], :error)
            #     return self
            # end

            if Deplate::Region.check_file(self, fout, fcode, @elt)
                log(["Files exist! Using", fout], :anyway)
                File.open(fout) {|io| @elt = io.readlines.collect {|l| l.chomp}}
                return self
            else
                highlighter_agent = nil
                begin
                    specialized_highlighter = self.class.syntax_highlighter[@syntax]
                    if specialized_highlighter
                        specialized_highlighter.each do |fmt, agent|
                            if @deplate.formatter.matches?(fmt)
                                highlighter_agent = agent
                                e = send(agent, @syntax, @style, text)
                                # p "DBG Code 1 #{e.class}"
                                break if e
                            end
                        end
                    end
                    unless e
                        highlighter_agent = agent = self.class.general_highlighter[fmt_name]
                        e     = send(agent, @syntax, @style, text) if agent
                        # p "DBG Code 2 #{e.class}"
                    end
                rescue StandardError => err
                    log("#Code: #{err}", :error)
                end
                # p "DBG", highlighter_agent, self.class.options[highlighter_agent], self.class.options[highlighter_agent][:no_cache]
                if e and !self.class.options[highlighter_agent][:no_cache]
                    File.open(fcode, "w") {|io| io.puts(text)} if fcode
                    File.open(fout, "w")  {|io| io.puts(e)}  if fout
                end
            end
        end
        if e
            case e
            when Array
                @elt = e
            when String
                p "DBG e should be an Array. Please report."
                @elt = e.split("\n")
            else
                raise "DBG Unknown class for e: #{e.class}. Please report."
            end
            return self
        end
    else
        log("Code: No syntax defined!", :error)
    end
    return Deplate::Regions::Verbatim.new(@deplate, @source, @elt, @match, self).finish.process
end

#setup(region) ⇒ Object



312
313
314
315
316
# File 'lib/deplate/regions.rb', line 312

def setup(region)
    super
    @syntax = @deplate.variables['codeSyntax']
    @style  = @deplate.variables['codeStyle']
end