-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocpad.js
455 lines (428 loc) · 20.2 KB
/
docpad.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*jslint nomen:true, vars:true, devel:true, browser:true, white:true */
/*globals require:true, _:true, module:true */
(function() {
"use strict";
var _ = require("underscore");
var fs = require("fs");
var path = require("path");
var os = require("os");
var eco = require("eco");
var __indexOf = [].indexOf || function(item) {
var i, l;
for (i = 0, l = this.length; i < l; i++) {
if (i in this && this[i] === item) {
return i;
}
}
return -1;
};
var writeToFile = function(fname, text) {
fs.writeFile(fname, text + "\n", function (err) {
if (err) { return console.log(err); }
});
};
var appendToFile = function(fname, text) {
fs.appendFile(fname, text + "\n", function (err) {
if (err) { return console.log(err); }
});
};
var asJSON = function(obj) {
return JSON.stringify(obj);
};
var getObjMembers = function(obj) {
var retStr = "Properties---------\n";
retStr += _.keys(obj).join("\n");
retStr += "\nFunctions---------\n";
retStr += _.functions(obj).join("\n");
return retStr;
};
// Creates a RegExp based on the origPath regular expression. On non-Windows machines it
// uses the provided string without modifying it. On Windows it tries to normalize the
// path first. If the regular expression contains backslashes the results can vary.
var createPathRegExp = function(origPath) {
var normalizedPath = origPath;
if (/^Windows/i.test(os.type())) {
normalizedPath = path.normalize(origPath).replace(/\\/g, "\\\\");
}
return new RegExp(normalizedPath);
};
var helpers = {
getIndexHtmlFrom: function(path, comparator) {
//var re = new RegExp("^" + path + "\/.*\/index.html");
var re = createPathRegExp("^" + path + "/.*/index.html");
return this.getCollection("html")
.findAllLive({ relativeOutPath: re }, comparator);
},
getDoc: function(query) {
return this.getCollection("html").findOne(query).toJSON();
}
};
var docpadConfig = {
templateData: {
site: {
url: "http://lefkadika.gr",
title: "Εταιρεία Λευκαδικών Μελετών",
// ToDo:
description: "H Εταιρεία Λευκαδικών Μελετών είναι ένα επιστημονικό σωματείο της Λευκάδας",
// ToDo:
keywords: "λευκάδα, λευκαδικά, επιστημονική εταιρεία, επιστημονικό σωματείο, lefkas, history, ιστορία, λαογραφία, φιλολογία, αρχαιολογία, φολκλόρ, τοπική ιστορία, επτανησιακή ιστορία",
styles: ['/vendor/pure-min.css',
'/styles/style.css'
],
scripts: ['/vendor/log.js',
'/vendor/modernizr.js',
'/vendor/ui.js',
'/vendor/hyphenate.js'
]
},
require: require,
getPreparedTitle: function() {
if (this.document.title) {
return eco.render(this.document.title, docpadConfig.templateData);
} else {
return this.site.title;
}
},
getPreparedDescription: function() {
return this.document.description || this.site.description;
},
getPreparedKeywords: function() {
return this.site.keywords.concat(this.document.keywords || [])
.join(', ');
},
console: console,
trace: {
pageFlag: false,
menuFlag: false,
page: function(msg) {
if (this.pageFlag) {
console.log(msg);
}
},
menu: function(msg) {
if (this.menuFlag) {
console.log(msg);
}
}
},
writeToFile: writeToFile,
appendToFile: appendToFile,
imagesRoot: "/images/",
getImage: function(page) {
var imagePath;
if (!page) {
imagePath = this.imagesRoot + "not_available.jpg";
}
else if (!page.image) {
imagePath = this.imagesRoot + page.relativeBase + ".jpg";
}
else {
imagePath = page.image;
}
return imagePath;
},
getThumb: function(page) {
var imagePath;
if (!page) {
imagePath = this.imagesRoot + "not_available.jpg";
}
else if (!page.thumbnail) {
imagePath = this.imagesRoot + page.relativeBase + "-th.jpg";
}
else {
imagePath = page.thumbnail;
}
return imagePath;
},
getPublicationGroups: function() {
var pubs = this.getCollection("publications").toJSON();
if (!pubs) { console.log("pubs is undefined"); }
var pubsByTag = _.groupBy(pubs, function(pub) {
//writeToFile("pub.json", JSON.stringify(pub));
if (pub.tag && pub.tag.length > 0) {
return pub.tag[0];
}
else {
return null;
}
});
//writeToFile("pubGroups.json", JSON.stringify(pubsByTag));
return pubsByTag;
},
getIndexHtmlFrom: helpers.getIndexHtmlFrom,
getDoc: function(query) {
var doc;
try {
doc = helpers.getDoc.call(this, query);
//writeToFile("file", getObjMembers(doc));
}
catch(e) {
console.error("Could not find document with query: " + asJSON(query));
}
return doc;
},
createPathRegExp: createPathRegExp,
getLogo: function() {
if (this.getDocument().toJSON().relativeOutPath !== "index.html") {
return "/images/logo_light_small.png";
}
else {
return "/images/logo_light.png";
}
},
getCurrentYear: function() {
return new Date().getFullYear();
},
// Returns the years since the foundation of ELM in 1970. Same as Unix!
getYears: function() {
return this.getCurrentYear() - 1970;
},
getPublicationCount: function() {
// Note that there are 3 multitome books.
return this.getCollection("publications").length - 3;
},
getSymposiumCount: function() {
return this.getTimelineActivitiesPerType()['Συμπόσια'].length;
},
getConferenceCount: function() {
return this.getTimelineActivitiesPerType()['Συνέδρια'].length;
},
// Major events are Symposiums and Conferences.
getMajorEvents: function() {
return this.getConferenceCount() + this.getSymposiumCount();
},
yearComparator: function(a, b) {
var aS = a.getMeta("year").toString().substr(0, 4);
var bS = b.getMeta("year").toString().substr(0, 4);
return parseInt(aS) - parseInt(bS);
},
// Returns the different board periods. Each period has a unique president.
getBoardPeriods: function() {
var boardPeriods = _.chain(this.getCollection("boards").toJSON())
.groupBy("period")
.map(function(p) {
p = _.sortBy(p, "from");
return { period: _.first(p).period,
from: _.first(p).from,
until: _.last(p).until };
})
.sortBy("from")
.value();
return boardPeriods;
},
// Returns board of directors by period in chronological order. For
// every president of the board, a unique period is defined.
getBoards: function() {
var boards = this.getCollection("boards");
_.chain(boards.groupBy("period")).each(function(p) {
_.chain(p)
.sortBy(function(b) { return b.getMeta("from"); })
.each(function(b, i) {
b.setMeta("periodNumber", i + 1);
});
});
return boards;
},
// Returns all(?) activities for the timeline of ELM.
getTimelineActivities: function() {
return this.getCollection("activities")
.findAll({ activity: { $in: ["Διαλέξεις",
"Εκδηλώσεις",
"Συμπόσια",
"Συνέδρια",
"Παρουσίαση βιβλίων",
"Βραβεία Ακαδημίας Αθηνών"] } });
},
// Returns the years with at least one activity or publication.
getTimelineYears: function() {
var timelineYears = this.getTimelineActivities() .pluck("year");
var publicationYears = _.chain(this.getCollection("publications").toJSON())
.map(function(p) {
return _.pluck(p.editions, "date");
})
.flatten()
.value();
return _.chain(timelineYears)
.union(publicationYears)
.sort()
.uniq(false)
.value();
},
getTimelineActivitiesPerType: function() {
return _.groupBy(this.getTimelineActivities().toJSON(), 'activity');
},
getYearPublications: function(year) {
return _.chain(this.getCollection("publications").toJSON())
.filter(function(p) {
return _.contains(_.pluck(p.editions, "date"), year);
})
.value();
},
// Returns the events grouped by the event subject.
getEventsBySubject: function() {
return _.chain(this.getCollection("activities")
.findAll({ activity: /^Εκδηλώσεις/})
.toJSON())
.groupBy('subject')
.sortBy(function(g) {
return _.min(_.pluck(g, 'year'));
})
.value();
}
},
collections: {
// All pages, by default, have the "default" layout and are hidden from the menu.
pages: function () {
return this.getCollection("html")
.findAllLive()
.on("add", function (model) {
model.setMetaDefaults({ layout: "default",
menuHidden: true });
//appendToFile("docs", asJSON(model));
});
},
// All auto generated documents (paging) should be hidden from the menu. The 'paged' plugin
// set the menuHidden property to true to all pages.
auto: function () {
return this.getCollection("pages")
.findAllLive({ isPagedAuto: { $eq: true } })
.on("add", function (model) {
model.setMeta({ menuHidden: true });
});
},
news: function () {
return this.getCollection("html")
.findAllLive({ relativeOutDirPath: "news" }, [{ date: -1 }])
.on("add", function (model) {
model.setMetaDefaults({ layout: "pr" });
});
},
// This collection contains only publications. By convention these are located in subdirectories
// of the "publications" directory.
publications: function () {
var noIndexHtml = function(model) {
var m = model.toJSON();
var isPub = createPathRegExp("^publications/.").test(m.relativeOutDirPath);
var isIndexHtml = m.basename === "index";
return isPub && !isIndexHtml;
};
return this.getCollection("html")
.createLiveChildCollection()
.setFilter("no_index_html", noIndexHtml)
.setComparator([{ date: -1 }, { orderIndex: -1 }, { title: 1 }])
.on("add", function (model) {
model.setMeta("layout", "publication");
var editions = model.getMeta("editions");
if (editions) {
var currentEdition = _.first(editions);
_.each(editions, function(e) {
if (e.number > currentEdition.number) {
currentEdition = e;
}
});
model.setMeta("currentEdition", currentEdition);
model.setMeta("date", currentEdition.date);
}
});
},
publicationCategories: function() {
return helpers.getIndexHtmlFrom.call(this, "publications", [{ menuOrder: 1 }]);
},
boards: function () {
return this.getCollection("html")
.findAllLive({ relativeOutDirPath: /boards/,
basename: { $ne: 'index' } }, [{ date: -1 }])
.on("add", function (model) {
var period = model.getMeta("period");
var from = model.getMeta("from");
var until = model.getMeta("until");
var title = "Περίοδος " + period + " (" + from + "-" + until + ")";
model.setMeta("title", title);
model.setMeta("menuHidden", false );
model.setMeta("layout", "board");
});
},
chronicle: function() {
return helpers.getIndexHtmlFrom.call(this, "xroniko")
.on("add", function (model) {
// If there is not menuTitle, add one based
// on the title.
var menuTitle = model.getMeta("menuTitle");
if (!menuTitle) {
model.setMeta("menuTitle", model.getMeta("title"));
}
this.sortBy("menuTitle")
.forEach(function(element, index) {
// Added the menu order now that the collection is sorted on
// menuTitle. Index is zero based.
element.setMeta("menuOrder", index + 1);
});
});
},
activities: function() {
return this.getCollection("html")
.findAllLive({ activity: { $exists: true } });
}
},
environments: {
development: {
port: 9777,
templateData: {
site: {
url: "http://localhost:9777"
}
}
}
},
events: {
serverExtend: function(opts) {
var docpad, latestConfig, newUrl, oldUrls, server;
server = opts.server;
docpad = this.docpad;
latestConfig = docpad.getConfig();
oldUrls = latestConfig.templateData.site.oldUrls || [];
newUrl = latestConfig.templateData.site.url;
return server.use(function(req, res, next) {
var _ref = req.headers.host;
if (_ref && __indexOf.call(oldUrls, _ref) >= 0) {
return res.redirect(newUrl + req.url, 301);
} else {
return next();
}
});
},
render: function(opts) {
//console.log("Custom render of: " + opts.file.toJSON().fullPath);
}
},
plugins: {
menu: {
menuOptions: {
//optimize: true,
//skipEmpty: true,
skipFiles: /\.(js|styl|css)/
}
},
datefromfilename: {
//removeDate: false,
//dateRegExp: /\b(\d{4})-(\d{2})-(\d{2})-/
},
moment: {
formats: [
{raw: 'date', format: 'YYYY', formatted: 'publicationDate'},
{raw: 'date', format: 'DD/MM/YYYY', formatted: 'newsDate'}
]
},
sitemap: {
//cachetime: 600000,
changefreq: 'always',
priority: 0.5,
filePath: 'sitemap.xml'
},
marked: {
tables: true
}
}
};
module.exports = docpadConfig;
}).call(this);