module Jekyll::TypogrifyFilter

Public Instance Methods

amp(text) click to toggle source

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.

@param [String] text input text @return [String] input text with ampersands wrapped

# File lib/jekyll/typogrify.rb, line 15
def amp(text)
  return Typogruby.amp(text.to_s)
end
caps(text) click to toggle source

surrounds two or more consecutive capital letters, perhaps with interspersed digits and periods in a span with a styled class.

@param [String] text input text @return [String] input text with caps wrapped

# File lib/jekyll/typogrify.rb, line 24
def caps(text)
  return Typogruby.caps(text.to_s)
end
entities(text) click to toggle source

Converts special characters (excluding HTML tags) to HTML entities.

@param [String] text input text @return [String] input text with all special characters converted to

HTML entities.
# File lib/jekyll/typogrify.rb, line 33
def entities(text)
  return Typogruby.entities(text.to_s)
end
improve(text) click to toggle source

main function to do all the functions from the method.

@param [String] text input text @return [String] input text with all filters applied

# File lib/jekyll/typogrify.rb, line 41
def improve(text)
  return Typogruby.improve(text.to_s)
end
initial_quotes(text) click to toggle source

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.

@param [String] text input text @return [String] input text with initial quotes wrapped

# File lib/jekyll/typogrify.rb, line 52
def initial_quotes(text)
  return Typogruby.initial_quotes(text.to_s)
end
jt_caps(text) click to toggle source

surrounds two or more consecutive capital letters, perhaps with interspersed digits and periods in a span with a styled class.

@param [String] text input text @return [String] input text with caps wrapped

# File lib/jekyll/typogrify.rb, line 102
def jt_caps(text)
  return custom_caps(text.to_s)
end
jt_emdash(text) click to toggle source

converts a — (em dash) by optional whitespace or a non-breaking space to the HTML entity and surrounds it in a span with a styled class.

@param [String] text input text @return [String] input text with em dashes wrapped

# File lib/jekyll/typogrify.rb, line 111
def jt_emdash(text)
  return emdash(text.to_s)
end
letter_spacing(text) click to toggle source

wraps words in a span class that can look like something else

@param [String] text input text @return [String] input text with words that look strange in a span

# File lib/jekyll/typogrify.rb, line 90
def letter_spacing(text)
  text.gsub(/(click\S*|clint\S*|final\S*|curt\S*|flick\S*)\b/im) { |str|
    tag, before, word = $1, $2, $3
    "#{before}<span class=\"fix-letter-spacing\">#{str}</span>"
  }
end
smartypants(text) click to toggle source

Applies smartypants to a given piece of text

@see rubygems.org/gems/rubypants @param [String] text input text @return [String] input text with smartypants applied

# File lib/jekyll/typogrify.rb, line 61
def smartypants(text)
  return Typogruby.smartypants(text.to_s)
end
titlecase(text) click to toggle source

convert a given piece of text to titlecase

@param [String] text input text @return [String] input text convert to titlecase

# File lib/jekyll/typogrify.rb, line 82
def titlecase(text)
  return text.to_s.titlecase
end
widont(text) click to toggle source

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.

@see mucur.name/posts/widon-t-and-smartypants-helpers-for-rails @see shauninman.com/archive/2006/08/22/widont_wordpress_plugin @param [String] text input text @return [String] input text with non-breaking spaces inserted

# File lib/jekyll/typogrify.rb, line 74
def widont(text)
  return Typogruby.widont(text.to_s)
end

Private Instance Methods

custom_caps(text) click to toggle source

surrounds two or more consecutive capital letters, perhaps with interspersed digits and periods in a span with a styled class.

@param [String] text input text @return [String] input text with caps wrapped

# File lib/jekyll/typogrify.rb, line 124
def custom_caps(text)
  # $1 and $2 are excluded HTML tags, $3 is the part before the caps and $4 is the caps match
  text.gsub(%r{
      (<[^/][^>]*?>)|                                      # Ignore any opening tag, so we don't mess up attribute values
      (\s|&nbsp;|^|'|"|>|)                                 # Make sure our capture is preceded by whitespace or quotes
      ([A-Z\d](?:(\.|'|-|&|&amp;|&\#38;)?[A-Z\d][\.']?){1,}) # Capture capital words, with optional dots, numbers or ampersands in between
      (?!\w)                                               # ...which must not be followed by a word character.
    }x) do |str|
    tag, before, caps = $1, $2, $3

    # Do nothing with the contents if ignored tags, the inside of an opening HTML element
    # so we don't mess up attribute values, or if our capture is only digits.
    if tag || caps =~ /^\d+\.?$/
      str
    elsif $3 =~ /^[\d\.]+$/
      before + caps
    else
      before + '<span class="caps">' + caps + '</span>'
    end
  end
end
emdash(text) click to toggle source

converts a — (em dash) by optional whitespace or a non-breaking space to the HTML entity and surrounds it in a span with a styled class.

@param [String] text input text @return [String] input text with em dashes wrapped

# File lib/jekyll/typogrify.rb, line 151
def emdash(text)
  text.gsub(/(\w|\s|&nbsp;)—(?:mdash;|#8212;)?(\w|\s|&nbsp;)/) { |str|
    $1 + '<span class="emdash">&mdash;</span>' + $2
  }.gsub(/(\w+)="(.*?)<span class="emdash">&mdash;<\/span>(.*?)"/, '\1="\2&mdash;\3"')
end