-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconfig.rb
233 lines (192 loc) · 6.64 KB
/
config.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
require "mime/types"
require "slim"
require "pp"
require_relative 'helpers'
ROOT_LOCALE = :en
SUPPORTED_LOCALES = [:de, :en, :fr, :es, :pt, :nl, :ar]
###
# Page options, layouts, aliases and proxies
###
# Per-page layout changes:
#
# With no layout
page "/*.xml", layout: false
page "/*.json", layout: false
page "/*.txt", layout: false
# General configuration
# Reload the browser automatically whenever files change
configure :development do
config[:api_host] = "//localhost:3000"
activate :livereload
end
# Build-specific configuration
configure :build do
config[:api_host] = "https://actions.eko.org"
# config[:api_host] = "https://action-staging.eko.org"
activate :gzip
end
###
# Helpers for confg
###
# middleman expects paths relative to the source/ directory and
# with no extensions except .html
def format_template_path(path)
path.gsub("source/", "").gsub(/\.html\..*/, ".html")
end
# strips all directories and file extensions from a file path
def slug_from_file_path(path)
File.basename(path).split(".").first
end
def translate_link(url, locale)
untethered = url.gsub(/\/(en|fr|de|es|pt|nl|ar)\//, "/").gsub(/\A\/(en|fr|de|es|pt|nl|ar)\z/, "/")
locale == ROOT_LOCALE ? untethered : "/#{locale}#{untethered}"
end
def load_prismic
puts "-" * 60
puts "| Fetching content from Prismic..."
api = Prismic.api("https://sou-homepage.cdn.prismic.io/api", { cache: false })
index = 1
continue = true
documents = []
financials_content = api.form('everything')
.query(Prismic::Predicates::at('document.type', 'financial_information'))
.submit(api.master_ref).results.first
parse_financials(financials_content)
while continue
result = api.all({ "lang" => "*", "pageSize" => 100, "page" => index })
documents += result.results
if result.next_page
index += 1
else
continue = false
end
end
prismic_content = SUPPORTED_LOCALES.map do |locale|
[locale, documents.select { |d| d.lang.slice(0, 2) == locale.to_s }]
end.to_h
puts "| Content loaded:"
SUPPORTED_LOCALES.each do |locale|
puts "| #{locale}: #{prismic_content[locale].size}"
end
puts "-" * 60
prismic_content
end
###
# Helpers for views
###
# Methods defined in the helpers block are available in templates
helpers do
def fetch_count
res = Net::HTTP.get("s3-us-west-2.amazonaws.com", "/sou-homepage-counter/count.json")
count = JSON.parse(res)["count"]
number_with_delimiter(count)
end
# strips all directories and file extensions from a file path
def slug_from_file_path(path)
File.basename(path).split(".").first
end
def translate_link(url, locale)
untethered = url.gsub(/\/(en|fr|de|es|pt|nl|ar)\//, "/").gsub(/\A\/(en|fr|de|es|pt|nl|ar)\z/, "/")
locale == ROOT_LOCALE ? untethered : "/#{locale}#{untethered}"
end
def frontmatters_from_dir(path)
Dir[File.join(path, "*")].map do |file|
frontmatter = read_frontmatter(file)
frontmatter.merge("slug" => slug_from_file_path(file))
end
end
def read_frontmatter(path)
yaml_regex = /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
content = File.read(path)
YAML.load(content[yaml_regex])
end
def sort_by_date(hashes)
hashes.sort_by do |h|
Date.parse(h["date"] || "1/1/1800")
end.reverse
end
def read_title(prismic_doc)
prismic_doc["#{prismic_doc.type}.title"]&.as_text
end
end
activate :i18n, mount_at_root: ROOT_LOCALE, langs: SUPPORTED_LOCALES
activate :asset_hash, ignore: [".*fontawesome.*"]
activate :directory_indexes
PAGE_PATHS = [["privacy", "basic"],
["contact", "about"],
["petitions/support", "about"],
["about", "about"],
["optout", "basic"],
["opted_out", "basic"],
["optin", "basic"],
["opted_in", "basic"],
["unsubscribed", "basic"],
["unsubscribe", "basic"],
["details", "basic"],
["about/board", "about"],
["about/faq", "about"],
["about/solidarity", "about"],
["about/funding", "about"],
["about/jobs", "about"],
["about/jobs/detail", "about"],
["media", "media"],
["campaigns", "campaigns"],
["eko", "basic"]]
prismic_content = load_prismic
SUPPORTED_LOCALES.each do |locale|
# basic page routes
PAGE_PATHS.each do |page_path, layout|
content = prismic_content[locale]
.select { |p| p["#{p.type}.path"]&.value == page_path }.first
if content.present?
puts "CONTENT FOUND #{locale}, #{page_path}"
proxy translate_link("/#{page_path}/index.html", locale), "/pages/prismic.html",
layout: layout, locale: locale, locals: { content: content, path: page_path, full: prismic_content }
else
begin
puts "CONTENT NOT FOUND #{locale}, #{page_path}"
proxy translate_link("/#{page_path}/index.html", locale), "/pages/#{locale}/#{page_path}.html", layout: layout, locale: locale, locals: { path: page_path }
rescue
puts "NOT FOUND #{locale}, #{page_path}"
end
end
end
# press releases
press_releases = prismic_content[locale].select { |p| p.type == "press_release" }
press_releases.each do |release|
if release.slug.present?
proxy "/media/#{release.slug}/index.html", "/pages/prismic.html", layout: "media", locale: locale,
locals: { content: release, path: "press_release", full: prismic_content }
end
end
end
# ensure 404 accessible at 404.html
page "404.html", directory_index: false
# don't try to build pages from templates
ignore "/pages/prismic.html"
# handle redirects
data.redirects.each_pair do |path, destination|
proxy "/#{path}/index.html", "/pages/redirect.html", layout: false, locals: { destination: destination }, ignore: true
end
paths = ["node_modules/selectize/dist/css"]
# workaround for long-standing issue with ruby implementation
# of SASS (see https://github.com/sass/sass/issues/193)
class CSSImporter < ::Sass::Importers::Filesystem
def extensions
super.merge("css" => :scss)
end
end
::Compass.configuration.sass_options = {
load_paths: paths.map { |p| File.join(root, p) },
filesystem_importer: CSSImporter,
}
activate :external_pipeline,
name: :browserify,
command: "./node_modules/.bin/browserify --transform [ babelify --presets [ es2015 ] ] --extension=\".js\" source/javascripts/homepage.js source/javascripts/homepage.js | ./node_modules/.bin/uglifyjs -c > .js-dist/compiled.js",
source: ".js-dist",
latency: 1
activate :external_pipeline, {
name: :parcel,
command: build? ? "npm run parcel-build" : "npm run parcel-watch",
source: ".js-dist",
}