Skip to content

Commit 78e06ac

Browse files
silverwindmarijnh
authored andcommitted
[meta] Add findModeByFileName, several meta improvements
1 parent f12f573 commit 78e06ac

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

doc/manual.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2658,12 +2658,13 @@ <h2 id="addons">Addons</h2>
26582658
properties <code>mimes</code>, which holds an array of MIME
26592659
types for modes with multiple MIMEs associated,
26602660
and <code>ext</code>, which holds an array of file extensions
2661-
associated with this mode. Three convenience
2661+
associated with this mode. Four convenience
26622662
functions, <code>CodeMirror.findModeByMIME</code>,
2663-
and <code>CodeMirror.findModeByExtension</code>,
2663+
<code>CodeMirror.findModeByExtension</code>,
2664+
<code>CodeMirror.findModeByFileName</code>
26642665
and <code>CodeMirror.findModeByName</code> are provided, which
2665-
return such an object given a MIME or extension string. Note
2666-
that, for historical reasons, this file resides in the
2666+
return such an object given a MIME, extension, file name or mode name
2667+
string. Note that, for historical reasons, this file resides in the
26672668
top-level <code>mode</code> directory, not
26682669
under <code>addon</code>. <a href="../demo/loadmode.html">Demo</a>.</dd>
26692670

mode/meta.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313

1414
CodeMirror.modeInfo = [
1515
{name: "APL", mime: "text/apl", mode: "apl", ext: ["dyalog", "apl"]},
16-
{name: "Asterisk", mime: "text/x-asterisk", mode: "asterisk"},
16+
{name: "Asterisk", mime: "text/x-asterisk", mode: "asterisk", file: /^extensions\.conf$/i},
1717
{name: "C", mime: "text/x-csrc", mode: "clike", ext: ["c", "h"]},
18-
{name: "C++", mime: "text/x-c++src", mode: "clike", ext: ["cpp", "c++", "hpp", "h++"], alias: ["cpp"]},
18+
{name: "C++", mime: "text/x-c++src", mode: "clike", ext: ["cpp", "c++", "cc", "cxx", "hpp", "h++", "hh", "hxx"], alias: ["cpp"]},
1919
{name: "Cobol", mime: "text/x-cobol", mode: "cobol", ext: ["cob", "cpy"]},
2020
{name: "C#", mime: "text/x-csharp", mode: "clike", ext: ["cs"], alias: ["csharp"]},
2121
{name: "Clojure", mime: "text/x-clojure", mode: "clojure", ext: ["clj"]},
2222
{name: "CoffeeScript", mime: "text/x-coffeescript", mode: "coffeescript", ext: ["coffee"], alias: ["coffee", "coffee-script"]},
2323
{name: "Common Lisp", mime: "text/x-common-lisp", mode: "commonlisp", ext: ["cl", "lisp", "el"], alias: ["lisp"]},
24-
{name: "Cypher", mime: "application/x-cypher-query", mode: "cypher"},
24+
{name: "Cypher", mime: "application/x-cypher-query", mode: "cypher", ext: ["cyp", "cypher"]},
2525
{name: "Cython", mime: "text/x-cython", mode: "python", ext: ["pyx", "pxd", "pxi"]},
2626
{name: "CSS", mime: "text/css", mode: "css", ext: ["css"]},
2727
{name: "CQL", mime: "text/x-cassandra", mode: "sql", ext: ["cql"]},
2828
{name: "D", mime: "text/x-d", mode: "d", ext: ["d"]},
2929
{name: "Dart", mimes: ["application/dart", "text/x-dart"], mode: "dart", ext: ["dart"]},
3030
{name: "diff", mime: "text/x-diff", mode: "diff", ext: ["diff", "patch"]},
3131
{name: "Django", mime: "text/x-django", mode: "django"},
32-
{name: "Dockerfile", mime: "text/x-dockerfile", mode: "dockerfile"},
32+
{name: "Dockerfile", mime: "text/x-dockerfile", mode: "dockerfile", file: /^Dockerfile$/},
3333
{name: "DTD", mime: "application/xml-dtd", mode: "dtd", ext: ["dtd"]},
3434
{name: "Dylan", mime: "text/x-dylan", mode: "dylan", ext: ["dylan", "dyl", "intr"]},
3535
{name: "EBNF", mime: "text/x-ebnf", mode: "ebnf"},
@@ -42,7 +42,7 @@
4242
{name: "F#", mime: "text/x-fsharp", mode: "mllike", ext: ["fs"], alias: ["fsharp"]},
4343
{name: "Gas", mime: "text/x-gas", mode: "gas", ext: ["s"]},
4444
{name: "Gherkin", mime: "text/x-feature", mode: "gherkin", ext: ["feature"]},
45-
{name: "GitHub Flavored Markdown", mime: "text/x-gfm", mode: "gfm"},
45+
{name: "GitHub Flavored Markdown", mime: "text/x-gfm", mode: "gfm", file: /^(readme|contributing|history).md$/i},
4646
{name: "Go", mime: "text/x-go", mode: "go", ext: ["go"]},
4747
{name: "Groovy", mime: "text/x-groovy", mode: "groovy", ext: ["groovy"]},
4848
{name: "HAML", mime: "text/x-haml", mode: "haml", ext: ["haml"]},
@@ -59,7 +59,7 @@
5959
{name: "JavaScript", mimes: ["text/javascript", "text/ecmascript", "application/javascript", "application/x-javascript", "application/ecmascript"],
6060
mode: "javascript", ext: ["js"], alias: ["ecmascript", "js", "node"]},
6161
{name: "JSON", mimes: ["application/json", "application/x-json"], mode: "javascript", ext: ["json", "map"], alias: ["json5"]},
62-
{name: "JSON-LD", mime: "application/ld+json", mode: "javascript", alias: ["jsonld"]},
62+
{name: "JSON-LD", mime: "application/ld+json", mode: "javascript", ext: ["jsonld"], alias: ["jsonld"]},
6363
{name: "Jinja2", mime: "null", mode: "jinja2"},
6464
{name: "Julia", mime: "text/x-julia", mode: "julia", ext: ["jl"]},
6565
{name: "Kotlin", mime: "text/x-kotlin", mode: "kotlin", ext: ["kt"]},
@@ -72,18 +72,18 @@
7272
{name: "Modelica", mime: "text/x-modelica", mode: "modelica", ext: ["mo"]},
7373
{name: "MS SQL", mime: "text/x-mssql", mode: "sql"},
7474
{name: "MySQL", mime: "text/x-mysql", mode: "sql"},
75-
{name: "Nginx", mime: "text/x-nginx-conf", mode: "nginx"},
75+
{name: "Nginx", mime: "text/x-nginx-conf", mode: "nginx", file: /nginx.*\.conf$/i},
7676
{name: "NTriples", mime: "text/n-triples", mode: "ntriples", ext: ["nt"]},
7777
{name: "Objective C", mime: "text/x-objectivec", mode: "clike", ext: ["m", "mm"]},
7878
{name: "OCaml", mime: "text/x-ocaml", mode: "mllike", ext: ["ml", "mli", "mll", "mly"]},
7979
{name: "Octave", mime: "text/x-octave", mode: "octave", ext: ["m"]},
8080
{name: "Pascal", mime: "text/x-pascal", mode: "pascal", ext: ["p", "pas"]},
81-
{name: "PEG.js", mime: "null", mode: "pegjs"},
81+
{name: "PEG.js", mime: "null", mode: "pegjs", ext: ["jsonld"]},
8282
{name: "Perl", mime: "text/x-perl", mode: "perl", ext: ["pl", "pm"]},
8383
{name: "PHP", mime: "application/x-httpd-php", mode: "php", ext: ["php", "php3", "php4", "php5", "phtml"]},
84-
{name: "Pig", mime: "text/x-pig", mode: "pig"},
84+
{name: "Pig", mime: "text/x-pig", mode: "pig", ext: ["pig"]},
8585
{name: "Plain Text", mime: "text/plain", mode: "null", ext: ["txt", "text", "conf", "def", "list", "log"]},
86-
{name: "PLSQL", mime: "text/x-plsql", mode: "sql"},
86+
{name: "PLSQL", mime: "text/x-plsql", mode: "sql", ext: ["pls"]},
8787
{name: "Properties files", mime: "text/x-properties", mode: "properties", ext: ["properties", "ini", "in"], alias: ["ini", "properties"]},
8888
{name: "Python", mime: "text/x-python", mode: "python", ext: ["py", "pyw"]},
8989
{name: "Puppet", mime: "text/x-puppet", mode: "puppet", ext: ["pp"]},
@@ -99,8 +99,8 @@
9999
{name: "Scheme", mime: "text/x-scheme", mode: "scheme", ext: ["scm", "ss"]},
100100
{name: "SCSS", mime: "text/x-scss", mode: "css", ext: ["scss"]},
101101
{name: "Shell", mime: "text/x-sh", mode: "shell", ext: ["sh", "ksh", "bash"], alias: ["bash", "sh", "zsh"]},
102-
{name: "Sieve", mime: "application/sieve", mode: "sieve"},
103-
{name: "Slim", mimes: ["text/x-slim", "application/x-slim"], mode: "slim"},
102+
{name: "Sieve", mime: "application/sieve", mode: "sieve", ext: ["siv", "sieve"]},
103+
{name: "Slim", mimes: ["text/x-slim", "application/x-slim"], mode: "slim", ext: ["slim"]},
104104
{name: "Smalltalk", mime: "text/x-stsrc", mode: "smalltalk", ext: ["st"]},
105105
{name: "Smarty", mime: "text/x-smarty", mode: "smarty", ext: ["tpl"]},
106106
{name: "SmartyMixed", mime: "text/x-smarty", mode: "smartymixed"},
@@ -114,15 +114,15 @@
114114
{name: "LaTeX", mime: "text/x-latex", mode: "stex", ext: ["text", "ltx"], alias: ["tex"]},
115115
{name: "SystemVerilog", mime: "text/x-systemverilog", mode: "verilog", ext: ["v"]},
116116
{name: "Tcl", mime: "text/x-tcl", mode: "tcl", ext: ["tcl"]},
117-
{name: "Textile", mime: "text/x-textile", mode: "textile"},
117+
{name: "Textile", mime: "text/x-textile", mode: "textile", ext: ["textile"]},
118118
{name: "TiddlyWiki ", mime: "text/x-tiddlywiki", mode: "tiddlywiki"},
119119
{name: "Tiki wiki", mime: "text/tiki", mode: "tiki"},
120-
{name: "TOML", mime: "text/x-toml", mode: "toml"},
120+
{name: "TOML", mime: "text/x-toml", mode: "toml", ext: ["toml"]},
121121
{name: "Tornado", mime: "text/x-tornado", mode: "tornado"},
122122
{name: "Turtle", mime: "text/turtle", mode: "turtle", ext: ["ttl"]},
123123
{name: "TypeScript", mime: "application/typescript", mode: "javascript", ext: ["ts"], alias: ["ts"]},
124124
{name: "VB.NET", mime: "text/x-vb", mode: "vb", ext: ["vb"]},
125-
{name: "VBScript", mime: "text/vbscript", mode: "vbscript"},
125+
{name: "VBScript", mime: "text/vbscript", mode: "vbscript", ext: ["vbs"]},
126126
{name: "Velocity", mime: "text/velocity", mode: "velocity", ext: ["vtl"]},
127127
{name: "Verilog", mime: "text/x-verilog", mode: "verilog", ext: ["v"]},
128128
{name: "XML", mimes: ["application/xml", "text/xml"], mode: "xml", ext: ["xml", "xsl", "xsd"], alias: ["rss", "wsdl", "xsd"]},
@@ -137,6 +137,7 @@
137137
}
138138

139139
CodeMirror.findModeByMIME = function(mime) {
140+
mime = mime.toLowerCase();
140141
for (var i = 0; i < CodeMirror.modeInfo.length; i++) {
141142
var info = CodeMirror.modeInfo[i];
142143
if (info.mime == mime) return info;
@@ -153,6 +154,16 @@
153154
}
154155
};
155156

157+
CodeMirror.findModeByFileName = function(filename) {
158+
for (var i = 0; i < CodeMirror.modeInfo.length; i++) {
159+
var info = CodeMirror.modeInfo[i];
160+
if (info.file && info.file.test(filename)) return info;
161+
}
162+
var dot = filename.lastIndexOf(".");
163+
var ext = dot > -1 && filename.substring(dot + 1, filename.length);
164+
if (ext) return CodeMirror.findModeByExtension(ext);
165+
};
166+
156167
CodeMirror.findModeByName = function(name) {
157168
name = name.toLowerCase();
158169
for (var i = 0; i < CodeMirror.modeInfo.length; i++) {

0 commit comments

Comments
 (0)