Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 70c77dc188b977dfea44285ac63cb1311c2a853b @rohit committed Jun 10, 2010
Showing with 359 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +20 −0 MIT-LICENSE
  3. +78 −0 README
  4. +23 −0 Rakefile
  5. +1 −0 init.rb
  6. +5 −0 lib/prarupa.rb
  7. +92 −0 lib/prarupa/text_helpers.rb
  8. +3 −0 lib/prarupa/version.rb
  9. +17 −0 prarupa.gemspec
  10. +101 −0 test/prarupa/text_helpers_test.rb
  11. +17 −0 test/test_helper.rb
2 .gitignore
@@ -0,0 +1,2 @@
+doc
+*.gem
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 [name of plugin creator]
+
+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.
78 README
@@ -0,0 +1,78 @@
+Prarupa
+=======
+
+Prarupa is plugin for Rails 3 that provides the 'textilize', 'textilize_without_paragraph' and 'markdown' helpers. These were extracted from Rails and have been modified slightly. Instead of using 'BlueCloth' for 'markdown', Prarupa uses 'RDiscount' which is supposed to be faster and better maintained. Other minor internal tweaks have been made.
+
+Install
+=======
+
+You can either install Prarupa as a gem or plugin. Installing as a gem is the preferred choice.
+
+Install as a gem
+----------------
+
+Simply add 'prarupa' to the Gemfile. Also add 'RedCloth' for textilize & textilize_without_paragraph and/or add 'rdiscount' for markdown. Now just 'bundle install' and you should be good to go. Note that installing 'prarupa' will also install 'rdiscount' and 'markdown' as dependencies.
+
+Install as a plugin
+-------------------
+
+To install as a plugin simply do rails plugin install git://github.com/rohit/prarupa.git
+
+Example
+=======
+
+In your views you can do the following.
+
+The 'textilize' and 'textilize_without_paragraph' helpers
+---------------------------------------------------------
+
+The `textilize` helper accepts a string of [Textile](http://redcloth.org/textile) and optionally one or more options.
+
+Without any options:
+
+ textilize("This is *how* we _textilize_ in Rails!")
+ # => "<p>This is <strong>how</strong> we <em>textilize</em> in Rails!</p>"
+
+With options:
+
+ textilize("This is *how* we <em>textilize</em> in Rails!", :filter_html)
+ # => "<p>This is <strong>how</strong> we &lt;em&gt;textilize&lt;/em&gt; in Rails!</p>"
+
+Note that you can provide multiple options like 'textilize("text", :filter_html, :lite_mode)'
+
+The 'textilize_without_paragraph' is similar to 'textilize', except that the surrounding &lt;p&gt; and &lt;/p&gt; tags are removed. It accepts options just like 'textilize'
+
+ textilize_without_paragraph("This is *how* we _textilize_ in Rails!")
+ # => "This is <strong>how</strong> we <em>textilize</em> in Rails!"
+
+The 'markdown' helper
+---------------------
+
+The `markdown` helper accepts a string of [Markdown](http://daringfireball.net/projects/markdown/) and optionally one or more options.
+
+Without any options:
+
+ markdown("This is *how* we **markdown** in Rails!")
+ # => "<p>This is <em>how</em> we <strong>markdown</strong> in Rails!</p>"
+
+With options:
+
+ markdown("Greedy urls are here! http://rohitarondekar.com", :autolink)
+ # => "<p>Greedy urls are here! <a href=\"http://rohitarondekar.com\">http://rohitarondekar.com</a></p>"
+
+To be ':safe'
+============
+
+Being Rails text helpers, all three methods accept the ':safe' option that tells the helpers that the input text need not be sanitized.
+
+Credits
+=======
+
+[David Trasbo](http://github.com/dtrasbo) for making the awesome (official?) plugin [formatize](http://github.com/dtrasbo/formatize) which extracts the helpers without any modification.
+
+Todo
+====
+
+* Tidy up the rdocs
+
+Copyright (c) 2010 Rohit Arondekar, released under the MIT license
23 Rakefile
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the prarupa plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the prarupa plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Prarupa'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1 init.rb
@@ -0,0 +1 @@
+require File.expand_path('../lib/prarupa', __FILE__)
5 lib/prarupa.rb
@@ -0,0 +1,5 @@
+require 'prarupa/text_helpers'
+
+class ActionView::Base
+ include Prarupa::TextHelpers
+end
92 lib/prarupa/text_helpers.rb
@@ -0,0 +1,92 @@
+module Prarupa
+ module TextHelpers
+ # Returns the text with all the Textile[http://www.textism.com/tools/textile] codes turned into HTML tags.
+ #
+ # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile].
+ # <i>This method requires RedCloth[http://redcloth.org/] to be installed</i>.
+ #
+ # ==== Examples
+ # textilize("*This is Textile!* Rejoice!")
+ # # => "<p><strong>This is Textile!</strong> Rejoice!</p>"
+ #
+ # textilize("I _love_ ROR(Ruby on Rails)!")
+ # # => "<p>I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!</p>"
+ #
+ # textilize("h2. Textile makes markup -easy- simple!")
+ # # => "<h2>Textile makes markup <del>easy</del> simple!</h2>"
+ #
+ # textilize("Visit the Rails website "here":http://www.rubyonrails.org/.)
+ # # => "<p>Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>.</p>"
+ #
+ # textilize("This is worded <strong>strongly</strong>")
+ # # => "<p>This is worded <strong>strongly</strong></p>"
+ #
+ # textilize("This is worded <strong>strongly</strong>", :filter_html)
+ # # => "<p>This is worded &lt;strong&gt;strongly&lt;/strong&gt;</p>"
+ #
+ def textilize(text, *options)
+ options ||= [:hard_breaks]
+ text = sanitize(text) unless text.html_safe? || options.delete(:safe)
+
+ if text.blank?
+ ""
+ else
+ textilized = RedCloth.new(text, options)
+ textilized.to_html.html_safe
+ end
+ end
+
+ # Returns the text with all the Textile codes turned into HTML tags,
+ # but without the bounding <p> tag that RedCloth adds.
+ #
+ # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile].
+ # <i>This method is only available if RedCloth[http://redcloth.org/] is available</i>.
+ #
+ # ==== Examples
+ # textilize_without_paragraph("*This is Textile!* Rejoice!")
+ # # => "<strong>This is Textile!</strong> Rejoice!"
+ #
+ # textilize_without_paragraph("I _love_ ROR(Ruby on Rails)!")
+ # # => "I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!"
+ #
+ # textilize_without_paragraph("h2. Textile makes markup -easy- simple!")
+ # # => "<h2>Textile makes markup <del>easy</del> simple!</h2>"
+ #
+ # textilize_without_paragraph("Visit the Rails website "here":http://www.rubyonrails.org/.)
+ # # => "Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>."
+ def textilize_without_paragraph(text, *options)
+ textiled = textilize(text, *options)
+ if textiled[0..2] == "<p>" then textiled = textiled[3..-1] end
+ if textiled[-4..-1] == "</p>" then textiled = textiled[0..-5] end
+ return textiled
+ end
+
+ # Returns the text with all the Markdown codes turned into HTML tags.
+ # <i>This method requires RDiscount[http://github.com/rtomayko/rdiscount/]
+ # to be available</i>.
+ #
+ # ==== Examples
+ # markdown("We are using __Markdown__ now!")
+ # # => "<p>We are using <strong>Markdown</strong> now!</p>"
+ #
+ # markdown("We like to _write_ `code`, not just _read_ it!")
+ # # => "<p>We like to <em>write</em> <code>code</code>, not just <em>read</em> it!</p>"
+ #
+ # markdown("The [Markdown website](http://daringfireball.net/projects/markdown/) has more information.")
+ # # => "<p>The <a href="http://daringfireball.net/projects/markdown/">Markdown website</a>
+ # # has more information.</p>"
+ #
+ # markdown('![The ROR logo](http://rubyonrails.com/images/rails.png "Ruby on Rails")')
+ # # => '<p><img src="http://rubyonrails.com/images/rails.png" alt="The ROR logo" title="Ruby on Rails" /></p>'
+ def markdown(text, *options)
+ text = sanitize(text) unless text.html_safe? || options.delete(:safe)
+
+ if text.blank?
+ ""
+ else
+ markdowned = RDiscount.new(text, *options)
+ markdowned.to_html.chomp.html_safe #used chomp to remove the \n appended by RDiscount
+ end
+ end
+ end
+end
3 lib/prarupa/version.rb
@@ -0,0 +1,3 @@
+module Prarupa
+ VERSION = '0.1'
+end
17 prarupa.gemspec
@@ -0,0 +1,17 @@
+__BASE__ = File.dirname(__FILE__)
+
+require File.join(__BASE__, 'lib', 'prarupa', 'version')
+
+Gem::Specification.new do |s|
+ s.name = 'prarupa'
+ s.version = Prarupa::VERSION
+ s.author = 'Rohit Arondekar'
+ s.email = 'hello@rohitarondekar.com'
+ s.homepage = 'http://github.com/rohit/prarupa'
+ s.summary = 'Plugin that adds the textilized, textilize_without_paragraph and markdown helpers to Rails 3'
+ s.files = Dir['lib/**/*', 'LICENSE', 'README.md', 'README.md']
+
+ s.add_dependency 'RedCloth', '~> 4.2'
+ s.add_dependency 'rdiscount', '~> 1.6'
+end
+
101 test/prarupa/text_helpers_test.rb
@@ -0,0 +1,101 @@
+require 'test_helper'
+
+class PrarupaTextHelpersTest < ActionView::TestCase
+ tests Prarupa::TextHelpers
+
+ def test_textilize_should_be_html_safe
+ assert textilize("*This is Textile!* Rejoice!").html_safe?
+ end
+
+ def test_textilize
+ assert_equal("<p><strong>This is Textile!</strong> Rejoice!</p>", textilize("*This is Textile!* Rejoice!"))
+ end
+
+ def test_textilize_with_blank
+ assert_equal("", textilize(""))
+ end
+
+ def test_textilize_with_options
+ assert_equal("<p>This is worded &lt;strong&gt;strongly&lt;/strong&gt;</p>", textilize("This is worded <strong>strongly</strong>", :filter_html))
+ end
+
+ def test_textilize_should_sanitize_unsafe_input
+ assert_equal("<p>This is worded <strong>strongly</strong></p>", textilize("This is worded <strong>strongly</strong><script>code!</script>"))
+ end
+
+ def test_textilize_should_not_sanitize_input_if_safe_option
+ assert_equal("<p>This is worded <strong>strongly</strong><script>code!</script></p>", textilize("This is worded <strong>strongly</strong><script>code!</script>", :safe))
+ end
+
+ def test_textilize_should_not_sanitize_safe_input
+ assert_equal("<p>This is worded <strong>strongly</strong><script>code!</script></p>", textilize("This is worded <strong>strongly</strong><script>code!</script>".html_safe))
+ end
+
+ def test_textilize_with_hard_breaks
+ assert_equal("<p>This is one scary world.<br />\n True.</p>", textilize("This is one scary world.\n True."))
+ end
+
+ def test_textilize_without_paragraph_should_be_html_safe
+ textilize_without_paragraph("*This is Textile!* Rejoice!").html_safe?
+ end
+
+ def test_textilize_without_paragraph
+ assert_equal("<strong>This is Textile!</strong> Rejoice!", textilize_without_paragraph("*This is Textile!* Rejoice!"))
+ end
+
+ def test_textilize_without_paragraph_with_blank
+ assert_equal("", textilize_without_paragraph(""))
+ end
+
+ def test_textilize_without_paragraph_with_options
+ assert_equal("This is worded &lt;strong&gt;strongly&lt;/strong&gt;", textilize_without_paragraph("This is worded <strong>strongly</strong>", :filter_html))
+ end
+
+ def test_textilize_without_paragraph_should_sanitize_unsafe_input
+ assert_equal("This is worded <strong>strongly</strong>", textilize_without_paragraph("This is worded <strong>strongly</strong><script>code!</script>"))
+ end
+
+ def test_textilize_without_paragraph_should_not_sanitize_input_if_safe_option
+ assert_equal("This is worded <strong>strongly</strong><script>code!</script>", textilize_without_paragraph("This is worded <strong>strongly</strong><script>code!</script>", :safe))
+ end
+
+ def test_textilize_without_paragraph_should_not_sanitize_safe_input
+ assert_equal("This is worded <strong>strongly</strong><script>code!</script>", textilize_without_paragraph("This is worded <strong>strongly</strong><script>code!</script>".html_safe))
+ end
+
+ def test_textilize_without_paragraph_with_hard_breaks
+ assert_equal("This is one scary world.<br />\n True.", textilize_without_paragraph("This is one scary world.\n True."))
+ end
+
+ def test_markdown_should_be_html_safe
+ assert markdown("We are using __Markdown__ now!").html_safe?
+ end
+
+ def test_markdown
+ assert_equal("<p>We are using <strong>Markdown</strong> now!</p>", markdown("We are using __Markdown__ now!"))
+ end
+
+ def test_markdown_with_blank
+ assert_equal("", markdown(""))
+ end
+
+ def test_markdown_with_options
+ assert_equal("<p>This is worded &lt;strong>strongly&lt;/strong></p>", markdown("This is worded <strong>strongly</strong>", :filter_html))
+ end
+
+ def test_markdown_should_sanitize_unsafe_input
+ assert_equal("<p>This is worded <strong>strongly</strong></p>", markdown("This is worded <strong>strongly</strong><script>code!</script>"))
+ end
+
+ def test_markdown_should_not_sanitize_input_if_safe_option
+ assert_equal("<p>This is worded <strong>strongly</strong><script>code!</script></p>", markdown("This is worded <strong>strongly</strong><script>code!</script>", :safe))
+ end
+
+ def test_markdown_should_not_sanitize_safe_input
+ assert_equal("<p>This is worded <strong>strongly</strong><script>code!</script></p>", markdown("This is worded <strong>strongly</strong><script>code!</script>".html_safe))
+ end
+
+ def test_markdown_with_hard_breaks
+ assert_equal("<p>This is one scary world.</p>\n\n<p>True.</p>", markdown("This is one scary world.\n\nTrue."))
+ end
+end
17 test/test_helper.rb
@@ -0,0 +1,17 @@
+require 'rubygems'
+require 'test/unit'
+require 'active_support/core_ext'
+require 'action_controller'
+require 'prarupa'
+
+begin
+ require 'redcloth'
+rescue LoadError
+ $stderr.puts "Skipping textile helper tests. 'gem install RedCloth' to enable."
+end
+
+begin
+ require 'rdiscount'
+rescue LoadError
+ $stderr.puts "Skipping markdown helper tests. 'gem install rdiscount' to enable."
+end

0 comments on commit 70c77dc

Please sign in to comment.
Something went wrong with that request. Please try again.