-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
179 lines (155 loc) · 4.85 KB
/
gulpfile.js
File metadata and controls
179 lines (155 loc) · 4.85 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
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
const gulp = require('gulp');
const gulpLoadPlugins = require('gulp-load-plugins');
const marked = require('marked');
const Prism = require('node-prismjs');
const pug = require('pug');
const del = require('del');
const browserSync = require('browser-sync');
const GithubSlugger = require('github-slugger');
const dakuonMap = [
['が','ぎ','ぐ','げ','ご','ざ','じ','ず','ぜ','ぞ','だ','ぢ','づ','で','ど','ば','び','ぶ','べ','ぼ','ぱ','ぴ','ぷ','ぺ','ぽ'],
['か','き','く','け','こ','さ','し','す','せ','そ','た','ち','つ','て','と','は','ひ','ふ','へ','ほ','は','ひ','ふ','へ','ほ'],
];
marked.setOptions({
highlight: function(code, lang, cb) {
const highlighted = (Prism.languages[lang])
? Prism.highlight(code, Prism.languages[lang])
: code;
return highlighted;
}
});
pug.filters.code = function(str, options, locals) {
const opts = Object.assign({}, options || {}, locals || {});
const lang = opts.lang || 'plain';
const start = (typeof opts.start === 'number')? opts.start : null;
const highlighted = (Prism.languages[opts.lang])
? Prism.highlight(str, Prism.languages[opts.lang])
: str;
const lineNumberGutter = (start !== null)
? `<span class="line-numbers-rows" style="counter-reset: linenumber ${start - 1}">`
+ Array(str.split('\n').length).join('<span></span>')
+ `</span>`
: '';
return `<pre class="${(start !== null)? 'line-numbers' : ''}" ${(start !== null)? 'data-start="'+start+'"' : ''}>`
+ `<code class="language-${lang}">${lineNumberGutter}${highlighted}</code>`
+ `</pre>`;
}
pug.filters.marked = (str, options) => {
return marked(str, options);
}
const keywordDict = {};
const slugger = new GithubSlugger();
const slugPrefix = 'kwd_';
pug.filters['define-keyword'] = (str, options) => {
const keywordList = str.split(/\n/)
.map(s => s.trim())
.filter(s => s !== '');
const tags = [];
for (let kw of keywordList) {
const slug = `${slugPrefix}${slugger.slug(kw)}`;
if (kw in keywordDict) {
keywordDict[kw].push(slug);
} else {
keywordDict[kw] = [slug];
}
tags.push(`<div class="index-keyword" id="${slug}"></div>`);
}
return tags.join('\n') + '\n';
}
pug.filters['render-index'] = (str, options) => {
const keywords = Object.keys(keywordDict).sort();
let firstLetter = null;
let output = '';
const pickFirstLetter = (str) => {
const c = str.charAt(0).toUpperCase();
const i = dakuonMap[0].indexOf(c);
return i >= 0? dakuonMap[1][i] : c;
}
keywords.forEach(k => {
if (pickFirstLetter(k) !== firstLetter) {
firstLetter = pickFirstLetter(k);
output += `<div class="first-letter">${firstLetter}</div>\n`;
}
// keywords are deined with a furigana
// ex: なまえ|名前
const keyName = k.indexOf('|') >= 0 ? k.substr(k.indexOf('|') + 1) : k;
const keyLink = keywordDict[k]
.map(slug => `<a href="#${slug}"></a>`)
.join(', ');
output += `<li class="index-item">\n`
+ ` <div class="index-keyword-name">${keyName}</div>\n`
+ ` <div class="index-keyword-links">${keyLink}</div>\n`
+ `</li>\n`;
});
return output;
}
const $ = gulpLoadPlugins();
const plumberOpt = {
errorHandler: function(err) {
console.error(err.stack);
this.emit('end');
},
}
gulp.task('default', ['pug', 'assets', 'stylus']);
gulp.task('pug', () => {
gulp.src('content/hyoshi.pug')
.pipe($.plumber(plumberOpt))
.pipe($.pug({
pug: pug,
pretty: true,
}))
.pipe(gulp.dest('dest/'));
gulp.src('content/index.pug')
.pipe($.plumber(plumberOpt))
.pipe($.pug({
pug: pug,
pretty: true,
}))
.pipe(gulp.dest('dest/'));
});
gulp.task('assets', ['assets:delete'], () =>
gulp.src('content/assets/**/*')
.pipe(gulp.dest('dest/assets/'))
);
gulp.task('assets:delete',
del.bind(null, ['dest/assets/**/*'])
);
gulp.task('stylus', () => {
gulp.src('style/hyoshi.styl')
.pipe($.plumber(plumberOpt))
.pipe($.stylus({
'include css': true,
}))
.pipe(gulp.dest('dest/'));
gulp.src('style/main.styl')
.pipe($.plumber(plumberOpt))
.pipe($.sourcemaps.init())
.pipe($.stylus({
'include css': true,
}))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('dest/'))
.pipe(browserSync.reload({
stream: true,
}));
});
gulp.task('browsersync', () => {
browserSync({
server: {
baseDir: 'dest/',
index: 'index.html',
},
});
});
gulp.task('bs-reload', () => {
browserSync.reload();
})
gulp.task('watch', ['default', 'browsersync'], () => {
gulp.watch('content/**/*.pug', ['pug']);
gulp.watch('content/assets/**/*', ['assets']);
gulp.watch('style/**/*.styl', ['stylus']);
gulp.watch('dest/*.html', ['bs-reload']);
});
gulp.task('watch:stylus', ['stylus'], () => {
gulp.watch('style/**/*.styl', ['stylus']);
});