-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmarker.js
116 lines (107 loc) · 3.04 KB
/
marker.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
// LICENSE : MIT
/*
* Feature: doxygen like snippet code.
* For code source documenting, see
* https://www.stack.nl/~dimitri/doxygen/manual/commands.html#cmdsnippet
*
* Gibook usage:
*
* [import:<markername>](path/to/file)
*
* NB: markername must begin with a letter to avoid conflict with slice
* line range.
*/
"use strict";
const logger = require("winston-color");
const commentOpen = "(/+/+|#|%|/\\*|<!--)";
const commentClose = "(\\*/|-->)?";
const doxChar = "[*!/#]"; // doxygen documentation character
const spaces = "[ \t]*"; // h spaces
const spacesAny = "\\s*"; // h+v spaces
const markerNameFormat = "(\\s*[a-zA-Z][\\w\\s]*)"; // Must contain a char.
/*
* format: [import:<markername>](path/to/file)
* @param {Object} keyValObject
* @return {string}
*/
export function getMarker(keyValObject) {
return keyValObject.marker;
}
/**
* format: [import:<markername>](path/to/file)
* check if the import filled has a markername.
* @example:
* hasMarker(label)
* @param {Object} keyValObject
* @returns {boolean}
*/
export function hasMarker(keyValObject) {
const marker = getMarker(keyValObject);
return marker !== undefined && marker !== "";
}
/* Parse the code from given markers
*
* see test/marker-test.js
*/
/**
* get sliced code by {@link markername}
* @param {string} code
* @param {string} markers
* @returns {string}
*/
export function markerSliceCode(code, markers) {
if (markers === undefined || markers === "") {
return code;
}
var parsedcode = "";
const markerlist = markers.split(",");
let i = 0;
// regex
markerlist.forEach(marker => {
const balise = "\\[" + marker + "\\]";
const pattern =
"\\n" +
spacesAny +
commentOpen +
doxChar +
spaces +
balise +
spaces +
commentClose +
spaces;
const regstr = pattern + "\\n*([\\s\\S]*)" + pattern;
const reg = new RegExp(regstr);
const res = code.match(reg);
if (res) {
parsedcode += res[3]; // count parenthesis in pattern.
} else {
logger.warn("markersSliceCode(): marker `" + marker + "` not found");
parsedcode += "Error: marker `" + marker + "` not found";
}
if (markerlist.length > 0 && i < markerlist.length - 1) {
parsedcode += "\n";
}
i++;
});
return parsedcode;
}
/** Replace all regex occurence by sub in the string str,
* @param {string} str
* @param {string} reg
* @param {string} sub
* @return {string}
*/
export function replaceAll(str, reg, sub) {
return str.replace(new RegExp(reg, "g"), sub);
}
/** Function that remove all markers in the given code
* @param {string} code
* @return {string}
*/
export function removeMarkers(code) {
// various language comment
const tag = "\\[" + markerNameFormat + "\\]";
const pattern =
spacesAny + commentOpen + doxChar + spaces + tag + spaces + commentClose + spaces;
return replaceAll(code, pattern, "");
}