Skip to content

Commit

Permalink
Merge pull request #1100 from zeitgeist87/FixPHP
Browse files Browse the repository at this point in the history
Fix the PHP language
  • Loading branch information
zeitgeist87 authored May 8, 2017
2 parents 6530709 + 3ce7488 commit 1453fa7
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 70 deletions.
47 changes: 21 additions & 26 deletions components/prism-php.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ Prism.languages.php = Prism.languages.extend('clike', {
'constant': /\b[A-Z0-9_]{2,}\b/,
'comment': {
pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,
lookbehind: true,
greedy: true
lookbehind: true
}
});

Expand All @@ -32,7 +31,10 @@ Prism.languages.insertBefore('php', 'class-name', {
});

Prism.languages.insertBefore('php', 'keyword', {
'delimiter': /\?>|<\?(?:php)?/i,
'delimiter': {
pattern: /\?>|<\?(?:php|=)?/i,
alias: 'important'
},
'variable': /\$\w+\b/i,
'package': {
pattern: /(\\|namespace\s+|use\s+)[\w\\]+/,
Expand All @@ -51,61 +53,54 @@ Prism.languages.insertBefore('php', 'operator', {
}
});

// Add HTML support of the markup language exists
// Add HTML support if the markup language exists
if (Prism.languages.markup) {

// Tokenize all inline PHP blocks that are wrapped in <?php ?>
// This allows for easy PHP + markup highlighting
Prism.hooks.add('before-highlight', function(env) {
if (env.language !== 'php') {
if (env.language !== 'php' || !/(?:<\?php|<\?)/ig.test(env.code)) {
return;
}

env.tokenStack = [];

env.backupCode = env.code;
env.code = env.code.replace(/(?:<\?php|<\?)[\s\S]*?(?:\?>)/ig, function(match) {
env.code = env.code.replace(/(?:<\?php|<\?)[\s\S]*?(?:\?>|$)/ig, function(match) {
env.tokenStack.push(match);

return '{{{PHP' + env.tokenStack.length + '}}}';
return '___PHP' + env.tokenStack.length + '___';
});

// Switch the grammar to markup
env.grammar = Prism.languages.markup;
});

// Restore env.code for other plugins (e.g. line-numbers)
Prism.hooks.add('before-insert', function(env) {
if (env.language === 'php') {
if (env.language === 'php' && env.backupCode) {
env.code = env.backupCode;
delete env.backupCode;
}
});

// Re-insert the tokens after highlighting
Prism.hooks.add('after-highlight', function(env) {
if (env.language !== 'php') {
if (env.language !== 'php' || !env.tokenStack) {
return;
}

// Switch the grammar back
env.grammar = Prism.languages.php;

for (var i = 0, t; t = env.tokenStack[i]; i++) {
// The replace prevents $$, $&, $`, $', $n, $nn from being interpreted as special patterns
env.highlightedCode = env.highlightedCode.replace('{{{PHP' + (i + 1) + '}}}', Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$$'));
env.highlightedCode = env.highlightedCode.replace('___PHP' + (i + 1) + '___',
"<span class=\"token php language-php\">" +
Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$$') +
"</span>");
}

env.element.innerHTML = env.highlightedCode;
});

// Wrap tokens in classes that are missing them
Prism.hooks.add('wrap', function(env) {
if (env.language === 'php' && env.type === 'markup') {
env.content = env.content.replace(/(\{\{\{PHP\d+\}\}\})/g, "<span class=\"token php\">$1</span>");
}
});

// Add the rules before all others
Prism.languages.insertBefore('php', 'comment', {
'markup': {
pattern: /<[^?]\/?(.*?)>/,
inside: Prism.languages.markup
},
'php': /\{\{\{PHP\d+\}\}\}/
});
}
2 changes: 1 addition & 1 deletion components/prism-php.min.js

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

2 changes: 1 addition & 1 deletion tests/helper/test-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ module.exports = {
code: code
};
Prism.hooks.run('before-highlight', env);
env.highlightedCode = Prism.highlight(env.code, Prism.languages[usedLanguages.mainLanguage], usedLanguages.mainLanguage);
env.highlightedCode = Prism.highlight(env.code, env.grammar, env.language);
Prism.hooks.run('before-insert', env);
env.element.innerHTML = env.highlightedCode;
Prism.hooks.run('after-highlight', env);
Expand Down
37 changes: 0 additions & 37 deletions tests/languages/markup+php/markup_feature.test

This file was deleted.

9 changes: 5 additions & 4 deletions tests/languages/markup+php/php_in_markup_feature.js

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

4 changes: 3 additions & 1 deletion tests/languages/php/comment_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
/**/
/* foo
bar */
/* <me@example.com> */

----------------------------------------------------

[
["comment", "//"],
["comment", "// foobar"],
["comment", "/**/"],
["comment", "/* foo\r\nbar */"]
["comment", "/* foo\r\nbar */"],
["comment", "/* <me@example.com> */"]
]

----------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions tests/languages/php/delimiter_feature.test
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<? ?>
<?php ?>
<?= ?>

----------------------------------------------------

[
["delimiter", "<?"],
["delimiter", "?>"],
["delimiter", "<?php"],
["delimiter", "?>"],
["delimiter", "<?="],
["delimiter", "?>"]
]

Expand Down
13 changes: 13 additions & 0 deletions tests/languages/php/string_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"https://example.com"
" /* not a comment */ "

----------------------------------------------------

[
["string", "\"https://example.com\""],
["string", "\" /* not a comment */ \""]
]

----------------------------------------------------

Checks for strings with comments.

0 comments on commit 1453fa7

Please sign in to comment.