-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
140 lines (119 loc) · 4.55 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
/* jshint camelcase:false, unused:false, laxbreak:true, laxcomma:true, expr:true, boss:true, eqnull:true, esversion:6 */
/* globals debugger */
/**
*
* @overview gulpfile.js - Создание html версии документации
*
* @author lilliputten <lilliputten@yandex.ru>
* @since 2018.08.10, 12:17
* @version 2018.08.10, 12:17
*
* $Date: 2018-08-10 15:16:38 +0300 (Пт, 10 авг 2018) $
* $Id: gulpfile.js 10530 2018-08-10 12:16:38Z miheev $
*
*/
// Params...
// Paths...
const targetPath = 'html';
// html template filenames
const htmlPreFile = 'templates/html-pre.html';
const htmlPostFile = 'templates/html-post.html';
// Markdown...
const myMarked = require('marked');
const highlight = require('highlight.js');
const markdownOptions = {
// renderer: new myMarked.Renderer(),
highlight: (code) => {
return highlight.highlightAuto(code).value;
},
// pedantic: false,
// gfm: true,
// tables: true,
// breaks: false,
// sanitize: false,
// smartLists: true,
// smartypants: false,
// xhtml: false,
};
// Requirements...
const fs = require('fs-extra');
const path = require('path');
const del = require('del');
const gulpMarkdown = require('gulp-markdown');
// const gulpMarkdown = require('gulp-markdown-github-style');
const gulp = require('gulp');
const gulpDebug = require('gulp-debug');
// const gulpReplace = require('gulp-replace'); // https://www.npmjs.com/package/gulp-replace
const gilpInsert = require('gulp-insert'); // https://www.npmjs.com/package/gulp-insert
const gulpRename = require('gulp-rename'); // https://www.npmjs.com/package/gulp-rename
const gulpIf = require('gulp-if'); // https://github.com/robrich/gulp-if
// const gulpSequence = require('gulp-sequence'); // https://www.npmjs.com/package/gulp-sequence
// const lazypipe = require('lazypipe'); // https://github.com/OverZealous/lazypipe
const gulpConcat = require('gulp-concat');
const gulpCssmin = require('gulp-cssmin'); // https://www.npmjs.com/package/gulp-cssmin
const gulpStylus = require('gulp-stylus'); // https://www.npmjs.com/package/gulp-stylus
// Assets...
// html templates
const htmlPre = fs.readFileSync(htmlPreFile).toString();
const htmlPost = fs.readFileSync(htmlPostFile).toString();
// Utils...
const parseVars = (content, vars) => {
const result = content.replace(/{{([\w\.-]+)}}/g, function (match, id) {
if ( vars[id] != null ) {
return vars[id];
}
return match;
});
return result;
};
// Tasks...
gulp.task('clean', (next) =>
del( targetPath + '/**/*', { force: true }, next )
);
gulp.task('styles', () =>
gulp.src([
'styles/**/*.{css,styl}',
// 'node_modules/highlight.js/styles/github.css',
// 'node_modules/highlight.js/styles/railscasts.css',
// 'node_modules/highlight.js/styles/zenburn.css',
// 'node_modules/highlight.js/styles/atom-one-dark.css',
'node_modules/highlight.js/styles/solarized-dark.css',
], { base: '.' })
// .pipe(gulpDebug({ title: 'styles <-' }) )
.pipe( gulpIf( file => file.path.endsWith('.styl'), gulpStylus() ) )
.pipe(gulpConcat('styles/all.css'))
.pipe(gulpCssmin())
// .pipe(gulpDebug({ title: 'styles ->' }) )
.pipe(gulp.dest(targetPath))
);
gulp.task('img', () =>
gulp.src('img/**/*', { base: '.' })
// .pipe(gulpDebug({ title: 'img <-' }) )
.pipe(gulp.dest(targetPath))
);
gulp.task('md', () =>
gulp.src('*.md')
// gulp.src('README.md')
// .pipe(gulpDebug({ title: 'md <-' }) )
.pipe(gulpMarkdown(markdownOptions))
.pipe(gilpInsert.transform((contents, file) => {
const filename = path.basename(file.path);
const matchTitle = contents.match(/<h1.*?>(.*?)<\/h1>/i);
const vars = {
filename,
title : ( matchTitle && matchTitle[1] ) || filename,
};
contents = contents
.replace(/(<a[^<>]* href=")README\.md(")/ig, '$1index.html$2')
.replace(/(<a[^<>]* href=")([^\/"]*)\.md(")/ig, '$1$2.html$3')
.replace(/<pre>/ig, '<pre class="hljs">')
;
return parseVars(htmlPre, vars) + contents + parseVars(htmlPost, vars);
}))
.pipe( gulpIf( file => file.path.endsWith('README.html'), gulpRename('index.html') ) )
// .pipe(gulpDebug({ title: 'md ->' }) )
.pipe(gulp.dest(targetPath))
);
gulp.task('make', gulp.parallel('styles', 'img', 'md'));
gulp.task('all', gulp.series('clean', 'make'));
gulp.task( 'default', gulp.parallel('all'));