markdown and textile filters (+ specs)

did committed Feb 01, 2015
commit fb4d16a149949d30e541c268453c4c4386575ca3
Showing 10 changed files with 192 additions and 49 deletions
Gemfile.lock +2 -0
@@ @@ -2,6 +2,7 @@ PATH
remote: .
specs:
locomotivecms_steam (0.1.2.pre.beta)
+ RedCloth (~> 4.2.9)
activesupport (~> 4.2.0)
coffee-script (~> 2.2.0)
compass (~> 1.0.3)
@@ @@ -26,6 +27,7 @@ PATH
GEM
remote: https://rubygems.org/
specs:
+ RedCloth (4.2.9)
actionpack (4.2.0)
actionview (= 4.2.0)
activesupport (= 4.2.0)
locomotive/steam/liquid/filters/text.rb b/lib/locomotive/steam/liquid/filters/text.rb +39 -37
@@ @@ -1,53 +1,55 @@
module Locomotive
- module Liquid
- module Filters
- module Text
+ module Steam
+ module Liquid
+ module Filters
+ module Text
- def underscore(input)
- input.to_s.gsub(' ', '_').gsub('/', '_').underscore
- end
+ def underscore(input)
+ input.to_s.gsub(' ', '_').gsub('/', '_').underscore
+ end
- def dasherize(input)
- input.to_s.gsub(' ', '-').gsub('/', '-').dasherize
- end
+ def dasherize(input)
+ input.to_s.gsub(' ', '-').gsub('/', '-').dasherize
+ end
- def encode(input)
- Rack::Utils.escape(input)
- end
+ def encode(input)
+ Rack::Utils.escape(input)
+ end
- # alias newline_to_br
- def multi_line(input)
- input.to_s.gsub("\n", '<br/>')
- end
+ # alias newline_to_br
+ def multi_line(input)
+ input.to_s.gsub("\n", '<br/>')
+ end
- def concat(input, *args)
- result = input.to_s
- args.flatten.each { |a| result << a.to_s }
- result
- end
+ def concat(input, *args)
+ result = input.to_s
+ args.flatten.each { |a| result << a.to_s }
+ result
+ end
- # right justify and padd a string
- def rjust(input, integer, padstr = '')
- input.to_s.rjust(integer, padstr)
- end
+ # right justify and padd a string
+ def rjust(input, integer, padstr = '')
+ input.to_s.rjust(integer, padstr)
+ end
- # left justify and padd a string
- def ljust(input, integer, padstr = '')
- input.to_s.ljust(integer, padstr)
- end
+ # left justify and padd a string
+ def ljust(input, integer, padstr = '')
+ input.to_s.ljust(integer, padstr)
+ end
- def textile(input)
- ::RedCloth.new(input).to_html
- end
+ def textile(input)
+ @context.registers[:services].textile.to_html(input)
+ end
- def markdown(input)
- Locomotive::Markdown.render(input)
- end
+ def markdown(input)
+ @context.registers[:services].markdown.to_html(input)
+ end
- end
+ end
- ::Liquid::Template.register_filter(Text)
+ ::Liquid::Template.register_filter(Text)
+ end
end
end
end
locomotive/steam/services.rb b/lib/locomotive/steam/services.rb +8 -0
@@ @@ -32,6 +32,14 @@ module Locomotive
Services::ImageResizer.new(::Dragonfly.app(:steam), configuration.assets_path)
end
+ register :markdown do
+ Services::Markdown.new
+ end
+
+ register :textile do
+ Services::Textile.new
+ end
+
register :configuration do
Locomotive::Steam.configuration
end
locomotive/steam/services/markdown.rb b/lib/locomotive/steam/services/markdown.rb +6 -7
@@ @@ -3,18 +3,17 @@ require 'kramdown'
module Locomotive
module Steam
module Services
+
class Markdown
- def render(text)
- self.class.parser.render(text)
- end
+ def to_html(text)
+ return '' if text.blank?
- # http://kramdown.gettalong.org/options.html
- def self.parser
- @@markdown ||= Kramdown::Document.new(text).to_html
+ Kramdown::Document.new(text, auto_ids: false).to_html
end
end
+
end
end
- end
\ No newline at end of file
+ end
locomotive/steam/services/textile.rb b/lib/locomotive/steam/services/textile.rb +19 -0
@@ @@ -0,0 +1,19 @@
+ require 'RedCloth'
+
+ module Locomotive
+ module Steam
+ module Services
+
+ class Textile
+
+ def to_html(text)
+ return '' if text.blank?
+
+ ::RedCloth.new(text).to_html
+ end
+
+ end
+
+ end
+ end
+ end
locomotivecms_steam.gemspec +1 -0
@@ @@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'dragonfly', '~> 1.0.7'
spec.add_dependency 'kaminari', '~> 0.16.2'
spec.add_dependency 'kramdown', '~> 1.5.0'
+ spec.add_dependency 'RedCloth', '~> 4.2.9'
spec.add_dependency 'coffee-script', '~> 2.2.0'
spec.add_dependency 'haml', '~> 4.0.6'
spec.add_dependency 'compass', '~> 1.0.3'
spec/unit/liquid/filters/text_spec.rb +41 -0
@@ @@ -0,0 +1,41 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Liquid::Filters::Text do
+
+ include Locomotive::Steam::Liquid::Filters::Text
+
+ let(:services) { Locomotive::Steam::Services.instance(nil) }
+ let(:context) { instance_double('Context', registers: { services: services }) }
+
+ before { @context = context }
+
+ it 'transforms a textile input into HTML' do
+ expect(textile('This is *my* text.')).to eq "<p>This is <strong>my</strong> text.</p>"
+ end
+
+ it 'transforms a markdown input into HTML' do
+ expect(markdown('# My title')).to eq "<h1>My title</h1>\n"
+ end
+
+ it 'underscores an input' do
+ expect(underscore('foo')).to eq 'foo'
+ expect(underscore('home page')).to eq 'home_page'
+ expect(underscore('My foo Bar')).to eq 'my_foo_bar'
+ expect(underscore('foo/bar')).to eq 'foo_bar'
+ expect(underscore('foo/bar/index')).to eq 'foo_bar_index'
+ end
+
+ it 'dasherizes an input' do
+ expect(dasherize('foo')).to eq 'foo'
+ expect(dasherize('foo_bar')).to eq 'foo-bar'
+ expect(dasherize('foo/bar')).to eq 'foo-bar'
+ expect(dasherize('foo/bar/index')).to eq 'foo-bar-index'
+ end
+
+ it 'concats strings' do
+ expect(concat('foo', 'bar')).to eq 'foobar'
+ expect(concat('hello', 'foo', 'bar')).to eq 'hellofoobar'
+ end
+
+
+ end
spec/unit/loaders/utils/localized_tree_spec.rb +6 -5
@@ @@ -3,18 +3,19 @@ require 'spec_helper'
describe Locomotive::Steam::Utils::LocalizedTree do
describe '#to_hash' do
+
let(:tree) { [ 'a.fr.haml', 'a.haml', 'b.xml', 'b.fr.haml', 'z.txt' ] }
let(:extensions) { ['haml','xml'] }
- subject { Locomotive::Steam::Utils::LocalizedTree.new(tree, extensions).to_hash }
+ subject { Locomotive::Steam::Utils::LocalizedTree.new(tree, extensions).to_hash }
- it { should be_kind_of Hash }
+ it { is_expected.to be_kind_of Hash }
it 'has root as key' do
- subject.keys.should eq ['a', 'b']
+ expect(subject.keys).to eq ['a', 'b']
end
- it { should eql ({
+ it { is_expected.to eql ({
'a' => {fr: 'a.fr.haml', default: 'a.haml'},
'b' => {default: 'b.xml', fr: 'b.fr.haml'}
})
@@ @@ -22,7 +23,7 @@ describe Locomotive::Steam::Utils::LocalizedTree do
context 'multiple extensions' do
let(:tree) { [ 'a.fr.haml.xml', 'a.haml', 'b.xml.haml', 'b.fr.haml'] }
- it { should eql ({
+ it { is_expected.to eql ({
'a' => {fr: 'a.fr.haml.xml', default: 'a.haml'},
'b' => {default: 'b.xml.haml', fr: 'b.fr.haml'}
})
spec/unit/services/markdown_spec.rb +36 -0
@@ @@ -0,0 +1,36 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Services::Markdown do
+
+ let(:service) { Locomotive::Steam::Services::Markdown.new }
+
+ describe '#to_html' do
+
+ let(:text) { <<-EOF
+ First level header
+ ==================
+
+ Second level header
+ -------------------
+ EOF
+ }
+
+ subject { service.to_html(text) }
+
+ it do
+ is_expected.to eq <<-EOF
+ <h1>First level header</h1>
+
+ <h2>Second level header</h2>
+ EOF
+ end
+
+ describe 'no text' do
+
+ let(:text) { nil }
+ it { is_expected.to eq '' }
+ end
+
+ end
+
+ end
spec/unit/services/textile_spec.rb +34 -0
@@ @@ -0,0 +1,34 @@
+ require 'spec_helper'
+
+ describe Locomotive::Steam::Services::Textile do
+
+ let(:service) { Locomotive::Steam::Services::Textile.new }
+
+ describe '#to_html' do
+
+ let(:text) { <<-EOF
+ h1. Give RedCloth a try!
+
+ A *simple* paragraph
+ EOF
+ }
+
+ subject { service.to_html(text) }
+
+ it do
+ is_expected.to eq <<-EOF
+ <h1>Give RedCloth a try!</h1>
+ <p>A <strong>simple</strong> paragraph</p>
+ EOF
+ .strip
+ end
+
+ describe 'no text' do
+
+ let(:text) { nil }
+ it { is_expected.to eq '' }
+ end
+
+ end
+
+ end