require 'rubypants' # A collection of simple helpers for improving web # typograhy. Based on TypographyHelper by Luke Hartman and Typogrify. # # @see http://github.com/hunter/typography-helper # @see http://code.google.com/p/typogrify # @author Arjan van der Gaag 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' # # @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 & two' # amp('One & two') # # => 'One & two' # amp('One & two') # # => 'One & two' # amp('One & two') # # => 'One & two' # # @example It won't mess up & that are already wrapped, in entities or URLs # # amp('One & two') # # => 'One & two' # amp('“this” & that') # # => '“this” & that' # # @example It should ignore standalone amps that are in attributes # amp('xyz') # # => 'xyz' # # @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 text.gsub(/<(code|pre).+?<\/\1>|(\s| )&(?:amp;|#38;)?(\s| )/) {|str| $1 ? str : $2 + '&' + $3 }.gsub(/(\w+)="(.*?)&<\/span>(.*?)"/, '\1="\2&\3"') end # replaces space(s) before the last word (or tag before the last word) # before an optional closing element (a, em, # span, strong) before a closing tag (p, h[1-6], # li, dt, dd) or the end of the string # # @example # > widont('A very simple test') # # => 'A very simple test' # # @example Single word items shouldn't be changed # widont('Test') # # => 'Test' # widont(' Test') # # => ' Test' # widont('