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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/nimbu/command/server.rb', line 143

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



188
189
190
191
192
193
194
# File 'lib/nimbu/command/server.rb', line 188

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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/nimbu/command/server.rb', line 170

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



132
133
134
135
# File 'lib/nimbu/command/server.rb', line 132

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

.plainError(message, nameoffile) ⇒ Object



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

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.



163
164
165
166
167
168
# File 'lib/nimbu/command/server.rb', line 163

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



137
138
139
140
141
# File 'lib/nimbu/command/server.rb', line 137

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

.sassErrorLine(message) ⇒ Object



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

def sassErrorLine message
  return message
end

.watchObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/nimbu/command/server.rb', line 113

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
    create do |base, relative|
      puts ">>> File created: #{relative}"
      HamlWatcher.compile(relative)
    end
    delete do |base, relative|
      puts ">>> File deleted: #{relative}"
      HamlWatcher.remove(relative)
    end
  end
end