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



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/nimbu/command/server.rb', line 289

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



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

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



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/nimbu/command/server.rb', line 316

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



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

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

.plainError(message, nameoffile) ⇒ Object



342
343
344
345
346
347
348
# File 'lib/nimbu/command/server.rb', line 342

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.



309
310
311
312
313
314
# File 'lib/nimbu/command/server.rb', line 309

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



283
284
285
286
287
# File 'lib/nimbu/command/server.rb', line 283

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

.sassErrorLine(message) ⇒ Object



350
351
352
# File 'lib/nimbu/command/server.rb', line 350

def sassErrorLine message
  return message
end

.watchObject



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/nimbu/command/server.rb', line 250

def watch
  refresh
  puts ">>> Haml is polling for changes. Press Ctrl-C to Stop."
  listener = Listen.to('haml')
  listener.relative_paths(true)
  listener.filter(/\.haml$/)
  modifier = lambda do |modified, added, removed|
    puts modified.inspect
    modified.each do |relative|
      puts ">>> Change detected to: #{relative}"
      HamlWatcher.compile(relative)
    end if modified

    added.each do |relative|
      puts ">>> File created: #{relative}"
      HamlWatcher.compile(relative)
    end if added

    removed.each do |relative|
      puts ">>> File deleted: #{relative}"
      HamlWatcher.remove(relative)
    end if removed
  end
  listener.change(&modifier)
  listener.start
  listener
end