Skip to content

Changed a bit the way PHP code is highlighted #41

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 4 commits into from
Mar 13, 2021

Conversation

javiereguiluz
Copy link
Collaborator

This is an example of the endless possibilities that having a PHP-based docs builder brings to us. We can easily change or update anything.

For example, I want to highlight code in the same way that GitHub does ... which requires showing a different color for the $ of the PHP variable names. The current highlighter doesn't support that:

image

But thanks to this super simple change, it works now:

image

Copy link
Collaborator

@nikophil nikophil left a comment

Choose a reason for hiding this comment

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

that's a shame the library is not configurable...

@javiereguiluz
Copy link
Collaborator Author

javiereguiluz commented Mar 5, 2021

I've updated tests ... but I don't know why this error is happening:

1) SymfonyDocsBuilder\Tests\IntegrationTest::testParseUnitBlock with data set "literal" ('nodes/literal')
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
                                         </span>
                                     </span>
                                     {
-                                    <span class="hljs-variable">$routes</span>
+                                    <span class="hljs-variable">
+                                        <span class="hljs-variable-other-marker">$</span>
+                                        routes
+                                    </span>
                                     <span class="hljs-operator">-&gt;</span>
                                     add(
                                     <span class="hljs-string">'about_us'</span>

docs-builder/tests/IntegrationTest.php:142

@weaverryan
Copy link
Contributor

@javiereguiluz that is probably fine and you should make the "expected" test cases reflect this. Comparing HTML is SO hard due to HTML that is really the "same" but appears different due to meaningless spacing changes. To try to help with that (though it's not perfect), we use a library gajus/dindent to normalize the indentation and spacing on both "source" and "expected" HTML. I'm guessing that's what you're seeing: that library is putting the routes text onto the next line via its normalization process.

@javiereguiluz
Copy link
Collaborator Author

Yes, I usually don't care about HTML spacing but in this case the generated HTML is inside a pre tag, so newlines are significant, right?

@nikophil
Copy link
Collaborator

nikophil commented Mar 7, 2021

actually we're forcing the indenter to indent spans:
https://github.com/weaverryan/docs-builder/blob/master/tests/IntegrationTest.php#L306-L307

thus it is normal that we get this result.

"real" generated code looks like this:

<pre class="hljs php"><span class="hljs-comment">// config/routes.php</span>
<span class="hljs-keyword">namespace</span> <span class="hljs-title">Symfony</span>\<span class="hljs-title">Component</span>\<span class="hljs-title">Routing</span>\<span class="hljs-title">Loader</span>\<span class="hljs-title">Configurator</span>;

<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(RoutingConfigurator <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>routes</span>)</span> </span>{
    <span class="hljs-variable"><span class="hljs-variable-other-marker">$</span>routes</span><span class="hljs-operator">-&gt;</span>add(<span class="hljs-string">'about_us'</span>, [<span class="hljs-string">'nl'</span> =&gt; <span class="hljs-string">'/over-ons'</span>, <span class="hljs-string">'en'</span> =&gt; <span class="hljs-string">'/about-us'</span>])
        <span class="hljs-operator">-&gt;</span>controller(<span class="hljs-string">'App\Controller\CompanyController::about'</span>);
};</pre>

so this is a false negative. We can create our expectation based on the result of the indenter, because we know it's ok, it sounds a bit like fragile test.
We can also don't use the "indent span" option for this particular test...

@javiereguiluz javiereguiluz force-pushed the improve_highlighter branch 2 times, most recently from fd82211 to d2e8890 Compare March 11, 2021 07:51
@nikophil nikophil force-pushed the improve_highlighter branch from 97d3c2b to 4e4bbd0 Compare March 13, 2021 09:55
@nikophil nikophil merged commit c7d27dc into symfony-tools:master Mar 13, 2021
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.

3 participants