forked from arve0/codeclub_lesson_builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
186 lines (173 loc) · 4.85 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
/*
* # DEPENDENCIES #
*/
var Metalsmith = require('metalsmith');
var templates = require('metalsmith-templates');
var collections = require('metalsmith-collections');
var setMetadata = require('metalsmith-filemetadata');
var filepath = require('metalsmith-filepath');
var pandoc = require('metalsmith-pandoc');
var ignore = require('metalsmith-ignore');
var relative = require('metalsmith-relative');
var define = require('metalsmith-define');
var marked = require('marked'); // for md strings in YAML header
var path = require('path');
var getPlaylists = require('./playlist');
var _ = require('lodash');
var changed = require('metalsmith-changed');
var paths = require('metalsmith-paths');
var fs = require('fs');
// code highlighting
var highlight = require('metalsmith-code-highlight');
var branch = require('metalsmith-branch');
// search
var lunr = require('lunr');
require('lunr-no/lunr.stemmer.support')(lunr);
require('lunr-no')(lunr);
var metlunr = require('metalsmith-lunr');
// get configuration variables
var config = require('./config.js');
// read front matter on demand
var matter = require('gray-matter');
/*
* # SETUP OBJECTS #
*/
// metadata
var metadataOptions = [
// search
{ pattern: path.join('**', '*.md'),
metadata: { lunr: true }},
// template for lessons
{ pattern: path.join('*', '**', '*.md'),
metadata: { template: 'lesson.jade' }},
// scratch lesson template
{ pattern: path.join('scratch', '**', '*.md'),
metadata: { template: 'scratch.jade' }},
// front page template
{ pattern: path.join('index.md'),
metadata: { template: 'index.jade' }},
// lesson index template
{ pattern: path.join('*', 'index.md'),
metadata: { template: 'lesson-index.jade' }},
];
// search - for is not a stopword in this context
var words = lunr.no.stopWordFilter.stopWords.elements;
words.splice(words.indexOf('for'), 1);
// ignores
var ignoreOptions = [
path.join('**', 'README.md'),
];
// collections
var collectionOptions = {};
config.collections.forEach(function(collection){
// options for collections
collectionOptions[collection] = {
pattern: path.join(collection, '**', '*.md'),
};
});
// defines available in template
var defineOptions = {
marked: marked,
_: _,
config: config,
isFile: isFile,
matter: frontmatter,
};
/** Returns true if file exists */
function isFile(dir, file){
var fullPath = path.join(config.sourceRoot, dir, file);
try {
fs.statSync(fullPath);
} catch (e) {
return false;
}
return true;
}
/** read front-matter from file, fail gracefully. */
function frontmatter(filename) {
var filepath = path.join(config.lessonRoot, config.sourceFolder, filename);
var m;
try {
m = matter.read(filepath);
} catch (e) {
return {};
}
return m.data;
}
// template
var templateOptions = {
engine: 'jade',
directory: path.join(config.builderRoot, 'templates'),
};
/*
* # EXPORT #
* build-function which takes a callback
*/
module.exports = function build(callback, options){
options = options || {};
var forceBuild = options.force || false;
// read playlists upon every build
var playlists = {};
config.collections.forEach(function(collection){
// playlists
var collectionFolder = path.join(config.lessonRoot, config.sourceFolder, collection);
playlists[collection] = getPlaylists(collectionFolder, config.playlistFolder);
});
// make it available in template
defineOptions.playlists = playlists;
// do the building
Metalsmith(config.lessonRoot)
.source(config.sourceFolder)
.use(ignore(ignoreOptions))
.clean(false) // do not delete files, allow gulp tasks in parallel
.use(paths())
// set template for exercises
.use(setMetadata(metadataOptions))
// add relative(path) for use in templates
.use(relative())
// create collections for index
.use(collections(collectionOptions))
.use(metlunr({
fields: {
contents: 1,
title: 10,
tags: 20,
},
pipelineFunctions: [
lunr.no.trimmer,
lunr.no.stopWordFilter,
lunr.no.stemmer
]
}))
// remove files not to build *after* we have set collections metadata
.use(paths())
.use(changed({
force: forceBuild,
extnames: {
'.md': '.html',
},
}))
// convert to html
.use(pandoc({
to: 'html5',
args: ['--section-divs', '--smart']
}))
// highlight code - exclude scratch code blocks
.use(branch()
.pattern(['**/*.html', '!scratch/**/*.html']) // no highlight on scratch blocks
.use(highlight())
)
// add file.link metadata (now files are .html)
.use(filepath())
// globals for use in templates
.use(define(defineOptions))
// apply templates
.use(templates(templateOptions))
//build
.destination('build')
.build(function(err){
if (err) console.log(err);
// callback when build is done
callback(err);
});
};