Class: HamlWatcher

Inherits:
Object
  • Object
show all
Extended by:
Term::ANSIColor
Defined in:
lib/nimbu/command/server.rb

Class Method Summary collapse

Class Method Details

.compile(file) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/nimbu/command/server.rb', line 283

def compile(file)
  begin
    output_file_name = output_file(file)
    origin = File.open(File.join('haml', file)).read
    result = Haml::Engine.new(origin, {escape_attrs: false}).render
    raise "Nothing rendered!" if result.empty?
    # Write rendered HTML to file
    color, action = File.exist?(output_file_name) ? [33, 'overwrite'] : [32, '   create']
    puts "\033[0;#{color}m#{action}\033[0m #{output_file_name}"
    FileUtils.mkdir_p(File.dirname(output_file_name))
    File.open(output_file_name,'w') {|f| f.write(result)}
  rescue Exception => e
    print red("#{plainError e, file}\n")
    output_file_name = output_file(file)
    result = goHere(e, file)
    File.open(output_file_name,'w') {|f| f.write(result)} if File::exists?(output_file_name)
  end
end

.get_line(exception) ⇒ Object



328
329
330
331
332
333
334
# File 'lib/nimbu/command/server.rb', line 328

def get_line(exception)
  # SyntaxErrors have weird line reporting
  # when there's trailing whitespace,
  # which there is for Haml documents.
  return (exception.message.scan(/:(\d+)/).first || ["??"]).first if exception.is_a?(::SyntaxError)
  (exception.backtrace[0].scan(/:(\d+)/).first || ["??"]).first
end

.goHere(message, nameoffile) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/nimbu/command/server.rb', line 310

def goHere(message, nameoffile)
  @messag = ""
  #@messag += "<html><head><title>ERROR IN CODE</title>"
  #CSS for error styling.
  @messag += "<style type = \"text/css\">"
  @messag +="body { background-color: #fff; margin: 40px; font-family: Lucida Grande, Verdana, Sans-serif; font-size: 12px; color: #000;}"
  @messag +="#content { border: #999 1px solid; background-color: #fff; padding: 20px 20px 12px 20px;}"
  @messag +="h1 { font-weight: normal; font-size: 14px; color: #990000; margin: 0 0 4px 0; }"
  @messag += "</style>"
  @messag += "<div id=\"content\">"
  @messag += "<h1>You have an Error in your HAML code </h1>"
  @messag += "<p>#{message} </p>"
  @messag += "<p>On Line : #{get_line message}.</p>"
  @messag += "<p>In file location: <strong>#{nameoffile}</strong></p>"
  @messag += "</div>"
  return @messag
end

.output_file(filename) ⇒ Object



272
273
274
275
# File 'lib/nimbu/command/server.rb', line 272

def output_file(filename)
  # './haml' retains the base directory structure
  filename.gsub(/\.html\.haml$/,'.html').gsub(/\.liquid\.haml$/,'.liquid')
end

.plainError(message, nameoffile) ⇒ Object



336
337
338
339
340
341
342
# File 'lib/nimbu/command/server.rb', line 336

def plainError(message, nameoffile)
  @plainMessage = ""
  @plainMessage += "Error: #{message} \n"
  @plainMessage += "Line number #{get_line message} "
  @plainMessage += "File error detected: #{nameoffile}"
  return @plainMessage
end

.refreshObject

Check that all haml templates have been rendered.



303
304
305
306
307
308
# File 'lib/nimbu/command/server.rb', line 303

def refresh
  Dir.glob('haml/**/*.haml').each do |file|
    file.gsub!(/^haml\//, '')
    compile(file) unless File.exist?(output_file(file))
  end
end

.remove(file) ⇒ Object



277
278
279
280
281
# File 'lib/nimbu/command/server.rb', line 277

def remove(file)
  output = output_file(file)
  File.delete output
  puts "\033[0;31m   remove\033[0m #{output}"
end

.sassErrorLine(message) ⇒ Object



344
345
346
# File 'lib/nimbu/command/server.rb', line 344

def sassErrorLine message
  return message
end

.watchObject



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/nimbu/command/server.rb', line 253

def watch
  refresh
  puts ">>> Haml is polling for changes. Press Ctrl-C to Stop."
  FSSM.monitor('haml', '**/*.haml') do
    update do |base, relative|
      puts ">>> Change detected to: #{relative}"
      HamlWatcher.compile(relative)
    end
    delete do |base, relative|
      puts ">>> File deleted: #{relative}"
      HamlWatcher.remove(relative)
    end
    create do |base, relative|
      puts ">>> File created: #{relative}"
      HamlWatcher.compile(relative)
    end
  end
end