1

I'm using the Bootstrap Template for ApiGen and I want to replace the default source code views with Highlight.js.

This requires me to remove the existing spans produced by ApiGen using the Nette replaceRE filter.

The original code looks like so:

<pre id="source">
    <code>{$source|replaceRE:'~<span class="line">(\s*)(\d+):(\s*)</span>([^\\n]*(?:\\n|$))~','<span id="$2" class="l">
        <a href="#$2">$1$2$3</a>$4</span>'|noescape}
    </code>
</pre>

The variable $4 contains the spans that need replacing, looking like so:

<span class="xlang">&lt;?php</span>
<span class="php-comment">/**</span>
<span class="php-comment"> * app/Base/Controller.php</span>
<span class="php-comment"> *</span>
<span class="php-comment"> * Local base controller for application.</span>
<span class="php-comment"> */</span>
<span class="php-keyword1">use</span> Illuminate\Foundation\Bus\DispatchesJobs;

and so on.

Obviously the code needs to remain unchanged, but the spans:

<span class="whatever">leave code alone</span>

need to be removed.

I tried a nested replaceRE but got parse errors from ApiGen.

<pre id="source">
    <code>{$source|replaceRE:'~<span class="line">(\s*)(\d+):(\s*)</span>([^\\n]*(?:\\n|$))~','<span id="$2" class="l">
        <a href="#$2">$1$2$3</a>{$4|replaceRE:'~<span .*?class="(.*?)">~',''|noescape}</span>'|noescape}
    </code>
</pre>
secondman
  • 3,233
  • 6
  • 43
  • 66
  • Looks like a job for [SimpleXML](http://php.net/manual/en/book.simplexml.php) and [XPath](http://php.net/manual/en/simplexmlelement.xpath.php) – Jan Jan 01 '16 at 09:55
  • That won’t work because `replaceRE` essentially just calls `preg_replace` and the result isn’t parsed (just like the result of `{$source}` isn’t parsed again). You can use two different filters like `replaceRE:'a','b'|replaceRE:'c','d'`. Alternatively you can disable the source code highlighter but AFAIK, there is no config option for that, so you would have to edit the ApiGen itself. – Jan Tojnar Jan 02 '16 at 00:41
  • Yeah I ended up creating my own version of ApiGen. It doesn't look like it's being maintained anymore anyway so I can just keep it in my pocket and use it for our in house builds. Thanks for the help. – secondman Jan 02 '16 at 00:50

1 Answers1

0

It ended up that highlight.js was the wrong tool for this since ApiGen requires line numbers for source code and hljs doesn't support line numbers.

Ace editor was the ticket.

secondman
  • 3,233
  • 6
  • 43
  • 66