This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateDoc.ts
162 lines (139 loc) · 5.33 KB
/
generateDoc.ts
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
///<reference path="node_modules/@types/node/index.d.ts"/>
import * as Asciidoctor from 'asciidoctor.js';
import * as readline from 'readline';
import {Interface} from 'readline';
import * as fs from 'fs';
let imgDir = './dist/pages/img/';
if (!fs.existsSync(imgDir)) {
fs.mkdirSync(imgDir, {recursive: true});
}
// letsGenerateSomeDocs('../rest/src/main/asciidoc/', '../rest/target/generated-snippets');
letsGenerateSomeDocs('./adoc/', './snippets');
function letsGenerateSomeDocs(fileLocation: string, snippetsLocation: string = '') {
//passsing directoryPath and callback function
fs.readdir(fileLocation, {withFileTypes: true}, (err, fileList) => {
const files = [];
//handling error
if (err) {
return console.error('Unable to scan directory: ' + err);
}
const regex = new RegExp(/(.*)\.adoc/i);
//listing all files using forEach
fileList.forEach(function (file: fs.Dirent) {
if (file.isFile()) {
const result = file.name.match(regex);
if (result) {
files.push(result[1]);
} else if (fileLocation.indexOf('/img/') >= 0) {
fs.copyFile(`${fileLocation}${file.name}`, `${imgDir}${file.name}`, (e) => {
console.log(file.name, e);
});
}
} else if (file.isDirectory()) {
letsGenerateSomeDocs(`${fileLocation}${file.name}/`, snippetsLocation);
}
});
compile(files, fileLocation, snippetsLocation);
});
}
function compile(fileList: string[], fileLocation, snippetsLocation) {
fileList.forEach((fileName: string) => {
let content = '';
const lineReader: Interface = readline.createInterface({
input: fs.createReadStream(`${fileLocation}${fileName}.adoc`)
});
let titlePrefix = '=';
lineReader.on('line', function (line) {
if (snippetsLocation) {
titlePrefix = getTitlePrefix(line, titlePrefix);
content = content + `${operate(line, titlePrefix)}\n`;
} else {
content = content + `${line}\n`;
}
});
lineReader.on('close', function () {
runAsciidoc(content, fileName, snippetsLocation)
});
});
}
function runAsciidoc(content: string, fileName: string, snippetsLocation: string) {
const asciidoctor = Asciidoctor();
console.log('Start AsciiDoc: ', fileName);
let html = asciidoctor.convert(content, {
'header_footer': false,
'verbose': true,
'backend': 'html',
'source-highlighter': 'highlightjs',
'mkdirs': true,
'safe': 'unsafe',
'to_dir': './src/partials/docs',
'to_file': `${fileName}.hbs`,
'attributes': {
'icons': 'font',
'snippets': snippetsLocation + '/noPin',
'pincodeSnippets': snippetsLocation + '/pin',
'toc': 'left',
'toclevels': 4,
},
});
}
function getTitlePrefix(line: string, currentPrefix) {
const rePattern = new RegExp(/^(=*) /i);
const arrMatches = line.match(rePattern);
if (arrMatches) {
let prefix = arrMatches[1];
if (prefix.length >= 5) {
return prefix;
} else {
return prefix + '='
}
} else {
return currentPrefix;
}
}
function capitalize(str) {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
function titlePrependExample(title, type) {
switch (type) {
case 'curl-request':
case 'http-request':
case 'http-response':
return 'Example ' + title;
default:
return title;
}
}
function operate(line: string, titlePrefix: string) {
const rePattern = new RegExp(/operation::([0-9a-zA-Z_-]+)\[(snippets=)?['"]([0-9a-zA-Z-,_]+)['"]\]/i);
const arrMatches = line.match(rePattern);
if (arrMatches) {
const name = arrMatches[1];
const snippets = arrMatches[3].split(',');
let result = '';
snippets.forEach((type: string) => {
const t = type.toLowerCase();
const u = t.replace(/-/g, '_');
const title = titlePrependExample(t.replace(/-/g, ' ').replace('http', 'HTTP'), t);
result += `\n\n[.sect-${type}]\n[[example_${u}_${name}]]\n${titlePrefix} ${capitalize(title)}\n\ninclude::{snippets}/${name}/${t}.adoc[]`;
});
return result.trimLeft();
}
const rePatternWithPin = new RegExp(/operation::([0-9a-zA-Z_-]+)\[(snippets=)?['"]([0-9a-zA-Z-,_]+)['"]\&pincode=true\]/i);
const arrMatchesWithPin = line.match(rePatternWithPin);
if (arrMatchesWithPin) {
const name = arrMatchesWithPin[1];
const snippets = arrMatchesWithPin[3].split(',');
let result = '';
snippets.forEach((type: string) => {
const t = type.toLowerCase();
const u = t.replace(/-/g, '_');
const title = titlePrependExample(t.replace(/-/g, ' ').replace('http', 'HTTP'), t);
result += `\n\n[.sect-${type}]\n[[example_${u}_${name}]]\n${titlePrefix} ${capitalize(title)}\n\ninclude::{pincodeSnippets}/${name}/${t}.adoc[]`;
});
return result.trimLeft();
}
return line;
}