Skip to content

Commit

Permalink
Autoloader: Added support for aliases (#1829)
Browse files Browse the repository at this point in the history
This adds alias support for the Autoloader plugin.
  • Loading branch information
RunDevelopment authored Mar 24, 2019
1 parent 8d28c74 commit 52889b5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
43 changes: 27 additions & 16 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function languagePlugins(cb) {
componentsPromise.then(data => {
const languagesMap = {};
const dependenciesMap = {};
const aliasMap = {};

/**
* Tries to guess the name of a language given its id.
Expand All @@ -116,38 +117,48 @@ function languagePlugins(cb) {
}
}

for (const p in data.languages) {
if (p !== 'meta') {
const title = data.languages[p].displayTitle || data.languages[p].title;
for (const id in data.languages) {
if (id !== 'meta') {
const language = data.languages[id];
const title = language.displayTitle || language.title;

addLanguageTitle(p, title);
addLanguageTitle(id, title);

for (const name in data.languages[p].aliasTitles) {
addLanguageTitle(name, data.languages[p].aliasTitles[name]);
for (const name in language.aliasTitles) {
addLanguageTitle(name, language.aliasTitles[name]);
}

if (data.languages[p].alias) {
if (typeof data.languages[p].alias === 'string') {
addLanguageTitle(data.languages[p].alias, title);
if (language.alias) {
if (typeof language.alias === 'string') {
aliasMap[language.alias] = id;
addLanguageTitle(language.alias, title);
} else {
data.languages[p].alias.forEach(function (alias) {
language.alias.forEach(function (alias) {
aliasMap[alias] = id;
addLanguageTitle(alias, title);
});
}
}

if (data.languages[p].require) {
dependenciesMap[p] = data.languages[p].require;
if (language.require) {
dependenciesMap[id] = language.require;
}
}
}

const jsonLanguagesMap = JSON.stringify(languagesMap);
const jsonDependenciesMap = JSON.stringify(dependenciesMap);
const jsonAliasMap = JSON.stringify(aliasMap);

const tasks = [
{ plugin: paths.showLanguagePlugin, map: jsonLanguagesMap },
{ plugin: paths.autoloaderPlugin, map: jsonDependenciesMap }
{
plugin: paths.showLanguagePlugin,
maps: { languages: jsonLanguagesMap}
},
{
plugin: paths.autoloaderPlugin,
maps: { aliases: jsonAliasMap, dependencies: jsonDependenciesMap }
}
];

let cpt = 0;
Expand All @@ -162,8 +173,8 @@ function languagePlugins(cb) {
for (const task of tasks) {
const stream = src(task.plugin)
.pipe(replace(
/\/\*languages_placeholder\[\*\/[\s\S]*?\/\*\]\*\//,
'/*languages_placeholder[*/' + task.map + '/*]*/'
/\/\*(\w+)_placeholder\[\*\/[\s\S]*?\/\*\]\*\//g,
(m, mapName) => `/*${mapName}_placeholder[*/${task.maps[mapName]}/*]*/`
))
.pipe(dest(task.plugin.substring(0, task.plugin.lastIndexOf('/'))));

Expand Down
3 changes: 3 additions & 0 deletions plugins/autoloader/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ <h1>Examples</h1>
<p>Basic usage with some Perl code:</p>
<pre><code class="language-perl">my ($class, $filename) = @_;</code></pre>

<p>Alias support with TypeScript's <code class="language-none">ts</code>:</p>
<pre><code class="language-ts">const a: number = 0;</code></pre>

<p>The Less filter used in Pug:</p>
<pre><code class="language-pug" data-dependencies="less">:less
foo {
Expand Down
21 changes: 13 additions & 8 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
}

// The dependencies map is built automatically with gulp
var lang_dependencies = /*languages_placeholder[*/{"javascript":"clike","actionscript":"javascript","arduino":"cpp","aspnet":["markup","csharp"],"bison":"c","c":"clike","csharp":"clike","cpp":"c","coffeescript":"javascript","crystal":"ruby","css-extras":"css","d":"clike","dart":"clike","django":"markup-templating","ejs":["javascript","markup-templating"],"erb":["ruby","markup-templating"],"fsharp":"clike","flow":"javascript","glsl":"clike","gml":"clike","go":"clike","groovy":"clike","haml":"ruby","handlebars":"markup-templating","haxe":"clike","java":"clike","javadoc":["markup","java","javadoclike"],"jolie":"clike","jsdoc":["javascript","javadoclike"],"js-extras":"javascript","jsonp":"json","json5":"json","kotlin":"clike","less":"css","markdown":"markup","markup-templating":"markup","n4js":"javascript","nginx":"clike","objectivec":"c","opencl":"cpp","parser":"markup","php":["clike","markup-templating"],"phpdoc":["php","javadoclike"],"php-extras":"php","plsql":"sql","processing":"clike","protobuf":"clike","pug":["markup","javascript"],"qore":"clike","jsx":["markup","javascript"],"tsx":["jsx","typescript"],"reason":"clike","ruby":"clike","sass":"css","scss":"css","scala":"java","smarty":"markup-templating","soy":"markup-templating","swift":"clike","tap":"yaml","textile":"markup","tt2":["clike","markup-templating"],"twig":"markup","typescript":"javascript","t4-cs":["t4-templating","csharp"],"t4-vb":["t4-templating","visual-basic"],"vala":"clike","vbnet":"basic","velocity":"markup","wiki":"markup","xeora":"markup","xquery":"markup"}/*]*/;
var lang_dependencies = /*dependencies_placeholder[*/{"javascript":"clike","actionscript":"javascript","arduino":"cpp","aspnet":["markup","csharp"],"bison":"c","c":"clike","csharp":"clike","cpp":"c","coffeescript":"javascript","crystal":"ruby","css-extras":"css","d":"clike","dart":"clike","django":"markup-templating","ejs":["javascript","markup-templating"],"erb":["ruby","markup-templating"],"fsharp":"clike","flow":"javascript","glsl":"clike","gml":"clike","go":"clike","groovy":"clike","haml":"ruby","handlebars":"markup-templating","haxe":"clike","java":"clike","javadoc":["markup","java","javadoclike"],"jolie":"clike","jsdoc":["javascript","javadoclike"],"js-extras":"javascript","jsonp":"json","json5":"json","kotlin":"clike","less":"css","markdown":"markup","markup-templating":"markup","n4js":"javascript","nginx":"clike","objectivec":"c","opencl":"cpp","parser":"markup","php":["clike","markup-templating"],"phpdoc":["php","javadoclike"],"php-extras":"php","plsql":"sql","processing":"clike","protobuf":"clike","pug":["markup","javascript"],"qore":"clike","jsx":["markup","javascript"],"tsx":["jsx","typescript"],"reason":"clike","ruby":"clike","sass":"css","scss":"css","scala":"java","smarty":"markup-templating","soy":"markup-templating","swift":"clike","tap":"yaml","textile":"markup","tt2":["clike","markup-templating"],"twig":"markup","typescript":"javascript","t4-cs":["t4-templating","csharp"],"t4-vb":["t4-templating","visual-basic"],"vala":"clike","vbnet":"basic","velocity":"markup","wiki":"markup","xeora":"markup","xquery":"markup"}/*]*/;

var lang_aliases = /*aliases_placeholder[*/{"html":"markup","xml":"markup","svg":"markup","mathml":"markup","js":"javascript","adoc":"asciidoc","shell":"bash","rbfn":"bnf","dotnet":"csharp","jinja2":"django","dockerfile":"docker","gamemakerlanguage":"gml","emacs":"lisp","elisp":"lisp","emacs-lisp":"lisp","n4jsd":"n4js","objectpascal":"pascal","ts":"typescript","t4":"t4-cs","vb":"visual-basic","xeoracube":"xeora"}/*]*/;

var lang_data = {};

Expand Down Expand Up @@ -65,6 +67,10 @@
* @param {HTMLElement} elt
*/
var registerElement = function (lang, elt) {
if (lang in lang_aliases) {
lang = lang_aliases[lang];
}

var data = lang_data[lang];
if (!data) {
data = lang_data[lang] = {};
Expand Down Expand Up @@ -123,14 +129,12 @@
* @param {function=} error
*/
var loadLanguage = function (lang, success, error) {
var load = function () {
var force = false;
// Do we want to force reload the grammar?
if (lang.indexOf('!') >= 0) {
force = true;
lang = lang.replace('!', '');
}
var force = lang.indexOf('!') >= 0;

lang = lang.replace('!', '');
lang = lang_aliases[lang] || lang;

var load = function () {
var data = lang_data[lang];
if (!data) {
data = lang_data[lang] = {};
Expand Down Expand Up @@ -166,6 +170,7 @@
});
}
};

var dependencies = lang_dependencies[lang];
if(dependencies && dependencies.length) {
loadLanguages(dependencies, load);
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 52889b5

Please sign in to comment.