-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
130 lines (97 loc) · 3.5 KB
/
gulpfile.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
var path = require("path");
var gulp = require("gulp");
var browsersync = require("browser-sync").create();
var data = require("gulp-data");
var swig = require("gulp-swig");
var del = require("del");
var marked = require("marked");
var frontmatter = require("front-matter");
var fs = require ("fs-extra");
var globalTokens = [];
function TokenMap (document, fullpath) {
this.tokens = [];
this.document = document.replace("index", "Introduction");
this.path = fullpath;
this.path = this.path.replace(".md", ".html");
this.addToken = function addToken (text) {
text = text.replace(/\<().*?\>/g, "");
var split = text.split(" ");
for (var i = 0; i < split.length; i++) {
if (split[i] == "br") continue;
if (split[i] == "") continue;
this.tokens.push(split[i]);
}
}
}
gulp.task("clean", function() {
return del(['dist/index.html']);
});
gulp.task("templates", ['clean'], function () {
return gulp.src(['src/docs/**/*.md'])
.pipe(data(function(file) {
var template = new Buffer(fs.readFileSync("templates/default"));
var content = frontmatter(String(file.contents));
var name = path.basename(file.path, ".md");
var relative = path.relative(__dirname + "/src/docs/", file.path);
var tokenPath = new TokenMap(name, relative);
globalTokens.push(tokenPath);
var title = content.attributes.title;
var additionalTags = content.attributes.tags;
var body = content.body;
if (additionalTags != null) {
for (var i = 0; i < additionalTags.length; i++) {
tokenPath.addToken(additionalTags[i]);
}
}
var headerRenderer = new marked.Renderer();
headerRenderer.heading = function (text, level) {
var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
tokenPath.addToken(text);
return '<h' + level + '><a name="' +
escapedText +
'" class="anchor" href="#' +
escapedText +
'">' + text + '</a></h' + level + '>';
}
try {
var markedBody = marked(body, { renderer: headerRenderer });
var tokens = marked.lexer(body);
var output = marked.parser(tokens);
file.contents = template;
var data = {
content: "content",
body: markedBody,
title: title
}
} catch (e) {
console.error(e);
}
return data;
}))
.pipe(swig({ defaults: {autoescape: false, cache: false} }))
.pipe(gulp.dest('dist/documentation'));
});
gulp.task('css', function() {
return gulp.src(['src/css/**.css'])
.pipe(gulp.dest('dist/css/'));
});
gulp.task('js', function() {
return gulp.src(['src/js/**.js'])
.pipe(gulp.dest('dist/js/'));
});
gulp.task('img', function() {
return gulp.src(['src/img/**'])
.pipe(gulp.dest('dist/img/'));
});
gulp.task("index", ['templates'], function () {
fs.writeFile("dist/documentation/documentation_index", JSON.stringify(globalTokens), "UTF-8");
})
gulp.task('build', ['templates', 'img', 'css', 'js', 'index'])
gulp.task('serve', ['build'], function() {
browsersync.init({
server: "./dist",
scrollRestoreTechnique: 'cookie'
});
gulp.watch(['src/**', 'templates/**'], ['build']);
gulp.watch("dist/**").on('change', browsersync.reload);
});