-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathploc.js
172 lines (141 loc) · 7.77 KB
/
ploc.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
'use strict';
const { regex } = require('regex');
const ploc = {};
ploc.conf = {};
ploc.conf.minItemsForToc = 3;
ploc.utils = {};
ploc.utils.reverseString = function (string) {
return string.split("").reverse().join("");
};
ploc.utils.capitalizeString = function (string) {
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
};
ploc.utils.getAnchor = function (name) {
return name.trim().toLowerCase().replace(/[^\w\- ]+/g, '').replace(/\s/g, '-').replace(/\-+$/, '');
};
// Helper function to get the doc data as JSON object - you can use this
// function to creata your own output instead of using ploc.getDoc(code).
ploc.getDocData = function (code) {
// We need to work on a reversed string to avoid to fetch too much text, so the keywords for package, function and so on are looking ugly...
// https://github.com/slevithan/regex
const
regexItem = regex('gim')` # !!! DO NOT FORGET: WE ARE WORKING ON A REVERSED STRING !!!
\s* /\*{2,} \s* # doc comment end
(?<description> (.|\s)*? ) # description in doc comment
\s* \*{2,}/ \s* # doc comment begin
(?<signature> (.|\s)*? # signature
(\s+ tcejbo \s+ sa)? \s* # optional "as object" clause
(?<name> \g<identifier> ) # name
(\s* \. \s* \g<identifier>)? \s* # optional name space (schema)
(?<type> (reggirt|epyt|erudecorp|noitcnuf|egakcap) # type
(\s+ rebmem)? # optional "member" keyword
(\s+ lanif)? # optional "final" keyword
(\s+ citats)? )) # optional "static" keyword (end of signature group)
(\s+ ecalper\s+ro)? # optional "or replace" clause
(\s+ etaerc)? # optional "create" clause
\s* # optional whitspace
,? # optional comma
$ # we need to stop at the line start (we have reversed text, so $ is really ^)
(?(DEFINE)
(?<identifier> ( [\w$#]+ | "\P{C}+" ) ) # \P{C}+ : Match only visible characters.
# https://www.regular-expressions.info/unicode.html#prop
# https://stackoverflow.com/questions/1247762/regex-for-all-printable-characters
)
`,
regexHeader = regex`
(?<header> \s* # whitespace including newlines
\g<leadingspace>
( # Setext header style:
\S # - one no whitespace character
.+ # - anything else then new lines
\g<newline>
\g<leadingspace>
=+ # - one or more equal signs
\ * # - zero or more spaces
| # ATX header style:
\# # - one hash sign
\ + # - one or more spaces
.+ # - anything else then new lines
) ) # end of header group
\g<newline>
(?(DEFINE)
(?<newline> ( \r\n | \n | \r ) ) # one new line (different shapes)
(?<leadingspace> \ {0,4} ) # up to four spaces
)
`,
regexLeadingWhitespace = /^\s*/,
anchors = [],
data = {};
let match;
data.header = '';
data.toc = (ploc.conf.tocStyles ? '<ul style="' + ploc.conf.tocStyles + '">\n' : '');
data.items = [];
code = ploc.utils.reverseString(code);
// Get base attributes
const matches = code.matchAll(regexItem);
for (const match of matches) {
//console.log('match:', match);
let item = {};
item.description = ploc.utils.reverseString(match.groups.description)
.replace(/{{@}}/g, '@') // Special SQL*Plus replacements. SQL*Plus is reacting on those special
.replace(/{{#}}/g, '#') // characters when they occur as the first character in a line of code.
.replace(/{{\/}}/g, '/'); // That can be bad when you try to write Markdown with sample code.
item.signature = ploc.utils.reverseString(match.groups.signature);
item.name = ploc.utils.reverseString(match.groups.name);
item.type = ploc.utils.capitalizeString(ploc.utils.reverseString(match.groups.type).replace(/\s+/g, ' '));
data.items.push(item);
}
// Calculate additional attributes.
data.items.reverse().forEach(function (item, i) {
// Process global document header, if provided in first item (index = 0).
if (i === 0) {
if (match = regexHeader.exec(data.items[i].description)) {
data.header = match.groups.header;
data.items[i].description = data.items[i].description
.replace(regexHeader, '')
.replace(regexLeadingWhitespace, '');
}
}
// Define item header and anchor for TOC.
data.items[i].header = data.items[i].type + ' ' + data.items[i].name;
data.items[i].anchor = ploc.utils.getAnchor(data.items[i].header);
// Ensure unique anchors.
if (anchors.indexOf(data.items[i].anchor) !== -1) {
let j = 0, anchor = data.items[i].anchor;
while (anchors.indexOf(data.items[i].anchor) !== -1 && j++ <= 100) {
data.items[i].anchor = anchor + '-' + j;
}
}
anchors.push(data.items[i].anchor);
data.toc += (
ploc.conf.tocStyles ?
`<li><a href="#${data.items[i].anchor}">${data.items[i].header}</a></li>\n` :
`- [${data.items[i].header}](#${data.items[i].anchor})\n`
);
});
data.toc += (ploc.conf.tocStyles ? '</ul>\n' : '');
return data;
};
// The main function to create the Markdown document.
ploc.getDoc = function (code) {
const docData = ploc.getDocData(code);
const provideToc = (docData.items.length >= ploc.conf.minItemsForToc);
let doc = '';
doc += (docData.header ? docData.header + '\n\n' : '');
doc += (provideToc ? docData.toc + '\n\n' : '');
docData.items.forEach(function (item, i) {
const level = (i === 0 && !docData.header ? 1 : 2);
const comment = (level === 1 ? '=' : '-').repeat((15 + item.header.length + item.anchor.length));
doc += [
(ploc.conf.autoHeaderIds ?
`<h${level}><a id="${item.anchor}"></a>${item.header}</h${level}>\n<!--${comment}-->` :
`${'#'.repeat(level)} ${item.header}`),
item.description,
'SIGNATURE',
'```sql\n' + item.signature + '\n```\n',
''
].join('\n\n');
});
return doc;
}
module.exports = ploc;