-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathconfig.rb
More file actions
419 lines (346 loc) · 11.8 KB
/
Copy pathconfig.rb
File metadata and controls
419 lines (346 loc) · 11.8 KB
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
require 'lib/event-handler.rb'
require 'lib/build_cleaner'
require 'ostruct'
require 'kramdown'
## We have to blow away the /build folder before deploys
configure :build do
activate :build_cleaner
end
###
# Blog settings
###
Time.zone = "UTC"
activate :blog do |blog|
blog.permalink = "{year}/{category}/{title}.html"
blog.layout = "article"
blog.tag_template = "tag.html"
blog.calendar_template = "calendar.html"
# Enable pagination
blog.paginate = true
blog.per_page = 10
blog.page_link = "page/{num}"
# Create Category Archive pages
blog.custom_collections = {
category: {
link: '/categories/{category}.html',
template: '/category.html'
}
}
end
page "/feed.xml", layout: false
page "/videos.xml", layout: false
page "/papers_feed.xml", layout: false
# Sitemap configuration
set :url_root, 'https://paperswelove.org'
activate :search_engine_sitemap,
default_priority: 0.5,
default_change_frequency: "monthly"
page '/pwlconf2017/*', layout: 'pwlconf'
# OpenGraph tags are now handled manually in layouts
###
# Helpers
###
helpers do
def category_list
sitemap.resources.group_by {|p| p.data["category"] }.map do |category, pages|
{ :category => category, :pages => pages }
end
end
# Auto-link URLs in text
def auto_link(text)
return '' if text.nil? || text.empty?
url_regex = %r{(https?://[^\s<>\[\]"']+)}
text.gsub(url_regex) do |url|
# Clean up trailing punctuation that's likely not part of the URL
clean_url = url.sub(/[.,;:!?\)]+$/, '')
trailing = url[clean_url.length..-1] || ''
%(<a href="#{clean_url}" target="_blank" rel="noopener">#{clean_url}</a>#{trailing})
end
end
# Format text with paragraphs and auto-linked URLs (no escaping)
def format_description(text)
return '' if text.nil? || text.empty?
# First, escape HTML entities for safety (but not our auto-generated links)
escaped = text.gsub('&', '&').gsub('<', '<').gsub('>', '>')
# Auto-link URLs
linked = auto_link_escaped(escaped)
# Convert double newlines to paragraphs, single newlines to <br>
paragraphs = linked.split(/\n\n+/).map { |p| "<p>#{p.gsub(/\n/, '<br>')}</p>" }.join("\n")
paragraphs
end
# Auto-link URLs in already-escaped text
def auto_link_escaped(text)
return '' if text.nil? || text.empty?
url_regex = %r{(https?://[^\s&<>\[\]"']+)}
text.gsub(url_regex) do |url|
clean_url = url.sub(/[.,;:!?\)]+$/, '')
trailing = url[clean_url.length..-1] || ''
%(<a href="#{clean_url}" target="_blank" rel="noopener">#{clean_url}</a>#{trailing})
end
end
# Get all videos as entry objects for mixing with blog articles
def video_entries
return @video_entries if @video_entries
@video_entries = data.videographer.map do |v|
title = v[:title] || v['title']
slug = slugify(title)
date_str = v[:published_at] || v['published_at']
date = begin
Date.parse(date_str.to_s)
rescue
Date.today
end
OpenStruct.new(
title: title,
date: date,
url: "/videos/#{slug}/",
category: 'video',
is_video: true,
youtube_id: v[:youtube_id] || v['youtube_id'],
description: v[:description] || v['description'] || ''
)
end
end
# Get combined blog articles and videos, sorted by date
def combined_entries(limit = nil)
articles = blog.articles.map do |a|
OpenStruct.new(
title: a.title,
date: a.date.to_date,
url: a.url,
category: a.data.category,
is_video: false,
article: a
)
end
all_entries = (articles + video_entries).sort_by { |e| e.date }.reverse
limit ? all_entries.first(limit) : all_entries
end
# Convert a title to a URL-friendly slug
def slugify(title)
title.to_s
.downcase
.gsub(/[^\w\s-]/, '') # Remove non-word characters except spaces and hyphens
.gsub(/\s+/, '-') # Replace spaces with hyphens
.gsub(/-+/, '-') # Replace multiple hyphens with single
.gsub(/^-|-$/, '') # Remove leading/trailing hyphens
.slice(0, 80) # Limit length
end
# Parse tags from comma-separated string and add defaults
def parse_video_tags(tags_string)
tags = ['meetup', 'video']
if tags_string && !tags_string.empty?
tags_string.split(',').each do |tag|
slug = slugify(tag.strip)
tags << slug unless slug.empty? || tags.include?(slug)
end
end
tags.uniq
end
# Parse date from published_at timestamp
def parse_video_date(published_at)
return Date.today.strftime('%Y-%m-%d') unless published_at
Date.parse(published_at.to_s).strftime('%Y-%m-%d')
rescue
Date.today.strftime('%Y-%m-%d')
end
def deslugify(slug)
slug.to_s.tr('-_', ' ')
end
end
###
# Page options, layouts, aliases and proxies
###
# Per-page layout changes:
#
# With no layout
# page "/path/to/file.html", layout: false
#
# With alternative layout
# page "/path/to/file.html", layout: :otherlayout
#
# A path which all have the same layout
# with_layout :admin do
# page "/admin/*"
# end
# Load Chapters YAML
@chapters = YAML.load_file('source/chapters.yml', permitted_classes: [Symbol])
###
# Helpers
###
# Directory indexes
activate :directory_indexes
# Chapter pages (defined after directory_indexes)
# Use /index.html suffix so directory_indexes creates proper directories
data.chapters.keys.reject { |i| i.nil? }.each do |chapter|
proxy "/chapter/#{chapter}/index.html", "/chapter.html", locals: { chapter: chapter, events: data[chapter] }, ignore: true
end
# Chapter index
proxy "/chapter/index.html", "/chapter_index.html", locals: { chapters: @chapters }, ignore: true
# Video pages (generated from videographer.json)
# Helper methods for video processing (defined at config level for proxy generation)
def config_slugify(title)
title.to_s
.downcase
.gsub(/[^\w\s-]/, '')
.gsub(/\s+/, '-')
.gsub(/-+/, '-')
.gsub(/^-|-$/, '')
.slice(0, 80)
end
def config_parse_tags(tags_string)
tags = ['meetup', 'video']
if tags_string && !tags_string.empty?
tags_string.split(',').each do |tag|
slug = config_slugify(tag.strip)
tags << slug unless slug.empty? || tags.include?(slug)
end
end
tags.uniq
end
def config_parse_date(published_at)
return Date.today.strftime('%Y-%m-%dT00:00:00Z') unless published_at
DateTime.parse(published_at.to_s).strftime('%Y-%m-%dT%H:%M:%SZ')
rescue
Date.today.strftime('%Y-%m-%dT00:00:00Z')
end
# Track slugs to handle duplicates and collect all videos for index
video_slugs = {}
all_videos = []
data.videographer.each do |video_entry|
base_slug = config_slugify(video_entry[:title] || video_entry['title'])
next if base_slug.empty?
# Handle duplicate slugs by appending a counter
slug = base_slug
if video_slugs[base_slug]
video_slugs[base_slug] += 1
slug = "#{base_slug}-#{video_slugs[base_slug]}"
else
video_slugs[base_slug] = 1
end
video_data = {
title: video_entry[:title] || video_entry['title'],
youtube_id: video_entry[:youtube_id] || video_entry['youtube_id'],
description: video_entry[:description] || video_entry['description'] || '',
date: config_parse_date(video_entry[:published_at] || video_entry['published_at']),
tags: config_parse_tags(video_entry[:tags] || video_entry['tags']),
thumbnail_url: video_entry[:thumbnail_url] || video_entry['thumbnail_url'],
duration: video_entry[:duration] || video_entry['duration'] || '',
slug: slug
}
all_videos << video_data
proxy "/videos/#{slug}/index.html", "/video.html", locals: { video: video_data }, ignore: true
end
# Video index page
proxy "/videos/index.html", "/video_index.html", locals: { videos: all_videos }, ignore: true
# Video tag pages - load JSON directly since data.videographer is lazy
require 'json'
videographer_json = JSON.parse(File.read('data/videographer.json'))
videos_by_tag = {}
video_slugs_for_tags = {}
videographer_json.each do |video_entry|
title = video_entry[:title] || video_entry['title']
base_slug = config_slugify(title)
next if base_slug.empty?
# Handle duplicate slugs
slug = base_slug
if video_slugs_for_tags[base_slug]
video_slugs_for_tags[base_slug] += 1
slug = "#{base_slug}-#{video_slugs_for_tags[base_slug]}"
else
video_slugs_for_tags[base_slug] = 1
end
video_data = {
title: title,
date: config_parse_date(video_entry[:published_at] || video_entry['published_at']),
slug: slug
}
tags = config_parse_tags(video_entry[:tags] || video_entry['tags'])
tags.each do |tag|
videos_by_tag[tag] ||= []
videos_by_tag[tag] << video_data
end
end
videos_by_tag.each do |tag, tag_videos|
proxy "/videos/tags/#{tag}/index.html", "/video_tag.html", locals: { tag_name: tag, tag_videos: tag_videos }, ignore: true
end
# Video tag index page
proxy "/videos/tags/index.html", "/video_tag_index.html", locals: { videos_by_tag: videos_by_tag }, ignore: true
# Ignore the video templates
ignore 'video_tag.html'
ignore 'video_tag_index.html'
ignore 'video_index.html'
# Paper pages (generated from data/json/)
def config_paper_slug(title, uuid)
slug = config_slugify(title).slice(0, 50)
"#{slug}-#{uuid[0..7]}"
end
papers_json_dir = File.join(root, 'data', 'json', 'papers')
categories_json = JSON.parse(File.read(File.join(root, 'data', 'json', 'categories.json')))
keywords_json = JSON.parse(File.read(File.join(root, 'data', 'json', 'keywords.json')))
all_papers = []
Dir.glob(File.join(papers_json_dir, '*.json')).each do |paper_file|
paper = JSON.parse(File.read(paper_file), symbolize_names: true)
next if paper[:title].nil? || paper[:title].to_s.strip.empty?
slug = config_paper_slug(paper[:title], paper[:uuid])
paper[:slug] = slug
paper[:summary_html] = Kramdown::Document.new(paper[:summary].to_s).to_html
all_papers << paper
proxy "/papers/#{slug}/index.html", "/paper.html",
locals: { paper: paper, categories_json: categories_json, keywords_json: keywords_json },
ignore: true
end
# Sort papers by created_on descending
all_papers.sort_by! { |p| p[:created_on].to_s }.reverse!
# Category index pages
categories_json.each do |category, papers|
proxy "/papers/categories/#{category}/index.html", "/paper_categories.html",
locals: { category_name: category, category_papers: papers, all_papers: all_papers },
ignore: true
end
proxy "/papers/categories/index.html", "/paper_categories_index.html",
locals: { categories: categories_json }, ignore: true
# Keyword index pages
keywords_json.each do |keyword, papers|
proxy "/papers/keywords/#{keyword}/index.html", "/paper_keywords.html",
locals: { keyword_name: keyword, keyword_papers: papers, all_papers: all_papers },
ignore: true
end
proxy "/papers/keywords/index.html", "/paper_keywords_index.html",
locals: { keywords: keywords_json }, ignore: true
# Ignore paper templates
ignore 'paper.html'
ignore 'paper_categories.html'
ignore 'paper_categories_index.html'
ignore 'paper_keywords.html'
ignore 'paper_keywords_index.html'
# Note: automatic_image_sizes was removed in Middleman 4
# Reload the browser automatically whenever files change
# activate :livereload
# Methods defined in the helpers block are available in templates
# helpers do
# def some_helper
# "Helping"
# end
# end
#set :markdown, :parse_block_html => true
set :css_dir, 'stylesheets'
set :js_dir, 'javascripts'
set :images_dir, 'images'
configure :development do
#activate :livereload
end
# Build-specific configuration
configure :build do
# Minify CSS for production
activate :minify_css
# Minify Javascript on build
# activate :minify_javascript
# Enable cache buster
# activate :asset_hash
# Use relative URLs
# activate :relative_assets
# Or use a different image path
# set :http_prefix, "/Content/images/"
end
# Deployment is handled via manual git push (see Makefile/GitHub Actions)