Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add tag_links Liquid filter that turns an array into a list of tags, …

…linked to a Google search.

A bit of a kludge. Hope to replace it if I get around to implementing Haml templates.

Inspired by http://metajack.im/.
  • Loading branch information...
commit 8859e9a41d90f68f54b3ae2a517920f3568336d5 1 parent fb3d937
@henrik henrik authored
Showing with 22 additions and 1 deletion.
  1. +22 −1 lib/jekyll/filters.rb
View
23 lib/jekyll/filters.rb
@@ -1,3 +1,5 @@
+require 'cgi'
+
module Jekyll
module Filters
@@ -25,14 +27,33 @@ def date_to_utc(date)
date.utc
end
+ def url_escape(input)
+ CGI.escape(input)
+ end
+
def xml_escape(input)
- input.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;")
+ CGI.escapeHTML(input)
end
def number_of_words(input)
input.split.length
end
+ # Example:
+ #
+ # Posted in <span class="tags">{{ page.tags | tag_links: "example.com" }}</span>.
+ #
+ # Then style '.tags span { display: none; }' so the "tag:" bits don't show.
+ # You can provide 'tags' as a YAML array in the post's front matter.
+ def tag_links(array, domain)
+ links = array.map { |tag|
+ qs = %{site:#{domain} "tag: #{tag}"}
+ url = "http://www.google.com/search?q=#{url_escape qs}"
+ %{<a href="#{xml_escape url}"><span>tag:</span> #{xml_escape tag}</a>}
+ }
+ array_to_sentence_string(links)
+ end
+
def array_to_sentence_string(array)
connector = "and"
case array.length
Please sign in to comment.
Something went wrong with that request. Please try again.