-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
172 lines (129 loc) · 4.17 KB
/
gulpfile.js
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
const gulp = require('gulp')
//const markdown = require('gulp-markdown');
const markdown = require('gulp-remarkable')
const wrapper = require('gulp-wrapper');
const flatten = require('gulp-flatten');
const writeGood = require('write-good')
const child_process = require('child_process')
const replace = require('gulp-replace')
const path = require('path')
const pygmentize = require('pygmentize-bundled')
const deasync = require('deasync')
const slug = require('slug')
const concat = require('gulp-concat')
const minifyCSS = require('gulp-minify-css')
const sitemap = require('gulp-sitemap')
const globMarkdown = ['./**/*.md', '!./node_modules/**'];
const mdWatcher = gulp.watch(globMarkdown, ['md'])
//mdWatcher.on('change', ({path}, stat)=>{
//run_and_print('./node_modules/write-good/bin/write-good.js', path)
//})
const htmlWatcherFromMd = gulp.watch('./**/*.md')
htmlWatcherFromMd.on('change', ({path: filePath}, stat)=>{
// Changes extension to .html
let pathObj = path.parse(filePath)
pathObj.ext = '.html'
pathObj.base = null // Obey .ext
let htmlPath = path.format(pathObj)
run_and_print('./node_modules/readability-checker/lib/readability.js', htmlPath)
})
function run_and_print(fname, path) {
child_process.execFile(fname, [path], (error, stdout, stderr)=>{
console.log(path)
console.log(stderr)
console.log(stdout)
})
}
gulp.watch(globMarkdown, ['md'])
gulp.watch('css/**/*.css', ['css'])
gulp.task('css', function(){
return gulp.src('css/**/*.css')
.pipe(minifyCSS())
.pipe(concat('style.min.css'))
.pipe(gulp.dest('.'))
})
gulp.task('md', function(){
//const opts = {title: 'File $BASENAME in $DIRNAME', stylesheet: 'css/modest.css'};
const opts = {
remarkableOptions: {
typographer: true,
highlight: highlighter
}
}
return gulp.src(globMarkdown)
//.pipe(markdown(opts))
.pipe(markdown(opts, (md=>{
md.renderer.rules.heading_open = headingAnchorRendererPlugin
})))
.pipe(wrapper({
header: '<!DOCTYPE html><html><head><meta charset="utf-8"/>!insertMeta<meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="@style.min.css"/></head><body>',
footer: '</body></html>\n'
}))
.pipe(replace('@style.min.css', relativePathReplace('style.min.css')))
.pipe(replace('/img/stock', relativePathReplace('img/stock')))
.pipe(replace('!insertMeta', insertMeta))
.pipe(gulp.dest('.'))
});
gulp.task('default', [ 'md', 'css' , 'sitemap']);
gulp.task('sitemap', ['md'], function () {
gulp.src(['./**/*.html', '!./node_modules/**', '!./schedule/**'], {
read: false
})
.pipe(sitemap({
siteUrl: 'http://charon77.github.io'
}))
.pipe(gulp.dest('.'));
});
function headingAnchorRendererPlugin(tokens, idx ) {
if (tokens[idx+1].type == 'inline') {
let heading_anchor = slug(tokens[idx+1].content, {lower: true})
return '<h' + tokens[idx].hLevel + ' id="' + heading_anchor + '">';
}
return '<h' + tokens[idx].hLevel + '>';
}
function insertMeta() {
let file = this.file
let pathObj = path.parse(file.path)
pathObj.base = null
pathObj.ext = ".meta.js"
let metaJsPath = path.format(pathObj)
try {
let metaJs = require(metaJsPath)
return metaJs()
} catch (e) {
if (e.name != "Error") // Print only error is not caused by missing .meta.js
{
console.error(e)
}
}
}
function relativePathReplace (fileName) {
function replaceDelegate () {
let file = this.file
let relativePathToHome = path.dirname( path.relative(file.path, file.base) )
let relativePathToFile = path.join(relativePathToHome, fileName)
return relativePathToFile
}
return replaceDelegate
}
function highlighter (code, lang) {
if (lang)
{
return syncPygmentize(code,lang)
} else {
return ''
}
}
function syncPygmentize(code, lang) {
let error = undefined
let result = undefined
pygmentize({ lang: lang, format: 'html' }, code, (err, res)=>{
error=err
result =res
if (err) {
throw err
}
})
deasync.loopWhile (()=>{return result == undefined})
return result.toString()
}