Class: PHPRB::Compiler

Inherits:
Object show all
Defined in:
lib/web/phprb.rb

Defined Under Namespace

Classes: Buffer, PercentLine, Scanner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_mode = nil) ⇒ Compiler

Returns a new instance of Compiler.



226
227
228
229
230
231
# File 'lib/web/phprb.rb', line 226

def initialize(xml_mode=nil)
  @xml_mode = xml_mode
  @put_cmd = 'Web::print'
  @pre_cmd = []
  @post_cmd = []
end

Instance Attribute Details

#post_cmdObject

Returns the value of attribute post_cmd.



232
233
234
# File 'lib/web/phprb.rb', line 232

def post_cmd
  @post_cmd
end

#pre_cmdObject

Returns the value of attribute pre_cmd.



232
233
234
# File 'lib/web/phprb.rb', line 232

def pre_cmd
  @pre_cmd
end

#put_cmdObject

Returns the value of attribute put_cmd.



232
233
234
# File 'lib/web/phprb.rb', line 232

def put_cmd
  @put_cmd
end

Instance Method Details

#compile(s) ⇒ Object



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
# File 'lib/web/phprb.rb', line 129

def compile(s)
  out = Buffer.new(self)

  content = ''
  scanner = make_scanner(s)
  scanner.scan do |token|
    if scanner.stag.nil?
      case token
      when PercentLine
        out.push("#{@put_cmd} #{content.dump}") if content.size > 0
        content = ''
        out.push(token.to_s)
        out.cr
      when :cr
        out.cr
      when '<?', '<?=', '<!---', '<?ruby', '<?ruby='
        scanner.stag = token
        out.push("#{@put_cmd} #{content.dump}") if content.size > 0
        content = ''
      when "\n"
        content << "\n"
        out.push("#{@put_cmd} #{content.dump}")
        out.cr
        content = ''
      when '<??'
        content << '<?'
      else
        content << token
      end
    else
      # comments should include everything inside themselves
      if scanner.stag == '<!---' && token != '--->'
        content << token
      else
        case token
        when '--->'
          scanner.stag = nil
          content = ''
          # out.push("# #{content.dump}")
        when '?>'
          case scanner.stag
          when '<?', '<?ruby'
            if content[-1] == ?\n
              content.chop!
              out.push(content)
              out.cr
            else
              out.push(content)
            end
          when '<?=', '<?ruby='
            out.push("#{@put_cmd}((#{content}).to_s)")
            #when '<?--'
            # out.push("# #{content.dump}")
          end
          scanner.stag = nil
          content = ''
        when '??>'
          content << '?>'
        else
          content << token
        end
      end
    end
  end
  out.push("#{@put_cmd} #{content.dump}") if content.size > 0
  out.close
  out.script
end

#make_scanner(src) ⇒ Object



222
223
224
# File 'lib/web/phprb.rb', line 222

def make_scanner(src)
  Scanner.new(src, @xml_mode)
end

#prepare_trim_mode(mode) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/web/phprb.rb', line 198

def prepare_trim_mode(mode)
  case mode
  when 1
    return [false, '>']
  when 2
    return [false, '<>']
  when 0
    return [false, nil]
  when String
    perc = mode.include?('?')
    if mode.include?('-')
      return [perc, '-']
    elsif mode.include?('<>')
      return [perc, '<>']
    elsif mode.include?('>')
      return [perc, '>']
    else
      [perc, nil]
    end
  else
    return [false, nil]
  end
end