Skip to content

Commit 0a5bbe4

Browse files
authored
add(php-template) Explicit language to detect PHP templates (#2417)
- Adds a separate language to detect PHP templates to avoid overloading `xml` XML never should have built-in PHP support in the first place as there are any number of templating languages out there, and they all can't be built into XML. This rectifies that situation.
1 parent 51a7af4 commit 0a5bbe4

File tree

7 files changed

+52
-15
lines changed

7 files changed

+52
-15
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
New languages:
44

5+
- (php-template) Explicit language to detect PHP templates (vs xml) [Josh Goebel][]
56
- enh(python) Added `python-repl` for Python REPL sessions
67

78
New themes:

VERSION_10_BREAKING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
## Version 10 Breaking Changes
22

33
Incompatibilities:
4+
45
- chore(parser): compressed version 9.x language definitions no longer supported (rebuild them minified) [Josh Goebel][]
56
- `nohightlight` and `no-highlight` are the only "ignore me" css classes now (`plain` and `text` no longer count)
67
(to get the old behavior you can customize `noHighlightRe`)
78
- a grammar with a top-level `self` reference will now always throw an error
89
(previously in safe mode this would be silently ignored)
10+
- PHP templates are now detected as `php-template`, not `xml`
911

1012
Renamed Language Files:
13+
1114
- chore(parser): rename `nimrod.js` to `nim.js` [Josh Goebel][]
1215
- chore(parser): rename `cs.js` to `csharp.js` [Josh Goebel][]
1316
- chore(parser): rename `tex.js` to `latex.js` [Josh Goebel][]
@@ -19,11 +22,13 @@ Renamed Language Files:
1922
(https://github.com/highlightjs/highlight.js/issues/2146)
2023

2124
Legacy Browser Issues:
25+
2226
- **We're now using ES2015 features in the codebase. Internet Explorer 11 is no longer supported.**
2327
- In general legacy browsers are no longer supported.
2428
- chore(parser): remove `load` listener in favor of only the newer `DOMContentLoaded` [Josh Goebel][]
2529

2630
Removed styles:
31+
2732
- chore(styles): darkula.css (use darcula.css instead) [Josh Goebel][]
2833

2934
[Josh Goebel]: https://github.com/yyyc514

src/languages/php-template.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Language: PHP Template
3+
Requires: xml.js, php.js
4+
Author: Josh Goebel <[email protected]>
5+
Website: https://www.php.net
6+
Category: common
7+
*/
8+
9+
export default function(hljs) {
10+
return {
11+
name: "PHP template",
12+
subLanguage: 'xml',
13+
contains: [
14+
{
15+
begin: /<\?(php|=)?/,
16+
end: /\?>/,
17+
subLanguage: 'php',
18+
contains: [
19+
// We don't want the php closing tag ?> to close the PHP block when
20+
// inside any of the following blocks:
21+
{begin: '/\\*', end: '\\*/', skip: true},
22+
{begin: 'b"', end: '"', skip: true},
23+
{begin: 'b\'', end: '\'', skip: true},
24+
hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null, className: null, contains: null, skip: true}),
25+
hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null, className: null, contains: null, skip: true})
26+
]
27+
}
28+
]
29+
};
30+
}

src/languages/php.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ export default function(hljs) {
1111
begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
1212
};
1313
var PREPROCESSOR = {
14-
className: 'meta', begin: /<\?(php|=)?|\?>/
14+
className: 'meta',
15+
variants: [
16+
{ begin: /<\?php/, relevance: 10 }, // boost for obvious PHP
17+
{ begin: /<\?[=]?/ },
18+
{ begin: /\?>/ } // end php tag
19+
]
1520
};
1621
var STRING = {
1722
className: 'string',

src/languages/xml.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,6 @@ export default function(hljs) {
9797
className: 'meta',
9898
begin: /<\?xml/, end: /\?>/, relevance: 10
9999
},
100-
{
101-
begin: /<\?(php)?/, end: /\?>/,
102-
subLanguage: 'php',
103-
contains: [
104-
// We don't want the php closing tag ?> to close the PHP block when
105-
// inside any of the following blocks:
106-
{begin: '/\\*', end: '\\*/', skip: true},
107-
{begin: 'b"', end: '"', skip: true},
108-
{begin: 'b\'', end: '\'', skip: true},
109-
hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null, className: null, contains: null, skip: true}),
110-
hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null, className: null, contains: null, skip: true})
111-
]
112-
},
113100
{
114101
className: 'tag',
115102
/*
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
</html>
3+
<body>
4+
<?php
5+
# Here echo command is used to print
6+
echo "Hello, world!";
7+
?>
8+
</body>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<span class="php"><span class="hljs-meta">&lt;?</span> <span class="hljs-keyword">echo</span> <span class="hljs-string">'php'</span>; <span class="hljs-comment">/* ?&gt; */</span> <span class="hljs-meta">?&gt;</span></span>
1+
<span class="php"><span class="hljs-meta">&lt;?</span> <span class="hljs-keyword">echo</span> <span class="hljs-string">'php'</span>; <span class="hljs-comment">/* ?&gt; */</span> <span class="hljs-meta">?&gt;</span></span><span class="xml">
22
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
33
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript"><span class="hljs-built_in">document</span>.write(<span class="hljs-string">'Legacy code'</span>);</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
44
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
5+
</span>

0 commit comments

Comments
 (0)