Class: AnyStyle::Refs
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#append(line, label = 'ref') ⇒ Object
-
#commit(**opts) ⇒ Object
-
#delta_score(delta = @delta) ⇒ Object
-
#indent_depth(string) ⇒ Object
-
#indent_score(indent) ⇒ Object
-
#initial_score(a, b) ⇒ Object
-
#initialize(max_delta: 10) ⇒ Refs
constructor
-
#join(a, b) ⇒ Object
-
#join?(a, b, indent = 0, delta = @delta) ⇒ Boolean
-
#length_score(a, b) ⇒ Object
-
#match_list?(string) ⇒ Boolean
-
#match_pages?(string, not_years = true) ⇒ Boolean
-
#match_year?(string) ⇒ Boolean
-
#pages_score(a, b) ⇒ Object
-
#pending? ⇒ Boolean
-
#reset(delta: 0, indent: 0, pending: nil) ⇒ Object
-
#strip(string) ⇒ Object
-
#terminal_score(a, b) ⇒ Object
-
#to_a ⇒ Object
-
#years_score(a, b) ⇒ Object
canonize, count, display_chars, display_width, indent, nnum, page_break?, scrub, strip_html, transliterate
Constructor Details
#initialize(max_delta: 10) ⇒ Refs
Returns a new instance of Refs.
34
35
36
37
|
# File 'lib/anystyle/refs.rb', line 34
def initialize(max_delta: 10)
@all, @max_delta = [], max_delta
reset
end
|
Instance Attribute Details
#all ⇒ Object
Returns the value of attribute all.
32
33
34
|
# File 'lib/anystyle/refs.rb', line 32
def all
@all
end
|
#max_delta ⇒ Object
Returns the value of attribute max_delta.
32
33
34
|
# File 'lib/anystyle/refs.rb', line 32
def max_delta
@max_delta
end
|
Class Method Details
.normalize!(lines, max_win_size: 2) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/anystyle/refs.rb', line 12
def normalize!(lines, max_win_size: 2)
win = []
lines.each do |line|
case line.label
when 'text'
win << line
when 'ref'
unless win.length == 0 || win.length > max_win_size
win.each { |tk| tk.label = 'ref' }
end
win = []
when 'blank', 'meta'
else
win = []
end
end
end
|
.parse(lines, **opts) ⇒ Object
6
7
8
9
10
|
# File 'lib/anystyle/refs.rb', line 6
def parse(lines, **opts)
lines.inject(new(**opts)) do |refs, line|
refs.append line.value, line.label
end
end
|
Instance Method Details
#append(line, label = 'ref') ⇒ Object
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
|
# File 'lib/anystyle/refs.rb', line 57
def append(line, label = 'ref')
case label
when 'ref'
(line, at) = strip line
if pending?
if join?(@pending, line, at - @indent)
@pending = join @pending, line
else
commit pending: line, indent: at
end
else
reset pending: line, indent: at
end
when 'meta'
when 'blank'
if pending?
if @delta > max_delta
commit
else
@delta += 1
end
end
else
commit if pending?
end
self
end
|
#commit(**opts) ⇒ Object
43
44
45
46
|
# File 'lib/anystyle/refs.rb', line 43
def commit(**opts)
@all << @pending
reset(**opts)
end
|
#delta_score(delta = @delta) ⇒ Object
110
111
112
113
114
115
116
117
118
|
# File 'lib/anystyle/refs.rb', line 110
def delta_score(delta = @delta)
case delta
when 0 then 1
when 1 then 0.5
when 2, 3 then 0
else
delta > 6 ? -1 : -0.5
end
end
|
#indent_depth(string) ⇒ Object
261
262
263
|
# File 'lib/anystyle/refs.rb', line 261
def indent_depth(string)
string[/^\s*/].length
end
|
#indent_score(indent) ⇒ Object
101
102
103
104
105
106
107
108
|
# File 'lib/anystyle/refs.rb', line 101
def indent_score(indent)
case
when indent > 0 then 1.25
when indent < 0 then -1
else
0
end
end
|
#initial_score(a, b) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/anystyle/refs.rb', line 185
def initial_score(a, b)
case
when b.match(/^\p{Ll}/) && !b.match(/^(de|v[oa]n|v\.)\s/)
1.5
when a.match(/\p{L}$/) && b.match(/^\p{L}/)
1
when b.match(/^["'”„’‚´«「『‘“`»]/)
1
when b.match(/^(url|doi|isbn|vol)\b/i)
1.5
when b.match(/^([\p{Pd}_*][\p{Pd}_* ]+|\p{Co})/)
-1.5
when b.match(/^\((1[4-9]|2[01])\d\d\)/) && !a.match(/(\p{Lu}|al|others)\.$/)
-1
when b.match(/^\p{Lu}[\p{Ll}-]+,?\s\p{Lu}/) && !a.match(/\p{L}$/)
-0.5
when match_list?(b)
if match_list?(a)
-1.5
else
-0.75
end
when b.match(/^\p{L}+:/), b.match(/^\p{L}+ \d/)
0.5
else
0
end
end
|
#join(a, b) ⇒ Object
244
245
246
247
248
249
250
251
252
253
254
|
# File 'lib/anystyle/refs.rb', line 244
def join(a, b)
if a =~ /\p{Pd}$/
if a =~ /\p{Ll}-$/ && b =~ /^\p{Ll}/
"#{a[0...-1]}#{b}"
else
"#{a}#{b}"
end
else
"#{a} #{b}"
end
end
|
#join?(a, b, indent = 0, delta = @delta) ⇒ Boolean
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/anystyle/refs.rb', line 88
def join?(a, b, indent = 0, delta = @delta)
score = [
indent_score(indent),
delta_score(delta),
years_score(a, b),
terminal_score(a, b),
initial_score(a, b),
length_score(a, b),
pages_score(a, b)
]
score.reduce(&:+) >= 1
end
|
#length_score(a, b) ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/anystyle/refs.rb', line 218
def length_score(a, b)
case
when b.length < a.length
case
when b.length < 10
2.5
when b.length < 15
2
when b.length < 20
1.75
when b.length < 25
1.5
when b.length < 30
1
when b.length < 50
0.75
else
0
end
when (b.length - a.length) > 12
-0.5
else
0
end
end
|
#match_list?(string) ⇒ Boolean
214
215
216
|
# File 'lib/anystyle/refs.rb', line 214
def match_list?(string)
string.match(/^(\d{1,3}(\.\s+|\s{2,})\p{L}|\[\p{Alnum}+\])/)
end
|
#match_pages?(string, not_years = true) ⇒ Boolean
161
162
163
164
165
166
|
# File 'lib/anystyle/refs.rb', line 161
def match_pages?(string, not_years = true)
m = string.match(/(\d+)\p{Pd}(\d+)|\bpp?\.|\d+\(\d+\)/)
return false if m.nil?
return false if not_years && m[1] && match_year?(m[1]) && match_year?(m[2])
return true
end
|
#match_year?(string) ⇒ Boolean
145
146
147
|
# File 'lib/anystyle/refs.rb', line 145
def match_year?(string)
!!string.match(/\b(1[4-9]|2[01])\d\d[a-z]?\b/)
end
|
#pages_score(a, b) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/anystyle/refs.rb', line 149
def pages_score(a, b)
if match_pages?(a, true)
-0.25
else
if match_pages?(b, false)
1
else
0
end
end
end
|
#pending? ⇒ Boolean
53
54
55
|
# File 'lib/anystyle/refs.rb', line 53
def pending?
!@pending.nil?
end
|
#reset(delta: 0, indent: 0, pending: nil) ⇒ Object
39
40
41
|
# File 'lib/anystyle/refs.rb', line 39
def reset(delta: 0, indent: 0, pending: nil)
@delta, @indent, @pending = delta, indent, pending
end
|
#strip(string) ⇒ Object
256
257
258
259
|
# File 'lib/anystyle/refs.rb', line 256
def strip(string)
string = display_chars(string)
[string.lstrip, indent_depth(string)]
end
|
#terminal_score(a, b) ⇒ Object
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/anystyle/refs.rb', line 168
def terminal_score(a, b)
case
when a.match(/https?:\/\/\w+/i)
-0.25
when a.match(/[,;:&\p{Pd}]$/), a.match(/\s(et al|pp|pg)\.$/)
2
when a.match(/\((1[4-9]|2[01])\d\d\)\.?$/)
0
when a.match(/(\p{^Lu}\.|\])$/)
-1
when a.match(/\d$/) && b.match(/^\p{Lu}/)
-0.25
else
0
end
end
|
#to_a ⇒ Object
48
49
50
51
|
# File 'lib/anystyle/refs.rb', line 48
def to_a
commit if pending?
@all
end
|
#years_score(a, b) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/anystyle/refs.rb', line 120
def years_score(a, b)
if match_year?(a)
if match_year?(b)
case
when b.length < 18
1
when b.length < 25
0.5
when b.length > 60
-0.75
else
0
end
else
if a.match(/[\d,] \(?(1[4-9]|2[01])\d\d[a-z]?\)?\.$/)
-0.5
else
1
end
end
else
1
end
end
|