Skip to content

Commit db5f817

Browse files
committed
split out keyword compiler
1 parent 12a00eb commit db5f817

File tree

2 files changed

+70
-74
lines changed

2 files changed

+70
-74
lines changed

src/lib/compile_keywords.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// keywords that should have no default relevance value
2+
const COMMON_KEYWORDS = 'of and for in not or if then'.split(' ');
3+
4+
/**
5+
* Given raw keywords from a language definition, compile them.
6+
*
7+
* @param {string | Record<string,string>} rawKeywords
8+
* @param {boolean} caseInsensitive
9+
*/
10+
export function compileKeywords(rawKeywords, caseInsensitive) {
11+
/** @type KeywordDict */
12+
const compiledKeywords = {};
13+
14+
if (typeof rawKeywords === 'string') { // string
15+
splitAndCompile('keyword', rawKeywords);
16+
} else {
17+
Object.keys(rawKeywords).forEach(function(className) {
18+
splitAndCompile(className, rawKeywords[className]);
19+
});
20+
}
21+
return compiledKeywords;
22+
23+
// ---
24+
25+
/**
26+
* Compiles an individual list of keywords
27+
*
28+
* Ex: "for if when while|5"
29+
*
30+
* @param {string} className
31+
* @param {string} keywordList
32+
*/
33+
function splitAndCompile(className, keywordList) {
34+
if (caseInsensitive) {
35+
keywordList = keywordList.toLowerCase();
36+
}
37+
keywordList.split(' ').forEach(function(keyword) {
38+
const pair = keyword.split('|');
39+
compiledKeywords[pair[0]] = [className, scoreForKeyword(pair[0], pair[1])];
40+
});
41+
}
42+
}
43+
44+
/**
45+
* Returns the proper score for a given keyword
46+
*
47+
* Also takes into account comment keywords, which will be scored 0 UNLESS
48+
* another score has been manually assigned.
49+
* @param {string} keyword
50+
* @param {string} [providedScore]
51+
*/
52+
function scoreForKeyword(keyword, providedScore) {
53+
// manual scores always win over common keywords
54+
// so you can force a score of 1 if you really insist
55+
if (providedScore) {
56+
return Number(providedScore);
57+
}
58+
59+
return commonKeyword(keyword) ? 0 : 1;
60+
}
61+
62+
/**
63+
* Determines if a given keyword is common or not
64+
*
65+
* @param {string} keyword */
66+
function commonKeyword(keyword) {
67+
return COMMON_KEYWORDS.includes(keyword.toLowerCase());
68+
}

src/lib/mode_compiler.js

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import * as regex from './regex.js';
22
import { inherit } from './utils.js';
33
import * as EXT from "./compiler_extensions.js";
4-
5-
// keywords that should have no default relevance value
6-
const COMMON_KEYWORDS = 'of and for in not or if then'.split(' ');
4+
import { compileKeywords } from "./compile_keywords.js";
75

86
// compilation
97

@@ -13,7 +11,7 @@ const COMMON_KEYWORDS = 'of and for in not or if then'.split(' ');
1311
* Given the raw result of a language definition (Language), compiles this so
1412
* that it is ready for highlighting code.
1513
* @param {Language} language
16-
* @param {{plugins: [HLJSPlugin] }} opts
14+
* @param {{plugins: HLJSPlugin[]] }} opts
1715
* @returns {CompiledLanguage}
1816
*/
1917
export function compileLanguage(language, {plugins}) {
@@ -423,73 +421,3 @@ function expandOrCloneMode(mode) {
423421
// no special dependency issues, just return ourselves
424422
return mode;
425423
}
426-
427-
/***********************************************
428-
Keywords
429-
***********************************************/
430-
431-
/**
432-
* Given raw keywords from a language definition, compile them.
433-
*
434-
* @param {string | Record<string,string>} rawKeywords
435-
* @param {boolean} caseInsensitive
436-
*/
437-
function compileKeywords(rawKeywords, caseInsensitive) {
438-
/** @type KeywordDict */
439-
const compiledKeywords = {};
440-
441-
if (typeof rawKeywords === 'string') { // string
442-
splitAndCompile('keyword', rawKeywords);
443-
} else {
444-
Object.keys(rawKeywords).forEach(function(className) {
445-
splitAndCompile(className, rawKeywords[className]);
446-
});
447-
}
448-
return compiledKeywords;
449-
450-
// ---
451-
452-
/**
453-
* Compiles an individual list of keywords
454-
*
455-
* Ex: "for if when while|5"
456-
*
457-
* @param {string} className
458-
* @param {string} keywordList
459-
*/
460-
function splitAndCompile(className, keywordList) {
461-
if (caseInsensitive) {
462-
keywordList = keywordList.toLowerCase();
463-
}
464-
keywordList.split(' ').forEach(function(keyword) {
465-
const pair = keyword.split('|');
466-
compiledKeywords[pair[0]] = [className, scoreForKeyword(pair[0], pair[1])];
467-
});
468-
}
469-
}
470-
471-
/**
472-
* Returns the proper score for a given keyword
473-
*
474-
* Also takes into account comment keywords, which will be scored 0 UNLESS
475-
* another score has been manually assigned.
476-
* @param {string} keyword
477-
* @param {string} [providedScore]
478-
*/
479-
function scoreForKeyword(keyword, providedScore) {
480-
// manual scores always win over common keywords
481-
// so you can force a score of 1 if you really insist
482-
if (providedScore) {
483-
return Number(providedScore);
484-
}
485-
486-
return commonKeyword(keyword) ? 0 : 1;
487-
}
488-
489-
/**
490-
* Determines if a given keyword is common or not
491-
*
492-
* @param {string} keyword */
493-
function commonKeyword(keyword) {
494-
return COMMON_KEYWORDS.includes(keyword.toLowerCase());
495-
}

0 commit comments

Comments
 (0)