Class: Smurf::Javascript

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

Constant Summary collapse

EOF =
-1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Javascript

Returns a new instance of Javascript.



48
49
50
51
52
# File 'lib/smurf/javascript.rb', line 48

def initialize(content)
  @input = StringIO.new(content)
  @output = StringIO.new
  jsmin
end

Class Method Details

.minifies?(paths) ⇒ Boolean

Returns:

  • (Boolean)


41
# File 'lib/smurf/javascript.rb', line 41

def self.minifies?(paths) !paths.grep(%r[\.js(\?\d+)?$]).empty?; end

Instance Method Details

#action(a) ⇒ Object

action – do something! What you do is determined by the argument: 1 Output A. Copy B to A. Get the next B. 2 Copy B to A. Get the next B. (Delete A). 3 Get the next B. (Delete B). action treats a string as a single character. Wow! action recognizes a regular expression if it is preceded by ( or , or =.



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
# File 'lib/smurf/javascript.rb', line 123

def action(a)
  if(a==1)
    @output.write @theA
  end
  if(a==1 || a==2)
    @theA = @theB
    if (@theA == "\'" || @theA == "\"")
      while (true)
        @output.write @theA
        @theA = get
        break if (@theA == @theB)

        # raise "Unterminated string literal"
        debugger if (@theA <= "\n")
        if (@theA == "\\")
          # allow multi-line strings if each line is terminated by \\n (or \\r\n)
          if ["\r", "\n"].include? peek
            get # throw away the line ending
            get if ["\r", "\n"].include? peek
          else
            @output.write @theA
          end
          @theA = get
        end
      end
    end
  end
  if(a==1 || a==2 || a==3)
    @theB = mynext
    if (@theB == "/" && (@theA == "(" || @theA == "," || @theA == "=" ||
                         @theA == ":" || @theA == "[" || @theA == "!" ||
                         @theA == "&" || @theA == "|" || @theA == "?" ||
                         @theA == "{" || @theA == "}" || @theA == ";" ||
                         @theA == "\n"))

      @output.write(@theA) unless peek(2) == '@'
      @output.write @theB
      while (true)
        @theA = get
        if (@theA == "/")
          break
        elsif (@theA == "\\")
          @output.write @theA
          @theA = get
        elsif (@theA <= "\n")
          raise "Unterminated RegExp Literal"
        end
        @output.write @theA
      end
      @theB = mynext
    end
  end
end

#getObject

get – return the next character from stdin. Watch out for lookahead. If the character is a control character, translate it to a space or linefeed.



70
71
72
73
74
75
76
77
# File 'lib/smurf/javascript.rb', line 70

def get()
  c = @input.getc
  return EOF if(!c)
  c = c.chr
  return c if (c >= " " || c == "\n" || c.unpack("c") == EOF)
  return "\n" if (c == "\r")
  return " "
end

#isAlphanum(c) ⇒ Object

isAlphanum – return true if the character is a letter, digit, underscore, # dollar sign, or non-ASCII character



61
62
63
64
65
66
# File 'lib/smurf/javascript.rb', line 61

def isAlphanum(c)
  return false if !c || c == EOF
  return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
    (c >= 'A' && c <= 'Z') || c == '_' || c == '$' ||
      c == '\\' || Array(c[0].bytes).first > 126)
end

#jsminObject

jsmin – Copy the input to the output, deleting the characters which are insignificant to JavaScript. Comments will be removed. Tabs will be replaced with spaces. Carriage returns will be replaced with linefeeds. Most spaces and linefeeds will be removed.



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
# File 'lib/smurf/javascript.rb', line 181

def jsmin
  @theA = "\n"
  action(3)
  while (@theA != EOF)
    case @theA
    when " "
      if (isAlphanum(@theB))
        action(1)
      else
        action(2)
      end
    when "\n"
      case (@theB)
      when "{","[","(","+","-"
        action(1)
      when " "
        action(3)
      else
        if (isAlphanum(@theB))
          action(1)
        else
          action(2)
        end
      end
    else
      case (@theB)
      when " "
        if (isAlphanum(@theA))
          action(1)
        else
          action(3)
        end
      when "\n"
        case (@theA)
        when "}","]",")","+","-","\"","\\", "'", '"'
          action(1)
        else
          if (isAlphanum(@theA))
            action(1)
          else
            action(3)
          end
        end
      else
        action(1)
      end
    end
  end
end

#minifiedObject



54
55
56
57
# File 'lib/smurf/javascript.rb', line 54

def minified
  @output.rewind
  @output.read
end

#mynextObject

mynext – get the next character, excluding legitimate comments. peek() is used to see if a ‘/’ is followed by a ‘/’ or ‘*’.



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
# File 'lib/smurf/javascript.rb', line 89

def mynext()
  c = get
  if (c == "/")
    if(peek == "/")
      while(true)
        c = get
        if (c == -1 || c <= "\n")
        return c
        end
      end
    end
    if(peek == "*" && peek(2) != "@") # not conditional comments
      get
      while(true)
        case get
        when "*"
         if (peek == "/")
            get
            return " "
          end
        when EOF
          raise "Unterminated comment"
        end
      end
    end
  end
  return c
end

#peek(aheadCount = 1) ⇒ Object

Get the next character without getting it.



80
81
82
83
84
85
# File 'lib/smurf/javascript.rb', line 80

def peek(aheadCount=1)
  history = []
  aheadCount.times { history << @input.getc }
  history.reverse.each { |chr| @input.ungetc(chr) }
  return history.last.chr
end