-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving all the logic from the helper to the Mustdown module (ref #2)
- Loading branch information
Simon Courtois
committed
Jul 30, 2013
1 parent
06e36ee
commit 5b08241
Showing
3 changed files
with
325 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
# Public: Wrapper class for Mustdown. | ||
# | ||
# This module is a Rails helper that provides an easy access to the Mustdown | ||
# rendering methods. | ||
module Mustdown | ||
module MustdownHelper | ||
def markdown(content, markdown_extensions = {}, renderer_options = {}) | ||
md_exts = Mustdown.markdown_extensions.merge(markdown_extensions) | ||
renderer_opts = Mustdown.renderer_options.merge(renderer_options) | ||
|
||
renderer = Mustdown.renderer.new(renderer_opts) | ||
markdown = Redcarpet::Markdown.new(renderer, md_exts) | ||
|
||
markdown.render(content).html_safe | ||
# Public: Calls Mustdown.markdown. | ||
# | ||
# See Mustdown.markdown. | ||
def markdown(*args) | ||
Mustdown.markdown(*args).html_safe | ||
end | ||
|
||
def mustache(template, resource) | ||
Mustache.render(template, resource).html_safe | ||
# Public: Calls Mustdown.mustache. | ||
# | ||
# See Mustdown.mustache. | ||
def mustache(*args) | ||
Mustdown.mustache(*args).html_safe | ||
end | ||
|
||
def mustdown(template, resource, *markdown_args) | ||
markdown mustache(template, resource), *markdown_args | ||
# Public: Calls Mustdown.mustdown. | ||
# | ||
# See Mustdown.mustdown. | ||
def mustdown(*args) | ||
Mustdown.mustdown(*args).html_safe | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
require 'spec_helper' | ||
|
||
class MustdownIncluder | ||
include Mustdown | ||
end | ||
|
||
shared_examples_for "Mustdown" do | ||
let(:binding_object) do | ||
{ name: "test" } | ||
end | ||
|
||
describe "configure" do | ||
it "yields with subject as argument" do | ||
subject.configure { |c| c.markdown_extensions = "configure" } | ||
subject.markdown_extensions.should eq("configure") | ||
end | ||
end | ||
|
||
describe "markdown_extensions=" do | ||
it "sets the markdown extensions of subject" do | ||
subject.markdown_extensions = "extensions" | ||
subject.markdown_extensions.should eq("extensions") | ||
end | ||
end | ||
|
||
describe "renderer_options=" do | ||
it "sets the markdown renderer options of subject" do | ||
subject.renderer_options = "options" | ||
subject.renderer_options.should eq("options") | ||
end | ||
end | ||
|
||
describe "markdown_renderer=" do | ||
it "sets the markdown renderer of subject" do | ||
subject.markdown_renderer = "md renderer" | ||
subject.markdown_renderer.should eq("md renderer") | ||
end | ||
end | ||
|
||
describe "markdown" do | ||
it "process a template into HTML" do | ||
output = subject.markdown('# Test') | ||
output.should eq("<h1>Test</h1>\n") | ||
end | ||
|
||
it "uses configured renderer class" do | ||
rndr_class = Class.new(Redcarpet::Render::HTML) do | ||
def header(*args); super; end | ||
end | ||
|
||
rndr = rndr_class.new | ||
rndr_class.stub(:new).and_return(rndr) | ||
|
||
subject.markdown_renderer = rndr_class | ||
|
||
rndr.should_receive(:header) | ||
subject.markdown('# Test') | ||
end | ||
|
||
context "when overriding autolink to true" do | ||
it "generates autolinks" do | ||
output = subject.markdown("http://test.com", { autolink: true }) | ||
output.should match(/<a /) | ||
end | ||
end | ||
|
||
context "when overriding autolink to false" do | ||
it "doesn't generate any autolink" do | ||
output = subject.markdown("http://test.com", { autolink: false }) | ||
output.should_not match(/<a /) | ||
end | ||
end | ||
|
||
context "when overriding no_links to true" do | ||
it "doesn't generate any link" do | ||
output = subject.markdown("[a](test)", {}, { no_links: true }) | ||
output.should_not match(/<a /) | ||
end | ||
end | ||
|
||
context "when overriding no_links to false" do | ||
it "doesn't generate any link" do | ||
output = subject.markdown("[a](test)", {}, { no_links: false }) | ||
output.should match(/<a /) | ||
end | ||
end | ||
end | ||
|
||
describe "mustache" do | ||
it "passes the binding object to the template" do | ||
output = subject.mustache("{{name}}", binding_object) | ||
output.should eq("test") | ||
end | ||
end | ||
|
||
describe "mustdown" do | ||
it "renders mustache and then markdown" do | ||
output = subject.mustdown("# {{name}}", binding_object) | ||
output.should eq("<h1>test</h1>\n") | ||
end | ||
end | ||
end | ||
|
||
describe Mustdown do | ||
subject { Mustdown.dup } | ||
it_behaves_like "Mustdown" | ||
end | ||
|
||
describe MustdownIncluder do | ||
subject { MustdownIncluder.new } | ||
it_behaves_like "Mustdown" | ||
end |