-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·122 lines (106 loc) · 5.08 KB
/
index.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
/**
* [moment: the moment module]
* @type {[type]}
*/
"use strict";
const moment = require('moment');
const debug = require('debug')('gitbook-plugin-signature');
const _ = require('lodash');
const GitCommandLine = require('git-command-line');
/**
* [main module]
* @type {Object}
*/
function formatSignature(key) {
return this.config.get('pluginsConfig')['gitbook-plugin-signature'].signature[key];
}
var Git;
module.exports = {
// Extend templating filters
filters: {
dateFormat: function (d, format, utc) {
return moment(d).utcOffset(parseInt(utc)).format(format);
},
copyright: function (organization) {
const copyright = '<br><br><br><span style="color:grey">Copyright © ' + organization + '<br>All rights reserved.</span>';
return '\n\n\n\n\n\n\n' + '<span style="text-align: center">' + copyright + '</span>';
},
signature: formatSignature,
s: formatSignature
},
// Hook process during build
hooks: {
// For all the hooks, this represent the current generator
// This is called before the book is generated
"init": function () {
console.log("generating signature......");
Git = new GitCommandLine('/tmp/gitTemp');
},
// This is called after the book generation
"finish": function () {
console.log("signature generated!");
Git = null;
},
"page:before": function (page) {
const defaultOption = {
'format': 'YYYY/MM/DD',
'utcOffset': '8'
};
var color = 'gray';
// autoTimeStamp
// var author = this.config.get('author');
var book = this;
return Git.log('--pretty="%an" -1 ' + page.path, {}).then(function (log) {
return log.res;
}).then(function (author) {
if (book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoTimeStamp) {
var timeStampFormat = defaultOption.format;
if (book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoTimeStamp.timeStampFormat) {
timeStampFormat = book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoTimeStamp.timeStampFormat;
}
if (book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoTimeStamp.color) {
color = book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoTimeStamp.color;
}
const colorLeft = ' <span style="color:' + color + '">';
var autolastModified = colorLeft + 'last modified by ' + author;
const autoTimeStamp = '{{ file.mtime | dateFormat("' + timeStampFormat + '", ' + defaultOption.utcOffset + ') }}</span>';
if (page.content.indexOf('{% set author') != -1) {
author = '{{author}}';
autolastModified = colorLeft + 'last modified by ' + author;
page.content = page.content.slice(0, (page.content.indexOf('%}\n') + 2))
+ autolastModified + ', ' + autoTimeStamp + '\n\n'
+ page.content.slice(page.content.indexOf('%}\n') + 2);
} else {
page.content = autolastModified + ', ' + autoTimeStamp + '\n\n' + page.content;
}
}
return page;
}).then(function (page) {
// auto copyright
if (book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoCopyright) {
var owner = book.config.get('author');
const year = '{{ file.mtime | dateFormat("' + 'YYYY' + '", ' + defaultOption.utcOffset + ') }}';
var style = "";
if (book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoCopyright.owner) {
owner = book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoCopyright.owner;
if (owner.toString().endsWith('.')) {
owner = owner.slice(0, -1);
}
}
if (book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoCopyright.center) {
style += 'text-align: center;';
}
if (book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoCopyright.color) {
color = book.config.get('pluginsConfig')['gitbook-plugin-signature'].autoCopyright.color;
}
style += 'color:' + color + ';';
page.content = page.content + '\n\n\n\n<br><br><div style="' + style + '">Copyright © ' + year + ' ' + owner + '.' + '<br>All rights reserved.</div>';
}
return page;
}).fail(function (err) {
console.error(err);
return page;
});
}
}
};