Support for InterWeb Links

This commit is contained in:
Jason Blevins 2007-10-06 09:06:55 -04:00
parent 8cdcbff13e
commit c1be34abcd
2 changed files with 72 additions and 51 deletions

View file

@ -14,6 +14,9 @@ module WikiChunk
# Name of the referenced page # Name of the referenced page
attr_reader :page_name attr_reader :page_name
# Name of the referenced page
attr_reader :web_name
# the referenced page # the referenced page
def refpage def refpage
@content.web.page(@page_name) @content.web.page(@page_name)
@ -49,6 +52,11 @@ module WikiChunk
not @textile_link_suffix.nil? not @textile_link_suffix.nil?
end end
def interweb_link?
not @web_name.nil? and Web.find_by_name(@web_name) or
Web.find_by_address(@web_name)
end
# replace any sequence of whitespace characters with a single space # replace any sequence of whitespace characters with a single space
def normalize_whitespace(line) def normalize_whitespace(line)
line.gsub(/\s+/, ' ') line.gsub(/\s+/, ' ')
@ -102,6 +110,7 @@ module WikiChunk
WIKI_LINK = /(":)?\[\[\s*([^\]\s][^\]]+?)\s*\]\]/ WIKI_LINK = /(":)?\[\[\s*([^\]\s][^\]]+?)\s*\]\]/
LINK_TYPE_SEPARATION = Regexp.new('^(.+):((file)|(pic))$', 0, 'utf-8') LINK_TYPE_SEPARATION = Regexp.new('^(.+):((file)|(pic))$', 0, 'utf-8')
ALIAS_SEPARATION = Regexp.new('^(.+)\|(.+)$', 0, 'utf-8') ALIAS_SEPARATION = Regexp.new('^(.+)\|(.+)$', 0, 'utf-8')
WEB_SEPARATION = Regexp.new('^(.+):(.+)$', 0, 'utf-8')
end end
def self.pattern() WIKI_LINK end def self.pattern() WIKI_LINK end
@ -112,7 +121,8 @@ module WikiChunk
@link_text = @page_name = normalize_whitespace(match_data[2]) @link_text = @page_name = normalize_whitespace(match_data[2])
separate_link_type separate_link_type
separate_alias separate_alias
@unmask_text = @content.page_link(@page_name, @link_text, @link_type) separate_web
@unmask_text = @content.page_link(@web_name, @page_name, @link_text, @link_type)
end end
private private
@ -137,7 +147,17 @@ module WikiChunk
# note that [[filename|link text:file]] is also supported # note that [[filename|link text:file]] is also supported
end end
# Interweb links have the form [[Web Name:Page Name]] or
# [[address:PageName]]. Alternate text links of the form
# [[address:PageName|Other text]] are also supported.
def separate_web
web_match = WEB_SEPARATION.match(@page_name)
if web_match
@web_name = normalize_whitespace(web_match[1])
@page_name = web_match[2]
end
end
end end
end end

View file

@ -145,9 +145,10 @@ class WikiContent < String
end end
# Call @web.page_link using current options. # Call @web.page_link using current options.
def page_link(name, text, link_type) def page_link(web_name, name, text, link_type)
web = Web.find_by_name(web_name) || Web.find_by_address(web_name) || @web
@options[:link_type] = (link_type || :show) @options[:link_type] = (link_type || :show)
@url_generator.make_link(name, @web, text, @options) @url_generator.make_link(name, web, text, @options)
end end
def build_chunks def build_chunks