Created
September 5, 2013 23:55
-
-
Save mweppler/6457828 to your computer and use it in GitHub Desktop.
wip - liquid tag plugin to build links...
This file contains hidden or 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
module Nvidia | |
class LinkHelper < Liquid::Tag | |
Link = Struct.new :classes, :href, :id, :local, :name | |
def initialize(tagName, markup, tokens) | |
super | |
end | |
def hash_to_link link | |
Link.new link.fetch('classes'), link.fetch('href'), link.fetch('id'), link.fetch('local'), link.fetch('name') | |
end | |
def render(context) | |
link = validate_hash_as_link context[@markup] | |
link_parts = [] | |
link_parts << '<a ' | |
link_parts << "class=\"#{link.classes}\" " unless link.classes.nil? | |
link_parts << "href=\"#{link.href}\" " | |
link_parts << "id=\"#{link.id}\" " unless link.id.nil? | |
link_parts << 'target="_blank" ' unless link.local | |
link_parts << "title=\"#{link.name}\" " | |
link_parts << '>' | |
link_parts << link.name | |
link_parts << '</a>' | |
link_parts.join | |
end | |
def validate_hash_as_link link | |
begin | |
link.fetch('classes') | |
link.fetch('href') | |
link.fetch('id') | |
link.fetch('local') | |
link.fetch('name') | |
rescue | |
raise 'Not a valid link object' | |
end | |
return hash_to_link link | |
end | |
end | |
end | |
Liquid::Template.register_tag 'link_helper', Nvidia::LinkHelper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment