提交 1dd884a8 编写于 作者: M Matt Bierner 提交者: GitHub

Use Gulp To Generate Markdown Langauge Includes (#22117)

**Bug**
The markdown grammar for fenced code blocks is rather unmaintainable since it involves lots of copy and pasted code

**Fix**
Use a gulp task and a template to generate the fenced code block grammars include directly. This allows adding new language support much more easily.
上级 3d349aa9
...@@ -186,7 +186,8 @@ ...@@ -186,7 +186,8 @@
} }
}, },
"scripts": { "scripts": {
"vscode:prepublish": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ../../build/gulpfile.extensions.js compile-extension:markdown ./tsconfig.json" "vscode:prepublish": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ../../build/gulpfile.extensions.js compile-extension:markdown ./tsconfig.json",
"update-grammar": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ./syntaxes/gulpfile.js"
}, },
"dependencies": { "dependencies": {
"highlight.js": "^9.3.0", "highlight.js": "^9.3.0",
...@@ -196,6 +197,8 @@ ...@@ -196,6 +197,8 @@
"vscode-nls": "^2.0.2" "vscode-nls": "^2.0.2"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^7.0.4" "@types/node": "^7.0.4",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.4"
} }
} }
\ No newline at end of file
var gulp = require('gulp');
var replace = require('gulp-replace');
var rename = require('gulp-rename');
const languages = [
{ name: 'css', identifiers: ['css', 'css.erb'], source: 'source.css' },
{ name: 'basic', identifiers: ['html', 'htm', 'shtml', 'xhtml', 'inc', 'tmpl', 'tpl'], source: 'text.html.basic' },
{ name: 'ini', identifiers: ['ini', 'conf'], source: 'source.ini' },
{ name: 'java', identifiers: ['java', 'bsh'], source: 'source.java' },
{ name: 'lua', identifiers: ['lua'], source: 'source.lua' },
{ name: 'makefile', identifiers: ['Makefile', 'makefile', 'GNUmakefile', 'OCamlMakefile'], source: 'source.makefile' },
{ name: 'perl', identifiers: ['perl', 'pl', 'pm', 'pod', 't', 'PL', 'psgi', 'vcl'], source: 'source.perl' },
{ name: 'r', identifiers: ['R', 'r', 's', 'S', 'Rprofile'], source: 'source.r' },
{ name: 'ruby', identifiers: ['ruby', 'rb', 'rbx', 'rjs', 'Rakefile', 'rake', 'cgi', 'fcgi', 'gemspec', 'irbrc', 'Capfile', 'ru', 'prawn', 'Cheffile', 'Gemfile', 'Guardfile', 'Hobofile', 'Vagrantfile', 'Appraisals', 'Rantfile', 'Berksfile', 'Berksfile.lock', 'Thorfile', 'Puppetfile'], source: 'source.ruby' },
// Left to its own devices, the PHP grammar will match HTML as a combination of operators
// and constants. Therefore, HTML must take precedence over PHP in order to get proper
// syntax highlighting.
{ name: 'php', identifiers: ['php', 'php3', 'php4', 'php5', 'phpt', 'phtml', 'aw', 'ctp'], source: ['text.html.basic', 'text.html.php#language'] },
{ name: 'sql', identifiers: ['sql', 'ddl', 'dml'], source: 'source.sql' },
{ name: 'vs_net', identifiers: ['vb'], source: 'source.asp.vb.net' },
{ name: 'xml', identifiers: ['xml', 'xsd', 'tld', 'jsp', 'pt', 'cpt', 'dtml', 'rss', 'opml'], source: 'text.xml' },
{ name: 'xsl', identifiers: ['xsl', 'xslt'], source: 'text.xml.xsl' },
{ name: 'yaml', identifiers: ['yaml', 'yml'], source: 'source.yaml' },
{ name: 'dosbatch', identifiers: ['bat', 'batch'], source: 'source.dosbatch' },
{ name: 'clojure', identifiers: ['clj', 'cljs', 'clojure'], source: 'source.clojure' },
{ name: 'coffee', identifiers: ['coffee', 'Cakefile', 'coffee.erb'], source: 'source.coffee' },
{ name: 'c', identifiers: ['c', 'h'], source: 'source.c' },
{ name: 'cpp', identifiers: ['cpp', 'c\\+\\+', 'cxx'], source: 'source.cpp' },
{ name: 'diff', identifiers: ['patch', 'diff', 'rej'], source: 'source.diff' },
{ name: 'dockerfile', identifiers: ['dockerfile', 'Dockerfile'], source: 'source.dockerfile' },
{ name: 'git_commit', identifiers: ['COMMIT_EDITMSG', 'MERGE_MSG'], source: 'text.git-commit' },
{ name: 'git_rebase', identifiers: ['git-rebase-todo'], source: 'text.git-rebase' },
{ name: 'go', identifiers: ['go', 'golang'], source: 'source.go' },
{ name: 'groovy', identifiers: ['groovy', 'gvy'], source: 'source.groovy' },
{ name: 'jade', identifiers: ['jade'], source: 'text.jade' },
{ name: 'js', identifiers: ['js', 'jsx', 'javascript'], source: 'source.js' },
{ name: 'js_regexp', identifiers: ['regexp'], source: 'source.js.regexp' },
{ name: 'json', identifiers: ['json', 'sublime-settings', 'sublime-menu', 'sublime-keymap', 'sublime-mousemap', 'sublime-theme', 'sublime-build', 'sublime-project', 'sublime-completions'], source: 'source.json' },
{ name: 'less', identifiers: ['less'], source: 'source.css.less' },
{ name: 'objc', identifiers: ['objectivec', 'objective-c', 'mm', 'objc', 'obj-c', 'm', 'h'], source: 'source.objc' },
{ name: 'perl6', identifiers: ['perl6', 'p6', 'pl6', 'pm6', 'nqp'], source: 'source.perl.6' },
{ name: 'powershell', identifiers: ['powershell', 'ps1', 'psm1', 'psd1'], source: 'source.powershell' },
{ name: 'python', identifiers: ['python', 'py', 'py3', 'rpy', 'pyw', 'cpy', 'SConstruct', 'Sconstruct', 'sconstruct', 'SConscript', 'gyp', 'gypi'], source: 'source.python' },
{ name: 'regexp_python', identifiers: ['re'], source: 'source.regexp.python' },
{ name: 'rust', identifiers: ['rust', 'rs'], source: 'source.rust' },
{ name: 'scala', identifiers: ['scala', 'sbt'], source: 'source.scala' },
{ name: 'shell', identifiers: ['shell', 'sh', 'bash', 'zsh', 'bashrc', 'bash_profile', 'bash_login', 'profile', 'bash_logout', '.textmate_init'], source: 'source.shell' },
{ name: 'ts', identifiers: ['typescript', 'ts'], source: 'source.ts' },
{ name: 'tsx', identifiers: ['tsx'], source: 'source.tsx' },
{ name: 'csharp', identifiers: ['cs', 'csharp', 'c#'], source: 'source.cs' },
{ name: 'fsharp', identifiers: ['fs', 'fsharp', 'f#'], source: 'source.fsharp' },
];
const fencedCodeBlockDefinition = (name, identifiers, sourceScope) => {
if (!Array.isArray(sourceScope)) {
sourceScope = [sourceScope];
}
const scopes = sourceScope.map(scope =>
`<dict>
<key>include</key>
<string>${scope}</string>
</dict>`).join('\n');
return `<key>fenced_code_block_${name}</key>
<dict>
<key>begin</key>
<string>(^|\\G)(\\s*)([\`~]{3,})\\s*((${identifiers.join('|')})(\\s+[^\`~]*)?$)</string>
<key>name</key>
<string>markup.fenced_code.block.markdown</string>
<key>end</key>
<string>(^|\\G)(\\2|\\s{0,3})(\\3)\\s*$</string>
<key>beginCaptures</key>
<dict>
<key>3</key>
<dict>
<key>name</key>
<string>punctuation.definition.markdown</string>
</dict>
<key>5</key>
<dict>
<key>name</key>
<string>fenced_code.block.language</string>
</dict>
<key>6</key>
<dict>
<key>name</key>
<string>fenced_code.block.language.attributes</string>
</dict>
</dict>
<key>endCaptures</key>
<dict>
<key>3</key>
<dict>
<key>name</key>
<string>punctuation.definition.markdown</string>
</dict>
</dict>
<key>patterns</key>
<array>
<dict>
<key>begin</key>
<string>(^|\\G)(\\s*)(.*)</string>
<key>while</key>
<string>(^|\\G)(?!\\s*([\`~]{3,})\\s*$)</string>
<key>patterns</key>
<array>
${indent(4, scopes)}
</array>
</dict>
</array>
</dict>`;
};
const indent = (count, text) => {
const indent = new Array(count + 1).join('\t');
return text.replace(/^/gm, indent);
};
const fencedCodeBlockInclude = (name) =>
`<dict>
<key>include</key>
<string>#fenced_code_block_${name}</string>
</dict>`;
const fencedCodeBlockDefinitions = () =>
languages
.map(language => fencedCodeBlockDefinition(language.name, language.identifiers, language.source))
.join('\n');
const fencedCodeBlockIncludes = () =>
languages
.map(language => fencedCodeBlockInclude(language.name))
.join('\n');
gulp.task('default', function () {
gulp.src(['markdown.tmLanguage.base'])
.pipe(replace('{{languageIncludes}}', indent(4, fencedCodeBlockIncludes())))
.pipe(replace('{{languageDefinitions}}', indent(4, fencedCodeBlockDefinitions())))
.pipe(rename('markdown.tmLanguage'))
.pipe(gulp.dest('.'));
});
...@@ -717,7 +717,6 @@ ...@@ -717,7 +717,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -769,7 +768,6 @@ ...@@ -769,7 +768,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -821,7 +819,6 @@ ...@@ -821,7 +819,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -873,7 +870,6 @@ ...@@ -873,7 +870,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -925,7 +921,6 @@ ...@@ -925,7 +921,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1028,7 +1023,6 @@ ...@@ -1028,7 +1023,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1048,18 +1042,6 @@ ...@@ -1048,18 +1042,6 @@
</dict> </dict>
<key>fenced_code_block_php</key> <key>fenced_code_block_php</key>
<dict> <dict>
<key>comment</key>
<string>
In fenced PHP code blocks, we match a "fuzzy" version of the PHP language.
This is because code blocks may be an incomplete PHP script that lacks a script
section start tag, and we still want to provide syntax highlighting, rather
than adhering to the actual grammar, where everything not prepended by a
start tag would be ignored. Additionally, we want to provide highlighting
for potential non-PHP languages (in particular HTML).
There are certainly plenty of edge cases here, but this seems to cover most popular
scenarios, and also appears to be consistent with the approach that GitHub takes.
</string>
<key>begin</key> <key>begin</key>
<string>(^|\G)(\s*)([`~]{3,})\s*((php|php3|php4|php5|phpt|phtml|aw|ctp)(\s+[^`~]*)?$)</string> <string>(^|\G)(\s*)([`~]{3,})\s*((php|php3|php4|php5|phpt|phtml|aw|ctp)(\s+[^`~]*)?$)</string>
<key>name</key> <key>name</key>
...@@ -1092,7 +1074,6 @@ ...@@ -1092,7 +1074,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1103,14 +1084,6 @@ ...@@ -1103,14 +1084,6 @@
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
<key>comment</key>
<string>
Left to its own devices, the PHP grammar will match HTML as a combination of operators
and constants. Therefore, HTML must take precedence over PHP in order to get proper
syntax highlighting.
</string>
<key>include</key> <key>include</key>
<string>text.html.basic</string> <string>text.html.basic</string>
</dict> </dict>
...@@ -1156,7 +1129,6 @@ ...@@ -1156,7 +1129,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1208,7 +1180,6 @@ ...@@ -1208,7 +1180,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1260,7 +1231,6 @@ ...@@ -1260,7 +1231,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1312,7 +1282,6 @@ ...@@ -1312,7 +1282,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1364,7 +1333,6 @@ ...@@ -1364,7 +1333,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1416,7 +1384,6 @@ ...@@ -1416,7 +1384,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1468,7 +1435,6 @@ ...@@ -1468,7 +1435,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1520,7 +1486,6 @@ ...@@ -1520,7 +1486,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1674,7 +1639,6 @@ ...@@ -1674,7 +1639,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1726,7 +1690,6 @@ ...@@ -1726,7 +1690,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1778,7 +1741,6 @@ ...@@ -1778,7 +1741,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1830,7 +1792,6 @@ ...@@ -1830,7 +1792,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1882,7 +1843,6 @@ ...@@ -1882,7 +1843,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1934,7 +1894,6 @@ ...@@ -1934,7 +1894,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -1986,7 +1945,6 @@ ...@@ -1986,7 +1945,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2038,7 +1996,6 @@ ...@@ -2038,7 +1996,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2090,7 +2047,6 @@ ...@@ -2090,7 +2047,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2142,7 +2098,6 @@ ...@@ -2142,7 +2098,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2194,7 +2149,6 @@ ...@@ -2194,7 +2149,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2215,7 +2169,7 @@ ...@@ -2215,7 +2169,7 @@
<key>fenced_code_block_objc</key> <key>fenced_code_block_objc</key>
<dict> <dict>
<key>begin</key> <key>begin</key>
<string>(^|\G)(\s*)([`~]{3,})\s*((objectivec|mm|objc|obj-c|m|h)(\s+[^`~]*)?$)</string> <string>(^|\G)(\s*)([`~]{3,})\s*((objectivec|objective-c|mm|objc|obj-c|m|h)(\s+[^`~]*)?$)</string>
<key>name</key> <key>name</key>
<string>markup.fenced_code.block.markdown</string> <string>markup.fenced_code.block.markdown</string>
<key>end</key> <key>end</key>
...@@ -2246,7 +2200,6 @@ ...@@ -2246,7 +2200,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2298,7 +2251,6 @@ ...@@ -2298,7 +2251,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2350,7 +2302,6 @@ ...@@ -2350,7 +2302,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2453,7 +2404,6 @@ ...@@ -2453,7 +2404,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2505,7 +2455,6 @@ ...@@ -2505,7 +2455,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2523,7 +2472,7 @@ ...@@ -2523,7 +2472,7 @@
</dict> </dict>
</array> </array>
</dict> </dict>
<key>fenced_code_block_scala</key> <key>fenced_code_block_scala</key>
<dict> <dict>
<key>begin</key> <key>begin</key>
<string>(^|\G)(\s*)([`~]{3,})\s*((scala|sbt)(\s+[^`~]*)?$)</string> <string>(^|\G)(\s*)([`~]{3,})\s*((scala|sbt)(\s+[^`~]*)?$)</string>
...@@ -2557,7 +2506,6 @@ ...@@ -2557,7 +2506,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2609,7 +2557,6 @@ ...@@ -2609,7 +2557,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2661,7 +2608,6 @@ ...@@ -2661,7 +2608,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
...@@ -2713,7 +2659,6 @@ ...@@ -2713,7 +2659,6 @@
<string>punctuation.definition.markdown</string> <string>punctuation.definition.markdown</string>
</dict> </dict>
</dict> </dict>
<key>patterns</key> <key>patterns</key>
<array> <array>
<dict> <dict>
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册