Skip to content

Commit d41df87

Browse files
author
Eric Prud'hommeaux
committed
+ support for multi-language 3rd-party packages
tested with: node ./tools/build.js -t all plaintext node ./tools/build.js -t all plaintext shexc solidity sparql ttl not tested for with test/detect or test/markup tests ('cause I don't know how).
1 parent 3ffc2bb commit d41df87

File tree

4 files changed

+45
-36
lines changed

4 files changed

+45
-36
lines changed

test/detect/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const fs = require('fs').promises;
77
const hljs = require('../../build');
88
const path = require('path');
99
const utility = require('../utility');
10-
const { getThirdPartyLanguages } = require('../../tools/lib/external_language')
10+
const { getThirdPartyPackages } = require('../../tools/lib/external_language')
1111

1212
function testAutoDetection(language, {detectPath}) {
1313
const languagePath = detectPath || utility.buildPath('detect', language);
@@ -34,7 +34,7 @@ function testAutoDetection(language, {detectPath}) {
3434

3535
describe('hljs.highlightAuto()', () => {
3636
before( async function() {
37-
let thirdPartyLanguages = await getThirdPartyLanguages();
37+
let thirdPartyPackages = await getThirdPartyPackages();
3838

3939
let languages = hljs.listLanguages();
4040
describe(`hljs.highlightAuto()`, function() {
@@ -44,11 +44,14 @@ describe('hljs.highlightAuto()', () => {
4444
});
4545
});
4646

47+
// assumes only one package provides the requested module name
4748
function detectTestDir(name) {
48-
let language = thirdPartyLanguages.find((x) => x.name == name);
49-
if (language) {
50-
return language.detectTestPath;
51-
}
49+
thirdPartyPackages.forEach(pkg => {
50+
const idx = pkg.names.indexOf(name);
51+
if (idx !== -1)
52+
return pkg.detectTestPaths[idx]
53+
});
54+
return null;
5255
}
5356
});
5457

test/markup/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const hljs = require('../../build');
77
const path = require('path');
88
const utility = require('../utility');
99

10-
const { getThirdPartyLanguages } = require("../../tools/lib/external_language")
10+
const { getThirdPartyPackages } = require("../../tools/lib/external_language")
1111

1212
function testLanguage(language, {testDir}) {
1313
describe(language, function() {
@@ -45,8 +45,12 @@ describe('highlight() markup', async () => {
4545
languages.forEach(testLanguage);
4646
}
4747

48-
let thirdPartyLanguages = await getThirdPartyLanguages();
49-
return thirdPartyLanguages.forEach((l) => testLanguage(l.name,{testDir: l.markupTestPath}));
48+
let thirdPartyPackages = await getThirdPartyPackages();
49+
return thirdPartyPackages.forEach(
50+
(pkg) => pkg.l.markupTestPaths.forEach(
51+
testDir => testLanguage(l.name, {testDir})
52+
)
53+
);
5054
})
5155

5256
it("adding dynamic tests...", async function() {} ); // this is required to work

tools/lib/external_language.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ class LanguagePackage {
1313
async trySrcLanguages() {
1414
let dir = path.join(this.dir,"src/languages/*");
1515
let languages = await glob(dir);
16-
if (languages[0]) {
17-
this.file = path.join(process.cwd(), languages[0]);
18-
this.name = path.basename(this.file,".js");
16+
if (languages.length) {
17+
this.files = languages.map(fn => path.join(process.cwd(), fn));
18+
this.names = this.files.map(fn => path.basename(fn,".js"));
1919
this._bundle = true;
2020
this._valid = true;
2121
return true;
22-
}
22+
} else { return false; }
2323
}
2424

25-
get markupTestPath() {
25+
get markupTestPaths() {
2626
if (this.bundle) {
27-
return `${this.dir}/test/markup/${this.name}`;
27+
return this.names.map(name => `${this.dir}/test/markup/${name}`);
2828
} else {
29-
return `${this.dir}/test/markup`;
29+
return [`${this.dir}/test/markup`];
3030
}
3131
}
3232

33-
get detectTestPath() {
33+
get detectTestPaths() {
3434
if (this.bundle) {
35-
return `${this.dir}/test/detect/${this.name}`;
35+
return this.names.map(name => `${this.dir}/test/detect/${name}`);
3636
} else {
37-
return `${this.dir}/test/detect`;
37+
return [`${this.dir}/test/detect`];
3838
}
3939
}
4040

@@ -50,8 +50,8 @@ class LanguagePackage {
5050
if (content.match(MODULE_DEFINER)) {
5151
this.loader = "definer";
5252
}
53-
this.file = file;
54-
this.name = path.basename(file,".js");
53+
this.files = [file];
54+
this.names = [path.basename(file,".js")];
5555
this._valid = true;
5656
return true;
5757
}
@@ -74,17 +74,17 @@ class LanguagePackage {
7474
}
7575
}
7676

77-
async function getThirdPartyLanguages() {
78-
let languages = [];
79-
let otherLanguages = await glob("./extra/*");
80-
for (let packageDir of otherLanguages) {
81-
let thirdPartyLanguage = new LanguagePackage(packageDir)
82-
let valid = await thirdPartyLanguage.valid();
77+
async function getThirdPartyPackages() {
78+
let packages = [];
79+
let otherPackages = await glob("./extra/*");
80+
for (let packageDir of otherPackages) {
81+
let thirdPartyPackage = new LanguagePackage(packageDir)
82+
let valid = await thirdPartyPackage.valid();
8383
if (valid) {
84-
languages.push(thirdPartyLanguage)
84+
packages.push(thirdPartyPackage)
8585
}
8686
}
87-
return languages;
87+
return packages;
8888
}
8989

90-
module.exports = { LanguagePackage, getThirdPartyLanguages}
90+
module.exports = { LanguagePackage, getThirdPartyPackages}

tools/lib/language.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const REQUIRES_REGEX = /\/\*.*?Requires: (.*?)\n/s
99
const CATEGORY_REGEX = /\/\*.*?Category: (.*?)\n/s
1010
const LANGUAGE_REGEX = /\/\*.*?Language: (.*?)\n/s
1111
const {rollupCode} = require("./bundling.js")
12-
const { getThirdPartyLanguages } = require("./external_language")
12+
const { getThirdPartyPackages } = require("./external_language")
1313

1414
class Language {
1515

@@ -98,11 +98,13 @@ async function getLanguages() {
9898
fs.readdirSync("./src/languages/").forEach((file) => {
9999
languages.push(Language.fromFile(file));
100100
});
101-
let extraLanguages = await getThirdPartyLanguages();
102-
for (let ext of extraLanguages) {
103-
let l = Language.fromFile(ext.file);
104-
l.loader = ext.loader;
105-
languages.push(l);
101+
let extraPackages = await getThirdPartyPackages();
102+
for (let ext of extraPackages) {
103+
for (let file of ext.files) {
104+
let l = Language.fromFile(file);
105+
l.loader = ext.loader;
106+
languages.push(l);
107+
}
106108
}
107109
return languages;
108110
}

0 commit comments

Comments
 (0)