From f0e41205fd2fa2b44c1e31c7ebe60b39682021e7 Mon Sep 17 00:00:00 2001 From: Didier Lafforgue Date: Mon, 2 Apr 2012 02:41:52 +0200 Subject: [PATCH] re-organize filters + drop unused tags --- lib/locomotive/liquid.rb | 1 + lib/locomotive/liquid/filters/html.rb | 80 +------------------ lib/locomotive/liquid/filters/misc.rb | 57 ++++++++----- lib/locomotive/liquid/filters/text.rb | 18 +++++ lib/locomotive/liquid/tags/blueprint.rb | 21 ----- lib/locomotive/liquid/tags/jquery.rb | 17 ---- lib/locomotive/liquid/tags/locale_switcher.rb | 4 +- .../locomotive/liquid/filters/html_spec.rb | 34 +------- .../locomotive/liquid/filters/misc_spec.rb | 54 ++++++++----- .../locomotive/liquid/filters/text_spec.rb | 21 +++++ 10 files changed, 119 insertions(+), 188 deletions(-) delete mode 100644 lib/locomotive/liquid/tags/blueprint.rb delete mode 100644 lib/locomotive/liquid/tags/jquery.rb diff --git a/lib/locomotive/liquid.rb b/lib/locomotive/liquid.rb index 9c0946d1..1747e6c3 100644 --- a/lib/locomotive/liquid.rb +++ b/lib/locomotive/liquid.rb @@ -1,5 +1,6 @@ require 'locomotive/liquid/drops/base' require 'locomotive/liquid/drops/proxy_collection' +require 'locomotive/liquid/filters/base' %w{. tags drops filters}.each do |dir| Dir[File.join(File.dirname(__FILE__), 'liquid', dir, '*.rb')].each { |lib| require lib } diff --git a/lib/locomotive/liquid/filters/html.rb b/lib/locomotive/liquid/filters/html.rb index c9841741..3c5f2957 100644 --- a/lib/locomotive/liquid/filters/html.rb +++ b/lib/locomotive/liquid/filters/html.rb @@ -6,7 +6,7 @@ module Locomotive # Returns a link tag that browsers and news readers can use to auto-detect an RSS or ATOM feed. # input: url of the feed # example: - # {{ '/foo/bar' | auto_discovery_link_tag: 'rel:alternate', 'type:atom', 'title:A title' }} + # {{ '/foo/bar' | auto_discovery_link_tag: 'rel:alternate', 'type:application/atom+xml', 'title:A title' }} def auto_discovery_link_tag(input, *args) options = args_to_options(args) @@ -17,7 +17,7 @@ module Locomotive %{} end - # Write the url to a stylesheet resource + # Write the url of a theme stylesheet # input: name of the css file def stylesheet_url(input) return '' if input.nil? @@ -31,7 +31,7 @@ module Locomotive input end - # Write the link to a stylesheet resource + # Write the link tag of a theme stylesheet # input: url of the css file def stylesheet_tag(input, media = 'screen') return '' if input.nil? @@ -106,80 +106,6 @@ module Locomotive }.gsub(/ >/, '>').strip end - # Render the navigation for a paginated collection - def default_pagination(paginate, *args) - return '' if paginate['parts'].empty? - - options = args_to_options(args) - - previous_label = options[:previous_label] || I18n.t('pagination.previous') - next_label = options[:next_label] || I18n.t('pagination.next') - - previous_link = (if paginate['previous'].blank? - "#{previous_label}" - else - "#{previous_label}" - end) - - links = "" - paginate['parts'].each do |part| - links << (if part['is_link'] - "#{part['title']}" - elsif part['hellip_break'] - "#{part['title']}" - else - "#{part['title']}" - end) - end - - next_link = (if paginate['next'].blank? - "#{next_label}" - else - "#{next_label}" - end) - - %{} - end - - protected - - # Convert an array of properties ('key:value') into a hash - # Ex: ['width:50', 'height:100'] => { :width => '50', :height => '100' } - def args_to_options(*args) - options = {} - args.flatten.each do |a| - if (a =~ /^(.*):(.*)$/) - options[$1.to_sym] = $2 - end - end - options - end - - # Write options (Hash) into a string according to the following pattern: - # ="", ="') - end - - def concat(input, *args) - result = input.to_s - args.flatten.each { |a| result << a.to_s } - result - end - def modulo(word, index, modulo) (index.to_i + 1) % modulo == 0 ? word : '' end @@ -37,6 +19,45 @@ module Locomotive input.blank? ? value : input end + # Render the navigation for a paginated collection + def default_pagination(paginate, *args) + return '' if paginate['parts'].empty? + + options = args_to_options(args) + + previous_label = options[:previous_label] || I18n.t('pagination.previous') + next_label = options[:next_label] || I18n.t('pagination.next') + + previous_link = (if paginate['previous'].blank? + "#{previous_label}" + else + "#{previous_label}" + end) + + links = "" + paginate['parts'].each do |part| + links << (if part['is_link'] + "#{part['title']}" + elsif part['hellip_break'] + "#{part['title']}" + else + "#{part['title']}" + end) + end + + next_link = (if paginate['next'].blank? + "#{next_label}" + else + "#{next_label}" + end) + + %{} + end + end ::Liquid::Template.register_filter(Misc) diff --git a/lib/locomotive/liquid/filters/text.rb b/lib/locomotive/liquid/filters/text.rb index fcebfdcf..5918f211 100644 --- a/lib/locomotive/liquid/filters/text.rb +++ b/lib/locomotive/liquid/filters/text.rb @@ -3,6 +3,24 @@ module Locomotive module Filters module Text + def underscore(input) + input.to_s.gsub(' ', '_').gsub('/', '_').underscore + end + + def dasherize(input) + input.to_s.gsub(' ', '-').gsub('/', '-').dasherize + end + + def multi_line(input) + input.to_s.gsub("\n", '
') + end + + def concat(input, *args) + result = input.to_s + args.flatten.each { |a| result << a.to_s } + result + end + def textile(input) ::RedCloth.new(input).to_html end diff --git a/lib/locomotive/liquid/tags/blueprint.rb b/lib/locomotive/liquid/tags/blueprint.rb deleted file mode 100644 index fe652238..00000000 --- a/lib/locomotive/liquid/tags/blueprint.rb +++ /dev/null @@ -1,21 +0,0 @@ -module Locomotive - module Liquid - module Tags - class Blueprint < ::Liquid::Tag - - def render(context) - %{ - - - - } - end - - end - - ::Liquid::Template.register_tag('blueprint_stylesheets', Blueprint) - end - end -end diff --git a/lib/locomotive/liquid/tags/jquery.rb b/lib/locomotive/liquid/tags/jquery.rb deleted file mode 100644 index 6f6661a9..00000000 --- a/lib/locomotive/liquid/tags/jquery.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Liquid - module Locomotive - module Tags - class Jquery < ::Liquid::Tag - - def render(context) - %{ - - - } - end - end - - ::Liquid::Template.register_tag('jQuery', Jquery) - end - end -end diff --git a/lib/locomotive/liquid/tags/locale_switcher.rb b/lib/locomotive/liquid/tags/locale_switcher.rb index 9e397146..286190f1 100644 --- a/lib/locomotive/liquid/tags/locale_switcher.rb +++ b/lib/locomotive/liquid/tags/locale_switcher.rb @@ -11,11 +11,11 @@ module Locomotive # # options: # - label: iso (de, fr, en, ...etc), locale (Deutsch, Français, English, ...etc), title (page title) - # - sep: piece of html code seperating 2 locales + # - sep: piece of html code separating 2 locales # # notes: # - "iso" is the default choice for label - # - " | " is the default seperating code + # - " | " is the default separating code # class LocaleSwitcher < ::Liquid::Tag diff --git a/spec/lib/locomotive/liquid/filters/html_spec.rb b/spec/lib/locomotive/liquid/filters/html_spec.rb index 3f3eb87d..58a3df3b 100644 --- a/spec/lib/locomotive/liquid/filters/html_spec.rb +++ b/spec/lib/locomotive/liquid/filters/html_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe Locomotive::Liquid::Filters::Html do + include Locomotive::Liquid::Filters::Base include Locomotive::Liquid::Filters::Html before(:each) do @@ -206,39 +207,6 @@ describe Locomotive::Liquid::Filters::Html do }.strip end - it 'should return a navigation block for the pagination' do - pagination = { - "previous" => nil, - "parts" => [ - { 'title' => '1', 'is_link' => false }, - { 'title' => '2', 'is_link' => true, 'url' => '/?page=2' }, - { 'title' => '…', 'is_link' => false, 'hellip_break' => true }, - { 'title' => '5', 'is_link' => true, 'url' => '/?page=5' } - ], - "next" => { 'title' => 'next', 'is_link' => true, 'url' => '/?page=2' } - } - html = default_pagination(pagination, 'css:flickr_pagination') - html.should match(/