Skip to content

Commit

Permalink
Add Google Analytics integration.
Browse files Browse the repository at this point in the history
Rework helpers to be individual modules, instead of mixing in to only Haml helpers.
  • Loading branch information
Bob McWhirter committed Mar 1, 2010
1 parent 582ba47 commit 18ac214
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 56 deletions.
2 changes: 1 addition & 1 deletion awestruct.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require 'rubygems'
Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = "awestruct"
s.version = "0.0.3"
s.version = "0.0.4"
s.author = "Bob McWhirter"
s.email = "bob@mcwhirter.org"
s.summary = "Static site-baking utility"
Expand Down
53 changes: 21 additions & 32 deletions lib/awestruct/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
require 'awestruct/extensions/indexifier'
require 'awestruct/extensions/paginator'
require 'awestruct/extensions/atomizer'
require 'awestruct/extensions/intense_debate'
require 'awestruct/extensions/google_analytics'

require 'awestruct/util/inflector'
require 'awestruct/util/default_inflections'
Expand All @@ -36,6 +38,7 @@ def initialize(dir, config=::Awestruct::Config.new)
@site = Site.new( @dir )
@site.engine = self
@site.tmp_dir = File.join( dir, '_tmp' )
@helpers = []
FileUtils.mkdir_p( @site.tmp_dir )
FileUtils.mkdir_p( @site.output_dir )
@max_yaml_mtime = nil
Expand Down Expand Up @@ -88,6 +91,22 @@ def load_page(path, relative_path=nil)
page
end

def create_context(page, content='')
context = OpenStruct.new( :site=>site, :content=>content )
@helpers.each do |h|
context.extend( h )
end
context.page = page
class << context
def interpolate_string(str)
result = instance_eval("%@#{(str||'').gsub('@', '\@')}@")
result
end
end
context
end



private

Expand Down Expand Up @@ -145,20 +164,8 @@ def requires_generation?(page,force)
false
end

def self.create_context(site, page, content='')
context = OpenStruct.new( :site=>site, :content=>content )
context.page = page
class << context
def interpolate_string(str)
result = instance_eval("%@#{(str||'').gsub('@', '\@')}@")
result
end
end
context
end

def render_page(page, with_layouts=true)
context = Engine.create_context( site, page )
context = create_context( page )
rendered = page.render( context )
if ( with_layouts )
cur = page
Expand Down Expand Up @@ -251,25 +258,7 @@ def load_extensions
end
pipeline = eval File.read( pipeline_file )
pipeline.execute( site )
end

def old_load_extensions
ext_dir_pathname = Pathname.new( File.join( dir, config.extension_dir ) )
Dir[ File.join( dir, config.extension_dir, '*.rb' ) ].each do |path|
ext_pathname = Pathname.new( path )
relative_path = ext_pathname.relative_path_from( ext_dir_pathname ).to_s
dir_name = File.dirname( relative_path )
if ( dir_name == '.' )
simple_path = File.basename( relative_path, '.rb' )
else
simple_path = File.join( dir_name, File.basename( relative_path, '.rb' ) )
end
ext_classname = camelize(simple_path)
require File.join( dir, config.extension_dir, simple_path )
ext_class = eval( ext_classname )
ext = ext_class.new
ext.execute( site )
end
@helpers = pipeline.helpers || []
end

def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
Expand Down
22 changes: 22 additions & 0 deletions lib/awestruct/extensions/google_analytics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

module Awestruct
module Extensions
module GoogleAnalytics

def google_analytics()
html = ''
html += %Q(<script type="text/javascript">\n)
html += %Q(var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");\n)
html += %Q(document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));\n)
html += %Q(</script>\n)
html += %Q(<script type="text/javascript">\n)
html += %Q(try {\n)
html += %Q(var pageTracker = _gat._getTracker("#{site.google_analytics}");\n)
html += %Q(pageTracker._trackPageview();\n)
html += %Q(} catch(err) {}</script>\n)
html
end
end
end

end
35 changes: 14 additions & 21 deletions lib/awestruct/extensions/intense_debate.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@

#module Awestruct
#module Extension
#class IntenseDebate
#
#def execute(site)
#end
#
#end
#end
#end
module Awestruct
module Extensions
module IntenseDebate

module Haml::Helpers

def intense_debate_comments()
html = %Q(<script>\n)
html += %Q( var idcomments_acct='#{site.intense_debate_acct}';\n)
html += %Q( var idcomments_post_id;\n)
html += %Q( var idcomments_post_url;\n)
html += %Q(</script>\n)
html += %Q(<span id="IDCommentsPostTitle" style="display:none"></span>\n)
html += %Q(<script type='text/javascript' src='http://www.intensedebate.com/js/genericCommentWrapperV2.js'></script>\n)
html
def intense_debate_comments()
html = %Q(<script>\n)
html += %Q( var idcomments_acct='#{site.intense_debate_acct}';\n)
html += %Q( var idcomments_post_id;\n)
html += %Q( var idcomments_post_url;\n)
html += %Q(</script>\n)
html += %Q(<span id="IDCommentsPostTitle" style="display:none"></span>\n)
html += %Q(<script type='text/javascript' src='http://www.intensedebate.com/js/genericCommentWrapperV2.js'></script>\n)
html
end
end
end

end
6 changes: 6 additions & 0 deletions lib/awestruct/extensions/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ module Extensions
class Pipeline

attr_reader :extensions
attr_reader :helpers

def initialize(&block)
@extensions = []
@helpers = []
instance_eval &block if block
end

def extension(ext)
@extensions << ext
end

def helper(helper)
@helpers << helper
end

def execute(site)
extensions.each do |ext|
ext.execute( site )
Expand Down
2 changes: 1 addition & 1 deletion lib/awestruct/hamlable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def render(context)
end

def content
context = OpenStruct.new( :site=>site, :page=>self )
context = site.engine.create_context( self )
render( context )
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/awestruct/marukuable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def render(context)
end

def content
context = Awestruct::Engine.create_context( site, self )
context = site.engine.create_context( self )
render( context )
end
end
Expand Down

0 comments on commit 18ac214

Please sign in to comment.