Module: Typogruby
Overview
A collection of simple helpers for improving web typograhy. Based on TypographyHelper by Luke Hartman and Typogrify.
Constant Summary
- VERSION =
'1.0.13'
- EXCLUDED_TAGS =
Array of all the senstive tags that should be ignored by all the text filters.
%w{pre code kbd math script}
Instance Method Summary (collapse)
-
- (String) amp(text)
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.
-
- (String) caps(text)
surrounds two or more consecutive captial letters, perhaps with interspersed digits and periods in a span with a styled class.
-
- (String) entities(text)
Converts special characters (excluding HTML tags) to HTML entities.
-
- (String) improve(text)
main function to do all the functions from the method.
-
- (String) initial_quotes(text)
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.
-
- (String) smartypants(text)
Applies smartypants to a given piece of text.
-
- (String) widont(text)
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.
Instance Method Details
- (String) amp(text)
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.
60 61 62 63 64 65 66 67 |
# File 'lib/typogruby.rb', line 60 def amp(text) # $1 is the part before the caps and $2 is the amp match (text) do |t| t.gsub(/(\s| )&(?:amp;|#38;)?(\s| )/) { |str| $1 + '<span class="amp">&</span>' + $2 }.gsub(/(\w+)="(.*?)<span class="amp">&<\/span>(.*?)"/, '\1="\2&\3"') end end |
- (String) caps(text)
surrounds two or more consecutive captial letters, perhaps with interspersed digits and periods in a span with a styled class.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/typogruby.rb', line 154 def caps(text) (text) do |t| # $1 and $2 are excluded HTML tags, $3 is the part before the caps and $4 is the caps match t.gsub(%r{ (<[^/][^>]+?>)| # Ignore any opening tag, so we don't mess up attribute values (\s| |^|'|"|>) # Make sure our capture is preceded by whitespace or quotes ([A-Z\d]([\.']?[A-Z\d][\.']?){1,}) # Capture capital words, with optional dots or numbers 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 end |
- (String) entities(text)
Converts special characters (excluding HTML tags) to HTML entities.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/typogruby.rb', line 213 def entities(text) o = '' text.scan(/(?x) ( <\?(?:[^?]*|\?(?!>))*\?> | <!-- (?m:.*?) --> | <\/? (?i:a|abbr|acronym|address|applet|area|b|base|basefont|bdo|big|blockquote|body|br|button|caption|center|cite|code|col|colgroup|dd|del|dfn|dir|div|dl|dt|em|fieldset|font|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|hr|html|i|iframe|img|input|ins|isindex|kbd|label|legend|li|link|map|menu|meta|noframes|noscript|object|ol|optgroup|option|p|param|pre|q|s|samp|script|select|small|span|strike|strong|style|sub|sup|table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|u|ul|var)\b (?:[^>"']|"[^"]*"|'[^']*')* > | &(?:[a-zA-Z0-9]+|\#[0-9]+|\#x[0-9a-fA-F]+); ) |([^<&]+|[<&]) /x) do |tag, text| o << tag.to_s o << encode(text.to_s) end o end |
- (String) improve(text)
main function to do all the functions from the method.
237 238 239 |
# File 'lib/typogruby.rb', line 237 def improve(text) initial_quotes(caps(smartypants(widont(amp(text))))) end |
- (String) initial_quotes(text)
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.
198 199 200 201 202 203 |
# File 'lib/typogruby.rb', line 198 def initial_quotes(text) # $1 is the initial part of the string, $2 is the quote or entitity, and $3 is the double quote (text) do |t| t.gsub(/((?:<(?:h[1-6]|p|li|dt|dd)[^>]*>|^)\s*(?:<(?:a|em|strong|span)[^>]*>)?)('|‘|‘|("|“|“))/) {$1 + "<span class=\"#{'d' if $3}quo\">#{$2}</span>"} end end |
- (String) smartypants(text)
Applies smartypants to a given piece of text
31 32 33 |
# File 'lib/typogruby.rb', line 31 def smartypants(text) ::RubyPants.new(text).to_html end |
- (String) widont(text)
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.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/typogruby.rb', line 112 def widont(text) (text) do |t| t.gsub(%r{ ((?:</?(?:a|em|span|strong|i|b)[^>]*>)|[^<>\s]) # must be proceeded by an approved inline opening or closing tag or a nontag/nonspace \s+ # the space to replace (([^<>\s]+) # must be flollowed by non-tag non-space characters \s* # optional white space! (</(a|em|span|strong|i|b)>\s*)* # optional closing inline tags with optional white space after each ((</(p|h[1-6]|li|dt|dd)>)|$)) # end with a closing p, h1-6, li or the end of the string }x) { |match| $1 + (match.include?(' ') ? ' ' : ' ') + $2 } # Make sure to not add another nbsp before one already there end end |