-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (106 loc) · 3.06 KB
/
index.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
/**
* Imports
*/
// through2 is a thin wrapper around node transform streams
var through = require('through2'),
gutil = require('gulp-util'),
fs = require('fs'),
path = require('path'),
_ = require('lodash'),
cheerio = require('cheerio'),
PluginError = gutil.PluginError;
/**
* Variables
*/
// read item template file
var itemsCollection = [],
dir,
itemTemplate = fs.readFileSync(__dirname + '/templates/item.html').toString(),
navTemplate = fs.readFileSync(__dirname + '/templates/nav.html').toString();
// consts
var PLUGIN_NAME = 'concat-html';
// Used to replace anything within a start and end tag.
var replaceRegExp = /<!--\s*?styleguide\s*?-->[\s\S]*?<!--\s*?endstyleguide\s*?-->/g;
// Used to remove start and end tags.
var removeRegExp = / *?<!--\s*?styleguide\s*?--> *\n?| *?<!--\s*?endstyleguide\s*?--> *\n?/g;
var navRegExp = /<!--\s*?sg-nav\s*?-->[\s\S]*?<!--\s*?endsg-nav\s*?-->/g;
/**
* Build each item
* @return {Stream} NodeJS Stream
*/
function build() {
var contentHtml = '';
// creating a stream through which each file will pass
var stream = through.obj(function(file, enc, cb) {
var data = {
code: file.contents.toString().replace(/\</gi, '<'),
contents: file.contents.toString(),
name: path.basename(file.relative, '.html'),
file: 'item.html'
};
$ = cheerio.load(file.contents.toString());
if ($(".sg-code").length) {
data.code = $(".sg-code").html().toString().replace(/\</gi, '<');
}
if ($(".sg-example").length) {
data.contents = $(".sg-example").html().toString();
}
var currentDir = path.dirname(file.relative);
if (dir !== currentDir) {
itemsCollection.push({
name: currentDir,
items: []
});
}
// add items list (nav)
itemsCollection[itemsCollection.length-1].items.push(data.name);
dir = currentDir;
// parse item template
contentHtml = gutil.template(itemTemplate, data);
file.contents = new Buffer(contentHtml);
this.push(file);
return cb();
});
// returning the file stream
return stream;
}
/**
* Replaces <!-- styleguide --> with the generated content
* @param {String} source - HTML template
* @param {String} dest - Generated HTML
* @return {Stream} NodeJS Stream
*/
function replaceHtml(source, dest) {
// read index file
var index = fs.readFileSync(source).toString();
if (!dest) {
throw new PluginError(PLUGIN_NAME, 'Missing index path!');
}
// creating a stream through which each file will pass
var stream = through.obj(function(file, enc, cb) {
if (file.isNull()) {
// do nothing if no contents
}
index = index.replace(replaceRegExp, file.contents.toString());
// create nav
var nav = _.template(navTemplate, {
folders: itemsCollection,
});
index = index.replace(navRegExp, nav);
// create new file
var processedFile = new gutil.File({
path: dest,
contents: new Buffer(index)
});
this.push(processedFile);
return cb();
});
// returning the file stream
return stream;
}
var concatHtml = {
replaceHtml: replaceHtml,
build: build
};
// exporting the plugin main function
module.exports = concatHtml;