From f465abb710ee8cc8f4d59381c10c94c81d1c4dc2 Mon Sep 17 00:00:00 2001 From: Keitaroh Kobayashi Date: Wed, 28 May 2014 11:26:32 +0900 Subject: [PATCH] Adds slim template support --- README.md | 2 +- lib/shortcode.rb | 5 +++++ lib/shortcode/configuration.rb | 4 ++-- lib/shortcode/tag.rb | 2 ++ shortcode.gemspec | 1 + spec/rails_helpers_spec.rb | 16 ++++++++++++++++ .../fixtures/rails_helper_output_slim.html | 1 + .../templates/slim/rails_helper.html.slim | 1 + 8 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 spec/support/fixtures/rails_helper_output_slim.html create mode 100644 spec/support/templates/slim/rails_helper.html.slim diff --git a/README.md b/README.md index 0f36a66..1179f3c 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ Shortcode.process("[quote]Hello World[/quote]") Shortcode.setup do |config| # the template parser to use - config.template_parser = :haml # :erb or :haml supported, :haml is default + config.template_parser = :haml # :erb, :haml, :slim supported, :haml is default # location of the template files, default is "app/views/shortcode_templates" config.template_path = "support/templates/haml" diff --git a/lib/shortcode.rb b/lib/shortcode.rb index 3b03d95..21f9b6f 100644 --- a/lib/shortcode.rb +++ b/lib/shortcode.rb @@ -1,5 +1,10 @@ require 'parslet' require 'haml' + +begin + require 'slim' +rescue LoadError; end + require 'erb' module Shortcode diff --git a/lib/shortcode/configuration.rb b/lib/shortcode/configuration.rb index 7fa4a2a..00db08e 100644 --- a/lib/shortcode/configuration.rb +++ b/lib/shortcode/configuration.rb @@ -1,8 +1,8 @@ class Shortcode::Configuration - # Sets the template parser to use, supports :erb and :haml, default is :haml + # Sets the template parser to use, supports :erb, :haml, and :slim, default is :haml attr_accessor :template_parser - # Sets the template parser to use, supports :erb and :haml, default is :haml + # Sets the path to search for template files attr_accessor :template_path # Allows templates to be set from strings rather than read from the filesystem diff --git a/lib/shortcode/tag.rb b/lib/shortcode/tag.rb index 1665690..5181e42 100644 --- a/lib/shortcode/tag.rb +++ b/lib/shortcode/tag.rb @@ -29,6 +29,8 @@ def render def render_template case Shortcode.configuration.template_parser + when :slim + Slim::Template.new { markup }.render(self) when :haml Haml::Engine.new(markup, ugly: true).render(binding) when :erb diff --git a/shortcode.gemspec b/shortcode.gemspec index d0eca09..f9e356a 100644 --- a/shortcode.gemspec +++ b/shortcode.gemspec @@ -25,4 +25,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rake" spec.add_development_dependency "rspec" spec.add_development_dependency "coveralls" + spec.add_development_dependency "slim", "~> 2.0" end diff --git a/spec/rails_helpers_spec.rb b/spec/rails_helpers_spec.rb index 9aa784e..62e8cd1 100644 --- a/spec/rails_helpers_spec.rb +++ b/spec/rails_helpers_spec.rb @@ -3,9 +3,25 @@ describe "rails helpers" do let(:template) { load_fixture :rails_helper } + let(:slim_output) { load_fixture :rails_helper_output_slim, :html } let(:haml_output) { load_fixture :rails_helper_output_haml, :html } let(:erb_output) { load_fixture :rails_helper_output_erb, :html } + describe "slim" do + + before(:each) do + Shortcode.setup do |config| + config.template_parser = :slim + config.template_path = File.join File.dirname(__FILE__), "support/templates/slim" + end + end + + it "are accessible within slim templates" do + Shortcode.process(template).gsub("\n",'').should == slim_output.gsub("\n",'') + end + + end + describe "haml" do it "are accessible within haml templates" do diff --git a/spec/support/fixtures/rails_helper_output_slim.html b/spec/support/fixtures/rails_helper_output_slim.html new file mode 100644 index 0000000..33a436c --- /dev/null +++ b/spec/support/fixtures/rails_helper_output_slim.html @@ -0,0 +1 @@ +blah diff --git a/spec/support/templates/slim/rails_helper.html.slim b/spec/support/templates/slim/rails_helper.html.slim new file mode 100644 index 0000000..3068f00 --- /dev/null +++ b/spec/support/templates/slim/rails_helper.html.slim @@ -0,0 +1 @@ += content_tag :i, @content