lib/typogruby.rb in typogruby-1.0.9 vs lib/typogruby.rb in typogruby-1.0.10
- old
+ new
@@ -4,62 +4,56 @@
# A collection of simple helpers for improving web
# typograhy. Based on TypographyHelper by Luke Hartman and Typogrify.
#
# @example Using all filters
-# require 'typogruby'
-# Typogruby.improve('my text')
+# require 'typogruby'
+# Typogruby.improve('my text')
#
# @example Using a single filter
-# Typogruby.initial_quotes('my text')
+# Typogruby.initial_quotes('my text')
#
# @see http://github.com/hunter/typography-helper
# @see http://code.google.com/p/typogrify
# @author Arjan van der Gaag <arjan.vandergaag@gmail.com>
module Typogruby
- # Get the current gem version number
- # @return [String]
- def version
- File.read(File.join(File.dirname(__FILE__), *%w{.. VERSION}))
- end
-
# Applies smartypants to a given piece of text
#
# @example
- # smartypants('The "Green" man')
- # # => 'The “Green” man'
+ # smartypants('The "Green" man')
+ # # => 'The “Green” man'
#
+ # @see https://rubygems.org/gems/rubypants
# @param [String] text input text
# @return [String] input text with smartypants applied
def smartypants(text)
::RubyPants.new(text).to_html
end
# converts a & surrounded by optional whitespace or a non-breaking space
# to the HTML entity and surrounds it in a span with a styled class.
#
# @example
- # amp('One & two')
- # # => 'One <span class="amp">&</span> two'
- # amp('One & two')
- # # => 'One <span class="amp">&</span> two'
- # amp('One & two')
- # # => 'One <span class="amp">&</span> two'
- # amp('One & two')
- # # => 'One <span class="amp">&</span> two'
+ # amp('One & two')
+ # # => 'One <span class="amp">&</span> two'
+ # amp('One & two')
+ # # => 'One <span class="amp">&</span> two'
+ # amp('One & two')
+ # # => 'One <span class="amp">&</span> two'
+ # amp('One & two')
+ # # => 'One <span class="amp">&</span> two'
#
# @example It won't mess up & that are already wrapped, in entities or URLs
+ # amp('One <span class="amp">&</span> two')
+ # # => 'One <span class="amp">&</span> two'
+ # amp('“this” & <a href="/?that&test">that</a>')
+ # # => '“this” <span class="amp">&</span> <a href="/?that&test">that</a>'
#
- # amp('One <span class="amp">&</span> two')
- # # => 'One <span class="amp">&</span> two'
- # amp('“this” & <a href="/?that&test">that</a>')
- # # => '“this” <span class="amp">&</span> <a href="/?that&test">that</a>'
- #
# @example It should ignore standalone amps that are in attributes
- # amp('<link href="xyz.html" title="One & Two">xyz</link>')
- # # => '<link href="xyz.html" title="One & Two">xyz</link>'
+ # amp('<link href="xyz.html" title="One & Two">xyz</link>')
+ # # => '<link href="xyz.html" title="One & Two">xyz</link>'
#
# @param [String] text input text
# @return [String] input text with ampersands wrapped
def amp(text)
# $1 is an excluded HTML tag, $2 is the part before the caps and $3 is the amp match
@@ -74,42 +68,42 @@
# before an optional closing element (<tt>a</tt>, <tt>em</tt>,
# <tt>span</tt>, strong) before a closing tag (<tt>p</tt>, <tt>h[1-6]</tt>,
# <tt>li</tt>, <tt>dt</tt>, <tt>dd</tt>) or the end of the string.
#
# @example
- # > widont('A very simple test')
- # # => 'A very simple test'
+ # widont('A very simple test')
+ # # => 'A very simple test'
#
# @example Single word items shouldn't be changed
- # widont('Test')
- # # => 'Test'
- # widont(' Test')
- # # => ' Test'
- # widont('<ul><li>Test</p></li><ul>')
- # # => '<ul><li>Test</p></li><ul>'
- # widont('<ul><li> Test</p></li><ul>')
- # # => '<ul><li> Test</p></li><ul>'
+ # widont('Test')
+ # # => 'Test'
+ # widont(' Test')
+ # # => ' Test'
+ # widont('<ul><li>Test</p></li><ul>')
+ # # => '<ul><li>Test</p></li><ul>'
+ # widont('<ul><li> Test</p></li><ul>')
+ # # => '<ul><li> Test</p></li><ul>'
#
# @example Nested tags
- # widont('<p>In a couple of paragraphs</p><p>paragraph two</p>')
- # # => '<p>In a couple of paragraphs</p><p>paragraph two</p>'
- # widont('<h1><a href="#">In a link inside a heading</i> </a></h1>')
- # # => '<h1><a href="#">In a link inside a heading</i> </a></h1>'
- # widont('<h1><a href="#">In a link</a> followed by other text</h1>')
- # # => '<h1><a href="#">In a link</a> followed by other text</h1>'
+ # widont('<p>In a couple of paragraphs</p><p>paragraph two</p>')
+ # # => '<p>In a couple of paragraphs</p><p>paragraph two</p>'
+ # widont('<h1><a href="#">In a link inside a heading</i> </a></h1>')
+ # # => '<h1><a href="#">In a link inside a heading</i> </a></h1>'
+ # widont('<h1><a href="#">In a link</a> followed by other text</h1>')
+ # # => '<h1><a href="#">In a link</a> followed by other text</h1>'
#
# @example Empty HTMLs shouldn't error
- # widont('<h1><a href="#"></a></h1>')
- # # => '<h1><a href="#"></a></h1>'
+ # widont('<h1><a href="#"></a></h1>')
+ # # => '<h1><a href="#"></a></h1>'
#
# @example Excluded tags
- # widont('<div>Divs get no love!</div>')
- # # => '<div>Divs get no love!</div>'
- # widont('<pre>Neither do PREs</pre>')
- # # => '<pre>Neither do PREs</pre>'
- # widont('<div><p>But divs with paragraphs do!</p></div>')
- # # => '<div><p>But divs with paragraphs do!</p></div>'
+ # widont('<div>Divs get no love!</div>')
+ # # => '<div>Divs get no love!</div>'
+ # widont('<pre>Neither do PREs</pre>')
+ # # => '<pre>Neither do PREs</pre>'
+ # widont('<div><p>But divs with paragraphs do!</p></div>')
+ # # => '<div><p>But divs with paragraphs do!</p></div>'
#
# @see http://mucur.name/posts/widon-t-and-smartypants-helpers-for-rails
# @see http://shauninman.com/archive/2006/08/22/widont_wordpress_plugin
# @param [String] text input text
# @return [String] input text with non-breaking spaces inserted
@@ -128,26 +122,26 @@
# surrounds two or more consecutive captial letters, perhaps with interspersed digits and periods
# in a span with a styled class.
#
# @example
- # caps("A message from KU")
- # # => 'A message from <span class="caps">KU</span>'
+ # caps("A message from KU")
+ # # => 'A message from <span class="caps">KU</span>'
#
# @example Allows digits
- # caps("A message from 2KU2 with digits")
- # # => 'A message from <span class="caps">2KU2</span> with digits'
+ # caps("A message from 2KU2 with digits")
+ # # => 'A message from <span class="caps">2KU2</span> with digits'
#
# @example Ignores HTML attributes
- # caps('Download <a href="file.doc" title="PDF document">this file</a>')
- # # => 'Download <a href="file.doc" title="PDF document">this file</a>'
+ # caps('Download <a href="file.doc" title="PDF document">this file</a>')
+ # # => 'Download <a href="file.doc" title="PDF document">this file</a>'
#
# @example All caps with with apostrophes in them shouldn't break. Only handles dump apostrophes though.
- # caps("JIMMY'S")
- # # => '<span class="caps">JIMMY\\'S</span>'
- # caps("<i>D.O.T.</i>HE34T<b>RFID</b>")
- # # => '<i><span class="caps">D.O.T.</span></i><span class="caps">HE34T</span><b><span class="caps">RFID</span></b>'
+ # caps("JIMMY'S")
+ # # => '<span class="caps">JIMMY\\'S</span>'
+ # caps("<i>D.O.T.</i>HE34T<b>RFID</b>")
+ # # => '<i><span class="caps">D.O.T.</span></i><span class="caps">HE34T</span><b><span class="caps">RFID</span></b>'
#
# @param [String] text input text
# @return [String] input text with caps wrapped
def caps(text)
ignore_scripts(text) do |t|
@@ -177,22 +171,22 @@
# encloses initial single or double quote, or their entities
# (optionally preceeded by a block element and perhaps an inline element)
# with a span that can be styled.
#
# @example
- # initial_quotes('"With primes"')
- # # => '<span class="dquo">"</span>With primes"'
- # initial_quotes("'With single primes'")
- # # => '<span class="quo">\\'</span>With single primes\\''
+ # initial_quotes('"With primes"')
+ # # => '<span class="dquo">"</span>With primes"'
+ # initial_quotes("'With single primes'")
+ # # => '<span class="quo">\\'</span>With single primes\\''
#
# @example With primes and links
- # initial_quotes('<a href="#">"With primes and a link"</a>')
- # # => '<a href="#"><span class="dquo">"</span>With primes and a link"</a>'
+ # initial_quotes('<a href="#">"With primes and a link"</a>')
+ # # => '<a href="#"><span class="dquo">"</span>With primes and a link"</a>'
#
# @example with Smartypants-quotes
- # initial_quotes('“With smartypanted quotes”')
- # # => '<span class="dquo">“</span>With smartypanted quotes”'
+ # initial_quotes('“With smartypanted quotes”')
+ # # => '<span class="dquo">“</span>With smartypanted quotes”'
#
# @param [String] text input text
# @return [String] input text with initial quotes wrapped
def initial_quotes(text)
# $1 is the initial part of the string, $2 is the quote or entitity, and $3 is the double quote
@@ -202,11 +196,11 @@
end
# Converts special characters (excluding HTML tags) to HTML entities.
#
# @example
- # entities("Aloë Vera") # => "Aloë Vera"
+ # entities("Aloë Vera") # => "Aloë Vera"
#
# @param [String] text input text
# @return [String] input text with all special characters converted to
# HTML entities.
def entities(text)
@@ -228,20 +222,22 @@
end
o
end
# main function to do all the functions from the method.
+ #
# @param [String] text input text
# @return [String] input text with all filters applied
def improve(text)
initial_quotes(caps(smartypants(widont(amp(text)))))
end
private
# Convert characters from the map in ./lib/characters.txt
# Code taken from TextMate HTML bundle
+ #
# @param [String] text input text
# @return [String] input text with all special characters converted to
# HTML entities.
def encode(text)
@char_to_entity ||= begin
@@ -263,9 +259,14 @@
#
# The idea is simple: every text filter is applied as a block to this
# method. This will preprocess the text and replace any inline scripts
# with a MD5 hash of its entire contents. Then the filter is called,
# and then the hashes are replaced back with their original content.
+ #
+ # @yield [hashed_text] Hands you the input text with all script tags
+ # hashed. The block's result will be unhashed and then returned.
+ # @param [String] text
+ # @return [String] input with script tags restored
def ignore_scripts(text)
@ignored_scripts = {}
modified_text = text.gsub(/<script[^>]*>.*?<\/script>/mi) do |script|
hash = Digest::MD5.hexdigest(script)
@ignored_scripts[hash] = script