Skip to content
This repository was archived by the owner on Nov 21, 2018. It is now read-only.

Commit b432657

Browse files
committed
Modifies to use handlebars, only adds checksums/git info to changed files, outputs only changed files
1 parent 35f0466 commit b432657

File tree

3 files changed

+75
-40
lines changed

3 files changed

+75
-40
lines changed

gulp/tasks/templates.js

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ var gulp = require('gulp'); // because this is a gulp task. duh.
66
var HTMLtemplate = require('html-template'); // substack's html template implementation
77
var md = require('markdown-it')({ html: true }); // to convert markdown to html
88
var source = require('vinyl-source-stream'); // used to convert substack's readStream to vinylStream
9-
var replaceStream = require('replacestream'); // used to add an element to the html: making sure each page has it's own class if needed in css
109
var moment = require('moment-timezone');
1110
var exec = require('child_process').exec
1211
var utils = require('../util/template-utils.js');
1312
var path = require('path');
13+
var crypto = require("crypto");
14+
var gulpif = require('gulp-if');
15+
var handlebars = require('gulp-compile-handlebars');
16+
var buffer = require('vinyl-buffer');
17+
var rename = require('gulp-rename');
1418

1519
gulp.task('templates', function() {
1620
var separator = '<SEPARATOR>';
@@ -40,30 +44,59 @@ gulp.task('templates', function() {
4044
var html = md.render(markdown); // convert md string to html string
4145
var thisFileJSON = _.cloneDeep(templateJSON); // clone in the template JSON object
4246
var pageTitle = thisFileJSON['browser-title'];
43-
thisFileJSON = _.omit(thisFileJSON, 'browser-title');
44-
var finalJSON = {};
45-
_.forEach(thisFileJSON, function(value, key) {
46-
finalJSON['[i18n-' + key + ']'] = value;
47-
})
48-
finalJSON['[i18n-content]'] = html; // Attach md2html string to the interpolation object
49-
var htmlObj = HTMLtemplate(); // finally using that holder for the template stream
50-
i18nObj = htmlObj.template('i18n', {
51-
include: false
52-
}); // same
5347
var filepath = __dirname.split('gulp/tasks')[0] + 'source/templates/main.html'; // get the main template file location. There can be multiple, this is just a proof of concept
5448
var destinationDirectory = path.dirname('public/' + file.filepathArray.join('/'));
55-
var fileStream = fs.createReadStream(filepath) // pulling this code from substack's example on html-template
56-
.pipe(replaceStream('<title i18n-title>io.js - JavaScript I/O</title>', '<title i18n-title>' + pageTitle + '</title>'))
57-
.pipe(replaceStream('markdown-page=""', 'markdown-page="' + file.filename + '"')) // add css-triggerable attribute to body
58-
.pipe(replaceStream('[page-stylesheet]', file.filename)) // require in specific stylesheet
59-
.pipe(replaceStream('Build Time:', 'Build Time: ' + buildTime))
60-
.pipe(replaceStream('Commit Sha:', 'Commit Sha: ' + commitSha))
61-
.pipe(replaceStream('Commit Msg:', 'Commit Msg: ' + commitMsg))
62-
.pipe(htmlObj)
63-
.pipe(source(file.filename + '.html')) // converting the readStream to a vinyl stream so gulp can write it back to the disk
64-
.pipe(gulp.dest(destinationDirectory)); // dump it in the appropriate language subfolder
65-
i18nObj.write(finalJSON); // write the interpolation JSON to the template
66-
i18nObj.end(); // saving? this is taken from substack too.
49+
var hashes = [];
50+
var isChangedFile = function(vinylFile) {
51+
if (vinylFile.isNull()) {
52+
return;
53+
}
54+
if (hashes[vinylFile.path] != null) {
55+
console.log('skipped', vinylFile.path, hashes[vinylFile.path]);
56+
return hashes[vinylFile.path];
57+
}
58+
59+
var currentContent = fs.readFileSync(path.join(destinationDirectory, file.filename + '.html'), {encoding: 'utf8'});
60+
var currentHash = currentContent.match(/Hashsum:\s(\b([a-f0-9]{40})\b)/);
61+
if (currentHash && currentHash.length > 2) {
62+
currentHash = currentHash[1]
63+
} else {
64+
currentHash = null
65+
}
66+
67+
var contents = String(vinylFile.contents);
68+
var newHash = crypto
69+
.createHash("sha1")
70+
.update(vinylFile.contents, "binary")
71+
.digest("hex");
72+
73+
var isChanged = (currentHash !== newHash);
74+
75+
console.log('is changed', vinylFile.path, currentHash, newHash)
76+
77+
if (isChanged) {
78+
contents = contents.replace(/Hashsum:(?:\s+\b([a-f0-9]{40})\b)?/, `Hashsum: ${newHash}`)
79+
contents = contents.replace('Build Time:', `Build Time: ${buildTime}`)
80+
contents = contents.replace('Commit Sha:', `Commit Sha: ${commitSha}`)
81+
contents = contents.replace('Commit Msg:', `Commit Msg: ${commitMsg}`)
82+
vinylFile.contents = new Buffer(contents);
83+
}
84+
hashes[vinylFile.path] = isChanged;
85+
return isChanged;
86+
};
87+
var fileStream = gulp.src(filepath) // pulling this code from substack's example on html-template
88+
.pipe(rename(file.filename + '.html')) // converting the readStream to a vinyl stream so gulp can write it back to the disk
89+
.pipe(buffer())
90+
.pipe(handlebars({
91+
i18n: thisFileJSON,
92+
content: html,
93+
lang: lang,
94+
build: {
95+
markdownPage: file.filename,
96+
pageStylesheet: file.filename
97+
}
98+
}))
99+
.pipe(gulpif(isChangedFile, gulp.dest(destinationDirectory))); // dump it in the appropriate language subfolder
67100
});
68101
});
69102
});

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,24 @@
4747
"glob": "^4.3.5",
4848
"gulp": "^3.8.10",
4949
"gulp-changed": "^1.1.0",
50+
"gulp-compile-handlebars": "^0.4.4",
5051
"gulp-connect": "^2.2.0",
5152
"gulp-filesize": "0.0.6",
5253
"gulp-htmlmin": "^1.0.0",
54+
"gulp-if": "^1.2.5",
5355
"gulp-imagemin": "^2.1.0",
5456
"gulp-less": "^2.0.1",
5557
"gulp-markdown": "^1.0.0",
5658
"gulp-postcss": "^4.0.2",
59+
"gulp-rename": "^1.2.0",
5760
"gulp-stylus": "^2.0.0",
5861
"html-template": "^1.2.1",
5962
"lodash": "^2.4.1",
6063
"markdown-it": "^3.0.4",
6164
"moment-timezone": "^0.3.0",
62-
"replacestream": "^2.0.0",
6365
"require-dir": "^0.1.0",
6466
"run-sequence": "^1.0.2",
67+
"vinyl-buffer": "^1.0.0",
6568
"vinyl-source-stream": "^1.0.0"
6669
}
6770
}

source/templates/main.html

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<!DOCTYPE html>
2-
<html lang="en">
2+
<html lang="{{lang}}">
33

44
<head>
55
<meta charset="utf-8">
6-
<title i18n-title>io.js - JavaScript I/O</title>
6+
<title i18n-title>{{i18n.browser-title}}</title>
77
<meta http-equiv="X-UA-Compatible" content="IE=edge">
88
<meta name="viewport" content="width=device-width,initial-scale=1">
99
<meta name="description" content="io.js is an npm compatible platform originally based on node.js">
1010
<meta name="keywords" content="iojs, io.js, io js, javascript io, uv, libuv, node-forward, node forward, node, node.js, node.js forward, nodejs, nodejs forward, javascript">
1111
<link href="https://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet">
1212
<link href="../main.css" rel="stylesheet">
1313
<link href="../mobile.css" rel="stylesheet">
14-
<link href="../[page-stylesheet].css" rel="stylesheet">
14+
<link href="../{{build.pageStylesheet}}.css" rel="stylesheet">
1515
<link rel="icon" href="/images/1.0.0.ico" type="image/x-icon">
1616
<link rel="apple-touch-icon" href="/images/apple-touch-icon-1.0.0.png">
1717
<meta property="og:image" content="https://iojs.org/images/1.0.0.png">
@@ -20,29 +20,27 @@
2020
<meta property="og:image:height" content="1563">
2121
</head>
2222

23-
<body template="i18n" markdown-page="">
23+
<body markdown-page="{{build.markdownPage}}">
2424

2525
<header>
2626
<div class="content">
27-
<a href="index.html" class="logo" i18n-logo-text>io.js</a>
27+
<a href="index.html" class="logo" i18n-logo-text>{{i18n.logo-text}}</a>
2828
<div class="spacer"></div>
29-
<a href="faq.html" i18n-faq-link>FAQ</a>
30-
<a href="es6.html" i18n-es6-link>ES6</a>
31-
<a href="https://iojs.org/api/" i18n-api-link>API Docs</a>
29+
<a href="faq.html" i18n-faq-link>{{i18n.faq-link}}</a>
30+
<a href="es6.html" i18n-es6-link>{{i18n.es6-link}}</a>
31+
<a href="https://iojs.org/api/" i18n-api-link>{{i18n.api-link}}</a>
3232
</div>
3333
</header>
3434

35-
<div class="content clearfix" i18n-content>
36-
<!-- Markdown Content Here -->
37-
</div>
35+
<div class="content clearfix" i18n-content>{{{content}}}</div>
3836

3937
<footer class="content">
4038
<nav>
41-
<a href="https://github.com/iojs/io.js/issues" i18n-issues-link>GitHub Issues</a><!--
42-
--><a href="https://github.com/iojs" i18n-org-link>GitHub Org</a><!--
43-
--><a href="https://webchat.freenode.net/?channels=io.js" i18n-irc-link>IRC Chat</a><!--
44-
--><a href="http://logs.libuv.org/io.js/latest" i18n-irc-logs-link>Logs</a><!--
45-
--><a href="https://github.com/iojs/io.js/blob/v1.x/GOVERNANCE.md#readme" i18n-gov-link>Governance</a>
39+
<a href="https://github.com/iojs/io.js/issues" i18n-issues-link>{{i18n.issues-link}}</a><!--
40+
--><a href="https://github.com/iojs" i18n-org-link>{{i18n.org-link}}</a><!--
41+
--><a href="https://webchat.freenode.net/?channels=io.js" i18n-irc-link>{{i18n.irc-link}}</a><!--
42+
--><a href="http://logs.libuv.org/io.js/latest" i18n-irc-logs-link>{{i18n.irc-logs-link}}</a><!--
43+
--><a href="https://github.com/iojs/io.js/blob/v1.x/GOVERNANCE.md#readme" i18n-gov-link>{{i18n.gov-link}}</a>
4644
</nav>
4745
</footer>
4846

@@ -57,6 +55,7 @@
5755
5856
Commit Sha:
5957
Commit Msg:
58+
Hashsum:
6059
6160
=========== END BUILD INFORMATION ===========
6261
-->

0 commit comments

Comments
 (0)