Skip to content

Instantly share code, notes, and snippets.

@antarestrader
Created December 5, 2008 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antarestrader/32158 to your computer and use it in GitHub Desktop.
Save antarestrader/32158 to your computer and use it in GitHub Desktop.
Wiki Formatter for RedClothe Example
#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