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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
var config = {
anchorClass : "glyphicon glyphicon-link"
}
var gulp = require('gulp')
var markdown = require('gulp-markdown')
var tap = require('gulp-tap')
var gutil = require('gulp-util')
var cheerio = require('gulp-cheerio')
var highlight = require('highlight.js')
var fs = require('fs')
var Handlebars = require('handlebars')
var notifier = require('node-notifier')
var path = require('path')
var handlebarshelperis = require('handlebars-helper-is')
handlebarshelperis(Handlebars)
var server = require('gulp-server-livereload')
// .replace(rStringToRegExp,'\\$1'))
// "[]" => "\[\]"
var rStringToRegExp = /([.$^{[(|)*+?\/])/g
var fStringToPrefixRegExp = function (str) {
return new RegExp(
'^' + (
str.replace(rStringToRegExp, '\\$1')
)
);
}
// 渲染数据源:为了让-include 能访问数据源,每次渲染都将数据源绑定在此对象上。
var oRenderData = {}
/*!
让模板支持引入其他模板作为模块
{{-include header}}
加载 _template/header.html 到当前代码行
*/
Handlebars.registerHelper('include', function (path) {
path = '_template/' + path + '.html'
if (fs.existsSync(path)) {
return Handlebars.compile(
new Handlebars.SafeString(
fs.readFileSync(path, 'utf-8')
).toString()
)(oRenderData)
} else {
var message = "Not find: {{include " + path + '}}'
gutil.log(gutil.colors.red(message))
notifier.notify({
'title': 'Gulp Not find',
'message': message
});
return new Handlebars.SafeString('<span style="color:red">' + message + '</span>')
}
})
/*!
比较2个参数
{{-is a b "==" "!=="}}
*/
Handlebars.registerHelper('-is', function (a, b, yes, no) {
return a == b?yes:no;
})
/*!
编译 markdown
@paths {string|array} - glob 或一个文件的绝对路径 gulp.src(paths)
@dist {string} - 相当于 gulp.dest(dist) 中的 dist
*/
var compileMD = function (paths) {
gulp.src(paths)
.pipe(markdown({
highlight: function (code) {
return highlight.highlightAuto(code).value
}
}))
.pipe(cheerio(function($, file){
var $titles = $('h1,h2,h3,h4,h5,h6')
$titles.removeAttr('id')
var $a = $('a')
$a.each(function () {
var $this = $(this)
var target = $this.attr('target');
if (!target){
$this.attr('target', '_blank')
}
})
}))
.pipe(tap(function (file) {
oRenderData = {
URL_PATH: file.path.replace(fStringToPrefixRegExp(__dirname),'')
}
var html = file.contents.toString()
// 将 <a href="demo.md"> 替换为 <a href="demo.html">
html = html.replace(/(href=['"][^"']+\.)md(['"])/g,'$1html$2')
// 将 <!-- #hash_demo --> 转换为锚记 <a href="#hash_demo" name="hash_demo"></a>
// oHash 用于防止 hash 重复
html = html.replace(/<!---([^-]+)-->/g,'$1')
// 将 <!---<div class="contents">--> 替换为 <div class="contents"> (因为官方文档中存在一些带 class 的 div,此处需要兼容官方文档样式)
var oHash = {}
html = html.replace(/<!--\s*#([^\s*]+)\s*-->/g, function () {
var hash = arguments[1];
if (oHash[hash]) {
gutil.log(gutil.colors.red(hash + 'is repeated'))
notifier.notify({
'title': 'hash repeated',
'message': Handlebars.escapeExpression(arguments[0])
})
} else {
oHash[hash] = true;
}
return '<a href="#' + hash + '" name="' + hash + '" class="' + config.anchorClass +' gulp-page-anchor" aria-hidden="true" ></a>'
})
/*!
获取 markdown 中的 JSON信息,做为 _template/*.html 模板的渲染数据
<!--_PAGEDATA
{
"title": "页面标题",
"github":"nimojs/gulp-page",
"githubissuesid": 1,
"createData": "2015-04-10",
"keywords": "关键词,以 , 为分隔符",
"description": "页面描述",
"_template": "default"
}
_PAGEDATA-->
*/
html = html.replace(/<\!--\_PAGEDATA([\s\S]*)?\_PAGEDATA-->/, function () {
try {
var pagedata = JSON.parse(arguments[1])
for (var key in pagedata) {
oRenderData[key] = pagedata[key]
}
} catch (err) {
gutil.log(gutil.colors.red(message))
notifier.notify({
'title': ' Not find',
'message': message
});
}
// 取到 JSON 后删除 <!--_PAGEDATA ... _PAGEDATA-->
return '';
})
// 将渲染数据的 content 属性定义为 *.md 编译后的 html
oRenderData.content = html
oRenderData._template = oRenderData._template || 'default'
var templatePath = '_template/' + oRenderData._template + '.html'
var filename = path.basename(file.path)
if (fs.existsSync(templatePath)) {
var template = fs.readFileSync(templatePath, 'utf-8')
template = template.replace(/\{\{\s*content\s*\}\}/g,'{{{content}}}')
.replace(/\{\{\s*include([^}]+)\}\}/g,'{{{$1}}}')
var output = Handlebars.compile(template)(oRenderData)
file.contents = new Buffer(output)
gutil.log('Markdown ' + gutil.colors.green(filename))
} else {
var message = "Not find:" + templatePath + '(' + filename + ')'
gutil.log(gutil.colors.red(message))
notifier.notify({
'title': 'Gulp Not find',
'message': message
});
file.contents = new Buffer(message)
}
}))
.pipe(gulp.dest(function(file) {
return file.base.replace(fStringToPrefixRegExp(__dirname + '/posts'), __dirname)
}))
}
gulp.task('watch-markdown', function () {
gulp.watch('**/*.md', function (event) {
compileMD(event.path)
})
})
gulp.task('watch-handlebars', function () {
gulp.watch('_template/*.html', function (event) {
compileMD(['*.md','!(node_modules/**/*)'])
})
})
gulp.task('posts', function (){
compileMD('posts/**/*.md')
})
gulp.task('reload', function () {
gulp.src('./')
.pipe(server({
livereload: true,
directoryListing: true,
defaultFile: 'index.html',
open: true
}))
})
gulp.task('default',['watch-markdown', 'watch-handlebars', 'reload'])
|