typogruby 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -21,3 +21,5 @@ pkg
21
21
  ## PROJECT::SPECIFIC
22
22
  .yardoc
23
23
  doc
24
+ .bundle
25
+ tmp
data/Gemfile CHANGED
@@ -5,3 +5,8 @@ group :development do
5
5
  gem 'yard'
6
6
  gem 'jeweler'
7
7
  end
8
+
9
+ group :test do
10
+ gem 'aruba'
11
+ gem 'cucumber'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ aruba (0.2.4)
5
+ background_process
6
+ cucumber (~> 0.9.3)
7
+ background_process (1.2)
8
+ builder (2.1.2)
9
+ cucumber (0.9.3)
10
+ builder (~> 2.1.2)
11
+ diff-lcs (~> 1.1.2)
12
+ gherkin (~> 2.2.9)
13
+ json (~> 1.4.6)
14
+ term-ansicolor (~> 1.0.5)
15
+ diff-lcs (1.1.2)
16
+ gemcutter (0.6.1)
17
+ gherkin (2.2.9)
18
+ json (~> 1.4.6)
19
+ term-ansicolor (~> 1.0.5)
20
+ git (1.2.5)
21
+ jeweler (1.4.0)
22
+ gemcutter (>= 0.1.0)
23
+ git (>= 1.2.5)
24
+ rubyforge (>= 2.0.0)
25
+ json (1.4.6)
26
+ json_pure (1.4.6)
27
+ rubyforge (2.0.4)
28
+ json_pure (>= 1.1.7)
29
+ rubypants (0.2.0)
30
+ term-ansicolor (1.0.5)
31
+ yard (0.6.1)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ aruba
38
+ cucumber
39
+ jeweler
40
+ rubypants
41
+ yard
data/HISTORY.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.8
4
+
5
+ * Allow command-line output to disk via -o option
6
+ * command-line tool reads both files and STDIN, enabling piping of content
7
+ * added filter to replace special characters with HTML entities
8
+ * better tests for the command-line tool with cucumber and aruba
9
+
3
10
  ## 1.0.7
4
11
 
5
12
  * Improved Ruby 1.9 compatibility
@@ -32,4 +39,4 @@
32
39
 
33
40
  ## 1.0
34
41
 
35
- * First release
42
+ * First release
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.7
1
+ 1.0.8
data/bin/typogruby CHANGED
@@ -1,10 +1,12 @@
1
1
  #!/usr/bin/env ruby -rubygems
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
3
  require 'typogruby'
3
4
  require 'optparse'
4
5
 
5
6
  operations = [:smartypants, :initial_quotes, :amp, :widont, :caps]
6
7
  explicit_on = []
7
8
  explicit_off = []
9
+ output_filename = nil
8
10
 
9
11
  OptionParser.new do |options|
10
12
  options.banner = <<-EOS
@@ -19,6 +21,8 @@ Examples:
19
21
  typogruby --no-widows FILE Runs all filters except for widows
20
22
  typogruby -w FILE Runs only the filter for widows
21
23
  typogruby --widows FILE Runs only the filter for widows
24
+ typogruby -o FILE2 FILE1 Filters FILE1 and saves to FILE2
25
+ cat FILE | typogruby Filter input from STDIN
22
26
 
23
27
  Usage: typogruby [options] filename [filename, ...]
24
28
  EOS
@@ -38,6 +42,10 @@ EOS
38
42
  (v ? explicit_on : explicit_off) << :amp
39
43
  end
40
44
 
45
+ options.on '-n', '--[no-]entities', 'Convert special characters to HTML entities' do |v|
46
+ (v ? explicit_on : explicit_off) << :entities
47
+ end
48
+
41
49
  options.on '-w', '--[no-]widows', 'Prevent widows' do |v|
42
50
  (v ? explicit_on : explicit_off) << :widont
43
51
  end
@@ -46,13 +54,17 @@ EOS
46
54
  (v ? explicit_on : explicit_off) << :caps
47
55
  end
48
56
 
57
+ options.on '-o', '--output FILENAME', 'Save output to file' do |v|
58
+ output_filename = v
59
+ end
60
+
49
61
  options.on_tail '-h', '--help', 'Show this message' do
50
62
  $stderr.print options
51
63
  exit
52
64
  end
53
65
 
54
66
  options.on_tail '-v', '--version', 'Display version information' do
55
- $stderr.print Typogruby.version
67
+ $stderr.print "Typogruby #{Typogruby.version}"
56
68
  exit
57
69
  end
58
70
  options.parse!
@@ -66,18 +78,34 @@ else
66
78
  operations
67
79
  end
68
80
 
69
- unless ARGV.any?
70
- $stderr.puts 'No file specified'
81
+ begin
82
+ # Apply every filter to our input text
83
+ output = operations_todo.inject(ARGF.read) { |t, o| Typogruby.send(o, t) }
84
+
85
+ # End the program nicely when the user interrupt with ctrl-c
86
+ rescue Interrupt
87
+ $stderr.puts "Interrupted by user."
88
+ exit 1
89
+
90
+ # Inform the user of any errors that have occured
91
+ rescue Exception => e
92
+ $stderr.puts "Error processing input:"
93
+ $stderr.puts e.message
71
94
  exit 1
72
95
  end
73
96
 
74
- output = ARGV.inject('') do |output, filename|
97
+ # Either use the output_filename to save the output to a file on disk,
98
+ # or print it to stdout
99
+ if output_filename
75
100
  begin
76
- output + operations_todo.inject(File.read(filename)) { |t, o| Typogruby.send(o, t) }
77
- rescue
78
- $stderr.print "Error processing '#{filename}'"
79
- output
101
+ File.open(output_filename, 'w') do |f|
102
+ f.print output
103
+ end
104
+ rescue Exception => e
105
+ $stdout.print "A problem occured when trying to write to file #{output_filename}:"
106
+ $stdout.print e.message
107
+ exit 1
80
108
  end
109
+ else
110
+ $stdout.print output
81
111
  end
82
-
83
- $stdout.print output
@@ -0,0 +1,23 @@
1
+ Feature: ignoring parts of a file
2
+
3
+ In order to not mess up mixed files
4
+ As a web developer using typogruby
5
+ I want to filter just HTML and not Javascript
6
+
7
+ Scenario:
8
+ Given a file named "input.html" with:
9
+ """
10
+ <script>
11
+ document.write("<p>Foo BAR!</p>");
12
+ </script>
13
+ <p>"This IS a simple file!</p>
14
+ """
15
+ When I run "typogruby input.html"
16
+ Then the output should contain exactly:
17
+ """
18
+ <script>
19
+ document.write("<p>Foo BAR!</p>");
20
+ </script>
21
+ <p><span class="dquo">&#8220;</span>This <span class="caps">IS</span> a simple&nbsp;file!</p>
22
+ """
23
+ And the exit status should be 0
@@ -0,0 +1,31 @@
1
+ Feature: various inputs
2
+
3
+ In order to work quickly
4
+ As a hasty web developer using typogruby
5
+ I want to use various input methods
6
+
7
+ Background:
8
+ Given a file named "input.html" with:
9
+ """
10
+ <p>"This IS a simple file!</p>
11
+ """
12
+ And a file named "input2.html" with:
13
+ """
14
+ <p>This is another file</p>
15
+ """
16
+
17
+ Scenario: take input from STDIN
18
+ When I run "cat input.html | typogruby"
19
+ Then the output should contain exactly:
20
+ """
21
+ <p><span class="dquo">&#8220;</span>This <span class="caps">IS</span> a simple&nbsp;file!</p>
22
+ """
23
+ And the exit status should be 0
24
+
25
+ Scenario: multiple input files
26
+ When I run "typogruby input.html input2.html"
27
+ Then the output should contain exactly:
28
+ """
29
+ <p><span class="dquo">&#8220;</span>This <span class="caps">IS</span> a simple&nbsp;file!</p><p>This is another&nbsp;file</p>
30
+ """
31
+ And the exit status should be 0
@@ -0,0 +1,43 @@
1
+ Feature: selective filtering
2
+
3
+ In order to get just the results I like
4
+ As a picky web developer using typogruby
5
+ I want to only apply certain filters to a file
6
+
7
+ Background:
8
+ Given a file named "input.html" with:
9
+ """
10
+ <p>"This IS a simple file!</p>
11
+ """
12
+
13
+ Scenario: using just one filter
14
+ When I run "typogruby -w input.html"
15
+ Then the output should contain exactly:
16
+ """
17
+ <p>"This IS a simple&nbsp;file!</p>
18
+ """
19
+ And the exit status should be 0
20
+
21
+ Scenario: using just several filters
22
+ When I run "typogruby -cw input.html"
23
+ Then the output should contain exactly:
24
+ """
25
+ <p>"This <span class="caps">IS</span> a simple&nbsp;file!</p>
26
+ """
27
+ And the exit status should be 0
28
+
29
+ Scenario: excluding a single filter
30
+ When I run "typogruby --no-widows input.html"
31
+ Then the output should contain exactly:
32
+ """
33
+ <p><span class="dquo">&#8220;</span>This <span class="caps">IS</span> a simple file!</p>
34
+ """
35
+ And the exit status should be 0
36
+
37
+ Scenario: excluding multiple filters
38
+ When I run "typogruby --no-widows --no-caps input.html"
39
+ Then the output should contain exactly:
40
+ """
41
+ <p><span class="dquo">&#8220;</span>This IS a simple file!</p>
42
+ """
43
+ And the exit status should be 0
@@ -0,0 +1,38 @@
1
+ Feature: Simple usage
2
+
3
+ In order to improve my web file's typography
4
+ As a web developer using typogruby
5
+ I want to apply typogruby to a file
6
+
7
+ Scenario: getting help
8
+ When I run "typogruby -h"
9
+ Then the output should contain "Usage: typogruby [options] filename [filename, ...]"
10
+ And the exit status should be 0
11
+
12
+ Scenario: getting the version number
13
+ When I run "typogruby -v"
14
+ Then the output should contain "Typogruby "
15
+
16
+ Scenario: filtering a file
17
+ Given a file named "input.html" with:
18
+ """
19
+ <p>"This IS a simple file!</p>
20
+ """
21
+ When I run "typogruby input.html"
22
+ Then the output should contain exactly:
23
+ """
24
+ <p><span class="dquo">&#8220;</span>This <span class="caps">IS</span> a simple&nbsp;file!</p>
25
+ """
26
+ And the exit status should be 0
27
+
28
+ Scenario: Writing output to a file
29
+ Given a file named "input.html" with:
30
+ """
31
+ <p>"This IS a simple file!</p>
32
+ """
33
+ When I run "typogruby -o output.html input.html"
34
+ Then the file "output.html" should contain exactly:
35
+ """
36
+ <p><span class="dquo">&#8220;</span>This <span class="caps">IS</span> a simple&nbsp;file!</p>
37
+ """
38
+ And the exit status should be 0
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'typogruby'
3
+ require 'bundler'
4
+ Bundler.require :default, :test
5
+
@@ -0,0 +1,252 @@
1
+ 160 nbsp
2
+ 161 iexcl
3
+ 162 cent
4
+ 163 pound
5
+ 164 curren
6
+ 165 yen
7
+ 166 brvbar
8
+ 167 sect
9
+ 168 uml
10
+ 169 copy
11
+ 170 ordf
12
+ 171 laquo
13
+ 172 not
14
+ 173 shy
15
+ 174 reg
16
+ 175 macr
17
+ 176 deg
18
+ 177 plusmn
19
+ 178 sup2
20
+ 179 sup3
21
+ 180 acute
22
+ 181 micro
23
+ 182 para
24
+ 183 middot
25
+ 184 cedil
26
+ 185 sup1
27
+ 186 ordm
28
+ 187 raquo
29
+ 188 frac14
30
+ 189 frac12
31
+ 190 frac34
32
+ 191 iquest
33
+ 192 Agrave
34
+ 193 Aacute
35
+ 194 Acirc
36
+ 195 Atilde
37
+ 196 Auml
38
+ 197 Aring
39
+ 198 AElig
40
+ 199 Ccedil
41
+ 200 Egrave
42
+ 201 Eacute
43
+ 202 Ecirc
44
+ 203 Euml
45
+ 204 Igrave
46
+ 205 Iacute
47
+ 206 Icirc
48
+ 207 Iuml
49
+ 208 ETH
50
+ 209 Ntilde
51
+ 210 Ograve
52
+ 211 Oacute
53
+ 212 Ocirc
54
+ 213 Otilde
55
+ 214 Ouml
56
+ 215 times
57
+ 216 Oslash
58
+ 217 Ugrave
59
+ 218 Uacute
60
+ 219 Ucirc
61
+ 220 Uuml
62
+ 221 Yacute
63
+ 222 THORN
64
+ 223 szlig
65
+ 224 agrave
66
+ 225 aacute
67
+ 226 acirc
68
+ 227 atilde
69
+ 228 auml
70
+ 229 aring
71
+ 230 aelig
72
+ 231 ccedil
73
+ 232 egrave
74
+ 233 eacute
75
+ 234 ecirc
76
+ 235 euml
77
+ 236 igrave
78
+ 237 iacute
79
+ 238 icirc
80
+ 239 iuml
81
+ 240 eth
82
+ 241 ntilde
83
+ 242 ograve
84
+ 243 oacute
85
+ 244 ocirc
86
+ 245 otilde
87
+ 246 ouml
88
+ 247 divide
89
+ 248 oslash
90
+ 249 ugrave
91
+ 250 uacute
92
+ 251 ucirc
93
+ 252 uuml
94
+ 253 yacute
95
+ 254 thorn
96
+ 255 yuml
97
+ 402 fnof
98
+ 913 Alpha
99
+ 914 Beta
100
+ 915 Gamma
101
+ 916 Delta
102
+ 917 Epsilon
103
+ 918 Zeta
104
+ 919 Eta
105
+ 920 Theta
106
+ 921 Iota
107
+ 922 Kappa
108
+ 923 Lambda
109
+ 924 Mu
110
+ 925 Nu
111
+ 926 Xi
112
+ 927 Omicron
113
+ 928 Pi
114
+ 929 Rho
115
+ 931 Sigma
116
+ 932 Tau
117
+ 933 Upsilon
118
+ 934 Phi
119
+ 935 Chi
120
+ 936 Psi
121
+ 937 Omega
122
+ 945 alpha
123
+ 946 beta
124
+ 947 gamma
125
+ 948 delta
126
+ 949 epsilon
127
+ 950 zeta
128
+ 951 eta
129
+ 952 theta
130
+ 953 iota
131
+ 954 kappa
132
+ 955 lambda
133
+ 956 mu
134
+ 957 nu
135
+ 958 xi
136
+ 959 omicron
137
+ 960 pi
138
+ 961 rho
139
+ 962 sigmaf
140
+ 963 sigma
141
+ 964 tau
142
+ 965 upsilon
143
+ 966 phi
144
+ 967 chi
145
+ 968 psi
146
+ 969 omega
147
+ 977 thetasym
148
+ 978 upsih
149
+ 982 piv
150
+ 8226 bull
151
+ 8230 hellip
152
+ 8242 prime
153
+ 8243 Prime
154
+ 8254 oline
155
+ 8260 frasl
156
+ 8472 weierp
157
+ 8465 image
158
+ 8476 real
159
+ 8482 trade
160
+ 8501 alefsym
161
+ 8592 larr
162
+ 8593 uarr
163
+ 8594 rarr
164
+ 8595 darr
165
+ 8596 harr
166
+ 8629 crarr
167
+ 8656 lArr
168
+ 8657 uArr
169
+ 8658 rArr
170
+ 8659 dArr
171
+ 8660 hArr
172
+ 8704 forall
173
+ 8706 part
174
+ 8707 exist
175
+ 8709 empty
176
+ 8711 nabla
177
+ 8712 isin
178
+ 8713 notin
179
+ 8715 ni
180
+ 8719 prod
181
+ 8721 sum
182
+ 8722 minus
183
+ 8727 lowast
184
+ 8730 radic
185
+ 8733 prop
186
+ 8734 infin
187
+ 8736 ang
188
+ 8743 and
189
+ 8744 or
190
+ 8745 cap
191
+ 8746 cup
192
+ 8747 int
193
+ 8756 there4
194
+ 8764 sim
195
+ 8773 cong
196
+ 8776 asymp
197
+ 8800 ne
198
+ 8801 equiv
199
+ 8804 le
200
+ 8805 ge
201
+ 8834 sub
202
+ 8835 sup
203
+ 8836 nsub
204
+ 8838 sube
205
+ 8839 supe
206
+ 8853 oplus
207
+ 8855 otimes
208
+ 8869 perp
209
+ 8901 sdot
210
+ 8968 lceil
211
+ 8969 rceil
212
+ 8970 lfloor
213
+ 8971 rfloor
214
+ 9001 lang
215
+ 9002 rang
216
+ 9674 loz
217
+ 9824 spades
218
+ 9827 clubs
219
+ 9829 hearts
220
+ 9830 diams
221
+ 34 quot
222
+ 38 amp
223
+ 60 lt
224
+ 62 gt
225
+ 338 OElig
226
+ 339 oelig
227
+ 352 Scaron
228
+ 353 scaron
229
+ 376 Yuml
230
+ 710 circ
231
+ 732 tilde
232
+ 8194 ensp
233
+ 8195 emsp
234
+ 8201 thinsp
235
+ 8204 zwnj
236
+ 8205 zwj
237
+ 8206 lrm
238
+ 8207 rlm
239
+ 8211 ndash
240
+ 8212 mdash
241
+ 8216 lsquo
242
+ 8217 rsquo
243
+ 8218 sbquo
244
+ 8220 ldquo
245
+ 8221 rdquo
246
+ 8222 bdquo
247
+ 8224 dagger
248
+ 8225 Dagger
249
+ 8240 permil
250
+ 8249 lsaquo
251
+ 8250 rsaquo
252
+ 8364 euro
data/lib/typogruby.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubypants'
2
2
  require 'digest/md5'
3
+ $KCODE = 'U'
3
4
 
4
5
  # A collection of simple helpers for improving web
5
6
  # typograhy. Based on TypographyHelper by Luke Hartman and Typogrify.
@@ -187,6 +188,34 @@ module Typogruby
187
188
  end
188
189
  end
189
190
 
191
+ # Converts special characters (excluding HTML tags) to HTML entities.
192
+ #
193
+ # @example
194
+ # entities("Aloë Vera") # => "Alo&euml; Vera"
195
+ #
196
+ # @param [String] text input text
197
+ # @return [String] input text with all special characters converted to
198
+ # HTML entities.
199
+ def entities(text)
200
+ o = ''
201
+ text.scan(/(?x)
202
+
203
+ ( <\?(?:[^?]*|\?(?!>))*\?>
204
+ | <!-- (?m:.*?) -->
205
+ | <\/? (?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
206
+ (?:[^>"']|"[^"]*"|'[^']*')*
207
+ >
208
+ | &(?:[a-zA-Z0-9]+|\#[0-9]+|\#x[0-9a-fA-F]+);
209
+ )
210
+ |([^<&]+|[<&])
211
+
212
+ /x) do |tag, text|
213
+ o << tag.to_s
214
+ o << encode(text.to_s)
215
+ end
216
+ o
217
+ end
218
+
190
219
  # main function to do all the functions from the method.
191
220
  # @param [String] text input text
192
221
  # @return [String] input text with all filters applied
@@ -196,6 +225,26 @@ module Typogruby
196
225
 
197
226
  private
198
227
 
228
+ # Convert characters from the map in ./lib/characters.txt
229
+ # Code taken from TextMate HTML bundle
230
+ # @param [String] text input text
231
+ # @return [String] input text with all special characters converted to
232
+ # HTML entities.
233
+ def encode(text)
234
+ @char_to_entity ||= begin
235
+ map = {}
236
+ File.read(File.join(File.dirname(__FILE__), 'characters.txt')).scan(/^(\d+)\s*(.+)$/) do |key, value|
237
+ map[[key.to_i].pack('U')] = value
238
+ end
239
+ map
240
+ end
241
+
242
+ text.gsub(/[^\x00-\x7F]|["'<>&]/) do |ch|
243
+ ent = @char_to_entity[ch]
244
+ ent ? "&#{ent};" : sprintf("&#x%02X;", ch.unpack("U")[0])
245
+ end
246
+ end
247
+
199
248
  # Hackish text filter that will make sure our text filters leave inline
200
249
  # javascript alone without resorting to a full-blown HTML parser.
201
250
  #
@@ -216,4 +265,4 @@ private
216
265
  end
217
266
 
218
267
  extend self
219
- end
268
+ end
@@ -91,4 +91,9 @@ class TestTypogruby < Test::Unit::TestCase
91
91
  assert_equal "<script>\nreturn window;\n</script>", widont("<script>\nreturn window;\n</script>")
92
92
  assert_equal '<div><p>But divs with paragraphs&nbsp;do!</p></div>', widont('<div><p>But divs with paragraphs do!</p></div>')
93
93
  end
94
- end
94
+
95
+ def test_should_convert_entities
96
+ assert_equal "Vari&euml;ren&hellip;", entities('Variëren…')
97
+ assert_equal "<p>Ol&eacute;</p>", entities('<p>Olé</p>')
98
+ end
99
+ 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: 25
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 7
10
- version: 1.0.7
9
+ - 8
10
+ version: 1.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Arjan van der Gaag
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-23 00:00:00 +02:00
18
+ date: 2010-11-06 00:00:00 +01:00
19
19
  default_executable: typogruby
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -73,12 +73,19 @@ files:
73
73
  - .document
74
74
  - .gitignore
75
75
  - Gemfile
76
+ - Gemfile.lock
76
77
  - HISTORY.md
77
78
  - LICENSE
78
79
  - README.md
79
80
  - Rakefile
80
81
  - VERSION
81
82
  - bin/typogruby
83
+ - features/ignoring.feature
84
+ - features/inputs.feature
85
+ - features/selective_filtering.feature
86
+ - features/simple.feature
87
+ - features/support/env.rb
88
+ - lib/characters.txt
82
89
  - lib/typogruby.rb
83
90
  - test/test_typogruby.rb
84
91
  - typogrify.gemspec