Skip to content

Loading…

Fork with a friendlier stripper #11

Closed
wants to merge 5 commits into from

7 participants

@janlimpens

I created a stripper that produces human friendlier output. Are you interested in this?

@jamiew

This should really be in markerb core! Great fork

@lucasmazza lucasmazza commented on the diff
lib/redcarpet/refined_stripper.rb
@@ -0,0 +1,25 @@
+require "redcarpet/render_strip"
+
+module Redcarpet
+ module Render
+
+ class RefinedStripper < StripDown
@lucasmazza Plataformatec member

This class should be named Markerb::TextRenderer or something on that line.

@lucasmazza Plataformatec member

Also, it took me a while to understand what this class does. What do you think about adding some documentation to it explaining that it strips down some markdown tags so the raw text can me more human friendly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@lucasmazza lucasmazza commented on the diff
lib/redcarpet/refined_stripper.rb
@@ -0,0 +1,25 @@
+require "redcarpet/render_strip"
+
+module Redcarpet
+ module Render
+
+ class RefinedStripper < StripDown
+
+ [:block_code, :block_quote, :block_html, :header, :paragraph].each do |method|
+ define_method method do |*args|
+ "\n#{args.first}\n"
+ end
+ end
+
+ def list_item *args
@lucasmazza Plataformatec member

This method definition should have parentheses since it accepts an argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@lucasmazza lucasmazza commented on the diff
lib/markerb.rb
@@ -17,10 +19,11 @@ def call(template)
if template.formats.include?(:html)
"Redcarpet::Markdown.new(Markerb.renderer, Markerb.processing_options).render(begin;#{compiled_source};end).html_safe"
else
- compiled_source
+ "Redcarpet::Markdown.new(Markerb.text_renderer, {}).render(begin;#{compiled_source};end)"
@lucasmazza Plataformatec member

I think you don't need to supply the empty hash here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@lucasmazza
Plataformatec member

Sorry about taking so long to check this one.

The feature looks promising, but we need to get along some bits before it, so I added some comments on the code. Also, github says it can't be automatically merged, so you need to pull the latest changes from master to it before we can merge the PR.

@rafaelfranca

I'm not sure about this change. It override some internal of Redcarpet and since Redcarpet can be deprecated it will this will make harder to change it to something else in the future.

See vmg/redcarpet#212

@rubytastic

Having exact this same issue, can this be merged?

@odigity

Best. Issue Title. Ever.

@janlimpens
@carlosantoniodasilva

Thanks, but I don't think we should have that into markerb. We could, however, have a text renderer option, pretty much as we have the renderer already (which will be in fact html renderer), and allow you to set any renderer you want. This way you could just configure it and have this code in your app, and customize it as you want. I think this is a better path going forward.

I'm giving this a close for now, thanks for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 25, 2012
  1. added stripping renderer for text

    Jan Limpens committed
  2. added better renderer

    Jan Limpens committed
  3. further refining

    Jan Limpens committed
Commits on Oct 31, 2012
  1. test for referenced links

    Jan Limpens committed
  2. test for injections

    Jan Limpens committed
This page is out of date. Refresh to see the latest.
View
3 Gemfile
@@ -1,4 +1,5 @@
source "http://rubygems.org"
-gem "rails", "3.0.7"
+gem "rails"
+
gemspec
View
128 Gemfile.lock
@@ -7,73 +7,87 @@ PATH
GEM
remote: http://rubygems.org/
specs:
- abstract (1.0.0)
- actionmailer (3.0.7)
- actionpack (= 3.0.7)
- mail (~> 2.2.15)
- actionpack (3.0.7)
- activemodel (= 3.0.7)
- activesupport (= 3.0.7)
- builder (~> 2.1.2)
- erubis (~> 2.6.6)
- i18n (~> 0.5.0)
- rack (~> 1.2.1)
- rack-mount (~> 0.6.14)
- rack-test (~> 0.5.7)
- tzinfo (~> 0.3.23)
- activemodel (3.0.7)
- activesupport (= 3.0.7)
- builder (~> 2.1.2)
- i18n (~> 0.5.0)
- activerecord (3.0.7)
- activemodel (= 3.0.7)
- activesupport (= 3.0.7)
- arel (~> 2.0.2)
- tzinfo (~> 0.3.23)
- activeresource (3.0.7)
- activemodel (= 3.0.7)
- activesupport (= 3.0.7)
- activesupport (3.0.7)
- arel (2.0.9)
- builder (2.1.2)
- erubis (2.6.6)
- abstract (>= 1.0.0)
- i18n (0.5.0)
- mail (2.2.18)
- activesupport (>= 2.3.6)
+ actionmailer (3.2.8)
+ actionpack (= 3.2.8)
+ mail (~> 2.4.4)
+ actionpack (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.4)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.3)
+ activemodel (3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ activerecord (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ activesupport (3.2.8)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ arel (3.0.2)
+ builder (3.0.4)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.1)
+ journey (1.0.4)
+ json (1.7.5)
+ mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
- mime-types (1.16)
- polyglot (0.3.1)
- rack (1.2.2)
- rack-mount (0.6.14)
- rack (>= 1.0.0)
- rack-test (0.5.7)
+ mime-types (1.19)
+ multi_json (1.3.6)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.2)
rack (>= 1.0)
- rails (3.0.7)
- actionmailer (= 3.0.7)
- actionpack (= 3.0.7)
- activerecord (= 3.0.7)
- activeresource (= 3.0.7)
- activesupport (= 3.0.7)
+ rails (3.2.8)
+ actionmailer (= 3.2.8)
+ actionpack (= 3.2.8)
+ activerecord (= 3.2.8)
+ activeresource (= 3.2.8)
+ activesupport (= 3.2.8)
bundler (~> 1.0)
- railties (= 3.0.7)
- railties (3.0.7)
- actionpack (= 3.0.7)
- activesupport (= 3.0.7)
+ railties (= 3.2.8)
+ railties (3.2.8)
+ actionpack (= 3.2.8)
+ activesupport (= 3.2.8)
+ rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
- thor (~> 0.14.4)
- rake (0.8.7)
- redcarpet (2.1.0)
- thor (0.14.6)
- treetop (1.4.9)
+ rdoc (~> 3.4)
+ thor (>= 0.14.6, < 2.0)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ redcarpet (2.2.2)
+ sprockets (2.1.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ thor (0.16.0)
+ tilt (1.3.3)
+ treetop (1.4.12)
+ polyglot
polyglot (>= 0.3.1)
- tzinfo (0.3.26)
+ tzinfo (0.3.34)
PLATFORMS
ruby
DEPENDENCIES
markerb!
- rails (= 3.0.7)
+ rails
View
7 lib/markerb.rb
@@ -1,11 +1,13 @@
require "action_view/template"
require "redcarpet"
require "markerb/railtie"
+require "redcarpet/refined_stripper"
module Markerb
- mattr_accessor :processing_options, :renderer
+ mattr_accessor :processing_options, :renderer, :text_renderer
@@processing_options = {}
@@renderer = Redcarpet::Render::HTML
+ @@text_renderer = Redcarpet::Render::RefinedStripper
class Handler
def erb_handler
@@ -17,10 +19,11 @@ def call(template)
if template.formats.include?(:html)
"Redcarpet::Markdown.new(Markerb.renderer, Markerb.processing_options).render(begin;#{compiled_source};end).html_safe"
else
- compiled_source
+ "Redcarpet::Markdown.new(Markerb.text_renderer, {}).render(begin;#{compiled_source};end)"
@lucasmazza Plataformatec member

I think you don't need to supply the empty hash here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
end
end
end
+
ActionView::Template.register_template_handler :markerb, Markerb::Handler.new
View
25 lib/redcarpet/refined_stripper.rb
@@ -0,0 +1,25 @@
+require "redcarpet/render_strip"
+
+module Redcarpet
+ module Render
+
+ class RefinedStripper < StripDown
@lucasmazza Plataformatec member

This class should be named Markerb::TextRenderer or something on that line.

@lucasmazza Plataformatec member

Also, it took me a while to understand what this class does. What do you think about adding some documentation to it explaining that it strips down some markdown tags so the raw text can me more human friendly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ [:block_code, :block_quote, :block_html, :header, :paragraph].each do |method|
+ define_method method do |*args|
+ "\n#{args.first}\n"
+ end
+ end
+
+ def list_item *args
@lucasmazza Plataformatec member

This method definition should have parentheses since it accepts an argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ " - #{args.first}"
+ end
+
+ def link(link, title, content)
+ "#{content}: #{link}"
+ end
+
+ end
+
+ end
+end
View
44 test/markerb_test.rb
@@ -24,17 +24,34 @@ def user(format_type)
end
end
+ def referenced_links
+ mail(:to => 'foo@bar.com', :from => "john.doe@example.com") do |format|
+ format.text { render 'referenced_links' }
+ format.html { render 'referenced_links' }
+ end
+ end
+
def multiple_format_contact(recipient)
@recipient = recipient
mail(:to => @recipient, :from => "john.doe@example.com", :template => "contact") do |format|
- format.text { render 'contact' }
- format.html { render 'contact' }
+ format.text { render 'contact' }
+ format.html { render 'contact' }
+ end
+ end
+
+ def injected
+ @injection = "[Hello][1]" +
+ "\n[1]: http://world.com"
+ mail(:to => 'foo@bar.com', :from => "john.doe@example.com") do |format|
+ format.text { render 'injected' }
+ format.html { render 'injected' }
end
end
end
class TestRenderer < Redcarpet::Render::HTML
attr_accessor :show_text
+
def initialize(render_options = {})
@show_text = render_options.delete(:show_text)
super(render_options)
@@ -60,7 +77,7 @@ class MarkerbTest < ActiveSupport::TestCase
test "plain text should be sent as a plain text" do
email = Notifier.contact("you@example.com", :text)
assert_equal "text/plain", email.mime_type
- assert_equal "Dual templates **rocks**!", email.body.encoded.strip
+ assert_equal "Dual templates rocks!", email.body.encoded.strip
end
test "html should be sent as html" do
@@ -74,11 +91,11 @@ class MarkerbTest < ActiveSupport::TestCase
assert_equal 2, email.parts.size
assert_equal "multipart/alternative", email.mime_type
assert_equal "text/plain", email.parts[0].mime_type
- assert_equal "Dual templates **rocks**!",
- email.parts[0].body.encoded.strip
+ assert_equal "Dual templates rocks!",
+ email.parts[0].body.encoded.strip
assert_equal "text/html", email.parts[1].mime_type
assert_equal "<p>Dual templates <strong>rocks</strong>!</p>",
- email.parts[1].body.encoded.strip
+ email.parts[1].body.encoded.strip
end
test "with a custom renderer" do
@@ -107,4 +124,19 @@ class MarkerbTest < ActiveSupport::TestCase
assert_equal "text/html", email.mime_type
assert_equal '<p>woot! <strong>Partial</strong></p>', email.body.encoded.strip
end
+
+ test 'with referenced links' do
+ email = Notifier.referenced_links
+ assert_equal "Hello world: http://hello.world!", email.parts[0].body.encoded.strip
+ expected_html = "<p>Hello <a href=\"http://hello.world\">world</a>!</p>"
+ got_html = email.parts[1].body.encoded.strip
+ assert_equal expected_html, got_html
+ end
+
+ test 'with injected markdown' do
+ email = Notifier.injected
+ assert_equal "Injected: Hello: http://world.com", email.parts[0].body.encoded.strip
+ assert_equal "<p><strong>Injected:</strong> <a href=\"http://world.com\">Hello</a></p>", email.parts[1].body.encoded.strip
+ end
+
end
View
1 test/views/notifier/injected.markerb
@@ -0,0 +1 @@
+**Injected:** <%= @injection %>
View
3 test/views/notifier/referenced_links.markerb
@@ -0,0 +1,3 @@
+Hello [world][1]!
+
+[1]: http://hello.world
Something went wrong with that request. Please try again.