/
mklist.rb
executable file
·404 lines (338 loc) · 9.82 KB
/
mklist.rb
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
213
214
215
216
217
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
require "erb"
require "ostruct"
require "yaml"
require "fileutils"
require "set"
require "levenshtein"
def day_title(day)
"#{day == 1 ? "1er" : day} Décembre"
end
def day_anchor(day)
"dec15-#{day}"
end
# Find (possible) duplicates in a strings sequence. This is an expensive
# computation for long sequences of long strings
def duplicates(seq)
pairs = []
seq.each_with_index do |e1, i1|
seq.each_with_index do |e2, i2|
next if i1 == i2 || e1 > e2
# don't modify in-place
pair = [e1, e2]
e1 = e1.downcase
e2 = e2.downcase
pairs << pair if e1 == e2 || Levenshtein.distance(e1, e2) < 3
end
end
pairs
end
class Book
# Note that we also use this HTML to generate the LaTeX used for the PDF.
# This is why we use <div class="ref"> instead of h{1..6} for the title.
TEMPLATE = <<-EOS
<article class="book">
<div class="ref" id="<%= anchor %>"><b class="title"><%= title %></b><%= author_html %><% if link %> (<a href="<%= link %>">lien</a>)<% end %></div>
<div class="sources">
<% if sources %>
<% for s in sources %>
<blockquote class="comment">
<%= s["comment"] %><br/>
<span class="comment-author">— <%= s["author"] %></span>
</blockquote>
<% end %>
<% end %>
<% if recommenders %>
<p class="recommenders"><% if sources %>Également recommandé<% else %>Recommandé<% end %> par <%= recommenders %>.</p>
<% end %>
</div>
</article>
EOS
@@renderer = ERB.new(TEMPLATE, 3)
def initialize(hash, day, idx)
@attrs = hash
@day = day
@idx = idx
validate!
@attrs["title"].strip!
@attrs["author"].strip!
end
def title
@attrs["title"]
end
def author
@attrs["author"]
end
def authors
author.split(/(?:,\s+(?:et\s+)?|\s+et\s+)/)
end
def ref
"#{title}, #{author}"
end
def sources
@sources || @sources = sources_array.select { |s| s.has_key? "comment" }
end
def recommenders
# A recommender is a source without a comment.
@recommenders || @recommenders = sources_array.reject { |s| s.has_key? "comment" }
end
def validate!
must "have a title" unless title
must "have an author" unless author
if has? "sources"
sources = @attrs["sources"]
must "have sources" if sources.nil? || sources.empty?
must "use 'source' instead of 'sources' if there's only one" if sources.is_a? String
sources.each_with_index do |s, i|
must "have a 'source' attr for its source #{i}" unless s.has_key? "source"
must "use 'source' instead of 'link' for its source #{i}" if s.has_key? "link"
end
elsif !has? "source"
must "have a source"
else
source = @attrs["source"]
must "use 'sources' instead of 'source'" if source.is_a? Array
must "use Twitter for its source" if source !~ %r{^https?://twitter\.com/[^/]+/}
end
end
def to_s
"#{@attrs["title"]} (day #{@day}, ##{@idx})"
end
def to_html
id = "#{@attrs["title"]} #{@attrs["author"]}".gsub(/[^A-Za-z0-9]+/, "-")
@attrs["anchor"] = "#{day_anchor @day}-#{id}"
@attrs["author_html"] = author_html
@@renderer.result to_binding
end
private
def to_binding
attrs = @attrs.clone
attrs["sources"] = html_sources
attrs["recommenders"] = html_recommenders
attrs["title"] = htmlize attrs["title"]
# ERB wants bindings; not hashs. See https://bugs.ruby-lang.org/issues/8643
OpenStruct.new(attrs).instance_eval { binding }
end
def sources_array
if has? "source"
s = {"source" => @attrs["source"]}
s["comment"] = @attrs["comment"] if @attrs.has_key? "comment"
[s]
else
@attrs["sources"]
end
end
def has?(k)
@attrs.has_key?(k) && !@attrs[k].nil?
end
def must(s)
raise "Book '#{self}' must #{s}"
end
def author_html
return " (collectif)" if @attrs["author"] == "collectif"
prefix = if @attrs["directed"]
"dirigé par"
elsif @attrs["coordinated"]
"coordonné par"
elsif @attrs["type"] == "movie"
"réalisé par"
else
"par"
end
%(, #{prefix} <span class="author">#{@attrs["author"]}</span>)
end
def html_sources
sources.empty? ? nil : sources.map do |s|
s["comment"] = htmlize s["comment"].strip.gsub("\n", "<br/>")
s["author"] = html_source_author s
s
end
end
def html_recommenders
rs = recommenders.map { |r| html_source_author r }
case rs.size
when 0 then nil
when 1 then rs.first
when 2 then "#{rs.first} et #{rs.last}"
else
"#{rs[0..-2].join(", ")}, et #{rs[-1]}"
end
end
def html_source_author(s)
source = s["source"]
author = source.split("/")[3]
%(<a class="handle" href="#{source}">@#{author}</a>)
end
def htmlize(s)
s.gsub(/« /, "« ").gsub(/ »/, " »")
end
end
class BooksList
def initialize(source)
@days = YAML.load_file(source)
validate!
end
def write_jekyll(target)
File.open(target, "w") do |f|
write_jekyll_prelude f
write_html_file f
end
end
def write_html(target)
File.open(target, "w") do |f|
write_html_prelude f
write_html_file f
write_html_end f
end
end
def each_book
@days.each do |day, books|
books.each_with_index do |h, i|
yield Book.new(h, day, i)
end
end
end
def write_compact_markdown(target)
File.open(target, "w") do |f|
each_book do |book|
f.write "- *#{book.title}* (#{book.author})\n"
end
end
end
def validate!
titles = Set.new
authors = Set.new
each_book do |book|
# find duplicated books
titles << book.title
authors += book.authors
end
return unless ARGV.include? "--strict"
duplicates(authors).each do |(a1, a2)|
puts "'#{a1}' and '#{a2}' might be the same authors"
end
duplicates(titles).each do |(t1, t2)|
puts "'#{t1}' and '#{t2}' might be the same books"
end
end
def stats
books_count = 0
mentions_count = 0
authors = []
authors_mentions = {}
sources = Set.new
days = []
@days.each do |day, books|
days << [day, books.size]
books.each_with_index do |h, i|
b = Book.new(h, day, i)
books_count += 1
book_sources = b.sources + b.recommenders
book_sources.each do |source|
mentions_count += 1
source["source"] =~ %r{^https?://twitter\.com/([^/]+)/}
sources << $1
end
b.authors.each do |a|
authors << a
authors_mentions[a] ||= 0
authors_mentions[a] += book_sources.size
end
end
end
authors = authors.sort.group_by { |a| a }.map { |a, x| [a, x.size]}.sort_by(&:last).reverse
authors_mentions = authors_mentions.entries.sort_by(&:last).reverse
if ARGV.include? "--sources"
puts sources.to_a.sort_by { |s| s.downcase.gsub(/^[^a-z0-9]/, "") }.map { |s| "@#{s}" }.join ", "
end
{
:books_count => books_count,
:authors_count => authors.size,
:mentions_count => mentions_count,
:sources_count => sources.size,
:most_common_authors => authors[0..2],
:most_mentioned_authors => authors_mentions[0..2],
:days => days,
}
end
private
def write_html_file(file)
@days.each do |day, books|
file.write %{<h2 id="#{day_anchor day}">#{day_title day}</h2>\n}
if books.nil? or books.empty?
file.write %(<p class="notice">Il n’y a pas encore de livres pour ce jour.</p>\n)
next
end
books.each_with_index do |hash, i|
book = Book.new(hash, day, i)
file.write book.to_html
end
end
end
def write_jekyll_prelude(file)
file.write <<-EOS
---
layout: default
---
<img src="./assets/banner.png" width="740" height="586" id="banner" alt="Un ouvrage de socio par jour du 1er au 24 décembre" />
<b>#SocioNoel</b> est une idée originale de [@CobbleAndFrame](https://twitter.com/CobbleAndFrame/status/671360041136558081), qui a également créé l’image ci-dessus (utilisée sans aucune permission).
La liste ci-dessous est compilée à la main à partir des tweets qui ont le hashtag [#SocioNoel](https://twitter.com/search?f=tweets&vertical=default&q=socionoel).
Le <a href="https://github.com/bfontaine/SocioNoel">code de cette page</a> est libre et la liste est dans le domaine public.
Contactez-moi sur Twitter (<a href="https://twitter.com/bfontn">@bfontn</a>) si vous la voulez dans un autre format (CSV/Excel, PDF, etc).
Accès direct à un jour :<br/>
EOS
@days.each_with_index do |p, i|
day = p[0]
file.write %{#{i + 1}. [#{day_title day}](##{day_anchor day})\n}
end
file.write "\n"
end
def write_html_prelude(file)
file.write <<-EOS
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width initial-scale=1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>#SocioNoël</title>
</head>
<body>
EOS
end
def write_html_end(file)
file.write "</body></html>"
end
end
def make_pdf(source, target)
tmp = ".tex2pdf#{rand}"
tex = "#{tmp}.tex"
begin
system "pandoc", "--latex-engine=xelatex", "--standalone", source, "-o", tex
system "xelatex", tex
FileUtils.mv "#{tmp}.pdf", target
ensure
Dir["#{tmp}.*"].each { |f| File.unlink f }
end
end
ls = BooksList.new "books.yml"
if ARGV.include? "--pdf"
html = "_books.html"
begin
ls.write_html html
make_pdf html, "books.pdf"
ensure
File.unlink html
end
elsif ARGV.include? "--html"
ls.write_html "books.html"
elsif ARGV.include? "--compact-markdown"
ls.write_compact_markdown "books.md"
elsif ARGV.include? "--stats"
ls.stats.each do |label, value|
puts "#{label}: #{value}"
end
else
ls.write_jekyll "index.md"
end