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

Add more modern PHP syntax highlighting #176

Merged
merged 1 commit into from
Jul 29, 2024

Conversation

wouterj
Copy link
Contributor

@wouterj wouterj commented May 27, 2024

Because I had fun playing around with this, I've added more changes to the PHP syntax file, bringing it more or less up to date with Highlight.js' syntax file.

Noteworthy changes:

  • Support arrow functions, enums, match statement, named arguments and more constants
  • Updated the full list of supported PHP types
  • Identify function class/class instantiations (this improves highlighting function arguments)

@javiereguiluz please test this out locally. This patch will generally add more color to the code blocks. You might want to tweak the CSS a bit depending on your favor (we seem to have relatively little coloring in our code examples).
E.g. function calls now get hljs-title as well (you can use .invoke__ to differentiate them from other titles), and named arguments will be labelled as hljs-attr (which is currently colored similar to hljs-meta used for PHP attributes).

Before After
image image
image image
image image

@wouterj wouterj requested a review from javiereguiluz May 27, 2024 08:48
@wouterj
Copy link
Contributor Author

wouterj commented Jul 25, 2024

@javiereguiluz I just rebased this PR and made it green. So it's ready for you to try out locally whenever you want :) (no pressure at all, things won't break)

@OskarStark
Copy link
Contributor

Wo, I love it, thanks @wouterj !

@javiereguiluz
Copy link
Collaborator

Wouter, thanks a lot for this great work. What a nice addition to the PHP highlighting 🙇🙇

I've been playing with it locally and it works exactly as expected:

  • It improves many edge cases that previously weren't highlighted...
  • ... without breaking any of the existing highlighting

A before/after example using one of the same examples that you showed:

image


So, we can totally merge this and start using it in production. Thanks!

@wouterj wouterj merged commit 027833c into symfony-tools:main Jul 29, 2024
3 checks passed
@wouterj wouterj deleted the new-php-syntax branch July 29, 2024 09:10
@wouterj
Copy link
Contributor Author

wouterj commented Jul 29, 2024

Thanks for testing Javier!

I'll let you handle releasing a version as you can best coordinate this with the website :)

@javiereguiluz
Copy link
Collaborator

Thanks! This was released as v0.26.0 in https://github.com/symfony-tools/docs-builder/releases/tag/v0.26.0

@stof
Copy link
Member

stof commented Jul 31, 2024

As those PHP highlighting rules have been forked from the ones in the highlight.php library, which were based on the highlight.js project, it would be great to identify which improvements could be submitted to upstream projects. Maybe at some point in the future, we would be able to avoid maintaining custom highlighting rules for PHP and Twig.

@wouterj
Copy link
Contributor Author

wouterj commented Jul 31, 2024

@stof in an ideal world, yes. But in practical world, the reality is that the PHP fork is more or less dead. The project relies on Highlight.js's JS syntax files and has a script to convert them to JSON. However, since Highlight.js 10 the JS syntax files can contain functions that will be used when highlighting code. These of course can not be converted to JSON, which broke the convert script.

There is an experiment to create a JS code to PHP code converter (scrivo/highlight.php#109), but that seems rather stalled (and sounds fragile). So we are forever stuck to the syntax files of v9.
What I did in essence in this PR is manually convert the newer PHP syntax file from Highlight.js to our JSON file.

On the long term, we probably would be better of to use another highlighting library. In the mean time, we can keep manually updating the PHP syntax file (given that is by far the most significant language in the Symfony docs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants