Module: Scms
- Includes:
- YAML
- Defined in:
- lib/scms.rb,
lib/scms/version.rb
Constant Summary collapse
- VERSION =
"3.0.4"
Class Method Summary collapse
- .build(website, config, mode) ⇒ Object
- .bundle(bundleConfig) ⇒ Object
- .copywebsite(website, pub) ⇒ Object
- .packr(asset) ⇒ Object
- .parsepages(bundles) ⇒ Object
- .parsetemplate(template, hash) ⇒ Object
- .sass(asset) ⇒ Object
- .sassall(crunchDir) ⇒ Object
Class Method Details
.build(website, config, mode) ⇒ Object
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 |
# File 'lib/scms.rb', line 16 def Scms.build(website, config, mode) @website = website @mode = mode #ScmsUtils.log("Mode: #{mode}") ScmsUtils.boldlog("Compiling #{@website}") Scms.sassall(File.join(@website)) yamlpath=File.join(config, "_config.yml") @settings = ScmsUtils.getsettings(yamlpath) if @settings #Bootstrap here if @settings["bootstrap"] != nil bootstrap = File.join(@website, @settings["bootstrap"]) #ScmsUtils.log("Bootstrap is: #{@settings["bootstrap"]}") if File.exists?(bootstrap) begin require_relative bootstrap rescue Exception=>e ScmsUtils.errLog(e.) ScmsUtils.log(e.backtrace.inspect) end else ScmsUtils.errLog("Bootstrap does not exist #{@settings["bootstrap"]}") ScmsUtils.writelog("::Bootstrap does not exist #{@settings["bootstrap"]}", @website) ScmsUtils.writelog("type NUL > #{bootstrap}", @website) end end bundles = Scms.bundle(@settings["bundles"]) Scms.parsepages(bundles) else ScmsUtils.errLog("Config is empty") end ScmsUtils.boldlog("Built website:") ScmsUtils.log(ScmsUtils.uriEncode("file:///#{@website}")) end |
.bundle(bundleConfig) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/scms.rb', line 294 def Scms.bundle(bundleConfig) bundles = Hash.new if bundleConfig != nil bundleConfig.each do |bundle| #ScmsUtils.log( "bundle (#{bundle.class}) = #{bundle}" ) bundle.each do |option| name = option[0] bundleName = File.join(option[1]["generate"]) bundles[name] = bundleName ScmsUtils.boldlog("Bundeling:") content = "" assetList = "" files = option[1]["files"] if files != nil files.each do |asset| assetList += " - #{asset}\n" assetdir = File.join(@website, asset) if File::exists?(assetdir) #try catch for permisions begin content = content + "\n" + File.read(assetdir) rescue Exception=>e ScmsUtils.errLog(e.) ScmsUtils.log(e.backtrace.inspect) end else ScmsUtils.errLog("Asset file doesn't exists: #{asset}") ScmsUtils.writelog("::Asset file doesn't exists: #{asset}", @website) ScmsUtils.writelog("type NUL > #{assetdir}", @website) end end ScmsUtils.log("#{assetList}") bundleFullPath = File.join(@website, bundleName) bundleDir = File.dirname(File.join(@website, bundleName)) begin Dir.mkdir(bundleDir, 755) unless File::directory?(bundleDir) File.open(bundleFullPath, 'w') {|f| f.write(content) } ScmsUtils.successLog("Created: #{bundleName}") rescue Exception=>e ScmsUtils.errLog("Error creating bundle: #{bundleName}") ScmsUtils.errLog(e.) ScmsUtils.log(e.backtrace.inspect) end if File.extname(bundleName) == ".js" puts "Minifing: #{bundleName}" Scms.packr(bundleFullPath) unless /(-min)|(\.min)/.match(bundleName) end else ScmsUtils.errLog("No files in bundle"); end end end end return bundles end |
.copywebsite(website, pub) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/scms.rb', line 402 def Scms.copywebsite(website, pub) if pub.to_s.strip.length != 0 FileUtils.mkdir pub unless Dir.exists? pub source = File.join(website) Dir.chdir(source) do Dir.glob("**/*").reject{|f| f['.svn']}.each do |oldfile| newfile = File.join(pub, oldfile.sub(source, '')) #puts newfile File.file?(oldfile) ? FileUtils.copy(oldfile, newfile) : FileUtils.mkdir(newfile) unless File.exist? newfile end end ScmsUtils.log("Output to: #{ScmsUtils.uriEncode("file:///#{pub}")}") end end |
.packr(asset) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/scms.rb', line 386 def Scms.packr(asset) if File.exists?(asset) begin code = File.read(asset) compressed = Packr.pack(code) File.open(asset, 'w') { |f| f.write(compressed) } ScmsUtils.log("Minified #{File.basename(asset)}") rescue Exception => e ScmsUtils.errLog("Error processing: #{asset}") ScmsUtils.errLog(e.) end else ScmsUtils.errLog("Can't minify asset because file doesn't exist: #{asset}") end end |
.parsepages(bundles) ⇒ Object
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 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 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 277 278 |
# File 'lib/scms.rb', line 55 def Scms.parsepages(bundles) # build views from templates @template = @settings["template"] if @settings["pages"] != nil # Build navigation = Array.new @settings["pages"].each do |pagedata| if pagedata != nil pagedata.each do || pagename = [0] pageconfig = [1] pageurl = "about:blank" pageurl = pageconfig["generate"] pageurl = pageconfig["url"] unless pageconfig["url"] == nil navtext = pageconfig["navigation"] = pageconfig["navigation_meta"] .push({"text" => navtext, "url" => pageurl, "pagename" => pagename, "meta" => }) unless navtext == nil end end end ScmsUtils.log("Compiling Pages:") @settings["pages"].each do |pagedata| if pagedata != nil pagedata.each do || pagename = [0] pageconfig = [1] pageurl = pageconfig["generate"] title = pagename title = pageconfig["title"] unless pageconfig["title"] == nil description = pageconfig["description"] keywords = pageconfig["keywords"] skin = @template skin = pageconfig["template"] unless pageconfig["template"] == nil resource = Hash.new if pageconfig["resource"] != nil resourcepath = File.join(@website, pageconfig["resource"]) if File.exists?(resourcepath) #ScmsUtils.log( "_Resource found: #{pageconfig["resource"]}_" ) begin resource = YAML.load_file(resourcepath) rescue Exception=>e ScmsUtils.errLog(e.) ScmsUtils.log(e.backtrace.inspect) end else ScmsUtils.errLog("Resource not found: #{pageconfig["resource"]}") ScmsUtils.writelog("::Resource not found #{pageconfig["resource"]}", @website) ScmsUtils.writelog("type NUL > #{resourcepath}", @website) end end hasHandler = false if pageconfig["handler"] != nil handlerpath = File.join(@website, pageconfig["handler"]) if File.exists?(handlerpath) #ScmsUtils.log( "Handler found: #{pageconfig["handler"]}" ) require handlerpath funDefined = defined? Handler.render if funDefined != nil hasHandler = true else ScmsUtils.errLog( "Handler doesnt have a render method" ) end else ScmsUtils.errLog("Handler not found: #{pageconfig["handler"]}") ScmsUtils.writelog("::Handler not found #{pageconfig["handler"]}", @website) ScmsUtils.writelog("type NUL > #{handlerpath}", @website) end end views = Hash.new if pageconfig["views"] != nil pageconfig["views"].each do |view| views[view[0]] = "" viewparts = view[1].split("?") # This allows views to have a query string in the config viewname = viewparts[0] viewqs = viewparts[1] #puts "viewname: #{viewname}, viewqs: #{viewqs}" viewpath = File.join(@website, viewname) if File.exists?(viewpath) begin htmlsnipet = File.read(viewpath) rescue Exception=>e ScmsUtils.errLog(e.) ScmsUtils.log(e.backtrace.inspect) end if htmlsnipet.empty? ScmsUtils.log("Empty view: #{view[1]}") end model = Hash.new model = Hash[viewqs.split('&').map{ |q| q.split('=') }] if viewqs != nil viewmodel = Hash.new viewmodel = { :name => pagename, :title => title, :url => pageurl, :data => pagedata, :rootdir => @website, :resource => resource, :view => { :name => viewname, :model => model } } if hasHandler ScmsUtils.log("Rendering with handler") begin viewSnippet = Handler.render(viewpath) rescue Exception=>e ScmsUtils.errLog(e.) ScmsUtils.log(e.backtrace.inspect) end else #todo: why not use htmlsnipet snnipetCode = File.read(viewpath) case File.extname(view[1]) when ".md" begin snnipetCode = snnipetCode.encode('UTF-8', :invalid => :replace, :undef => :replace) doc = Maruku.new(snnipetCode) viewSnippet = doc.to_html rescue Exception => e viewSnippet = snnipetCode ScmsUtils.errLog(e.) ScmsUtils.log(e.backtrace.inspect) end else viewSnippet = snnipetCode end end if @mode == "cms" views[view[0]] = "<div class='cms' data-view='#{view[1]}' data-page='#{pageurl}'>#{Scms.parsetemplate(viewSnippet, viewmodel)}</div>" else views[view[0]] = Scms.parsetemplate(viewSnippet, viewmodel) end else ScmsUtils.errLog("View not found: #{view[0]} - #{view[1]} [#{viewpath}]") ScmsUtils.writelog("::View not found: #{view[0]} - #{view[1]} [#{viewpath}]", @website) ScmsUtils.writelog("type NUL > #{viewpath}", @website) end #ScmsUtils.log( "view = #{view[0]} - #{view[1]}" ) end end monkeyhook = ""; monkeyhook = "<script src='scripts/air-monkey-hook.js'></script>" if @mode == "cms" livereload = "" if @mode != "deploy" livereload = "<script>document.write('<script src=\"http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1\"></' + 'script>')</script>" if @mode != "cms" end pagemodel = Hash.new pagemodel = { :name => pagename, :title => title, :description => description, :keywords => keywords, :url => pageurl, :views => views, :resource => resource, :config => pageconfig, :bundles => bundles, :navigation => , :data => pagedata, :rootdir => @website, :monkeyhook => monkeyhook, :livereload => livereload } break if pageconfig["generate"] == nil erb = File.join(@website, skin) out = File.join(@website, File.join(pageconfig["generate"].sub('~/',''))) ScmsUtils.successLog("#{pageurl}") if File.exists?(erb) pubsubdir = File.dirname(out) Dir.mkdir(pubsubdir, 755) unless File::directory?(pubsubdir) erbtemplate = File.read(erb) #erbtemplate = erbtemplate.gsub('data.','page.')#lagasy fix #File.open(erb, 'w') {|f| f.write(erbtemplate) }#lagasy fix html = Scms.parsetemplate(erbtemplate, pagemodel) html = html.gsub('~/', ScmsUtils.uriEncode("file:///#{@website}/")) if @mode == "cms" websiteroot = '/' websiteroot = @settings["url"] unless @settings["rooturl"] == nil html = html.gsub('~/', websiteroot) begin File.open(out, 'w') {|f| f.write(html) } rescue Exception=>e ScmsUtils.errLog(e.) ScmsUtils.log(e.backtrace.inspect) end else ScmsUtils.errLog("Template doesn't exist: #{skin}") ScmsUtils.writelog("::Template doesn't exist #{skin}", @website) ScmsUtils.writelog("type NUL > #{erb}", @website) end end end #ScmsUtils.log( out ) end end end |
.parsetemplate(template, hash) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/scms.rb', line 280 def Scms.parsetemplate(template, hash) page = OpenStruct.new(hash) result = "" begin result = ERB.new(template).result(page.instance_eval { binding }) rescue Exception => e ScmsUtils.errLog("Critical Error: Could not parse template") ScmsUtils.errLog(e.) end return result end |
.sass(asset) ⇒ Object
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/scms.rb', line 362 def Scms.sass(asset) if File.exists?(asset) begin template = File.read(asset) sass_engine = Sass::Engine.new(template, { :style => :compressed, :cache => false, :syntax => :scss }.freeze) output = sass_engine.to_css css_file = "#{File.dirname(asset)}/#{File.basename(asset,'.*')}.css" File.open(css_file, 'w') { |file| file.write(output) } ScmsUtils.log( "CSS minified (sassed): #{css_file}" ) rescue Exception => e ScmsUtils.errLog("Error processing: #{asset}") ScmsUtils.errLog(e.) end else ScmsUtils.errLog("Sass file doesn't exists: #{asset}") ScmsUtils.writelog("::Sass file doesn't exist #{asset}", @website) ScmsUtils.writelog("type NUL > #{asset}", @website) end end |