Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the PHP language #1100

Merged
merged 3 commits into from
May 8, 2017
Merged

Fix the PHP language #1100

merged 3 commits into from
May 8, 2017

Conversation

zeitgeist87
Copy link
Collaborator

This patch is an attempt to fix the PHP language, when it is combined
with markup.

The problem is, that markup has a higher priority than all other tokens.
This leads to weird errors, where HTML tags are highlighted inside of
comments #197. One solution to this was to set the greedy flag for the
comment token, but this leads to far worse errors like #1097.

This patch should fix both issues #197 and #1097, by switching the
grammar to markup on the fly. One potential problem is, that it relies
on the <?php tag to detect if markup is present. So if a PHP file
contains only markup and no PHP code at all, the result will look
broken.

@zeitgeist87
Copy link
Collaborator Author

Before (Note: The JavaScript function is erroneously highlighted by PHP. CSS is not highlighted at all. Strings inside comments do not work):
before

After (Note: The string "px" looks different, because CSS has custom styles for the class token string. JavaScript and CSS are highlighted by the correct language.) :
after

@zeitgeist87
Copy link
Collaborator Author

There is one problem with this PR. If there are no <?php tags, then it assumes, that it is pure PHP code, which could be wrong:
wrong
correct2

If a <?php tag is present, then markup is also highlighted:
correct

@zeitgeist87
Copy link
Collaborator Author

@LeaVerou @Golmote What do you think about this PR?

@LeaVerou
Copy link
Member

If there are no <?php tags, then it assumes, that it is pure PHP code, which could be wrong:

I don't think that's a huge problem (they can use markup in those cases), as long as it supports the shortened PHP tags as well (<?= etc). Though I don't see why we can't check if a PHP tag is present and use the markup definition if not.

@zeitgeist87
Copy link
Collaborator Author

Though I don't see why we can't check if a PHP tag is present and use the markup definition if not.

Yes that is one option, but I think it is more intuitive to assume that it is all PHP in that case. For example someone could use small snippets of PHP code between paragraphs on a blog to explain a larger file:

$test = "<div>" . $_SERVER['PHP_SELF'] . "</div>";

If we switch to markup in that scenario, we would force the users to put PHP tags around every small snippet:

<?php $test = "<div>" . $_SERVER['PHP_SELF'] . "</div>"; ?>

If there is no PHP code at all, people should use markup directly.

@LeaVerou
Copy link
Member

Ah yeah, excellent point! Completely agree.

@Golmote
Copy link
Contributor

Golmote commented May 8, 2017

@zeitgeist87 Sorry for the looong time before review. This looks ok to me! ;) Feel free to merge.

This patch is an attempt to fix the PHP language, when it is combined
with markup.

The problem is, that markup has a higher priority than all other tokens.
This leads to weird errors, where HTML tags are highlighted inside of
comments PrismJS#197. One solution to this was to set the `greedy` flag for the
comment token, but this leads to far worse errors like PrismJS#1097.

This patch should fix both issues PrismJS#197 and PrismJS#1097, by switching the
grammar to markup on the fly. One potential problem is, that it relies
on the `<?php` tag to detect if markup is present. So if a PHP file
contains only markup and no PHP code at all, the result will look
broken.
highlighted as punctuation in JavaScript and CSS.
@zeitgeist87
Copy link
Collaborator Author

@Golmote Thanks again for the review! Merging...

@zeitgeist87 zeitgeist87 merged commit 1453fa7 into PrismJS:gh-pages May 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants