0
+# = RubyPants -- SmartyPants ported to Ruby
0
+# Ported by Christian Neukirchen <mailto:chneukirchen@gmail.com>
0
+# Copyright (C) 2004 Christian Neukirchen
0
+# Incooporates ideas, comments and documentation by Chad Miller
0
+# Copyright (C) 2004 Chad Miller
0
+# Original SmartyPants by John Gruber
0
+# Copyright (C) 2003 John Gruber
0
+# = RubyPants -- SmartyPants ported to Ruby
0
+# RubyPants is a Ruby port of the smart-quotes library SmartyPants.
0
+# The original "SmartyPants" is a free web publishing plug-in for
0
+# Movable Type, Blosxom, and BBEdit that easily translates plain ASCII
0
+# punctuation characters into "smart" typographic punctuation HTML
0
+# RubyPants can perform the following transformations:
0
+# * Straight quotes (<tt>"</tt> and <tt>'</tt>) into "curly" quote
0
+# * Backticks-style quotes (<tt>``like this''</tt>) into "curly" quote
0
+# * Dashes (<tt>--</tt> and <tt>---</tt>) into en- and em-dash
0
+# * Three consecutive dots (<tt>...</tt> or <tt>. . .</tt>) into an
0
+# This means you can write, edit, and save your posts using plain old
0
+# ASCII straight quotes, plain dashes, and plain dots, but your
0
+# published posts (and final HTML output) will appear with smart
0
+# quotes, em-dashes, and proper ellipses.
0
+# RubyPants does not modify characters within <tt><pre></tt>,
0
+# <tt><code></tt>, <tt><kbd></tt>, <tt><math></tt> or
0
+# <tt><script></tt> tag blocks. Typically, these tags are used to
0
+# display text where smart quotes and other "smart punctuation" would
0
+# not be appropriate, such as source code or example markup.
0
+# If you need to use literal straight quotes (or plain hyphens and
0
+# periods), RubyPants accepts the following backslash escape sequences
0
+# to force non-smart punctuation. It does so by transforming the
0
+# escape sequence into a decimal-encoded HTML entity:
0
+# This is useful, for example, when you want to use straight quotes as
0
+# foot and inch marks: 6'2" tall; a 17" iMac. (Use <tt>6\'2\"</tt>
0
+# resp. <tt>17\"</tt>.)
0
+# == Algorithmic Shortcomings
0
+# One situation in which quotes will get curled the wrong way is when
0
+# apostrophes are used at the start of leading contractions. For
0
+# 'Twas the night before Christmas.
0
+# In the case above, RubyPants will turn the apostrophe into an
0
+# opening single-quote, when in fact it should be a closing one. I
0
+# don't think this problem can be solved in the general case--every
0
+# word processor I've tried gets this wrong as well. In such cases,
0
+# it's best to use the proper HTML entity for closing single-quotes
0
+# ("<tt>’</tt>") by hand.
0
+# To file bug reports or feature requests (except see above) please
0
+# send email to: mailto:chneukirchen@gmail.com
0
+# If the bug involves quotes being curled the wrong way, please send
0
+# example text to illustrate.
0
+# John Gruber did all of the hard work of writing this software in
0
+# Perl for Movable Type and almost all of this useful documentation.
0
+# Chad Miller ported it to Python to use with Pyblosxom.
0
+# Christian Neukirchen provided the Ruby port, as a general-purpose
0
+# library that follows the *Cloth API.
0
+# == Copyright and License
0
+# === SmartyPants license:
0
+# Copyright (c) 2003 John Gruber
0
+# (http://daringfireball.net)
0
+# Redistribution and use in source and binary forms, with or without
0
+# modification, are permitted provided that the following conditions
0
+# * Redistributions of source code must retain the above copyright
0
+# notice, this list of conditions and the following disclaimer.
0
+# * Redistributions in binary form must reproduce the above copyright
0
+# notice, this list of conditions and the following disclaimer in
0
+# the documentation and/or other materials provided with the
0
+# * Neither the name "SmartyPants" nor the names of its contributors
0
+# may be used to endorse or promote products derived from this
0
+# software without specific prior written permission.
0
+# This software is provided by the copyright holders and contributors
0
+# "as is" and any express or implied warranties, including, but not
0
+# limited to, the implied warranties of merchantability and fitness
0
+# for a particular purpose are disclaimed. In no event shall the
0
+# copyright owner or contributors be liable for any direct, indirect,
0
+# incidental, special, exemplary, or consequential damages (including,
0
+# but not limited to, procurement of substitute goods or services;
0
+# loss of use, data, or profits; or business interruption) however
0
+# caused and on any theory of liability, whether in contract, strict
0
+# liability, or tort (including negligence or otherwise) arising in
0
+# any way out of the use of this software, even if advised of the
0
+# possibility of such damage.
0
+# === RubyPants license
0
+# RubyPants is a derivative work of SmartyPants and smartypants.py.
0
+# Redistribution and use in source and binary forms, with or without
0
+# modification, are permitted provided that the following conditions
0
+# * Redistributions of source code must retain the above copyright
0
+# notice, this list of conditions and the following disclaimer.
0
+# * Redistributions in binary form must reproduce the above copyright
0
+# notice, this list of conditions and the following disclaimer in
0
+# the documentation and/or other materials provided with the
0
+# This software is provided by the copyright holders and contributors
0
+# "as is" and any express or implied warranties, including, but not
0
+# limited to, the implied warranties of merchantability and fitness
0
+# for a particular purpose are disclaimed. In no event shall the
0
+# copyright owner or contributors be liable for any direct, indirect,
0
+# incidental, special, exemplary, or consequential damages (including,
0
+# but not limited to, procurement of substitute goods or services;
0
+# loss of use, data, or profits; or business interruption) however
0
+# caused and on any theory of liability, whether in contract, strict
0
+# liability, or tort (including negligence or otherwise) arising in
0
+# any way out of the use of this software, even if advised of the
0
+# possibility of such damage.
0
+# John Gruber:: http://daringfireball.net
0
+# SmartyPants:: http://daringfireball.net/projects/smartypants
0
+# Chad Miller:: http://web.chad.org
0
+# Christian Neukirchen:: http://kronavita.de/chris
0
+class RubyPants < String
0
+ # Create a new RubyPants instance with the text in +string+.
0
+ # Allowed elements in the options array:
0
+ # 1 :: enable all, using only em-dash shortcuts
0
+ # 2 :: enable all, using old school en- and em-dash shortcuts (*default*)
0
+ # 3 :: enable all, using inverted old school en and em-dash shortcuts
0
+ # -1 :: stupefy (translate HTML entities to their ASCII-counterparts)
0
+ # If you don't like any of these defaults, you can pass symbols to change
0
+ # RubyPants' behavior:
0
+ # <tt>:quotes</tt> :: quotes
0
+ # <tt>:backticks</tt> :: backtick quotes (``double'' only)
0
+ # <tt>:allbackticks</tt> :: backtick quotes (``double'' and `single')
0
+ # <tt>:dashes</tt> :: dashes
0
+ # <tt>:oldschool</tt> :: old school dashes
0
+ # <tt>:inverted</tt> :: inverted old school dashes
0
+ # <tt>:ellipses</tt> :: ellipses
0
+ # <tt>:convertquotes</tt> :: convert <tt>"</tt> entities to
0
+ # <tt>"</tt> for Dreamweaver users
0
+ # <tt>:stupefy</tt> :: translate RubyPants HTML entities
0
+ # to their ASCII counterparts.
0
+ def initialize(string, options=[2])
0
+ # Apply SmartyPants transformations.
0
+ do_quotes = do_backticks = do_dashes = do_ellipses = do_stupify = nil
0
+ convert_quotes = false
0
+ if @options.include? 0
0
+ elsif @options.include? 1
0
+ # Do everything, turn all options on.
0
+ do_quotes = do_backticks = do_ellipses = true
0
+ elsif @options.include? 2
0
+ # Do everything, turn all options on, use old school dash shorthand.
0
+ do_quotes = do_backticks = do_ellipses = true
0
+ do_dashes = :oldschool
0
+ elsif @options.include? 3
0
+ # Do everything, turn all options on, use inverted old school
0
+ do_quotes = do_backticks = do_ellipses = true
0
+ elsif @options.include?(-1)
0
+ do_quotes = @options.include? :quotes
0
+ do_backticks = @options.include? :backticks
0
+ do_backticks = :both if @options.include? :allbackticks
0
+ do_dashes = :normal if @options.include? :dashes
0
+ do_dashes = :oldschool if @options.include? :oldschool
0
+ do_dashes = :inverted if @options.include? :inverted
0
+ do_ellipses = @options.include? :ellipses
0
+ convert_quotes = @options.include? :convertquotes
0
+ do_stupefy = @options.include? :stupefy
0
+ # Keep track of when we're inside <pre> or <code> tags.
0
+ # Here is the result stored in.
0
+ # This is a cheat, used to get some context for one-character
0
+ # tokens that consist of just a quote char. What we do is remember
0
+ # the last character of the previous text token, to use as context
0
+ # to curl single- character quote tokens correctly.
0
+ prev_token_last_char = nil
0
+ if token.first == :tag
0
+ if token[1] =~ %r!<(/?)(?:pre|code|kbd|script|math)[\s>]!
0
+ in_pre = ($1 != "/") # Opening or closing tag?
0
+ # Remember last char of this token before processing.
0
+ t.gsub!(/"/, '"') if convert_quotes
0
+ t = educate_dashes t if do_dashes == :normal
0
+ t = educate_dashes_oldschool t if do_dashes == :oldschool
0
+ t = educate_dashes_inverted t if do_dashes == :inverted
0
+ t = educate_ellipses t if do_ellipses
0
+ # Note: backticks need to be processed before quotes.
0
+ t = educate_backticks t
0
+ t = educate_single_backticks t if do_backticks == :both
0
+ # Special case: single-character ' token
0
+ if prev_token_last_char =~ /\S/
0
+ # Special case: single-character " token
0
+ if prev_token_last_char =~ /\S/
0
+ t = stupefy_entities t if do_stupefy
0
+ prev_token_last_char = last_char
0
+ # Return the string, with after processing the following backslash
0
+ # escape sequences. This is useful if you want to force a "dumb" quote
0
+ # or other character to appear.
0
+ def process_escapes(str)
0
+ str.gsub('\\\\', '\').
0
+ gsub("\\\'", ''').
0
+ # The string, with each instance of "<tt>--</tt>" translated to an
0
+ # em-dash HTML entity.
0
+ def educate_dashes(str)
0
+ str.gsub(/--/, '—')
0
+ # The string, with each instance of "<tt>--</tt>" translated to an
0
+ # en-dash HTML entity, and each "<tt>---</tt>" translated to an
0
+ # em-dash HTML entity.
0
+ def educate_dashes_oldschool(str)
0
+ str.gsub(/---/, '—').gsub(/--/, '–')
0
+ # Return the string, with each instance of "<tt>--</tt>" translated
0
+ # to an em-dash HTML entity, and each "<tt>---</tt>" translated to
0
+ # an en-dash HTML entity. Two reasons why: First, unlike the en- and
0
+ # em-dash syntax supported by +educate_dashes_oldschool+, it's
0
+ # compatible with existing entries written before SmartyPants 1.1,
0
+ # back when "<tt>--</tt>" was only used for em-dashes. Second,
0
+ # em-dashes are more common than en-dashes, and so it sort of makes
0
+ # sense that the shortcut should be shorter to type. (Thanks to
0
+ # Aaron Swartz for the idea.)
0
+ def educate_dashes_inverted(str)
0
+ str.gsub(/---/, '–').gsub(/--/, '—')
0
+ # Return the string, with each instance of "<tt>...</tt>" translated
0
+ # to an ellipsis HTML entity. Also converts the case where there are
0
+ # spaces between the dots.
0
+ def educate_ellipses(str)
0
+ str.gsub('...', '…').gsub('. . .', '…')
0
+ # Return the string, with "<tt>``backticks''</tt>"-style single quotes
0
+ # translated into HTML curly quote entities.
0
+ def educate_backticks(str)
0
+ str.gsub("``", '“').gsub("''", '”')
0
+ # Return the string, with "<tt>`backticks'</tt>"-style single quotes
0
+ # translated into HTML curly quote entities.
0
+ def educate_single_backticks(str)
0
+ str.gsub("`", '‘').gsub("'", '’')
0
+ # Return the string, with "educated" curly quote HTML entities.
0
+ def educate_quotes(str)
0
+ punct_class = '[!"#\$\%\'()*+,\-.\/:;<=>?\@\[\\\\\]\^_`{|}~]'
0
+ # Special case if the very first character is a quote followed by
0
+ # punctuation at a non-word-break. Close the quotes by brute
0
+ str.gsub!(/^'(?=#{punct_class}\B)/, '’')
0
+ str.gsub!(/^"(?=#{punct_class}\B)/, '”')
0
+ # Special case for double sets of quotes, e.g.:
0
+ # <p>He said, "'Quoted' words in a larger quote."</p>
0
+ str.gsub!(/"'(?=\w)/, '“‘')
0
+ str.gsub!(/'"(?=\w)/, '‘“')
0
+ # Special case for decade abbreviations (the '80s):
0
+ str.gsub!(/'(?=\d\ds)/, '’')
0
+ close_class = %![^\ \t\r\n\\[\{\(\-]!
0
+ dec_dashes = '–|—'
0
+ # Get most opening single quotes:
0
+ str.gsub!(/(\s| |--|&[mn]dash;|#{dec_dashes}|ȁ[34];)'(?=\w)/,
0
+ # Single closing quotes:
0
+ str.gsub!(/(#{close_class})'/, '\1’')
0
+ str.gsub!(/'(\s|s\b|$)/, '’\1')
0
+ # Any remaining single quotes should be opening ones:
0
+ str.gsub!(/'/, '‘')
0
+ # Get most opening double quotes:
0
+ str.gsub!(/(\s| |--|&[mn]dash;|#{dec_dashes}|ȁ[34];)"(?=\w)/,
0
+ # Double closing quotes:
0
+ str.gsub!(/(#{close_class})"/, '\1”')
0
+ str.gsub!(/"(\s|s\b|$)/, '”\1')
0
+ # Any remaining quotes should be opening ones:
0
+ str.gsub!(/"/, '“')
0
+ # Return the string, with each RubyPants HTML entity translated to
0
+ # its ASCII counterpart.
0
+ # Note: This is not reversible (but exactly the same as in SmartyPants)
0
+ def stupefy_entities(str)
0
+ gsub(/–/, '-'). # en-dash
0
+ gsub(/—/, '--'). # em-dash
0
+ gsub(/‘/, "'"). # open single quote
0
+ gsub(/’/, "'"). # close single quote
0
+ gsub(/“/, '"'). # open double quote
0
+ gsub(/”/, '"'). # close double quote
0
+ gsub(/…/, '...') # ellipsis
0
+ # Return an array of the tokens comprising the string. Each token is
0
+ # either a tag (possibly with nested, tags contained therein, such
0
+ # as <tt><a href="<MTFoo>"></tt>, or a run of text between
0
+ # tags. Each element of the array is a two-element array; the first
0
+ # is either :tag or :text; the second is the actual value.
0
+ # Based on the <tt>_tokenize()</tt> subroutine from Brad Choate's
0
+ # MTRegex plugin. <http://www.bradchoate.com/past/mtregex.php>
0
+ # This is actually the easier variant using tag_soup, as used by
0
+ # Chad Miller in the Python port of SmartyPants.
0
+ tag_soup = /([^<]*)(<[^>]*>)/
0
+ tokens << [:text, $1] if $1 != ""
0
+ tokens << [:text, self[prev_end..-1]]