Class: Jeanny::JSCode

Inherits:
Code
  • Object
show all
Defined in:
lib/jeanny/engine.rb

Instance Attribute Summary

Attributes inherited from Code

#code

Instance Method Summary collapse

Methods inherited from Code

#initialize

Constructor Details

This class inherits a constructor from Jeanny::Code

Instance Method Details

#each_stringObject



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
# File 'lib/jeanny/engine.rb', line 264

def each_string
    
    @status = :in_code

    @char, @last_char, @start_char, @value = '', '', '', ''
    
    scanner = StringScanner.new @code.dup

    until scanner.eos?

        scanner.getch or next
        @char = scanner.matched

        case @status


            when :in_code
                # Если мы в коде, а текущий символ один из тех что нам надо
                # значит запоминаем, этот символ и переходим в режим "в строке"
                if %w(" ').include? @char
                    @start_char = @char
                    @status = :in_string
                end

                # Если мы в коде, текущий символ слеш, а следующий не звездочка,
                # значит мы в регулярном выражении
                if @char.eql? '/' and @last_char =~ /=|\(|:/ and not %w(* /).include? scanner.post_match[0, 1]
                    @start_char = @char
                    @status = :in_regexp
                end

                # Если мы в коде, текущий символ звездочка, а предыдущий — слеш,
                # значит это начала комментария. Перехоим в режим "в комментарии"
                @status = :in_full_comment if @char.eql? '*' and @last_char.eql? '/'

                @status = :in_line_comment if @char.eql? '/' and @last_char.eql? '/'

            when :in_string, :in_regexp
                # Если мы в строке (или регулярке), текущий символ такой же как и начальный,
                # а предыдущий не экранирует его, значит строка законченна.
                # Переходим в режим "в коде"
                if @char.eql? @start_char and not @last_char.eql? '\\'
                    if block_given?
                        yield "#{@start_char}#{@value}#{@start_char}"
                    end

                    @status, @start_char, @value = :in_code, '', ''
                # Иначе, прибавляем текущий символ к уже полученной строке
                else
                    @value = @value + @char
                end

            when :in_full_comment
                # Если мы в комментарии, текущий символ слеш, а предыдущий
                # звездочка, значит комментарий закончился, переходим в режим "в коде"
                @status = :in_code if @char.eql? '/' and @last_char.eql? '*'

            when :in_line_comment
                @status = :in_code if @char.eql? "\n"

        end

        @last_char = @char unless @char.nil? or @char =~ /\s/

    end
end

#replace(classes) ⇒ Object



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
# File 'lib/jeanny/engine.rb', line 230

def replace classes

    skips = Array.new

    @code.gsub! /(\s*\/\*!?\s*jeanny-ignore\s*\*\/\s*)\s*(.*?)\s*\1/ do |match|
        skips.push $2
        "_skip_#{skips.length}_"
    end

    strings = Array.new
    each_string do |string|
        after = string.dup
        classes.each do |full_class, short_class|
            while pos = after =~ /([^#]|^)#{full_class}(?=[^a-z0-9\-_\.\/]|$)/i
               if $1.nil? or $1.empty?
                    after[pos, full_class.length] = short_class
                else
                    after[pos + 1, full_class.length] = short_class
                end
            end
        end
        
        @code[@code.index(string), string.length] = after unless string.eql? after

    end

    skips.each_with_index do |code, index|
        @code.gsub! /_skip_#{index + 1}_/, code
    end
    
    @code
    
end