This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.html
164 lines (155 loc) · 12.9 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>nytimes-style.rb</title>
<link rel="stylesheet" href="docco.css">
</head>
<body>
<div id='container'>
<div id="background"></div>
<table cellspacing=0 cellpadding=0>
<thead>
<tr>
<th class=docs><h1>nytimes-style.rb</h1></th>
<th class=code></th>
</tr>
</thead>
<tbody>
<tr id='section-1'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</td>
<td class=code>
<div class='highlight'><pre><span class="nb">require</span> <span class="s1">'date'</span>
<span class="nb">require</span> <span class="s1">'yaml'</span></pre></div>
</td>
</tr>
<tr id='section-2'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>A small set of helper methods for generating text that conforms to <em>The New York Times Manual of Style and Usage</em>,
hosted on <a href="https://github.com/ascheink/nytimes-style">Github</a>.</p>
</td>
<td class=code>
<div class='highlight'><pre><span class="k">module</span> <span class="nn">Nytimes</span>
<span class="k">module</span> <span class="nn">Style</span>
</pre></div>
</td>
</tr>
<tr id='section-3'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<blockquote>
<p>“In general, spell out the first nine cardinal and
ordinal numbers [but] spell any number that begins a sentence…”
Exceptions include “ages of people
and animals,” “sums of money,” “degrees of temperature” and “mentions of the Twelve
Apostles and the Ten Commandments.”</p>
</blockquote>
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">nytimes_number</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="k">if</span> <span class="n">n</span> <span class="o"><</span> <span class="mi">10</span>
<span class="sx">%w(one two three four five six seven eight nine)</span><span class="o">[</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="o">]</span>
<span class="k">else</span>
<span class="n">n</span><span class="o">.</span><span class="n">to_s</span>
<span class="k">end</span>
<span class="k">end</span></pre></div>
</td>
</tr>
<tr id='section-4'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<blockquote>
<p>“Abbreviate the names of months from August through
February in news copy when they are followed by numerals: Aug. 1; Sept.
2; Oct. 3; Nov. 4; Dec. 5; Jan. 6; Feb. 7. Do not abbreviate March,
April, May, June and July except as a last resort in a chart or table.”</p>
</blockquote>
</td>
<td class=code>
<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> <span class="n">opts</span><span class="o">=</span><span class="p">{})</span>
<span class="n">str</span> <span class="o">=</span> <span class="s2">""</span>
<span class="n">str</span> <span class="o"><<</span> <span class="n">date</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">'%A, '</span><span class="p">)</span> <span class="k">if</span> <span class="n">opts</span><span class="o">[</span><span class="ss">:day_of_week</span><span class="o">]</span>
<span class="n">str</span> <span class="o"><<</span> <span class="n">nytimes_month_and_day</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
<span class="n">str</span> <span class="o"><<</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">"</span> <span class="k">unless</span> <span class="n">opts</span><span class="o">[</span><span class="ss">:hide_current_year</span><span class="o">]</span> <span class="o">&&</span> <span class="n">date</span><span class="o">.</span><span class="n">year</span> <span class="o">==</span> <span class="no">Date</span><span class="o">.</span><span class="n">today</span><span class="o">.</span><span class="n">year</span>
<span class="k">return</span> <span class="n">str</span>
<span class="k">end</span>
<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>
<span class="s2">"</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">"</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">nytimes_month</span><span class="p">(</span><span class="n">month</span><span class="p">)</span>
<span class="k">raise</span> <span class="no">ArgumentError</span><span class="o">.</span><span class="n">new</span> <span class="s2">"Unknown month: </span><span class="si">#{</span><span class="n">month</span><span class="si">}</span><span class="s2">"</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>
<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>
<span class="k">end</span>
</pre></div>
</td>
</tr>
<tr id='section-5'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<blockquote>
<p>“Use numerals in giving clock time: 10:30 a.m.; 10:30.
Do not use half-past 10 except in a direct quotation.
Also avoid the redundant 10:30 a.m. yesterday morning and Monday afternoon at 2 p.m.”</p>
</blockquote>
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">nytimes_time</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">opts</span><span class="o">=</span><span class="p">{})</span>
<span class="k">raise</span> <span class="no">ArgumentError</span><span class="o">.</span><span class="n">new</span> <span class="s2">"Time or DateTime required"</span> <span class="k">unless</span> <span class="n">time</span><span class="o">.</span><span class="n">class</span> <span class="o">==</span> <span class="no">DateTime</span> <span class="o">||</span> <span class="n">time</span><span class="o">.</span><span class="n">class</span> <span class="o">==</span> <span class="no">Time</span>
<span class="n">str</span> <span class="o">=</span> <span class="s2">""</span>
<span class="n">str</span> <span class="o"><<</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">"%l:%M"</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span>
<span class="n">str</span> <span class="o"><<</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">" %p"</span><span class="p">)</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">'PM'</span><span class="p">,</span><span class="s1">'p.m.'</span><span class="p">)</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">'AM'</span><span class="p">,</span><span class="s1">'a.m.'</span><span class="p">)</span> <span class="k">unless</span> <span class="n">opts</span><span class="o">[</span><span class="ss">:hide_abbreviation</span><span class="o">]</span>
<span class="n">str</span>
<span class="k">end</span></pre></div>
</td>
</tr>
<tr id='section-6'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<blockquote>
<p>“The abbreviation to be used for each state, after the names of cities,
towns and counties… Use no
spaces between initials like N.H. Do not abbreviate Alaska, Hawaii, Idaho,
Iowa, Ohio and Utah. (Do not ordinarily use the Postal Service’s
two-letter abbreviations; some are hard to tell apart on quick reading.)”</p>
</blockquote>
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">nytimes_state_abbrev</span><span class="p">(</span><span class="n">state_name_or_code</span><span class="p">)</span>
<span class="n">state</span> <span class="o">=</span> <span class="n">states</span><span class="o">[</span><span class="n">state_name_or_code</span><span class="o">]</span>
<span class="k">raise</span> <span class="no">ArgumentError</span><span class="o">.</span><span class="n">new</span> <span class="s2">"Unknown postal code, state name or FIPS code: </span><span class="si">#{</span><span class="n">state_name_or_code</span><span class="si">}</span><span class="s2">"</span> <span class="k">unless</span> <span class="n">state</span>
<span class="n">state</span><span class="o">[</span><span class="s1">'nytimes_abbrev'</span><span class="o">]</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">nytimes_state_name</span><span class="p">(</span><span class="n">state_abbrev_or_code</span><span class="p">)</span>
<span class="n">state</span> <span class="o">=</span> <span class="n">states</span><span class="o">[</span><span class="n">state_abbrev_or_code</span><span class="o">]</span>
<span class="k">raise</span> <span class="no">ArgumentError</span><span class="o">.</span><span class="n">new</span> <span class="s2">"Unknown postal code, abbreviation or FIPS code: </span><span class="si">#{</span><span class="n">state_abbrev_or_code</span><span class="si">}</span><span class="s2">"</span> <span class="k">unless</span> <span class="n">state</span>
<span class="n">state</span><span class="o">[</span><span class="s1">'name'</span><span class="o">]</span>
<span class="k">end</span>
<span class="kp">private</span>
<span class="no">STATE_DATA_FILE</span> <span class="o">=</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">'nytimes-style/states.yml'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">states</span>
<span class="vi">@states</span> <span class="o">||=</span> <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">STATE_DATA_FILE</span><span class="p">))</span><span class="o">.</span><span class="n">inject</span><span class="p">({})</span> <span class="k">do</span> <span class="o">|</span><span class="n">h</span><span class="p">,</span> <span class="n">state</span><span class="o">|</span>
<span class="n">h</span><span class="o">.</span><span class="n">merge</span><span class="p">({</span> <span class="n">state</span><span class="o">[</span><span class="s1">'postal_code'</span><span class="o">]</span> <span class="o">=></span> <span class="n">state</span><span class="p">,</span> <span class="n">state</span><span class="o">[</span><span class="s1">'name'</span><span class="o">]</span> <span class="o">=></span> <span class="n">state</span><span class="p">,</span> <span class="n">state</span><span class="o">[</span><span class="s1">'fips_code'</span><span class="o">]</span> <span class="o">=></span> <span class="n">state</span> <span class="p">})</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span></pre></div>
</td>
</tr>
</table>
</div>
</body>