module Zena
module Use
module Zafu
module Common
# Return a template's content from an url. If the url does not start with a '/', we try by replacing the
# first element with the current skin_name and if it does not work, we try with the full url. If the url
# start with a '/' we use the full url directly.
# tested in MainControllerTest
def get_template_text(opts)
return nil unless res = find_document_for_template(opts)
doc, url = *res
# TODO: could we use this for caching or will we loose dynamic context based loading ?
self.expire_with_nodes[url] = doc
text = dev_mode? ? doc.version.text : doc.version(true).text
return text, url, doc
end
# Return the zen_path ('/en/image34.png') for an asset given its name ('img/footer.png').
# The rule is not the same whether we are rendering a template and find tags
# or if we are parsing assets in a CSS file.
def template_url_for_asset(opts)
src = opts[:src]
if src =~ /\A(.*)\.(\w+)\Z/
src, format = $1, $2
end
if opts[:parse_assets]
current_folder = opts[:current_folder] || ''
current_folder = current_folder[1..-1] if current_folder[0..0] == '/'
if src =~ /\A(.*)_(\w+)\Z/
# if the element was not found, maybe it was not a name with underscore but it was an image mode
src2, mode2 = $1, $2
end
paths = []
if src[0..0] == '/'
paths << src[1..-1]
paths << src2[1..-1] if src2
else
paths << (current_folder + '/' + src)
paths << (current_folder + '/' + src2) if src2
end
# make sure path elements are url_names
paths.map! do |path|
res = []
path.split('/').each do |e|
if e == '..'
res.pop
else
res << e.url_name
end
end
res.join('/')
end
if asset = secure(Document) { Document.find_by_path(paths[0]) }
elsif src2 && (asset = secure(Document) { Document.find_by_path(paths[1]) })
mode = mode2
else
return nil
end
else
if src =~ /\A(.*)_(\w+)\Z/
src, mode = $1, $2
end
src2 = opts[:src].split('/').map {|s| s.url_name!}.join('/')
unless res = find_document_for_template(opts)
# '_...' did not mean mode but was an old name.
mode = nil
return nil unless res = find_document_for_template(opts.merge(:src => src2))
end
asset, url = *res
self.renamed_assets[url] = asset
end
data_path(asset, :mode => mode)
end
# TODO: test
def save_erb_to_url(template, template_url)
path = fullpath_from_template_url(template_url)
path += ".erb" unless path =~ /\.\w+\Z/
FileUtils.mkpath(File.dirname(path)) unless File.exists?(File.dirname(path))
File.open(path, "wb") { |f| f.syswrite(template) }
""
end
# TODO: test
def fullpath_from_template_url(template_url=params[:t_url])
if template_url =~ /\A\.|[^\w\+\._\-\/]/
raise Zena::AccessViolation.new("'template_url' contains illegal characters : #{template_url.inspect}")
end
template_url = template_url[1..-1].split('/')
path = "/#{template_url[0]}/#{template_url[1]}/#{dev_mode? ? "dev_#{lang}" : lang}/#{template_url[2..-1].join('/')}"
"#{SITES_ROOT}/#{current_site.host}/zafu#{path}"
end
# Make sure some vital templates never get broken
def valid_template?(content, opts)
mode = opts[:mode]
case mode
when '+login'
content =~ %r{