Class: Glue::Template::Parser

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/glue/template/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#run

Constructor Details

#initialize(root) ⇒ Parser

Returns a new instance of Parser.



11
12
13
14
15
16
# File 'lib/glue/template/parser.rb', line 11

def initialize(root)
  @root = root
  @config = Glue::Config.new        
  @sitemap = Glue::Template::Sitemap.new(@config)
  @feed = Glue::Template::Feed.new(@config)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/glue/template/parser.rb', line 7

def config
  @config
end

#feedObject

Returns the value of attribute feed.



6
7
8
# File 'lib/glue/template/parser.rb', line 6

def feed
  @feed
end

#rootObject

Returns the value of attribute root.



4
5
6
# File 'lib/glue/template/parser.rb', line 4

def root
  @root
end

#sitemapObject

Returns the value of attribute sitemap.



5
6
7
# File 'lib/glue/template/parser.rb', line 5

def sitemap
  @sitemap
end

Instance Method Details

#render_haml(path) ⇒ Object



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/glue/template/parser.rb', line 60

def render_haml(path)
  # extract metadata from file
  $meta = Glue::Template::Meta.new(path)
  
  # instantiate template helper
  helper = Glue::Template::Helper.new
  
  # instantiate and process haml for page
  haml = Haml::Engine.new($meta.content)
  content_for_layout = haml.to_html(helper, $meta.data)
  
  # instantiate haml for layout
  layout = File.read("#{root}/views/layouts/#{$meta.data[:layout]}.haml")
  haml = Haml::Engine.new(layout)
  
  # set haml variables
  vars = {}
  vars.merge!($meta.data)
  vars.merge!({
    :content_for_layout => content_for_layout,
    :generator => Glue::VERSION,
    :last_update => File.mtime(path).xmlschema
  })
  
  # retrieve relative file path and set target
  relative_path = path.sub("#{root}/views/", "").gsub(/\.haml$/, "")
  target = File.join(root, "public", relative_path + ".html")
  
  # sitemap data
  if config[:site][:friendly_url]
    path_for_sitemap = relative_path.gsub(/\/?index$/, "")
  else
    path_for_sitemap = relative_path + ".html"
  end
  
  # merge meta data with some template info
  mapping = $meta.data.merge({
    :path => path_for_sitemap,
    :updated_at => File.mtime(path)
  })
  
  unless $meta.data[:sitemap] == "no"
    self.sitemap << mapping
    self.sitemap << mapping.merge(:path => "/") if $meta.data[:index] == "yes"
  end
  
  unless $meta.data[:feed] == "no"
    self.feed << mapping
  end
  
  # create directories if necessary
  FileUtils.mkdir_p File.dirname(target)
  
  # retrieve parsed content
  full_page = haml.to_html(helper, vars)
  
  # save processed page
  File.open(target, "w+") do |f|
    f << full_page
  end
  
  # save as index page
  if $meta.data[:index] == "yes"
    index_path = File.join(root, "public", "index.html")
  
    File.open(index_path, "w+") do |f|
      f << full_page
    end
  end
end

#render_sass(path) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/glue/template/parser.rb', line 131

def render_sass(path)
  name = File.basename(path).gsub(/\.sass$/, "")
  sass = Sass::Engine.new(File.read(path))
  output = "#{root}/public/stylesheets/#{name}.css"
  
  FileUtils.mkdir_p File.dirname(output)
  
  File.open(output, "w+") do |file|
    file << sass.render
  end
end

#run!Object



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
# File 'lib/glue/template/parser.rb', line 18

def run!
  # load user helper file if exist
  if File.exists?("#{root}/config/helper.rb")
    require "#{root}/config/helper.rb"
    Glue::Template::Helper.__send__ :include, ::Helper
  end
  
  # process all stylesheets
  Dir["#{root}/views/stylesheets/**/*.sass"].each do |path|
    run "processing #{Colorize.yellow(path)}" do
      render_sass path
    end
  end
  
  # process all templates
  Dir["#{root}/views/**/*.haml"].each do |path|
    # layout files should be skipped
    next if path =~ /#{Regexp.escape("#{root}/views/layouts/")}/
    
    # partial files should be skipped
    next if File.basename(path) =~ /_.*?\.haml$/
    
    run "processing #{Colorize.yellow(path)}" do
      render_haml(path)
    end
  end
  
  # save sitemap
  if config[:sitemap][:enabled]
    run "generating #{Colorize.yellow("public/sitemap.xml")}..." do
      sitemap.save File.join(root, "public", "sitemap.xml")
    end
  end
  
  # save feed
  if config[:feed][:enabled]
    run "generating #{Colorize.yellow("public/feed.xml")}..." do
      feed.save File.join(root, "public", "feed.xml")
    end
  end
end