forked from galaxyproject/galaxy-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
267 lines (252 loc) · 8.92 KB
/
build.js
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
// Build with Metalsmith
let metalsmith = require('metalsmith');
let minimatch = require('minimatch');
// Plugin for Bower support
let bower = function(files, metalsmith, done) {
let bower_files = require( 'bower-files' )();
let { readFileSync } = require('fs');
let { basename } = require('path');
let include = (root, included) =>
(() => {
let result = [];
for (let file of Array.from(included)) {
let contents = readFileSync(file);
result.push(files[`${root}/${basename(file)}`] =
{contents});
}
return result;
})()
;
include('css', bower_files.self().ext('css').files);
include('js', bower_files.self().ext('js').files);
include('fonts', bower_files.self().ext(['eot','otf','ttf','woff','woff2']).files);
return done();
};
let set_metadata_defaults = function(files, metalsmith, done) {
// Simple way to apply metadata defaults
for (let k in files) {
let v = files[k];
if (k.endsWith('.md')) {
// Link to original path
files[k].orig_path = k;
// Autotoc defaults to true
// Set domain templates
if (Array.from(v.collection).includes('events')) {
if (files[k].layout === undefined) { files[k].layout = 'events.pug'; }
if (files[k].autotoc === undefined) { files[k].autotoc = false; }
} else if (Array.from(v.collection).includes('news')) {
if (files[k].layout === undefined) { files[k].layout = 'news.pug'; }
if (files[k].autotoc === undefined) { files[k].autotoc = false; }
} else {
if (files[k].autotoc === undefined) { files[k].autotoc = true; }
}
}
}
return done();
};
let fs = require('fs');
let path = require('path');
let hb_partials = require("handlebars");
var partials_from_dir = (source, dir) =>
(() => {
let result = [];
let object = fs.readdirSync(source);
for (let i in object) {
let p_f = object[i];
let p_path = path.join(source, p_f);
let stats = fs.statSync(p_path);
if (stats.isDirectory()) {
result.push(partials_from_dir(p_path, dir));
} else {
let contents = fs.readFileSync(p_path, 'utf8');
p_path = p_path.replace("partials/", "").replace(".html", "");
dir[p_path] = contents;
result.push(hb_partials.registerPartial(p_path, contents));
}
}
return result;
})()
;
let partials = {};
partials_from_dir('partials', partials);
let md_link_pattern = /\[([^\]]*)\]\(([^\)]*)\)/g;
let html_link_pattern = /href=[\'"]?([^\'" >]+)[\'"]/g;
let html_img_pattern = /src=[\'"]\/src?([^\'" >]+)[\'"]/g;
let handlebars_partial_handling = function(files, metalsmith, done) {
for (let file in files) {
let c = files[file];
(function(file, c) {
if (file.endsWith('.md')) {
let contents = files[file].contents.toString();
let template = hb_partials.compile(contents);
contents = template({});
return files[file].contents = contents;
}
})(file, c);
}
return done();
};
let subs = function(files, metalsmith, done) {
// Quick hack to temporarily handle INCLUDE migration
// Followed by another set of hacks to strip /src and index.md out of
// source. We have these full references in the source to make GitHub
// render correctly in the preview and web editor. TODO: Come up with a
// better long term solution that renders both in github, and correctly for
// publishing, that isn't a big nest of regexes and special cases.
for (let file in files) {
let c = files[file];
(function(file, c) {
if (file.endsWith('.md')) {
let match, rep;
let contents = files[file].contents.toString();
let matches = [];
while ((match = md_link_pattern.exec(contents))) { matches.push(match); }
for (match of Array.from(matches)) {
rep = match[2];
//TODO: Do this with a regex too
if (rep.startsWith('/src')) {
// Drop leading /src
rep = rep.substr(4);
}
if (rep.startsWith('/')) {
// If it's a local URL, drop index.md's when they exist.
// Replace is simpler here because we have to consider
// in-page anchors.
rep = rep.replace('index.md', '');
}
contents = contents.split(match[0]).join(`[${match[1]}](${rep})`);
}
matches = [];
while ((match = html_link_pattern.exec(contents))) { matches.push(match); }
for (match of Array.from(matches)) {
rep = match[1];
if (rep.startsWith('/src')) {
rep = rep.substr(4);
}
if (rep.startsWith('/')) {
rep = rep.replace('index.md', '');
}
contents = contents.split(match[0]).join(`href="${rep}"`);
}
matches = [];
while ((match = html_img_pattern.exec(contents))) { matches.push(match); }
for (match of Array.from(matches)) {
// Simply match and drop leading /src/ from images.
contents = contents.split(match[0]).join(`src="${match[1]}"`);
}
return files[file].contents = contents;
}
})(file, c);
}
return done();
};
// Extend `marked.Renderer` to increase all heading levels by 1 since we reserve
// h1 for the page title. Will be passed to `metalsmith-markdown` plugin.
let marked = require("marked");
class Renderer extends marked.Renderer {
heading( text, level, raw ) {
return super.heading( text, level + 1, raw );
}
table(header, body) {
return `<table class="table table-striped">
<thead>
${header}
</thead>
<tbody>
${body}
</tbody>
</table>`;
}
image(href, title, text) {
let out = `<img class="img-responsive" src="${href}" alt="${text}"`;
if (title) {
out += ` title="${title}"`;
}
out += '/>';
return out;
}
}
let timer = require( "metalsmith-timer" );
let ms = metalsmith(__dirname)
.use(require('metalsmith-metadata')({
menu: "config/menu.yaml"})).use(timer('metalsmith-metadata'))
.use(require('metalsmith-collections')({
news: {
pattern: "news/*/*.md",
sortBy: "date",
reverse: true
},
devnewsbrief: {
pattern: "archive/dev-news-briefs/*/*.md",
sortBy: "date",
reverse: true
},
events: {
pattern: "events/*/*.md",
sortBy: "date",
reverse: true
},
publications: {
pattern: "publications/*/*.md",
sortBy: "date",
reverse: true
},
splash: {
pattern: "splash/*/*.md",
sortBy: "date",
reverse: true
}
})).use(timer('metalsmith-collections'))
.use(set_metadata_defaults)
.use(timer('set_metadata_defaults'))
.use(handlebars_partial_handling)
.use(timer('handlebars_partial_handling'))
.use(subs)
.use(timer('subs'))
.use(require('metalsmith-markdown')({
gfm: true,
renderer: new Renderer()
})).use(timer('metalsmith-markdown'))
.use(require('metalsmith-autotoc')({
selector: "h2, h3, h4"})).use(timer('metalsmith-autotoc'))
.use(require('metalsmith-alias')())
.use(timer('metalsmith-alias'))
.use(require('metalsmith-filepath')({
absolute: true,
permalinks: true
})).use(timer('metalsmith-filepath'))
.use(require('metalsmith-layouts')({
engine: "pug",
cache: true,
default: "default.pug",
pattern: "**/*.html",
helpers: {
moment: require('moment'),
marked: require('marked'),
_: require('lodash')
}
})).use(timer('metalsmith-layouts'))
.use(require('metalsmith-less')())
.use(timer('metalsmith-less'))
.use(bower)
.use(timer('bower'))
.use(require('metalsmith-uglify')())
.use(timer('metalsmith-uglify'));
let argv = require('minimist')(process.argv.slice(2));
if (argv['serve']) {
ms.use( require('metalsmith-serve')( { port: 8080 } ) );
}
if (argv['check']) {
ms.use(require('metalsmith-broken-link-checker')({
allowRedirects: true,
warn: true
})
);
}
ms.build(function(e) {
if (e) {
throw e;
} else {
return console.log("Done");
}
});