Skip to content

Instantly share code, notes, and snippets.

@mattstevens
Created December 9, 2009 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattstevens/252609 to your computer and use it in GitHub Desktop.
Save mattstevens/252609 to your computer and use it in GitHub Desktop.
Webby filter to create paths readable by the file system for CHM and Mac help bundles
module Webby
class Renderer
attr_reader :page
end
module Filters
# To use add localpath to the end of the list of filters in the layout.
# Converts all links starting with '/' to use relative paths and appends
# 'index.html' to all links both starting and ending with '/'.
class LocalPath
def initialize( str, dir )
@str = str
@dir = dir
end
def rel_path(base, path)
base.gsub!(/\\/,'/')
path.gsub!(/\\/,'/')
base = base.split('/')
path = path.split('/')
i = 0
while ((base[i] == path[i]) && (i < base.size))
i += 1
end
'../'*(base.size - i) + path[i..-1].join('/')
end
def filter
rgxp = %r/="\/([^"]*)"/
@str.gsub!(rgxp) do
path = $1
path << "index.html" if path[-1,1] == '/' || path.empty?
path = rel_path(@dir, path)
%Q(="#{path}")
end
end
end
register :localpath do |input, cursor|
LocalPath.new(input, cursor.renderer.page.dir).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