Skip to content

Instantly share code, notes, and snippets.

@mauriciozaffari
Created August 18, 2010 16:25
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 mauriciozaffari/535318 to your computer and use it in GitHub Desktop.
Save mauriciozaffari/535318 to your computer and use it in GitHub Desktop.
require 'shellwords'
class Widget
def self.parse_widgets(content)
regex = /\[widget (.+)\]/
content =~ regex
params = Hash[*Shellwords.shellwords($1).map{ |s| s.split('=', 2) }.flatten]
widget = Kernel.const_get(params.delete("type")).render(params)
widget ? content.gsub(regex, widget) : content
end
end
# Example:
#
content = "[widget type=Feed src=http://example.com/rss]"
Widget.parse_widgets(content)
# This will result in a call like this:
Feed.render("src" => "http://example.com/rss")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment