Class: MediaWikiLinkHandler
- Inherits:
-
Object
- Object
- MediaWikiLinkHandler
- 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
-
#absolute_link_for(page, text, link_type) ⇒ Object
This is invoked to generate an absolute link to a page when user either puts url onto the page or uses regular [] syntax.
- #category_add(name, sort) ⇒ Object
-
#link_attributes_for(page) ⇒ Object
Provides a hash with the attributes for page links.
-
#link_for(page, text) ⇒ Object
Renders a link to a wiki page as a string.
- #link_for_category(category, text) ⇒ Object
-
#link_for_resource(prefix, resource, options = []) ⇒ Object
Method invoked to resolve references to resources of unknown types.
-
#url_for(page) ⇒ Object
Method invoked to resolve references to wiki pages when they occur in an internal link.
Instance Method Details
#absolute_link_for(page, text, link_type) ⇒ Object
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 |
#link_attributes_for(page) ⇒ Object
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 |
#link_for(page, text) ⇒ Object
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 |
#link_for_category(category, text) ⇒ Object
64 65 66 |
# File 'lib/mediacloth/mediawikilinkhandler.rb', line 64 def link_for_category(category, text) "<a href=\"javascript:void(0)\">#{text}</a>" end |
#link_for_resource(prefix, resource, options = []) ⇒ Object
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]]
(prefixMedia
, resourcevideo.mpg
) -
[[Image:pretty.png|100px|A ''pretty'' picture]]
(prefixImage
,
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, =[]) "<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 |