Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First release of markerb.

  • Loading branch information...
commit e14611893f1cec097e654aa092b297dd880f411c 0 parents
@josevalim josevalim authored
2  .gitignore
@@ -0,0 +1,2 @@
+.bundle
+test/tmp
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+gem "rails", "3.0.7"
+gemspec
79 Gemfile.lock
@@ -0,0 +1,79 @@
+PATH
+ remote: .
+ specs:
+ markerb (0.1.0)
+ redcarpet
+
+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)
+ 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)
+ 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)
+ bundler (~> 1.0)
+ railties (= 3.0.7)
+ railties (3.0.7)
+ actionpack (= 3.0.7)
+ activesupport (= 3.0.7)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
+ rake (0.8.7)
+ redcarpet (1.11.2)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.26)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ markerb!
+ rails (= 3.0.7)
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright 2010 PlataformaTec (blog.plataformatec.com.br)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 README.rdoc
@@ -0,0 +1,33 @@
+= Markerb
+
+Markerb allows you to render multipart e-mails from a single template. The template is written in Markdown, which is delivered as a text part, but also rendered and delivered as an HTML part.
+
+The usage is quite simple. Assuming you have a notifier as below:
+
+ class Notifier < ActionMailer::Base
+ def contact(recipient)
+ @recipient = recipient
+ mail(:to => @recipient, :from => "john.doe@example.com") do |format|
+ format.text
+ format.html
+ end
+ end
+ end
+
+If you create a template at +app/views/notifier/contact.markerb+:
+
+ Multipart templates **rocks**, right <%= @recipient %>?!
+
+It will generate two parts, one in text and another in html when delivered. Before you go, here are a few things you might need to know:
+
+* The "contact.markerb" template should not have a format in its name. Adding a format would make it unavailable to be rendered in different formats;
+
+* The order of the parts matter. It is important you call +format.text+ before you call +format.html+;
+
+* Notice you can normally use ERb inside the template.
+
+Enjoy!
+
+== Copyright and License
+
+Created by the fine folks at PlataformaTec under the MIT-LICENSE (please check MIT-LICENSE file for more info).
29 Rakefile
@@ -0,0 +1,29 @@
+# encoding: UTF-8
+require 'rubygems'
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+
+require 'rake'
+require 'rake/rdoctask'
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+task :default => :test
+
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Handlers'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
19 lib/generators/markerb/mailer/mailer_generator.rb
@@ -0,0 +1,19 @@
+require "rails/generators/erb/mailer/mailer_generator"
+
+module Markerb
+ module Generators
+ class MailerGenerator < Erb::Generators::MailerGenerator
+ source_root File.expand_path("../templates", __FILE__)
+
+ protected
+
+ def format
+ nil # Our templates have no format
+ end
+
+ def handler
+ :markerb
+ end
+ end
+ end
+end
3  lib/generators/markerb/mailer/templates/view.markerb
@@ -0,0 +1,3 @@
+<%= class_name %>#<%= @action %>
+
+<%%= @greeting %>, find me in app/views/<%= @path %>
25 lib/markerb.rb
@@ -0,0 +1,25 @@
+require "action_view/template"
+require "redcarpet"
+require "markerb/railtie"
+
+module Markerb
+ mattr_accessor :processing_options
+ @@processing_options = []
+
+ class Handler
+ def erb_handler
+ @erb_handler ||= ActionView::Template.registered_template_handler(:erb)
+ end
+
+ def call(template)
+ compiled_source = erb_handler.call(template)
+ if template.formats.include?(:html)
+ "Redcarpet.new(begin;#{compiled_source};end, *Markerb.processing_options).to_html"
+ else
+ compiled_source
+ end
+ end
+ end
+end
+
+ActionView::Template.register_template_handler :markerb, Markerb::Handler.new
6 lib/markerb/railtie.rb
@@ -0,0 +1,6 @@
+module Markerb
+ class Railtie < ::Rails::Railtie
+ config.markerb = Markerb
+ config.app_generators.mailer :template_engine => :markerb
+ end
+end
3  lib/markerb/version.rb
@@ -0,0 +1,3 @@
+module Markerb
+ VERSION = "0.1.0"
+end
21 markerb.gemspec
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "markerb/version"
+
+Gem::Specification.new do |s|
+ s.name = "markerb"
+ s.version = Markerb::VERSION.dup
+ s.platform = Gem::Platform::RUBY
+ s.summary = "Multipart templates made easy with Markdown + ERb"
+ s.email = "contact@plataformatec.com.br"
+ s.homepage = "http://github.com/plataformatec/markerb"
+ s.description = "Multipart templates made easy with Markdown + ERb"
+ s.authors = ['José Valim']
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency "redcarpet"
+end
16 test/generator_test.rb
@@ -0,0 +1,16 @@
+require "test_helper"
+require "generators/markerb/mailer/mailer_generator"
+
+class GeneratorTest < Rails::Generators::TestCase
+ tests Markerb::Generators::MailerGenerator
+ destination File.expand_path("../tmp", __FILE__)
+ setup :prepare_destination
+
+ test "assert all views are properly created with given name" do
+ run_generator %w(notifier foo bar baz)
+
+ assert_file "app/views/notifier/foo.markerb"
+ assert_file "app/views/notifier/bar.markerb"
+ assert_file "app/views/notifier/baz.markerb"
+ end
+end
29 test/markerb_test.rb
@@ -0,0 +1,29 @@
+require "test_helper"
+
+class Notifier < ActionMailer::Base
+ self.view_paths = File.expand_path("../views", __FILE__)
+
+ layout false
+
+ def contact(recipient)
+ @recipient = recipient
+ mail(:to => @recipient, :from => "john.doe@example.com") do |format|
+ format.text
+ format.html
+ end
+ end
+end
+
+class MarkerbTest < ActiveSupport::TestCase
+ test 'dual template with .merb' do
+ email = Notifier.contact("you@example.com")
+ 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 "text/html", email.parts[1].mime_type
+ assert_equal "<p>Dual templates <strong>rocks</strong>!</p>",
+ email.parts[1].body.encoded.strip
+ end
+end
17 test/test_helper.rb
@@ -0,0 +1,17 @@
+require "rubygems"
+require "bundler"
+Bundler.setup
+
+require "test/unit"
+require "active_support/test_case"
+
+require "action_mailer"
+require "rails/railtie"
+require "rails/generators"
+require "rails/generators/test_case"
+
+$:.unshift File.expand_path("../../lib", __FILE__)
+require "markerb"
+
+ActionMailer::Base.delivery_method = :test
+ActionMailer::Base.perform_deliveries = true
1  test/views/notifier/contact.markerb
@@ -0,0 +1 @@
+Dual templates **rocks**!
Please sign in to comment.
Something went wrong with that request. Please try again.