Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

New languages:

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

New themes:
Expand Down
5 changes: 5 additions & 0 deletions VERSION_10_BREAKING.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
## Version 10 Breaking Changes

Incompatibilities:

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

Renamed Language Files:

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

Legacy Browser Issues:

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

Removed styles:

- chore(styles): darkula.css (use darcula.css instead) [Josh Goebel][]

[Josh Goebel]: https://github.com/yyyc514
Expand Down
30 changes: 30 additions & 0 deletions src/languages/php-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Language: PHP Template
Requires: xml.js, php.js
Author: Josh Goebel <[email protected]>
Website: https://www.php.net
Category: common
*/

export default function(hljs) {
return {
name: "PHP template",
subLanguage: 'xml',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say I like this two-grammars approach very much...
But looking at your PR I got a thought: what if we allow two sublanguages here? I. e.

    subLanguage: ['xml','php'],

This way we got one grammar that resolves into either xml + contained php, or just php.
If only we had a way to disallow php to act as a top-level language...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only we had a way to disallow php to act as a top-level language...

But it IS a top-level language... someone could post a snippet of PHP without the tags... in which case it's a php snippet, ie php code... meanwhile a php-template is HTML+PHP...

what if we allow two sublanguages here?

That makes me head hurt - and what is the advantage of doing it that way? PHP code on it's own is NOT a php template.

Copy link
Member Author

@joshgoebel joshgoebel Feb 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say I like this two-grammars approach very much...

It's consistent with how we do VBScript-HTML and how I've suggested we do other templating languages. For these type of things you have 3 things happening:

  • The core language (say Ruby)
  • The tagging/templating language (say ERB)
  • The actual output format (say HTML)

Those would be 3 different grammars. This is true for PHP also... the language itself is technically distinct from the templating language.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just rename php to _php, and php-template to php.
So that you can use single php for both php snippets and php templates.

Copy link
Member Author

@joshgoebel joshgoebel Feb 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would that work for ERB or VBscript? I'm trying to also look at the BIG picture here on how we handling templating in general - we should try and be consistent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that you can use single php for both php snippets and php templates.

REMEMBER, no one is using php now for PHP... well they might be, but they're more likely using xml since that highlights HTML + PHP.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REMEMBER, no one is using php now for PHP... well they might be, but they're more likely using xml since that highlights HTML + PHP.

Right. And we consider it bad, because it makes xml grammar bloated.
On the other hand, adding xml support to the core language doesn't seem bad. Or it does?

How would that work for ERB or VBscript?

The same way.
(leaving aside existing ERB and VBScript-html for the sake of discussion)

contains: [
{
begin: /<\?(php|=)?/,
end: /\?>/,
subLanguage: 'php',
contains: [
// We don't want the php closing tag ?> to close the PHP block when
// inside any of the following blocks:
{begin: '/\\*', end: '\\*/', skip: true},
{begin: 'b"', end: '"', skip: true},
{begin: 'b\'', end: '\'', skip: true},
hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null, className: null, contains: null, skip: true}),
hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null, className: null, contains: null, skip: true})
]
}
]
};
}
7 changes: 6 additions & 1 deletion src/languages/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export default function(hljs) {
begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
};
var PREPROCESSOR = {
className: 'meta', begin: /<\?(php|=)?|\?>/
className: 'meta',
variants: [
{ begin: /<\?php/, relevance: 10 }, // boost for obvious PHP
{ begin: /<\?[=]?/ },
{ begin: /\?>/ } // end php tag
]
};
var STRING = {
className: 'string',
Expand Down
13 changes: 0 additions & 13 deletions src/languages/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,6 @@ export default function(hljs) {
className: 'meta',
begin: /<\?xml/, end: /\?>/, relevance: 10
},
{
begin: /<\?(php)?/, end: /\?>/,
subLanguage: 'php',
contains: [
// We don't want the php closing tag ?> to close the PHP block when
// inside any of the following blocks:
{begin: '/\\*', end: '\\*/', skip: true},
{begin: 'b"', end: '"', skip: true},
{begin: 'b\'', end: '\'', skip: true},
hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null, className: null, contains: null, skip: true}),
hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null, className: null, contains: null, skip: true})
]
},
{
className: 'tag',
/*
Expand Down
8 changes: 8 additions & 0 deletions test/detect/php-template/default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
</html>
<body>
<?php
# Here echo command is used to print
echo "Hello, world!";
?>
</body>
3 changes: 2 additions & 1 deletion test/fixtures/expect/sublanguages.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<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="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">
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
<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>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</span>