lib/typogruby.rb in typogruby-1.0.11 vs lib/typogruby.rb in typogruby-1.0.12
- old
+ new
@@ -54,14 +54,14 @@
# # => '<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
+ # $1 is the part before the caps and $2 is the amp match
exclude_sensitive_tags(text) do |t|
- t.gsub(/<(code|pre).+?<\/\1>|(\s| )&(?:amp;|#38;)?(\s| )/) { |str|
- $1 ? str : $2 + '<span class="amp">&</span>' + $3
+ t.gsub(/(\s| )&(?:amp;|#38;)?(\s| )/) { |str|
+ $1 + '<span class="amp">&</span>' + $2
}.gsub(/(\w+)="(.*?)<span class="amp">&<\/span>(.*?)"/, '\1="\2&\3"')
end
end
# replaces space(s) before the last word (or tag before the last word)
@@ -145,21 +145,20 @@
# @return [String] input text with caps wrapped
def caps(text)
exclude_sensitive_tags(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{
- (?i:<(code|pre).+?</\1>)| # Ignore the contents of code and pre elements
(<[^/][^>]+?>)| # 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 captial words, with optional dots or numbers in between
(?!\w) # ...which must not be followed by a word character.
}x) do |str|
- excluded, tag, before, caps = $1, $2, $3, $4
+ 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 excluded || tag || caps =~ /^\d+\.?$/
+ if tag || caps =~ /^\d+\.?$/
str
elsif $3 =~ /^[\d\.]+$/
before + caps
else
before + '<span class="caps">' + caps + '</span>'
@@ -252,11 +251,11 @@
ent = @char_to_entity[ch]
ent ? "&#{ent};" : sprintf("&#x%02X;", ch.unpack("U")[0])
end
end
- # Hackish text filter that will make sure our text filters leave
+ # Hackish text filter that will make sure our text filters leave
# sensitive tags alone without resorting to a full-blown HTML parser.
#
# Sensitive tags are tags with literal contents, which we do not
# want to change. It currently ignores: <pre>, <code>, <kbd>, <math>
# and <script>.
@@ -276,10 +275,10 @@
hash = Digest::MD5.hexdigest(script)
@exluded_sensitive_tags[hash] = script
hash
end
yield(modified_text).gsub(/#{@exluded_sensitive_tags.keys.join('|')}/) do |h|
- @exluded_sensitive_tags.delete(h)
+ @exluded_sensitive_tags[h]
end
end
# Array of all the senstive tags that should be ignored by all the text filters.
EXCLUDED_TAGS = %w{pre code kbd math script}