Class: MediaWikiLinkHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/mediacloth/mediawikilinkhandler.rb

Overview

A link handler is responsible for resolving the URL and generating the HTML code linking to pages and other resources (images and media, for example) based on a wiki short name.

This is the default link handler. A custom link handler will usually extend this class and provide the needed functionality by overriding some of the methods. The custom handler can be injected into an HTML generator via the link_handler= method. See MediaWikiHTMLGenerator for details.

Instance Method Summary collapse

Instance Method Details

This is invoked to generate an absolute link to a page when user either puts url onto the page or uses regular [] syntax

link_type argument is either empty string or “[”. Empty string indicates url written as plain text and “[” indicates that [] syntax for links was used



74
75
76
77
78
79
80
# File 'lib/mediacloth/mediawikilinkhandler.rb', line 74

def absolute_link_for(page, text, link_type)
  if page =~ /(^|\/)([^\/]*)((\.png)|(\.jpg)|(\.jpeg)|(\.gif))$/ and link_type.blank?
    "<img src=\"#{page}\" alt=\"#{$2}#{$3}\" />"
  else
    "<a href=\"#{page}\">#{text}</a>"
  end
end

#category_add(name, sort) ⇒ Object



61
62
# File 'lib/mediacloth/mediawikilinkhandler.rb', line 61

def category_add(name, sort)
end

Provides a hash with the attributes for page links. The options provided here will be added to the ‘a’ tag attributes and overwrite any options provided by the url_for method. If this method needs to be overriden, an URL reference must be provided here indexed by the :href symbol.



31
32
33
# File 'lib/mediacloth/mediawikilinkhandler.rb', line 31

def link_attributes_for(page)
   { :href => url_for(page) }
end

Renders a link to a wiki page as a string. The default behaviour is to return an ‘a’ tag, but any string can be used here like span, or bold tags. This method overwrites anything provided by the url_for, options_for or link_attributes_for methods.

The elem method may be used by subclasses for easier and safer text handling. For example: elem.a(:href => 'http://www.example.com') { |x| x << text } will emit a link for example.com with the given link text



44
45
46
# File 'lib/mediacloth/mediawikilinkhandler.rb', line 44

def link_for(page, text)
  elem.a(link_attributes_for(page)) { |x| x << text }
end


64
65
66
# File 'lib/mediacloth/mediawikilinkhandler.rb', line 64

def link_for_category(category, text)
  "<a href=\"javascript:void(0)\">#{text}</a>"
end

Method invoked to resolve references to resources of unknown types. The type is indicated by the resource prefix. Examples of inline links to unknown references include:

  • [[Media:video.mpg]] (prefix Media, resource video.mpg)

  • [[Image:pretty.png|100px|A ''pretty'' picture]] (prefix Image,

resource <tt>pretty.png</tt>, and options <tt>100px</tt> and <tt>A
<i>pretty</i> picture</tt>.

The return value should be a well-formed hyperlink, image, object or applet tag.



57
58
59
# File 'lib/mediacloth/mediawikilinkhandler.rb', line 57

def link_for_resource(prefix, resource, options=[])
  "<a href=\"#{prefix}:#{resource}\">#{prefix}:#{resource}</a>"
end

#url_for(page) ⇒ Object

Method invoked to resolve references to wiki pages when they occur in an internal link. In all the following internal links, the page name is My Page:

  • [[My Page]]

  • [[My Page|Click here to view my page]]

  • [[My Page|Click ''here'' to view my page]]

The return value should be an URL that references the page resource



21
22
23
24
25
# File 'lib/mediacloth/mediawikilinkhandler.rb', line 21

def url_for(page)
  page_name, page_title = page.split('|')
  page_title ||= page_name
  "/" + page_name.gsub(/\s+/, "_") + ".html"
end