forked from nknapp/comment-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (73 loc) · 2.46 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
var path = require("path");
var _ = require("lodash");
var byMatcher = require("./db-generated/by-matcher.js");
/**
* Determine the index of a language specifiation in the languages-array.
* @param filename
* @returns {*}
*/
var langIndex = function (filename) {
// Look for whole filename and for extension ("Makefile" is not an extesion,
// but should be matched to the appropriate language)
var index = byMatcher[filename];
if (_.isUndefined(index)) {
index = byMatcher[path.extname(filename)];
}
if (_.isUndefined(index)) {
throw new Error("Cannot find language definition for '" + filename + "'");
}
return index;
};
/**
* Load the comment-pattern for a given file.
* The file-language is determined by the file-extension.
* @param {string} `filename` the name of the file
* @returns {object} the comment-patterns
* @api public
*/
function commentPattern(filename) {
var base = require("./db-generated/base.js");
var clone = _.cloneDeep(base[langIndex(filename)]);
delete clone.srcFile;
return clone;
}
/**
* Load the comment-regex for a given file.
* The result contains a regex that matches the comments
* in the specification. It also has information about
* which the different capturing groups of an object.
*
* @param {string} `filename` the name of the file
* @returns {object} an object containing regular expressions and capturing-group metadata,
* see usage example for details
* @api public
* @name commentPattern.regex
*/
commentPattern.regex = function commentRegex(filename) {
var regex = require("./db-generated/regexes.js");
return _.cloneDeep(regex[langIndex(filename)]);
};
/**
* Returns the code-context detector for a given filename
* (based on the name or the file-extension
* @param {string} filename the name of the file that will be evaluated.
* @return {function(string,number)} a function that detects the code-context
* based on a line of code (and a line-number)
*/
commentPattern.codeContext = function codeContext(filename) {
var base = require("./db-generated/base.js");
var langFile = base[langIndex(filename)].srcFile;
return require("./languages/code-context/" + langFile);
};
/**
* Returns patterns for all supported languages
* @return {array}
*/
commentPattern.allPatterns = function allPatterns() {
return require("./db-generated/base.js").map((config) => {
const newConfig = { ...config };
delete newConfig.srcFile;
return newConfig;
});
};
module.exports = commentPattern;