typogruby 1.0.10 → 1.0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/HISTORY.md +4 -0
- data/README.md +5 -1
- data/lib/typogruby.rb +22 -15
- data/lib/version.rb +1 -1
- data/test/test_typogruby.rb +7 -0
- metadata +36 -36
data/Gemfile.lock
CHANGED
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -57,9 +57,13 @@ See HISTORY.md for the complete changelog.
|
|
57
57
|
|
58
58
|
By Arjan van der Gaag ([avdgaag @ github][6]) based on the hard work of lots of others. See LICENSE for license details (same as Ruby).
|
59
59
|
|
60
|
+
This gem includes contributions by:
|
61
|
+
|
62
|
+
* Justin Hileman
|
63
|
+
|
60
64
|
[1]: http://jeffcroft.com/blog/2007/may/29/typogrify-easily-produce-web-typography-doesnt-suc/
|
61
65
|
[2]: http://github.com/hunter/typography-helper
|
62
66
|
[3]: http://code.google.com/p/typogrify
|
63
67
|
[4]: http://avdgaag.github.com/typogruby
|
64
68
|
[5]: http://github.com/avdgaag/Typography-tmbundle
|
65
|
-
[6]: http://github.com/avdgaag
|
69
|
+
[6]: http://github.com/avdgaag
|
data/lib/typogruby.rb
CHANGED
@@ -57,7 +57,7 @@ module Typogruby
|
|
57
57
|
# @return [String] input text with ampersands wrapped
|
58
58
|
def amp(text)
|
59
59
|
# $1 is an excluded HTML tag, $2 is the part before the caps and $3 is the amp match
|
60
|
-
|
60
|
+
exclude_sensitive_tags(text) do |t|
|
61
61
|
t.gsub(/<(code|pre).+?<\/\1>|(\s| )&(?:amp;|#38;)?(\s| )/) { |str|
|
62
62
|
$1 ? str : $2 + '<span class="amp">&</span>' + $3
|
63
63
|
}.gsub(/(\w+)="(.*?)<span class="amp">&<\/span>(.*?)"/, '\1="\2&\3"')
|
@@ -108,7 +108,7 @@ module Typogruby
|
|
108
108
|
# @param [String] text input text
|
109
109
|
# @return [String] input text with non-breaking spaces inserted
|
110
110
|
def widont(text)
|
111
|
-
|
111
|
+
exclude_sensitive_tags(text) do |t|
|
112
112
|
t.gsub(%r{
|
113
113
|
((?:</?(?:a|em|span|strong|i|b)[^>]*>)|[^<>\s]) # must be proceeded by an approved inline opening or closing tag or a nontag/nonspace
|
114
114
|
\s+ # the space to replace
|
@@ -144,7 +144,7 @@ module Typogruby
|
|
144
144
|
# @param [String] text input text
|
145
145
|
# @return [String] input text with caps wrapped
|
146
146
|
def caps(text)
|
147
|
-
|
147
|
+
exclude_sensitive_tags(text) do |t|
|
148
148
|
# $1 and $2 are excluded HTML tags, $3 is the part before the caps and $4 is the caps match
|
149
149
|
t.gsub(%r{
|
150
150
|
(?i:<(code|pre).+?</\1>)| # Ignore the contents of code and pre elements
|
@@ -190,7 +190,7 @@ module Typogruby
|
|
190
190
|
# @return [String] input text with initial quotes wrapped
|
191
191
|
def initial_quotes(text)
|
192
192
|
# $1 is the initial part of the string, $2 is the quote or entitity, and $3 is the double quote
|
193
|
-
|
193
|
+
exclude_sensitive_tags(text) do |t|
|
194
194
|
t.gsub(/((?:<(?:h[1-6]|p|li|dt|dd)[^>]*>|^)\s*(?:<(?:a|em|strong|span)[^>]*>)?)('|‘|‘|("|“|“))/) {$1 + "<span class=\"#{'d' if $3}quo\">#{$2}</span>"}
|
195
195
|
end
|
196
196
|
end
|
@@ -254,29 +254,36 @@ private
|
|
254
254
|
end
|
255
255
|
end
|
256
256
|
|
257
|
-
# Hackish text filter that will make sure our text filters leave
|
258
|
-
#
|
257
|
+
# Hackish text filter that will make sure our text filters leave
|
258
|
+
# sensitive tags alone without resorting to a full-blown HTML parser.
|
259
|
+
#
|
260
|
+
# Sensitive tags are tags with literal contents, which we do not
|
261
|
+
# want to change. It currently ignores: <pre>, <code>, <kbd>, <math>
|
262
|
+
# and <script>.
|
259
263
|
#
|
260
264
|
# The idea is simple: every text filter is applied as a block to this
|
261
|
-
# method. This will preprocess the text and replace any
|
265
|
+
# method. This will preprocess the text and replace any sensitive tags
|
262
266
|
# with a MD5 hash of its entire contents. Then the filter is called,
|
263
267
|
# and then the hashes are replaced back with their original content.
|
264
268
|
#
|
265
|
-
# @yield [hashed_text] Hands you the input text with all
|
269
|
+
# @yield [hashed_text] Hands you the input text with all sensitive tags
|
266
270
|
# hashed. The block's result will be unhashed and then returned.
|
267
271
|
# @param [String] text
|
268
|
-
# @return [String] input with
|
269
|
-
def
|
270
|
-
@
|
271
|
-
modified_text = text.gsub(/<
|
272
|
+
# @return [String] input with sensitive tags restored
|
273
|
+
def exclude_sensitive_tags(text)
|
274
|
+
@exluded_sensitive_tags = {}
|
275
|
+
modified_text = text.gsub(/<(#{EXCLUDED_TAGS .join('|')})[^>]*>.*?<\/\1>/mi) do |script|
|
272
276
|
hash = Digest::MD5.hexdigest(script)
|
273
|
-
@
|
277
|
+
@exluded_sensitive_tags[hash] = script
|
274
278
|
hash
|
275
279
|
end
|
276
|
-
yield(modified_text).gsub(/#{@
|
277
|
-
@
|
280
|
+
yield(modified_text).gsub(/#{@exluded_sensitive_tags.keys.join('|')}/) do |h|
|
281
|
+
@exluded_sensitive_tags.delete(h)
|
278
282
|
end
|
279
283
|
end
|
280
284
|
|
285
|
+
# Array of all the senstive tags that should be ignored by all the text filters.
|
286
|
+
EXCLUDED_TAGS = %w{pre code kbd math script}
|
287
|
+
|
281
288
|
extend self
|
282
289
|
end
|
data/lib/version.rb
CHANGED
data/test/test_typogruby.rb
CHANGED
@@ -97,4 +97,11 @@ class TestTypogruby < Test::Unit::TestCase
|
|
97
97
|
assert_equal "Variëren…", entities('Variëren…')
|
98
98
|
assert_equal "<p>Olé</p>", entities('<p>Olé</p>')
|
99
99
|
end
|
100
|
+
|
101
|
+
def test_should_leave_sensitive_tags_alone
|
102
|
+
%w{script pre code kbd math}.each do |tag_name|
|
103
|
+
test_string = "<#{tag_name}>“CAPS & chårs” and widows</#{tag_name}>"
|
104
|
+
assert_equal test_string, improve(test_string)
|
105
|
+
end
|
106
|
+
end
|
100
107
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typogruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 1
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 11
|
10
|
+
version: 1.0.11
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Arjan van der Gaag
|
@@ -15,13 +15,14 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-24 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name: rubypants
|
23
22
|
prerelease: false
|
24
|
-
|
23
|
+
type: :runtime
|
24
|
+
name: rubypants
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
26
|
none: false
|
26
27
|
requirements:
|
27
28
|
- - ">="
|
@@ -30,12 +31,12 @@ dependencies:
|
|
30
31
|
segments:
|
31
32
|
- 0
|
32
33
|
version: "0"
|
33
|
-
|
34
|
-
version_requirements: *id001
|
34
|
+
requirement: *id001
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
|
-
name: yard
|
37
36
|
prerelease: false
|
38
|
-
|
37
|
+
type: :development
|
38
|
+
name: yard
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
40
|
none: false
|
40
41
|
requirements:
|
41
42
|
- - ">="
|
@@ -44,12 +45,12 @@ dependencies:
|
|
44
45
|
segments:
|
45
46
|
- 0
|
46
47
|
version: "0"
|
47
|
-
|
48
|
-
version_requirements: *id002
|
48
|
+
requirement: *id002
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
|
-
name: rake
|
51
50
|
prerelease: false
|
52
|
-
|
51
|
+
type: :development
|
52
|
+
name: rake
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
53
54
|
none: false
|
54
55
|
requirements:
|
55
56
|
- - ">="
|
@@ -58,12 +59,12 @@ dependencies:
|
|
58
59
|
segments:
|
59
60
|
- 0
|
60
61
|
version: "0"
|
61
|
-
|
62
|
-
version_requirements: *id003
|
62
|
+
requirement: *id003
|
63
63
|
- !ruby/object:Gem::Dependency
|
64
|
-
name: aruba
|
65
64
|
prerelease: false
|
66
|
-
|
65
|
+
type: :development
|
66
|
+
name: aruba
|
67
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
67
68
|
none: false
|
68
69
|
requirements:
|
69
70
|
- - ">="
|
@@ -72,12 +73,12 @@ dependencies:
|
|
72
73
|
segments:
|
73
74
|
- 0
|
74
75
|
version: "0"
|
75
|
-
|
76
|
-
version_requirements: *id004
|
76
|
+
requirement: *id004
|
77
77
|
- !ruby/object:Gem::Dependency
|
78
|
-
name: cucumber
|
79
78
|
prerelease: false
|
80
|
-
|
79
|
+
type: :development
|
80
|
+
name: cucumber
|
81
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
81
82
|
none: false
|
82
83
|
requirements:
|
83
84
|
- - ">="
|
@@ -86,12 +87,12 @@ dependencies:
|
|
86
87
|
segments:
|
87
88
|
- 0
|
88
89
|
version: "0"
|
89
|
-
|
90
|
-
version_requirements: *id005
|
90
|
+
requirement: *id005
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
|
-
name: rcov
|
93
92
|
prerelease: false
|
94
|
-
|
93
|
+
type: :development
|
94
|
+
name: rcov
|
95
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
95
96
|
none: false
|
96
97
|
requirements:
|
97
98
|
- - ">="
|
@@ -100,12 +101,12 @@ dependencies:
|
|
100
101
|
segments:
|
101
102
|
- 0
|
102
103
|
version: "0"
|
103
|
-
|
104
|
-
version_requirements: *id006
|
104
|
+
requirement: *id006
|
105
105
|
- !ruby/object:Gem::Dependency
|
106
|
-
name: rspec
|
107
106
|
prerelease: false
|
108
|
-
|
107
|
+
type: :development
|
108
|
+
name: rspec
|
109
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
109
110
|
none: false
|
110
111
|
requirements:
|
111
112
|
- - ">="
|
@@ -114,12 +115,12 @@ dependencies:
|
|
114
115
|
segments:
|
115
116
|
- 0
|
116
117
|
version: "0"
|
117
|
-
|
118
|
-
version_requirements: *id007
|
118
|
+
requirement: *id007
|
119
119
|
- !ruby/object:Gem::Dependency
|
120
|
-
name: bluecloth
|
121
120
|
prerelease: false
|
122
|
-
|
121
|
+
type: :development
|
122
|
+
name: bluecloth
|
123
|
+
version_requirements: &id008 !ruby/object:Gem::Requirement
|
123
124
|
none: false
|
124
125
|
requirements:
|
125
126
|
- - ">="
|
@@ -128,8 +129,7 @@ dependencies:
|
|
128
129
|
segments:
|
129
130
|
- 0
|
130
131
|
version: "0"
|
131
|
-
|
132
|
-
version_requirements: *id008
|
132
|
+
requirement: *id008
|
133
133
|
description: Improve web typography using various text filters. This gem prevents widows and applies markup to ampersans, consecutive capitals and initial quotes.
|
134
134
|
email: arjan@arjanvandergaag.nl
|
135
135
|
executables:
|