darthapo (owner)

Revisions

gist: 21381 Download_button fork
public
Public Clone URL: git://gist.github.com/21381.git
Embed All Files: show embed
hamlet.rake.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
desc "Compiles haml files (and hamlets) - v1.2"
task :hamlet do
  # by M@
  require 'rubygems'
  require 'haml'
  require 'fileutils'
 
  main_files = []
  @@src_path = File.expand_path( ENV.fetch('SRC', './') )
  @@out_path = File.expand_path( ENV.fetch('OUT', './') )
  @@cache = {}
 
  # For better 'code'/pre support...
  class Code
    include Haml::Helpers
    def initialize(text)
      @text = text
    end
    def render
      "<pre>#{preserve @text}</pre>"
    end
  end
 
  # Javascript pass-through filter...
  class Javascript
    def initialize(script)
      @script = script
    end
    def render
      "<script type=\"text/javascript\">\n#{@script.chomp}\n</script>"
    end
  end
  
  # Loads from file, or cache, and renders content as HAML
  def render_hamlet(name, html, opts={})
    @@src_path
    haml_file = File.join(@@src_path, "_#{name}.haml" )
    @@cache[name] = IO.readlines( haml_file ).join unless @@cache.has_key?( name )
    haml_source = @@cache[name]
    engine = Haml::Engine.new( haml_source,
                               :attr_wrapper=>'"',
                               :locals=>opts.merge({ :content=>html }),
                               :filename=>haml_file,
                               :filters=>{ 'javascript'=>Javascript,
                                           'code' =>Code} )
    engine.render
  end
  
  # Create hamlets... Heh
  Dir.glob( "#{@@src_path}/*.haml" ).each do |haml_file|
    if /_(\w*).haml/ =~ File.basename( haml_file )
      hamlet_name = $1.chomp
      eval <<-EOR
def #{hamlet_name}(opts={}, &block)
html = block_given? ? capture_haml(&block).to_s : ''
render_hamlet('#{hamlet_name}', html, opts)
end
EOR
    else
      main_files << haml_file
    end
  end
 
  # Process all the files that don't start with _
  main_files.each do |haml_file|
    begin
      source = IO.readlines(haml_file).join
      engine = Haml::Engine.new( source,
                                 :attr_wrapper=>'"',
                                 :filename=>haml_file,
                                 :filters=>{'javascript'=>Javascript,
                                            'code' =>Code} )
      html_file = File.expand_path( File.join(@@out_path, File.basename(haml_file).gsub('.haml', '.html')) )
      FileUtils.mkdir_p( @@out_path ) unless File.exists?( @@out_path )
      File.open( html_file, 'w') do |f|
        f.write engine.render
        puts " - Created #{html_file}"
      end
    rescue
      puts " x Couldn't create html from #{haml_file}"
      puts " #{$!}"
    end
  end
 
  # Ze End
  puts "Done."
end
 
task :default=>:hamlet