forked from gnab/remark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.js
176 lines (144 loc) · 4.38 KB
/
make.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
require('shelljs/make');
require('shelljs/global');
// Targets
target.all = function () {
target.test();
target.minify();
target.boilerplate();
};
target.highlighter = function () {
console.log('Bundling highlighter...');
rm('-rf', 'vendor/highlight.js');
mkdir('-p', 'vendor');
pushd('vendor');
exec('git clone https://github.com/isagalaev/highlight.js.git');
pushd('highlight.js');
exec('git checkout tags/8.0');
popd();
popd();
bundleHighlighter('src/remark/highlighter.js');
};
target.test = function () {
target['lint']();
target['bundle']();
target['test-bundle']();
console.log('Running tests...');
run('mocha-phantomjs test/runner.html');
};
target.lint = function () {
console.log('Linting...');
run('jshint src', {silent: true});
};
target.bundle = function () {
console.log('Bundling...');
bundleResources('src/remark/resources.js');
run('browserify ' + components() + ' src/remark.js',
{silent: true}).output.to('out/remark.js');
};
function components () {
var componentsPath = './src/remark/components';
return ls(componentsPath)
.map(function (component) {
return '-r ' + componentsPath + '/' + component + '/' + component +
'.js:' + 'components/' + component;
})
.join(' ');
}
target['test-bundle'] = function () {
console.log('Bundling tests...');
[
"require('should');",
"require('sinon');"
]
.concat(find('./test')
.filter(function(file) { return file.match(/\.js$/); })
.map(function (file) { return "require('./" + file + "');" })
)
.join('\n')
.to('_tests.js');
run('browserify ' + components() + ' _tests.js',
{silent: true}).output.to('out/tests.js');
rm('_tests.js');
};
target.boilerplate = function () {
console.log('Generating boilerplate...');
generateBoilerplateSingle("boilerplate-single.html");
};
target.minify = function () {
console.log('Minifying...');
run('uglifyjs out/remark.js', {silent: true}).output.to('out/remark.min.js');
};
// Helper functions
var path = require('path')
, config = require('./package.json').config
, ignoredStyles = ['brown_paper', 'school_book', 'pojoaque']
;
function bundleResources (target) {
var resources = {
DOCUMENT_STYLES: JSON.stringify(
less('src/remark.less'))
, CONTAINER_LAYOUT: JSON.stringify(
cat('src/remark.html'))
};
cat('src/templates/resources.js.template')
.replace(/%(\w+)%/g, function (match, key) {
return resources[key];
})
.to(target);
}
function bundleHighlighter (target) {
var highlightjs = 'vendor/highlight.js/src/'
, resources = {
HIGHLIGHTER_STYLES: JSON.stringify(
ls(highlightjs + 'styles/*.css').reduce(mapStyle, {}))
, HIGHLIGHTER_ENGINE:
cat(highlightjs + 'highlight.js')
, HIGHLIGHTER_LANGUAGES:
config.highlighter.languages.map(function (language) {
return '{name:"' + language + '",create:' +
cat(highlightjs + 'languages/' + language + '.js') + '}';
}).join(',')
};
cat('src/templates/highlighter.js.template')
.replace(/%(\w+)%/g, function (match, key) {
return resources[key];
})
.to(target);
}
function generateBoilerplateSingle(target) {
var resources = {
REMARK_MINJS: escape(cat('out/remark.min.js')
// highlighter has a ending script tag as a string literal, and
// that causes early termination of escaped script. Split that literal.
.replace('"</script>"', '"</" + "script>"'))
};
cat('src/templates/boilerplate-single.html.template')
.replace(/%(\w+)%/g, function (match, key) {
return resources[key];
})
.to(target);
}
function mapStyle (map, file) {
var key = path.basename(file, path.extname(file))
, tmpfile = path.join(tempdir(), 'remark.tmp')
;
if (ignoredStyles.indexOf(key) === -1) {
('.hljs-' + key + ' {\n' + cat(file) + '\n}').to(tmpfile);
map[key] = less(tmpfile);
rm(tmpfile);
}
return map;
}
function less (file) {
return run('lessc -x ' + file, {silent: true}).output.replace(/\n/g, '');
}
function run (command, options) {
var result = exec(pwd() + '/node_modules/.bin/' + command, options);
if (result.code !== 0) {
if (!options || options.silent) {
console.error(result.output);
}
exit(1);
}
return result;
}