generated from greenelab/lab-website-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.rb
87 lines (79 loc) · 2.37 KB
/
misc.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
require 'liquid'
require 'html-proofer'
module Jekyll
module MiscFilters
# fallback if value unspecified
def is_nil(value, fallback)
return value == nil ? fallback : value
end
# get list of hash keys or array entries
def object_items(object)
if object.is_a?(Hash)
return object.keys
elsif object.is_a?(Array)
return object
end
return object
end
# filter a list of hashes by comma-sep'd field:value pairs
def data_filter(data, filters)
if not data.is_a?(Array) or not filters.is_a?(String)
return data
end
data = data.clone
for filter in array_filter(filters.split(","))
key, value = array_filter(filter.split(":"))
# find unspecified fields
if value == nil
data.select!{|d| d[key] == nil}
# find fields that match regex
elsif value.is_a?(String)
data.select!{|d| d[key].to_s =~ /#{value}/m}
end
end
return data
end
# from css text, find font family definitions and construct google font url
def google_fonts(css)
names = regex_scan(css, '--\S*:\s*"(.*)",?.*;', false, true).sort.uniq
weights = regex_scan(css, '--\S*:\s(\d{3});', false, true).sort.uniq
url = "https://fonts.googleapis.com/css2?display=swap&"
for name in names do
name.sub!" ", "+"
url += "&family=#{name}:ital,wght@"
for ital in [0, 1] do
for weight in weights do
url += "#{ital},#{weight};"
end
end
url.delete_suffix!(";")
end
return url
end
end
# based on https://github.com/episource/jekyll-html-proofer
module HtmlProofer
priority = Jekyll::Hooks::PRIORITY_MAP[:high] + 1000
Jekyll::Hooks.register(:site, :post_write, priority: priority) do |site|
if not site.config["proofer"] == false
options = {
allow_missing_href: true,
enforce_https: false,
ignore_files: [/.*testbed.html/],
ignore_urls: [
/fonts\.gstatic\.com/,
/localhost:/,
/0\.0\.0\.0:/,
],
}
begin
HTMLProofer.check_directory(site.dest, options).run
rescue Exception => error
STDERR.puts error
# raise error
end
end
end
end
end
Liquid::Template.register_filter(Jekyll::MiscFilters)