From 9b3214d3828570bd1f7357b0550eafdb5390a16d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 5 Jan 2012 12:39:29 +0100 Subject: Node: implement optional static page generation --- lib/zorglub/config.rb | 6 ++++++ lib/zorglub/node.rb | 45 +++++++++++++++++++++++++++++++++++++++------ spec/node_spec.rb | 15 +++++++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lib/zorglub/config.rb b/lib/zorglub/config.rb index 26fd449..7cdb965 100644 --- a/lib/zorglub/config.rb +++ b/lib/zorglub/config.rb @@ -9,6 +9,7 @@ module Zorglub :layout => 'default', :view_dir => 'view', :layout_dir => 'layout', + :static_dir => 'static', :session_on => false, :session_key => 'zorglub.sid', :session_secret => 'session-secret-secret', @@ -42,6 +43,11 @@ module Zorglub ( p.nil? ? File.join(@options[:root], @options[:layout_dir]) : p ) end # + def static_base_path + p = @options[:static_path] + ( p.nil? ? File.join(@options[:root], @options[:static_dir]) : p ) + end + # def register_engine name, ext, proc return unless name @engines[name]=[ ext, proc ] diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb index c85e42d..a3552a9 100644 --- a/lib/zorglub/node.rb +++ b/lib/zorglub/node.rb @@ -32,6 +32,11 @@ module Zorglub @layout ||= Config.layout end # + def static val=nil + @static = val if (val==true or val==false) + @static ||=false + end + # def inherited_var sym, *args var = @inherited_vars[sym] ||=[] unless args.empty? @@ -55,13 +60,13 @@ module Zorglub def call env meth, *args = env['PATH_INFO'].sub(/^\//,'').split '/' meth||= 'index' - node = self.new env, {:engine=>engine,:layout=>layout,:view=>r(meth),:method=>meth,:args=>args} + node = self.new env, {:engine=>engine,:layout=>layout,:view=>r(meth),:method=>meth,:args=>args,:static=>static} return error_404 node if not node.respond_to? meth node.realize! end # def partial meth, *args - node = self.new nil, {:engine=>engine,:layout=>nil,:view=>r(meth),:method=>meth.to_s,:args=>args} + node = self.new nil, {:engine=>engine,:layout=>nil,:view=>r(meth),:method=>meth.to_s,:args=>args,:static=>static} return error_404 node if not node.respond_to? meth node.feed! node.content @@ -119,6 +124,32 @@ module Zorglub Node.call_before_hooks self state :meth @content = self.send @options[:method], *@options[:args] + static_path = static + if static_path.nil? + compile! + else + static! static_path + end + state :post_cb + Node.call_after_hooks self + state :finished + return @content, @mime + end + # + def static! path + if not File.exists? path + compile! + Dir.mkdir Config.static_base_path + Dir.mkdir File.dirname path + File.open(path, 'w') {|f| f.write(@content); f.write("\n@mime:"+@mime) } + else + content = File.open(path, 'r') {|f| f.read } + @content = content.sub /\n@mime:(.*)$/,'' + @mime = $1 + end + end + # + def compile! v, l, e = view, layout, Config.engine_proc(@options[:engine]) state (@options[:layout].nil? ? :partial : :view) @content, mime = e.call v, self if e and File.exists? v @@ -126,10 +157,6 @@ module Zorglub state :layout @content, mime = e.call l, self if e and File.exists? l @mime = mime unless mime.nil? - state :post_cb - Node.call_after_hooks self - state :finished - return @content, @mime end # def redirect target, options={}, &block @@ -163,6 +190,12 @@ module Zorglub @options[:layout] = nil end # + def static val=nil + @options[:static] = val if (val==true or val==false) + return nil if not @options[:static] or @options[:view].nil? + File.join(Config.static_base_path, @options[:view])+Config.engine_ext(@options[:engine]) + end + # def view view=nil @options[:view] = view unless view.nil? or view.empty? return '' if @options[:view].nil? diff --git a/spec/node_spec.rb b/spec/node_spec.rb index 0a9ee10..5b5d97d 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -2,10 +2,25 @@ # require 'spec_helper' # +def clean_static_path + static_base_path = Zorglub::Config.static_base_path + Dir.glob( File.join(static_base_path,'**','*') ).each do |f| File.unlink f if File.file? f end + Dir.glob( File.join(static_base_path,'*') ).each do |d| Dir.rmdir d end + Dir.rmdir static_base_path if File.directory? static_base_path +end +# describe Zorglub do # describe Zorglub::Node do # + before(:all) do + clean_static_path + end + # + after(:all) do + clean_static_path + end + # it "engine should return default Node's engine" do Node0.engine.should == Zorglub::Config.engine Node0.engine.should == Zorglub::Config[:engine] -- cgit v1.1-2-g2b99