mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-19 14:26:14 -04:00
Quick links to crop pages in Markdown.
This commit is contained in:
31
lib/haml/filters/growstuff_markdown.rb
Normal file
31
lib/haml/filters/growstuff_markdown.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
require 'bluecloth'
|
||||
include ActionDispatch::Routing
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
module Haml::Filters
|
||||
module GrowstuffMarkdown
|
||||
include Haml::Filters::Base
|
||||
|
||||
def render(text)
|
||||
bc = BlueCloth.new(text)
|
||||
orig = bc.text
|
||||
|
||||
# turn [tomato](crop) into [tomato](http://growstuff.org/crops/tomato)
|
||||
return orig.gsub(/\[(.*?)\]\(crop\)/) do |m|
|
||||
crop_str = $1
|
||||
crop = Crop.find_by_system_name(crop_str)
|
||||
if crop
|
||||
url = url_for(crop_path(crop))
|
||||
"[#{crop_str}](#{url})"
|
||||
else
|
||||
crop_str
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Register it as the handler for the :growstuff_markdown HAML command.
|
||||
# The automatic system gives us :growstuffmarkdown, which is ugly.
|
||||
defined['growstuff_markdown'] = GrowstuffMarkdown
|
||||
|
||||
end
|
||||
39
spec/lib/haml/filters/growstuff_markdown_spec.rb
Normal file
39
spec/lib/haml/filters/growstuff_markdown_spec.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
require 'spec_helper'
|
||||
require 'haml/filters'
|
||||
require 'haml/filters/growstuff_markdown'
|
||||
# require 'haml/helpers'
|
||||
|
||||
def input_link(name)
|
||||
return "[#{name}](crop)"
|
||||
end
|
||||
|
||||
def output_link(crop)
|
||||
return "[#{crop.system_name}](#{crop_path(crop)})"
|
||||
end
|
||||
|
||||
describe 'Haml::Filters::Growstuff_Markdown' do
|
||||
it 'is registered as the handler for :growstuff_markdown' do
|
||||
Haml::Filters::defined['growstuff_markdown'].should ==
|
||||
Haml::Filters::GrowstuffMarkdown
|
||||
end
|
||||
|
||||
it 'converts quick crop links' do
|
||||
@crop = FactoryGirl.create(:crop)
|
||||
rendered = Haml::Filters::GrowstuffMarkdown.render(input_link(@crop.system_name))
|
||||
rendered.should eq output_link(@crop)
|
||||
end
|
||||
|
||||
it "doesn't convert nonexistent crops" do
|
||||
rendered = Haml::Filters::GrowstuffMarkdown.render(input_link("not a crop"))
|
||||
rendered.should eq "not a crop"
|
||||
end
|
||||
|
||||
it "handles multiple crop links" do
|
||||
tomato = FactoryGirl.create(:tomato)
|
||||
maize = FactoryGirl.create(:maize)
|
||||
string = "#{input_link(tomato)} #{input_link(maize)}"
|
||||
rendered = Haml::Filters::GrowstuffMarkdown.render(string)
|
||||
rendered.should eq "#{output_link(tomato)} #{output_link(maize)}"
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user