-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocess.js
158 lines (136 loc) · 4.24 KB
/
preprocess.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
var fs = require('fs');
var path = require('path');
const marked = require('marked');
// var tmpdir = __dirname + './tmp/';
var tmpdir = './tmp/';
if (!fs.existsSync(tmpdir)){
fs.mkdirSync(tmpdir);
}
function ensureExists(path, mask, cb) {
if (typeof mask == 'function') { // allow the `mask` parameter to be optional
cb = mask;
mask = 0777;
}
fs.mkdir(path, mask, function(err) {
if (err) {
if (err.code == 'EEXIST') cb(null); // ignore the error if the folder already exists
else cb(err); // something else went wrong
} else cb(null); // successfully created folder
});
}
function readFiles(dirname, onFileContent, onError) {
fs.readdir(dirname, function(err, filenames) {
if (err) {
onError(err);
return;
}
filenames.forEach(function(filename) {
var stat = fs.statSync(dirname + filename);
if (stat && stat.isDirectory()) {
}else{
fs.readFile(dirname + filename, 'utf-8', function(err, content) {
if (err) {
onError(err);
return;
}
onFileContent(dirname + filename, content);
});
}
});
});
}
function copyFileSync( source, target ) {
var targetFile = target;
//if target is a directory a new file with the same name will be created
if ( fs.existsSync( target ) ) {
if ( fs.lstatSync( target ).isDirectory() ) {
targetFile = path.join( target, path.basename( source ) );
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync( source, target ) {
var files = [];
//check if folder needs to be created or integrated
var targetFolder = path.join( target, path.basename( source ) );
if ( !fs.existsSync( targetFolder ) ) {
fs.mkdirSync( targetFolder );
}
//copy
if ( fs.lstatSync( source ).isDirectory() ) {
files = fs.readdirSync( source );
files.forEach( function ( file ) {
var curSource = path.join( source, file );
if ( fs.lstatSync( curSource ).isDirectory() ) {
copyFolderRecursiveSync( curSource, targetFolder );
} else {
copyFileSync( curSource, targetFolder );
}
} );
}
}
var glossary = [
{
name: '哈希',
detail: 'Hash',
ref: '#/section1/1-Overview'
},
{
name: '协因子',
detail: 'cofactor',
},
{
name: '未使用交易',
detail: 'UTXO: Unspent Transaction(TX) Output',
},
{
name: '公钥哈希值支付',
detail: 'P2PKH: Pay To Public Key Hash',
},
];
function preprocess(content) {
let pcnt = content
.replace(
/<!-- code:(.*?)(?:;branch:(.*?))?(?:;line:(\d+?)-(\d+?))? -->/g,
function(match, path, branch, lstart, lend){
branch = branch || "master";
const loc = (lstart && lend) ? `#L${lstart}-L${lend}` : "";
const locDesc = (lstart && lend) ? `\n代码行:${lstart}-${lend}` : "";
return `<p class="coderef">
参考代码:
<a href="https://github.com/uchaindb/LearnBlockchainByCode/blob/${branch}/${path}${loc}"
title="分支:\[${branch}\]${locDesc}" target="_blank">
${path}
</a>
</p>`;
})
.replace(/读书提示:.*?。/g, "");
for (var i = 0, len = glossary.length; i < len; i++) {
var re = new RegExp('`' + glossary[i].name + '`', 'g');
pcnt = pcnt.replace(re, `<abbr title="${glossary[i].detail}">${glossary[i].name}</abbr>`);
}
pcnt = pcnt.replace(/^(#.*) \{docsify-.*\}/gm, "$1");
pcnt = pcnt.replace(/\[\s*!\[NuGet].*\s*.*\s*\]\((.*)\)/gm, "[Nuget]($1)");
return pcnt;
}
readFiles('docs/', function(filename, content) {
let pcnt = preprocess(content);
// pcnt = marked(pcnt);
// filename = path.basename(filename, '.md') + '.html';
filename = path.basename(filename);
let filepath = tmpdir + filename;
// console.log(filepath);
ensureExists(path.dirname(filepath), 0777, function(err) {
if (err)
console.log(err);
else {
fs.writeFile(filepath, pcnt, function(err) {
if(err) { return console.log(err); }
console.log("The file [" + filename + "] was saved!");
});
}
});
}, function(err) {
throw err;
});
copyFolderRecursiveSync('docs/images', tmpdir);