Created
December 5, 2008 00:00
-
-
Save antarestrader/32158 to your computer and use it in GitHub Desktop.
Wiki Formatter for RedClothe Example
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
#mixin for RedCloth that will convert [[wiki links]] to redcloth links | |
module WikiFormatter | |
#match a wiki link (e.g. [[ Foo : Bar ]] or [[foo]]) | |
# match the sting '[[' | |
# followed zero or more spaces | |
# followed by any amount of text, but as little as possible as Group 1 | |
# followed zero or more spaces | |
# followed (potentially) by a set that is not stored as a match group | |
# which has the charictor ':' | |
# followed zero or more spaces | |
# followed by any amount of text, but as little as possible as Group 2 | |
# followed zero or more spaces | |
# which is matched zero or one times | |
# followed by the string ']]' | |
WIKI_MATCH = /\[\[\s*(.*?)\s*(?:\:\s*(.*?)\s*)?\]\]/ | |
#When mixed into RedCloth, this method can be used by | |
#passing its name to #to_html. ex RedCloth.new(str).to_html(:wiki_format) | |
def wiki_format(str) | |
str.gsub!(WIKI_MATCH) do |s| | |
text = $1 | |
# This calls a function on the model class that normalizes the name so | |
# that [[Some Cool Page]] becomes some_cool_page | |
uri = Wiki.format_url_title($2 || $1) | |
# $2 allows the link text to differ from the linked page e.g. [[Cool : Some Cool Page]] | |
#Change to a textile link. Could also be an actual <a> tag. | |
%Q<["#{text}":#{Merb::Router.url :wiki_slice_page , :url_title=>uri}]> | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment