diff --git a/lib/awestruct/context_helper.rb b/lib/awestruct/context_helper.rb new file mode 100644 index 00000000..eb40bcb9 --- /dev/null +++ b/lib/awestruct/context_helper.rb @@ -0,0 +1,34 @@ +require 'hpricot' + +module Awestruct + module ContextHelper + def html_to_text(str) + str.gsub( /<[^>]+>/, '' ) + end + + def summarize(text, numwords=20) + text.split()[0, numwords].join(' ') + end + + def fully_qualify_urls(base_url, text) + doc = Hpricot( text ) + + doc.search( "//a" ).each do |a| + a['href'] = fix_url( base_url, a['href'] ) + end + doc.search( "//link" ).each do |link| + link['href'] = fix_url( base_url, link['href'] ) + end + doc.search( "//img" ).each do |img| + img['src'] = fix_url( base_url, img['src'] ) + end + return doc.to_s + end + + def fix_url(base_url, url) + return url unless ( url =~ /^\// ) + "#{base_url}#{url}" + end + end + +end diff --git a/lib/awestruct/engine.rb b/lib/awestruct/engine.rb index 83443c81..3840390c 100644 --- a/lib/awestruct/engine.rb +++ b/lib/awestruct/engine.rb @@ -11,7 +11,7 @@ require 'awestruct/sass_file' require 'awestruct/verbatim_file' -require 'awestruct/haml_helpers' +require 'awestruct/context_helper' require 'awestruct/extensions/pipeline' require 'awestruct/extensions/posts' @@ -93,6 +93,7 @@ def load_page(path, relative_path=nil) def create_context(page, content='') context = OpenStruct.new( :site=>site, :content=>content ) + context.extend( Awestruct::ContextHelper ) @helpers.each do |h| context.extend( h ) end diff --git a/lib/awestruct/haml_helpers.rb b/lib/awestruct/haml_helpers.rb deleted file mode 100644 index 1086b735..00000000 --- a/lib/awestruct/haml_helpers.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'hpricot' - -module Haml::Helpers - def html_to_text(str) - str.gsub( /<[^>]+>/, '' ) - end - - def summarize(text, numwords=20) - text.split()[0, numwords].join(' ') - end - - def fully_qualify_urls(base_url, text) - doc = Hpricot( text ) - - doc.search( "//a" ).each do |a| - a['href'] = fix_url( base_url, a['href'] ) - end - doc.search( "//link" ).each do |link| - link['href'] = fix_url( base_url, link['href'] ) - end - doc.search( "//img" ).each do |img| - img['src'] = fix_url( base_url, img['src'] ) - end - return doc.to_s - end - - def fix_url(base_url, url) - return url unless ( url =~ /^\// ) - "#{base_url}#{url}" - end - -end