Skip to content

Drop \n when highlighting #79

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

Merged
merged 2 commits into from
Aug 10, 2023
Merged
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
5 changes: 4 additions & 1 deletion phpdotnet/phd/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public function highlight($text, $role, $format)

if ($role == 'php') {
try {
return str_replace(' ', ' ', highlight_string($text, 1));
Copy link
Member

Choose a reason for hiding this comment

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

str_replace() can also take an array of values to replace and an array of what those values should be, why not just use that?

But the issue seems to be more that \n tags before and after are being converted to <br> so wouldn't it make more sense to just ltrim() new lines of the $text variable?

Copy link
Member Author

Choose a reason for hiding this comment

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

str_replace() can also take an array of values to replace and an array of what those values should be, why not just use that?

see #79 (comment)

But the issue seems to be more that \n tags before and after are being converted to
so wouldn't it make more sense to just ltrim() new lines of the $text variable?

This wouldn't work, because the extra newlines are inserted by highlight_string(), namely here:

https://github.com/php/php-src/blob/f8b42da3a4635aa2ace3d4dbfdbae6835fb43d7f/Zend/zend_highlight.c#L92

and here:

https://github.com/php/php-src/blob/f8b42da3a4635aa2ace3d4dbfdbae6835fb43d7f/Zend/zend_highlight.c#L165-L167

Copy link
Member

Choose a reason for hiding this comment

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

sigh why is it doing that >.>

return strtr(highlight_string($text, 1), [
'&nbsp;' => ' ',
"\n" => '',
]);
} catch (\ParseException $e) {
v("Parse error while highlighting PHP code: %s\nText: %s", (string) $e, $text, E_USER_WARNING);

Expand Down