-
-
Save nathos/253636 to your computer and use it in GitHub Desktop.
Interlink - MediaWiki-style link generation for Webby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Webby | |
class Renderer | |
attr_reader :page | |
end | |
end | |
# To use add interlink to the list of filters in the layout. | |
# Creates links to pages using a MediaWiki-style syntax: | |
# | |
# [[main-page]] | |
# <a href="/main-page.html">Main Page Title</a> | |
# | |
# [[Main Page Title]] | |
# <a href="/main-page.html">Main Page Title</a> | |
# | |
# [[main-page|other text]] | |
# <a href="/main-page.html">other text</a> | |
Webby::Filters.register :interlink do |input, cursor| | |
renderer = cursor.renderer | |
page = cursor.page | |
pages = Webby::Resources.pages | |
input.gsub %r/\[\[([^\]|\|]+)\|?(.*)\]\]/ do | |
name = $1.strip | |
display_name = $2.strip | |
found_page = nil | |
comps = page.dir.split('/') | |
# Walk up the directory tree searching for a match. | |
# We do this so that if there are multiple files with the same name we | |
# find the most closely related match rather than the first match in the | |
# entire tree. | |
begin | |
search_dir = comps.join('/') | |
found_page = pages.find(:name => name, :in_directory => search_dir, :recursive => true) | |
found_page = pages.find(:title => name, :in_directory => search_dir, :recursive => true) if found_page.nil? | |
end until found_page || comps.delete_at(-1) == nil | |
if found_page | |
display_name = found_page.title if display_name.nil? || display_name.empty? | |
renderer.link_to_page(display_name, :path => found_page.path) | |
else | |
display_name = name if display_name.nil? || display_name.empty? | |
%Q(#{display_name}) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment