From c1be34abcd69bcb11e4d9ff651476d64370ddb74 Mon Sep 17 00:00:00 2001 From: Jason Blevins Date: Sat, 6 Oct 2007 09:06:55 -0400 Subject: [PATCH] Support for InterWeb Links --- lib/chunks/wiki.rb | 52 +++++++++++++++++++++++---------- lib/wiki_content.rb | 71 +++++++++++++++++++++++---------------------- 2 files changed, 72 insertions(+), 51 deletions(-) diff --git a/lib/chunks/wiki.rb b/lib/chunks/wiki.rb index 617e6da5..7e02a732 100644 --- a/lib/chunks/wiki.rb +++ b/lib/chunks/wiki.rb @@ -13,18 +13,21 @@ module WikiChunk # Name of the referenced page attr_reader :page_name - + + # Name of the referenced page + attr_reader :web_name + # the referenced page def refpage @content.web.page(@page_name) end - + end # A wiki link is the top-level class for links that refers to # another wiki page. class WikiLink < WikiReference - + attr_reader :link_text, :link_type def initialize(match_data, content) @@ -49,11 +52,16 @@ module WikiChunk not @textile_link_suffix.nil? 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 def normalize_whitespace(line) line.gsub(/\s+/, ' ') end - + end # This chunk matches a WikiWord. WikiWords can be escaped @@ -63,7 +71,7 @@ module WikiChunk class Word < WikiLink attr_reader :escaped_text - + unless defined? WIKI_WORD WIKI_WORD = Regexp.new('(":)?(\\\\)?(' + WikiWords::WIKI_WORD_PATTERN + ')\b', 0, "utf-8") end @@ -75,7 +83,7 @@ module WikiChunk def initialize(match_data, content) super @textile_link_suffix, @escape, @page_name = match_data[1..3] - if @escape + if @escape @unmask_mode = :escape @escaped_text = @page_name else @@ -87,7 +95,7 @@ module WikiChunk end - # This chunk handles [[bracketted wiki words]] and + # This chunk handles [[bracketted wiki words]] and # [[AliasedWords|aliased wiki words]]. The first part of an # aliased wiki word must be a WikiWord. If the WikiWord # is aliased, the +link_text+ field will contain the @@ -95,15 +103,16 @@ module WikiChunk # contents within the double brackets. # # NOTE: This chunk must be tested before WikiWord since - # a WikiWords can be a substring of a WikiLink. + # a WikiWords can be a substring of a WikiLink. class Link < WikiLink - + unless defined? WIKI_LINK WIKI_LINK = /(":)?\[\[\s*([^\]\s][^\]]+?)\s*\]\]/ LINK_TYPE_SEPARATION = Regexp.new('^(.+):((file)|(pic))$', 0, 'utf-8') ALIAS_SEPARATION = Regexp.new('^(.+)\|(.+)$', 0, 'utf-8') - end - + WEB_SEPARATION = Regexp.new('^(.+):(.+)$', 0, 'utf-8') + end + def self.pattern() WIKI_LINK end def initialize(match_data, content) @@ -112,12 +121,13 @@ module WikiChunk @link_text = @page_name = normalize_whitespace(match_data[2]) separate_link_type 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 private - # if link wihin the brackets has a form of [[filename:file]] or [[filename:pic]], + # if link wihin the brackets has a form of [[filename:file]] or [[filename:pic]], # this means a link to a picture or a file def separate_link_type link_type_match = LINK_TYPE_SEPARATION.match(@page_name) @@ -135,9 +145,19 @@ module WikiChunk @link_text = alias_match[2] end # 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 diff --git a/lib/wiki_content.rb b/lib/wiki_content.rb index 88d9aee0..7bebc1da 100644 --- a/lib/wiki_content.rb +++ b/lib/wiki_content.rb @@ -11,46 +11,46 @@ require 'chunks/nowiki' # actions. The actions can modify wiki content so that certain parts of # it are protected from being rendered by later actions. # -# When wiki content is rendered, it can be interrogated to find out -# which chunks were rendered. This means things like categories, wiki +# When wiki content is rendered, it can be interrogated to find out +# which chunks were rendered. This means things like categories, wiki # links, can be determined. # -# Exactly how wiki content is rendered is determined by a number of +# Exactly how wiki content is rendered is determined by a number of # settings that are optionally passed in to a constructor. The current # options are: -# * :engine +# * :engine # => The structural markup engine to use (Textile, Markdown, RDoc) # * :engine_opts # => A list of options to pass to the markup engines (safe modes, etc) # * :pre_engine_actions # => A list of render actions or chunks to be processed before the -# markup engine is applied. By default this is: -# Category, Include, URIChunk, WikiChunk::Link, WikiChunk::Word +# markup engine is applied. By default this is: +# Category, Include, URIChunk, WikiChunk::Link, WikiChunk::Word # * :post_engine_actions -# => A list of render actions or chunks to apply after the markup -# engine. By default these are: +# => A list of render actions or chunks to apply after the markup +# engine. By default these are: # Literal::Pre, Literal::Tags # * :mode -# => How should the content be rendered? For normal display (show), +# => How should the content be rendered? For normal display (show), # publishing (:publish) or export (:export)? module ChunkManager attr_reader :chunks_by_type, :chunks_by_id, :chunks, :chunk_id - - ACTIVE_CHUNKS = [ NoWiki, Category, WikiChunk::Link, URIChunk, LocalURIChunk, - WikiChunk::Word ] + + ACTIVE_CHUNKS = [ NoWiki, Category, WikiChunk::Link, URIChunk, LocalURIChunk, + WikiChunk::Word ] HIDE_CHUNKS = [ Literal::Pre, Literal::Tags ] - MASK_RE = { + MASK_RE = { ACTIVE_CHUNKS => Chunk::Abstract.mask_re(ACTIVE_CHUNKS), HIDE_CHUNKS => Chunk::Abstract.mask_re(HIDE_CHUNKS) } - + def init_chunk_manager @chunks_by_type = Hash.new - Chunk::Abstract::derivatives.each{|chunk_type| - @chunks_by_type[chunk_type] = Array.new + Chunk::Abstract::derivatives.each{|chunk_type| + @chunks_by_type[chunk_type] = Array.new } @chunks_by_id = Hash.new @chunks = [] @@ -77,20 +77,20 @@ module ChunkManager def scan_chunkid(text) text.scan(MASK_RE[ACTIVE_CHUNKS]){|a| yield a[0] } end - + def find_chunks(chunk_type) @chunks.select { |chunk| chunk.kind_of?(chunk_type) and chunk.rendered? } end end -# A simplified version of WikiContent. Useful to avoid recursion problems in +# A simplified version of WikiContent. Useful to avoid recursion problems in # WikiContent.new class WikiContentStub < String - + attr_reader :options include ChunkManager - + def initialize(content, options) super(content) @options = options @@ -99,15 +99,15 @@ class WikiContentStub < String # Detects the mask strings contained in the text of chunks of type chunk_types # and yields the corresponding chunk ids - # example: content = "chunk123categorychunk
chunk456categorychunk
" + # example: content = "chunk123categorychunk
chunk456categorychunk
" # inside_chunks(Literal::Pre) ==> yield 456 def inside_chunks(chunk_types) chunk_types.each{|chunk_type| chunk_type.apply_to(self) } - + chunk_types.each{|chunk_type| @chunks_by_type[chunk_type].each{|hide_chunk| scan_chunkid(hide_chunk.text){|id| yield id } } - } + } end end @@ -132,12 +132,12 @@ class WikiContent < String @web = @revision.page.web @options = DEFAULT_OPTS.dup.merge(options) - @options[:engine] = Engines::MAP[@web.markup] + @options[:engine] = Engines::MAP[@web.markup] @options[:engine_opts] = [:filter_html, :filter_styles] if @web.safe_mode? @options[:active_chunks] = (ACTIVE_CHUNKS - [WikiChunk::Word] ) if @web.brackets_only? - + @not_rendered = @pre_rendered = nil - + super(@revision.content) init_chunk_manager build_chunks @@ -145,9 +145,10 @@ class WikiContent < String end # 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) - @url_generator.make_link(name, @web, text, @options) + @url_generator.make_link(name, web, text, @options) end def build_chunks @@ -158,7 +159,7 @@ class WikiContent < String # Handle hiding contexts like "pre" and "code" etc.. # The markup (textile, rdoc etc) can produce such contexts with its own syntax. # To reveal them, we work on a copy of the content. - # The copy is rendered and used to detect the chunks that are inside protecting context + # The copy is rendered and used to detect the chunks that are inside protecting context # These chunks are reverted on the original content string. copy = WikiContentStub.new(self, @options) @@ -170,25 +171,25 @@ class WikiContent < String end def pre_render! - unless @pre_rendered + unless @pre_rendered @chunks_by_type[Include].each{|chunk| chunk.unmask } @pre_rendered = String.new(self) end - @pre_rendered + @pre_rendered end def render! pre_render! @options[:engine].apply_to(self) # unmask in one go. $~[1] is the chunk id - gsub!(MASK_RE[ACTIVE_CHUNKS]) do + gsub!(MASK_RE[ACTIVE_CHUNKS]) do chunk = @chunks_by_id[$~[1].to_i] - if chunk.nil? + if chunk.nil? # if we match a chunkmask that existed in the original content string # just keep it as it is $~[0] - else - chunk.unmask_text + else + chunk.unmask_text end end self