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



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/nimbu/command/server.rb', line 363

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



408
409
410
411
412
413
414
# File 'lib/nimbu/command/server.rb', line 408

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



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/nimbu/command/server.rb', line 390

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



352
353
354
355
# File 'lib/nimbu/command/server.rb', line 352

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

.plainError(message, nameoffile) ⇒ Object



416
417
418
419
420
421
422
# File 'lib/nimbu/command/server.rb', line 416

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.



383
384
385
386
387
388
# File 'lib/nimbu/command/server.rb', line 383

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



357
358
359
360
361
# File 'lib/nimbu/command/server.rb', line 357

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

.sassErrorLine(message) ⇒ Object



424
425
426
# File 'lib/nimbu/command/server.rb', line 424

def sassErrorLine message
  return message
end

.watchObject



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/nimbu/command/server.rb', line 327

def watch
  refresh
  current_dir = File.join(Nimbu.cli_options[:dir] || Dir.pwd, 'haml/')
  puts ">>> Haml is polling for changes. Press Ctrl-C to Stop."
  Filewatcher.new('haml/**/*.haml', every: true).watch do |filename, event|
    begin
      relative = filename.to_s.gsub(current_dir, '')

      case event.to_s
      when 'updated'
        puts ">>> Change detected to: #{relative}"
        HamlWatcher.compile(relative)
      when 'deleted'
        puts ">>> File deleted: #{relative}"
        HamlWatcher.remove(relative)
      when 'created'
        puts ">>> File created: #{relative}"
        HamlWatcher.compile(relative)
      end
    rescue => e
      puts "#{e.inspect}"
    end
  end
end