lib/bloggit/template.rb in bloggit-1.0.3 vs lib/bloggit/template.rb in bloggit-1.0.7
- old
+ new
@@ -61,76 +61,78 @@
puts "Cannot find renderer for #{params.to_yaml}"
end
end
def url_for(params={})
+ absolute = params.fetch(:absolute, false)
+
if post = params.delete(:post)
if post.is_a? String
- Template.path_to_root + site.get_post_by_slug(post).permalink
+ Template.path_to_root(absolute) + site.get_post_by_slug(post).permalink
elsif post.is_a? Post
- Template.path_to_root + post.permalink
+ Template.path_to_root(absolute) + post.permalink
end
elsif page = params.delete(:page)
if page.is_a? String
- Template.path_to_root + site.get_page_by_slug(page).permalink
+ Template.path_to_root(absolute) + site.get_page_by_slug(page).permalink
elsif page.is_a? Page
- Template.path_to_root + page.permalink
+ Template.path_to_root(absolute) + page.permalink
end
elsif tag = params.delete(:tag)
if tag.is_a? String
- Template.path_to_root + Tag.tag_list[tag].permalink
+ Template.path_to_root(absolute) + Tag.tag_list[tag].permalink
elsif tag.is_a? Tag
- Template.path_to_root + tag.permalink
+ Template.path_to_root(absolute) + tag.permalink
end
elsif media = params.delete(:media)
if media.is_a? String
- Template.path_to_root + site.media[media].permalink
+ Template.path_to_root(absolute) + site.media[media].permalink
elsif media.is_a? Media
- Template.path_to_root + media.permalink
+ Template.path_to_root(absolute) + media.permalink
end
elsif script = params.delete(:script)
script += '.js' unless script.ends_with? '.js'
- "#{ Template.path_to_root }theme/scripts/#{ script }"
+ "#{ Template.path_to_root(absolute) }theme/scripts/#{ script }"
elsif style = params.delete(:style)
style += '.css' unless style.ends_with? '.css'
- "#{ Template.path_to_root }theme/styles/#{ style }"
+ "#{ Template.path_to_root(absolute) }theme/styles/#{ style }"
elsif image = params.delete(:image)
- "#{ Template.path_to_root }theme/images/#{ image }"
+ "#{ Template.path_to_root(absolute) }theme/images/#{ image }"
elsif path = params.delete(:from_root)
- Template.path_to_root + path
+ Template.path_to_root(absolute) + path
elsif url = params.delete(:url)
url
end
end
- def tag_links(obj)
+ def tag_links(obj, absolute=false)
links = []
obj.tags.each do |tag|
- links << link_to(tag, :tag=>tag)
+ links << link_to(tag, :tag=>tag, :absolute=>absolute)
end
links
end
- def url_for_blog
- "#{ Template.path_to_root }#{site.build_post_path('index.html')}"
+ def url_for_blog(params={})
+ "#{ Template.path_to_root(params.fetch(:absolute, false)) }#{site.build_post_path('index.html')}"
end
- def url_for_home
- "#{ Template.path_to_root }index.html"
+ def url_for_home(params={})
+ "#{ Template.path_to_root(params.fetch(:absolute, false)) }index.html"
end
- def url_for_feed
- "#{ Template.path_to_root }#{site.settings.syndication.filename}"
+ def url_for_feed(params={})
+ "#{ Template.path_to_root(params.fetch(:absolute, false)) }#{site.settings.syndication.filename}"
end
def link_to(title, params={})
url = url_for(params)
title ||= url
@@ -164,10 +166,13 @@
atts << %Q|width="#{size}" | if size.ends_with?('%')
image_tag = %Q|<img src="#{media_item_url}" #{atts} />|
link_to image_tag, params
end
+ def path_to_root(absolute=false)
+ Template.path_to_root(absolute)
+ end
# def link_to_archive(title, params={})
# atts = ""
# params.each {|k,v| atts << %Q|#{k}="#{v.to_s}"|}
# %Q|<a #{atts} href="#{Template.path_to_root}index.html">#{title}</a>|
@@ -253,21 +258,22 @@
class << self
private :new
- attr_accessor :site, :template_dir, :content_parts, :src_path, :current_template, :folder_depth
+ attr_accessor :site, :template_dir, :content_parts, :src_path, :current_template, :folder_depth, :force_absolute_path
include Test::Unit::Assertions
def config
yield self
assert !@site.nil?, 'Site cannot be nil'
@template_dir = File.join(@site.base_path, 'themes', @site.theme, 'templates') if @template_dir.nil?
@is_ready = true
@template_cache = {}
@content_parts = {}
+ @force_absolute_path = false
end
# Do not call directly!
def generate(template, ctx) #:nodoc:
assert @is_ready, "Template isn't ready"
@@ -296,11 +302,17 @@
def []=(key,value)
set_content_for(key, value)
end
- def path_to_root
- '../' * @folder_depth
+ def path_to_root(absolute_path = false)
+ path_to_root = '../' * @folder_depth
+ if @force_absolute_path || absolute_path
+ root_url = @site.settings.syndication.fetch('base_url', '')
+ [root_url, path_to_root].join('/')
+ else
+ path_to_root
+ end
end
def from_file(path)
path = File.expand_path(path)
raise "Template file must exist" unless File.exists?( path )
\ No newline at end of file