-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (50 loc) · 1.52 KB
/
index.js
File metadata and controls
61 lines (50 loc) · 1.52 KB
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
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var shell = require('shelljs');
var cheerio = require("cheerio");
var PLUGIN_NAME = 'gulp-static-php';
var DOCUMENT_LIST = {'/': null};
var doRequest = function doRequest(processor, url) {
var html = shell.exec("php " + processor + "--url=" + url, {silent:true});
return html.stdout;
}
var scrubLinks = function scrubLinks(html) {
var $ = cheerio.load(html);
$("a").each(function() {
var link = $(this).attr("href");
if(link.charAt(0) == "/" && typeof DOCUMENT_LIST[link] == "undefined") {
DOCUMENT_LIST[link] = null;
}
});
}
var getLink = function getLink() {
var link = false;
Object.keys(DOCUMENT_LIST).forEach(function(key,index) {
if (DOCUMENT_LIST[key] == null) {
link = key;
return true;
}
}.bind(link));
return link;
}
var gulpStaticPhp = function gulpStaticPhp(options, sync) {
return through.obj(function(file, enc, cb) {
var processor = file.path;
var link;
while((link = getLink()) !== false ) {
var html = doRequest(processor, link);
var filename = "." + link;
this.push(new gutil.File({
cwd: "",
base: "",
path: filename,
contents: new Buffer(html)
}));
DOCUMENT_LIST[link] = filename;
scrubLinks(html);
}
cb(null);
});
}
module.exports = gulpStaticPhp;