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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/nimbu/command/server.rb', line 209

def compile(file)
  begin
    output_file_name = output_file(file)
    origin = File.open(File.join('haml', file)).read
    result = Haml::Engine.new(origin).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



254
255
256
257
258
259
260
# File 'lib/nimbu/command/server.rb', line 254

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/nimbu/command/server.rb', line 236

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



198
199
200
201
# File 'lib/nimbu/command/server.rb', line 198

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

.plainError(message, nameoffile) ⇒ Object



262
263
264
265
266
267
268
# File 'lib/nimbu/command/server.rb', line 262

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.



229
230
231
232
233
234
# File 'lib/nimbu/command/server.rb', line 229

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



203
204
205
206
207
# File 'lib/nimbu/command/server.rb', line 203

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

.sassErrorLine(message) ⇒ Object



270
271
272
# File 'lib/nimbu/command/server.rb', line 270

def sassErrorLine message
  return message
end

.watchObject



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/nimbu/command/server.rb', line 170

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