Skip to content

Instantly share code, notes, and snippets.

@cristibalan
Created March 25, 2009 10:29
Show Gist options
  • Save cristibalan/85413 to your computer and use it in GitHub Desktop.
Save cristibalan/85413 to your computer and use it in GitHub Desktop.
# from http://pastie.org/426366
require 'hpricot'
module Webby
class Renderer
attr_reader :page
end
module Filters
# To use: add relpath to the end of the list of filters in the layout. It won't
# work in the content file itself.
# add it to the css directly, since it doesn't have a layout and is processed in
# a special way
class RelPath
def initialize( str, mode, path )
@str = str
@mode = mode.downcase.to_sym
dir = File.dirname(path)
depth = 0
while (parent = File.dirname(dir)) != dir
dir = parent
depth += 1
end
depth -= 1 # because of the 'content/' prefix
@prefix = "../" * depth
end
def filter
if @mode == :css
# matches url attributes like: url("/images/test.gif")
# and replaces it with: url("../images/test.gif")
# if the css file is in css/some.css
# no clue about the xpaths, should read up about that :(
doc = @str.gsub(/(url\(")\/(.*)("\))/) do |s|
"#{$1}#{@prefix}#{$2}#{$3}"
end
doc
else
doc = @mode == :xml ? Hpricot.XML(@str) : Hpricot(@str)
attr_rgxp = %r/\[@(\w+)\]$/o
sub_rgxp = %r/\A\//o # <-- note this rgxp _consumes_ the "/"
::Webby.site.xpaths.each do |xpath|
@attr_name = nil
doc.search(xpath).each do |element|
@attr_name ||= attr_rgxp.match(xpath)[1]
a = element.get_attribute(@attr_name)
element.set_attribute(@attr_name, a) if a.sub!(sub_rgxp, @prefix)
end
end
doc.to_html
end
end
end
register :relpath do |input, cursor|
RelPath.new(input, cursor.page.extension, cursor.renderer.page.path).filter
end
end # module Filters
end # module Webby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment