@@ -6,11 +6,15 @@ var gulp = require('gulp'); // because this is a gulp task. duh.
6
6
var HTMLtemplate = require ( 'html-template' ) ; // substack's html template implementation
7
7
var md = require ( 'markdown-it' ) ( { html : true } ) ; // to convert markdown to html
8
8
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
10
9
var moment = require ( 'moment-timezone' ) ;
11
10
var exec = require ( 'child_process' ) . exec
12
11
var utils = require ( '../util/template-utils.js' ) ;
13
12
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' ) ;
14
18
15
19
gulp . task ( 'templates' , function ( ) {
16
20
var separator = '<SEPARATOR>' ;
@@ -40,30 +44,59 @@ gulp.task('templates', function() {
40
44
var html = md . render ( markdown ) ; // convert md string to html string
41
45
var thisFileJSON = _ . cloneDeep ( templateJSON ) ; // clone in the template JSON object
42
46
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
53
47
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
54
48
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 ( / H a s h s u m : \s ( \b ( [ a - f 0 - 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 ( / H a s h s u m : (?: \s + \b ( [ a - f 0 - 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
67
100
} ) ;
68
101
} ) ;
69
102
} ) ;
0 commit comments