forked from fluid-project/infusion-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocpad.js
128 lines (113 loc) · 4.65 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
/*
Copyright 2014 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
var URI = require("URIjs");
var path = require("path");
var fs = require("fs-extra");
var docsVersion = "development";
// The documentation root on GitHub:
// Used to build URLs for "Edit on GitHub" links
var githubDocRoot = "https://github.com/fluid-project/infusion-docs/blob/master/src/documents/";
// Helper function to rewrite *.md links to *.html:
// With this helper, we can write links to *.md files in our source files but
// generate links to *.html in the DocPad output. This arrangement gives us
// links that work both on the GitHub website and in the generated HTML.
var rewriteMdLinks = function (content) {
return content.replace(/(<a\s[^>]*href="[\w-/\.]+)\.md(["#])/gm, "$1.html$2");
};
// Helper function to build a URL for "Edit on GitHub" for the current document
var githubLocation = function () {
// in case we're on Windows, replace "\" in the path with "/"
var relativePath = this.document.relativePath.replace(/\\/g, "/");
return githubDocRoot + relativePath;
};
// Helper function to build relative URLs:
// Used for links to static resources such as CSS files. So that the generated
// DocPad output is independent of the URL that it is hosted at.
var relativeUrl = function (forUrl, relativeToUrl) {
return URI(forUrl).relativeTo(relativeToUrl);
};
// Helper function to determine if two values are equal
// Used to determine which table of contents category to display on a particular
// page.
var ifEqual = function (a, b, options) {
if (a == b) {
return options.fn(this);
} else {
return options.inverse(this);
}
};
var siteStructure = JSON.parse(fs.readFileSync("site-structure.json"));
// We locate the images within the src/documents directory so that images can
// be viewed on GitHub, as well as in the DocPad output. We need to
// instruct DocPad to treat the images specially so that they are not
// processed. We tell DocPad to ignore the images using "ignorePaths" and we
// then copy them ourselves with a "writeAfter" event handler.
var rootPath = process.cwd();
var imagesSrcDir = path.join(rootPath, "src", "documents", "images");
var imagesDestDir = "out/images";
module.exports = {
rootPath: rootPath,
ignorePaths: [ imagesSrcDir ],
renderSingleExtensions: true,
templateData: {
siteStructure: siteStructure
},
plugins: {
handlebars: {
helpers: {
rewriteMdLinks: rewriteMdLinks,
githubLocation: githubLocation,
relativeUrl: relativeUrl,
ifEqual: ifEqual
}
},
highlightjs: {
aliases: {
stylus: "css"
}
}
},
events: {
generateBefore: function (options) {
// Remove the previous incarnation of index.html since it causes a faulty redirect
fs.removeSync("out/index.html");
},
writeAfter: function () {
// Copy the images
fs.copySync(imagesSrcDir, imagesDestDir);
// Copy the contents of the out directory to
// out/infusion/<version>. We need to do this to prepare the
// structure for the ghpages plugin as it does not support
// deploying to a location other than the root.
fs.removeSync("tmp-out");
try {
fs.renameSync("out", "tmp-out");
fs.mkdirsSync("out");
} catch (e) {
console.log("Failed to rename out to tmp-out - copying instead: error ", e);
fs.copySync("out", "tmp-out");
try {
fs.emptyDirSync("out");
} catch (e2) {
console.log("Failed to empty dir out: error ", e2);
}
}
// Preserve anything that was correctly dumped into "infusion" on the previous round
if (fs.existsSync("tmp-out/infusion")) {
fs.renameSync("tmp-out/infusion", "out/infusion");
}
// Anything that was generated on this round gets copied into infusion
fs.copySync("tmp-out", "out/infusion/" + docsVersion);
fs.removeSync("tmp-out");
// Copy the files for GitHub Pages:
// redirect index.htmls and CNAME
fs.copySync(path.join(rootPath, "src", "ghpages-files"), "out");
}
}
};