nytimes-style 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/README.rdoc ADDED
@@ -0,0 +1,24 @@
1
+ = nytimes-style
2
+
3
+ Helper methods for generating text that conforms to _The New York Times Manual of Style and Usage_.
4
+
5
+ Annotated source code: http://ascheink.github.com/nytimes-style
6
+
7
+ == INSTALLATION
8
+
9
+ gem install nytimes-style
10
+
11
+ == USAGE
12
+
13
+ require 'nytimes-style'
14
+ include Nytimes::Style
15
+
16
+ >> nytimes_date Date.today
17
+ # => "May 12, 2011"
18
+
19
+ >> nytimes_state_abbrev 'AZ'
20
+ # => "Ariz."
21
+
22
+ == AUTHOR
23
+
24
+ Andrei Scheinkman, andrei@nytimes.com
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ desc 'Run all tests'
2
+ task :test do
3
+ $LOAD_PATH.unshift(File.expand_path('test'))
4
+ require 'redgreen' if Gem.available?('redgreen')
5
+ require 'test/unit'
6
+ require './test/test_nytimes_style'
7
+ end
8
+
9
+ desc 'Build annotated source code'
10
+ task :docs do
11
+ sh 'rocco lib/nytimes-style.rb && mv lib/nytimes-style.html ./index.html'
12
+ end
13
+
14
+ namespace :gem do
15
+
16
+ desc 'Build and install the gem'
17
+ task :install do
18
+ sh "gem build nytimes-style.gemspec"
19
+ sh "sudo gem install #{Dir['*.gem'].join(' ')} --local --no-ri --no-rdoc"
20
+ end
21
+
22
+ desc 'Uninstall the jammit gem'
23
+ task :uninstall do
24
+ sh "sudo gem uninstall -x nytimes-style"
25
+ end
26
+
27
+ end
data/index.html ADDED
@@ -0,0 +1,105 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html;charset=utf-8">
5
+ <title>nytimes-style.rb</title>
6
+ <link rel="stylesheet" href="http://github.com/jashkenas/docco/raw/0.3.0/resources/docco.css">
7
+ </head>
8
+ <body>
9
+ <div id='container'>
10
+ <div id="background"></div>
11
+ <table cellspacing=0 cellpadding=0>
12
+ <thead>
13
+ <tr>
14
+ <th class=docs><h1>nytimes-style.rb</h1></th>
15
+ <th class=code></th>
16
+ </tr>
17
+ </thead>
18
+ <tbody>
19
+ <tr id='section-1'>
20
+ <td class=docs>
21
+ <div class="pilwrap">
22
+ <a class="pilcrow" href="#section-1">&#182;</a>
23
+ </div>
24
+
25
+ </td>
26
+ <td class=code>
27
+ <div class='highlight'><pre><span class="nb">require</span> <span class="s1">&#39;date&#39;</span>
28
+ <span class="nb">require</span> <span class="s1">&#39;yaml&#39;</span></pre></div>
29
+ </td>
30
+ </tr>
31
+ <tr id='section-2'>
32
+ <td class=docs>
33
+ <div class="pilwrap">
34
+ <a class="pilcrow" href="#section-2">&#182;</a>
35
+ </div>
36
+ <p>Helper methods for generating text that conforms to <em>The New York Times Manual of Style and Usage</em>.</p>
37
+ </td>
38
+ <td class=code>
39
+ <div class='highlight'><pre><span class="k">module</span> <span class="nn">Nytimes</span>
40
+ <span class="k">module</span> <span class="nn">Style</span></pre></div>
41
+ </td>
42
+ </tr>
43
+ <tr id='section-3'>
44
+ <td class=docs>
45
+ <div class="pilwrap">
46
+ <a class="pilcrow" href="#section-3">&#182;</a>
47
+ </div>
48
+ <blockquote><p>&ldquo;Abbreviate the names of months from August through
49
+ February in news copy when they are followed by numerals: Aug. 1; Sept.
50
+ 2; Oct. 3; Nov. 4; Dec. 5; Jan. 6; Feb. 7. Do not abbreviate March,
51
+ April, May, June and July except as a last resort in a chart or table.&rdquo;</p></blockquote>
52
+ </td>
53
+ <td class=code>
54
+ <div class='highlight'><pre> <span class="k">def</span> <span class="nf">nytimes_date</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
55
+ <span class="s2">&quot;</span><span class="si">#{</span><span class="n">nytimes_month_and_day</span> <span class="n">date</span><span class="si">}</span><span class="s2">, </span><span class="si">#{</span><span class="n">date</span><span class="o">.</span><span class="n">year</span><span class="si">}</span><span class="s2">&quot;</span>
56
+ <span class="k">end</span>
57
+
58
+ <span class="k">def</span> <span class="nf">nytimes_month_and_day</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
59
+ <span class="s2">&quot;</span><span class="si">#{</span><span class="n">nytimes_month</span> <span class="n">date</span><span class="o">.</span><span class="n">month</span><span class="si">}</span><span class="s2"> </span><span class="si">#{</span><span class="n">date</span><span class="o">.</span><span class="n">day</span><span class="si">}</span><span class="s2">&quot;</span>
60
+ <span class="k">end</span>
61
+
62
+ <span class="k">def</span> <span class="nf">nytimes_month</span><span class="p">(</span><span class="n">month</span><span class="p">)</span>
63
+ <span class="k">raise</span> <span class="no">ArgumentError</span><span class="o">.</span><span class="n">new</span> <span class="s2">&quot;Unknown month: </span><span class="si">#{</span><span class="n">month</span><span class="si">}</span><span class="s2">&quot;</span> <span class="k">unless</span> <span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="n">.</span><span class="mi">12</span><span class="p">)</span><span class="o">.</span><span class="n">include?</span> <span class="n">month</span>
64
+ <span class="sx">%w(Jan. Feb. March April May June July Aug. Sept. Oct. Nov. Dec.)</span><span class="o">[</span><span class="n">month</span> <span class="o">-</span> <span class="mi">1</span><span class="o">]</span>
65
+ <span class="k">end</span></pre></div>
66
+ </td>
67
+ </tr>
68
+ <tr id='section-4'>
69
+ <td class=docs>
70
+ <div class="pilwrap">
71
+ <a class="pilcrow" href="#section-4">&#182;</a>
72
+ </div>
73
+ <blockquote><p>&ldquo;The abbreviation to be used for each state, after the names of cities,
74
+ towns and counties&hellip; Use no
75
+ spaces between initials like N.H. Do not abbreviate Alaska, Hawaii, Idaho,
76
+ Iowa, Ohio and Utah. (Do not ordinarily use the Postal Service’s
77
+ two-letter abbreviations; some are hard to tell apart on quick reading.)&rdquo;</p></blockquote>
78
+
79
+ </td>
80
+ <td class=code>
81
+ <div class='highlight'><pre> <span class="k">def</span> <span class="nf">nytimes_state_abbrev</span><span class="p">(</span><span class="n">postal_code_or_state_name</span><span class="p">)</span>
82
+ <span class="n">state</span> <span class="o">=</span> <span class="n">states</span><span class="o">[</span><span class="n">postal_code_or_state_name</span><span class="o">]</span>
83
+ <span class="k">raise</span> <span class="no">ArgumentError</span><span class="o">.</span><span class="n">new</span> <span class="s2">&quot;Unknown postal code or state name: </span><span class="si">#{</span><span class="n">postal_code_or_state_name</span><span class="si">}</span><span class="s2">&quot;</span> <span class="k">unless</span> <span class="n">state</span>
84
+ <span class="n">state</span><span class="o">[</span><span class="s1">&#39;nytimes_abbrev&#39;</span><span class="o">]</span>
85
+ <span class="k">end</span>
86
+
87
+ <span class="kp">private</span>
88
+
89
+ <span class="k">def</span> <span class="nf">states</span>
90
+ <span class="k">unless</span> <span class="vi">@states</span>
91
+ <span class="vi">@states</span> <span class="o">=</span> <span class="p">{}</span>
92
+ <span class="no">YAML</span><span class="o">::</span><span class="nb">load</span><span class="p">(</span><span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="no">File</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="no">File</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="bp">__FILE__</span><span class="p">),</span> <span class="s1">&#39;nytimes-style/states.yml&#39;</span><span class="p">)))</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">state</span><span class="o">|</span>
93
+ <span class="vi">@states</span><span class="o">[</span><span class="n">state</span><span class="o">[</span><span class="s1">&#39;postal_code&#39;</span><span class="o">]]</span> <span class="o">=</span> <span class="vi">@states</span><span class="o">[</span><span class="n">state</span><span class="o">[</span><span class="s1">&#39;name&#39;</span><span class="o">]]</span> <span class="o">=</span> <span class="n">state</span>
94
+ <span class="k">end</span>
95
+ <span class="k">end</span>
96
+ <span class="k">return</span> <span class="vi">@states</span>
97
+ <span class="k">end</span>
98
+
99
+ <span class="k">end</span>
100
+ <span class="k">end</span></pre></div>
101
+ </td>
102
+ </tr>
103
+ </table>
104
+ </div>
105
+ </body>
@@ -0,0 +1,49 @@
1
+ require 'date'
2
+ require 'yaml'
3
+
4
+ # Helper methods for generating text that conforms to _The New York Times Manual of Style and Usage_.
5
+ module Nytimes
6
+ module Style
7
+
8
+ # > "Abbreviate the names of months from August through
9
+ # > February in news copy when they are followed by numerals: Aug. 1; Sept.
10
+ # > 2; Oct. 3; Nov. 4; Dec. 5; Jan. 6; Feb. 7. Do not abbreviate March,
11
+ # > April, May, June and July except as a last resort in a chart or table."
12
+ def nytimes_date(date)
13
+ "#{nytimes_month_and_day date}, #{date.year}"
14
+ end
15
+
16
+ def nytimes_month_and_day(date)
17
+ "#{nytimes_month date.month} #{date.day}"
18
+ end
19
+
20
+ def nytimes_month(month)
21
+ raise ArgumentError.new "Unknown month: #{month}" unless (1..12).include? month
22
+ %w(Jan. Feb. March April May June July Aug. Sept. Oct. Nov. Dec.)[month - 1]
23
+ end
24
+
25
+ # > "The abbreviation to be used for each state, after the names of cities,
26
+ # > towns and counties&hellip; Use no
27
+ # > spaces between initials like N.H. Do not abbreviate Alaska, Hawaii, Idaho,
28
+ # > Iowa, Ohio and Utah. (Do not ordinarily use the Postal Service’s
29
+ # > two-letter abbreviations; some are hard to tell apart on quick reading.)"
30
+ def nytimes_state_abbrev(postal_code_or_state_name)
31
+ state = states[postal_code_or_state_name]
32
+ raise ArgumentError.new "Unknown postal code or state name: #{postal_code_or_state_name}" unless state
33
+ state['nytimes_abbrev']
34
+ end
35
+
36
+ private
37
+
38
+ def states
39
+ unless @states
40
+ @states = {}
41
+ YAML::load(File.open(File.join(File.dirname(__FILE__), 'nytimes-style/states.yml'))).each do |state|
42
+ @states[state['postal_code']] = @states[state['name']] = state
43
+ end
44
+ end
45
+ return @states
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,153 @@
1
+ - nytimes_abbrev: Ala.
2
+ postal_code: AL
3
+ name: Alabama
4
+ - nytimes_abbrev: Alaska
5
+ postal_code: AK
6
+ name: Alaska
7
+ - nytimes_abbrev: Ariz.
8
+ postal_code: AZ
9
+ name: Arizona
10
+ - nytimes_abbrev: Ark.
11
+ postal_code: AR
12
+ name: Arkansas
13
+ - nytimes_abbrev: Calif.
14
+ postal_code: CA
15
+ name: California
16
+ - nytimes_abbrev: Colo.
17
+ postal_code: CO
18
+ name: Colorado
19
+ - nytimes_abbrev: Conn.
20
+ postal_code: CT
21
+ name: Connecticut
22
+ - nytimes_abbrev: Del.
23
+ postal_code: DE
24
+ name: Delaware
25
+ - nytimes_abbrev: D.C.
26
+ postal_code: DC
27
+ name: District of Columbia
28
+ - nytimes_abbrev: Fla.
29
+ postal_code: FL
30
+ name: Florida
31
+ - nytimes_abbrev: Ga.
32
+ postal_code: GA
33
+ name: Georgia
34
+ - nytimes_abbrev: Hawaii
35
+ postal_code: HI
36
+ name: Hawaii
37
+ - nytimes_abbrev: Idaho
38
+ postal_code: ID
39
+ name: Idaho
40
+ - nytimes_abbrev: Ill.
41
+ postal_code: IL
42
+ name: Illinois
43
+ - nytimes_abbrev: Ind.
44
+ postal_code: IN
45
+ name: Indiana
46
+ - nytimes_abbrev: Iowa
47
+ postal_code: IA
48
+ name: Iowa
49
+ - nytimes_abbrev: Kan.
50
+ postal_code: KS
51
+ name: Kansas
52
+ - nytimes_abbrev: Ky.
53
+ postal_code: KY
54
+ name: Kentucky
55
+ - nytimes_abbrev: La.
56
+ postal_code: LA
57
+ name: Louisiana
58
+ - nytimes_abbrev: Me.
59
+ postal_code: ME
60
+ name: Maine
61
+ - nytimes_abbrev: Md.
62
+ postal_code: MD
63
+ name: Maryland
64
+ - nytimes_abbrev: Mass.
65
+ postal_code: MA
66
+ name: Massachusetts
67
+ - nytimes_abbrev: Mich.
68
+ postal_code: MI
69
+ name: Michigan
70
+ - nytimes_abbrev: Minn.
71
+ postal_code: MN
72
+ name: Minnesota
73
+ - nytimes_abbrev: Miss.
74
+ postal_code: MS
75
+ name: Mississippi
76
+ - nytimes_abbrev: Mo.
77
+ postal_code: MO
78
+ name: Missouri
79
+ - nytimes_abbrev: Mont.
80
+ postal_code: MT
81
+ name: Montana
82
+ - nytimes_abbrev: Neb.
83
+ postal_code: NE
84
+ name: Nebraska
85
+ - nytimes_abbrev: Nev.
86
+ postal_code: NV
87
+ name: Nevada
88
+ - nytimes_abbrev: N.H.
89
+ postal_code: NH
90
+ name: New Hampshire
91
+ - nytimes_abbrev: N.J.
92
+ postal_code: NJ
93
+ name: New Jersey
94
+ - nytimes_abbrev: N.M.
95
+ postal_code: NM
96
+ name: New Mexico
97
+ - nytimes_abbrev: N.Y.
98
+ postal_code: NY
99
+ name: New York
100
+ - nytimes_abbrev: N.C.
101
+ postal_code: NC
102
+ name: North Carolina
103
+ - nytimes_abbrev: N.D.
104
+ postal_code: ND
105
+ name: North Dakota
106
+ - nytimes_abbrev: Ohio
107
+ postal_code: OH
108
+ name: Ohio
109
+ - nytimes_abbrev: Okla.
110
+ postal_code: OK
111
+ name: Oklahoma
112
+ - nytimes_abbrev: Ore.
113
+ postal_code: OR
114
+ name: Oregon
115
+ - nytimes_abbrev: Pa.
116
+ postal_code: PA
117
+ name: Pennsylvania
118
+ - nytimes_abbrev: R.I.
119
+ postal_code: RI
120
+ name: Rhode Island
121
+ - nytimes_abbrev: S.C.
122
+ postal_code: SC
123
+ name: South Carolina
124
+ - nytimes_abbrev: S.D.
125
+ postal_code: SD
126
+ name: South Dakota
127
+ - nytimes_abbrev: Tenn.
128
+ postal_code: TN
129
+ name: Tennessee
130
+ - nytimes_abbrev: Tex.
131
+ postal_code: TX
132
+ name: Texas
133
+ - nytimes_abbrev: Utah
134
+ postal_code: UT
135
+ name: Utah
136
+ - nytimes_abbrev: Vt.
137
+ postal_code: VT
138
+ name: Vermont
139
+ - nytimes_abbrev: Va.
140
+ postal_code: VA
141
+ name: Virginia
142
+ - nytimes_abbrev: Wash.
143
+ postal_code: WA
144
+ name: Washington
145
+ - nytimes_abbrev: W.Va.
146
+ postal_code: WV
147
+ name: West Virginia
148
+ - nytimes_abbrev: Wis.
149
+ postal_code: WI
150
+ name: Wisconsin
151
+ - nytimes_abbrev: Wyo.
152
+ postal_code: WY
153
+ name: Wyoming
@@ -0,0 +1,5 @@
1
+ module Nytimes
2
+ module Style
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "nytimes-style/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "nytimes-style"
7
+ s.version = Nytimes::Style::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Andrei Scheinkman"]
10
+ s.email = ["andrei@nytimes.com"]
11
+ s.homepage = "http://github.com/ascheink/nytimes-style"
12
+ s.summary = %q{New York Times style}
13
+ s.description = %q{Format numbers and dates according to The New York Times Manual of Style}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,19 @@
1
+ require './lib/nytimes-style'
2
+
3
+ class NytimesStyleTest < Test::Unit::TestCase
4
+ include Nytimes::Style
5
+
6
+ def test_dates
7
+ date = Date.civil(2001, 9, 11)
8
+ assert_equal nytimes_month_and_day(date), "Sept. 11"
9
+ assert_equal nytimes_date(date), "Sept. 11, 2001"
10
+ assert_raise(ArgumentError) { nytimes_month(0) }
11
+ end
12
+
13
+ def test_state_abbrevs
14
+ assert_equal nytimes_state_abbrev('AZ'), 'Ariz.'
15
+ assert_equal nytimes_state_abbrev('New Hampshire'), 'N.H.'
16
+ assert_raise(ArgumentError) { nytimes_state_abbrev('Canada') }
17
+ end
18
+
19
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nytimes-style
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Andrei Scheinkman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-12 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Format numbers and dates according to The New York Times Manual of Style
23
+ email:
24
+ - andrei@nytimes.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - README.rdoc
34
+ - Rakefile
35
+ - index.html
36
+ - lib/nytimes-style.rb
37
+ - lib/nytimes-style/states.yml
38
+ - lib/nytimes-style/version.rb
39
+ - nytimes-style.gemspec
40
+ - test/test_nytimes_style.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/ascheink/nytimes-style
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: New York Times style
75
+ test_files:
76
+ - test/test_nytimes_style.rb