-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtemplates-tasks.js
105 lines (92 loc) · 3.21 KB
/
templates-tasks.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
var grunt = require("grunt");
module.exports = {
markdown: {
src: "./Readme.md",
dest: "<%= package.templates.data %>readme.json",
// dest: "./Readme.html",
options: {
template: "<%= package.paths.templates %>misc/readme.template.html",
postCompile: function(src, context) {
var json = {};
// Remove the first two lines of the readme
json.content = src.split("\n").slice(2).join("\n");
// Load Cheerio for DOM manipulation
var cheerio = require("cheerio"),
$ = cheerio.load(src);
// For each header in the document, build a navigation tree
var headers = [];
$("h2").each(function(idx, header) {
headers.push({
id: $(header).attr("id"),
title: $(header).text()
});
});
json.headers = headers;
return JSON.stringify(json);
}
}
},
swig: {
options: {
templatePath: "<%= package.paths.templates %>",
data: function() {
/**
Take the .json file in the data directory,
and then merge the objects together
*/
var path = require("path"),
fs = require("fs"),
_ = require("lodash");
var pathToDatafiles = path.join(grunt.file.readJSON("package.json").templates.data, "**/*.json");
var dataFiles = grunt.file.glob.sync(pathToDatafiles),
dataObj = {};
dataFiles.forEach(function(df) {
var dataToMerge = {};
var name = path.parse(df).name,
data = grunt.file.readJSON(df);
if (name === "data") {
/**
Variables within the data.json file are global.
They can be used in templates without needing prefixes
(e.g. {{var}} instead of {{data.var}})
*/
dataToMerge = data;
} else {
dataToMerge[name] = data;
}
dataObj = _.merge(dataObj, dataToMerge);
});
return dataObj;
}()
},
files: [{
expand: true,
cwd: "<%= package.templates.pages %>",
ext: ".html",
src: ["**/*.html"],
dest: "<%= package.paths.build %>"
}],
},
prettify: {
files: [{
expand: true,
cwd: "<%= package.paths.build %>",
src: ["**/*.html"],
dest: "<%= package.paths.build %>"
}]
},
htmllint: {
files: [{
expand: true,
cwd: "<%= package.paths.build %>",
src: ["**/*.html"],
}]
},
watch: {
files: [
"<%= package.paths.templates %>**/*.html",
"<%= package.templates.data %>**/*.{json,yml}"
],
tasks: ["swig"]
}
};