【问题标题】:Regex Replace Nette and Latte in ApiGen正则表达式替换 ApiGen 中的 Nette 和 Latte
【发布时间】:2016-01-01 08:57:00
【问题描述】:

我正在使用 ApiGen 的引导模板,我想用 Highlight.js 替换默认源代码视图。

这需要我使用 Nette replaceRE 过滤器删除由 ApiGen 生成的现有 spans

原来的代码是这样的:

<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>

变量$4 包含需要替换的跨度,如下所示:

<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;

等等。

显然代码需要保持不变,但是跨度:

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

需要删除。

我尝试了嵌套的replaceRE,但从 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>

【问题讨论】:

  • 看起来像是 SimpleXMLXPath 的工作
  • 这行不通,因为replaceRE 本质上只是调用preg_replace 并且结果不会被解析(就像{$source} 的结果不会再次被解析)。您可以使用两种不同的过滤器,例如replaceRE:'a','b'|replaceRE:'c','d'。或者,您可以禁用源代码荧光笔,但 AFAIK,没有配置选项,因此您必须编辑 ApiGen 本身。
  • 是的,我最终创建了自己的 ApiGen 版本。无论如何,它看起来不再被维护了,所以我可以把它放在我的口袋里,用它来做我们的内部构建。感谢您的帮助。

标签: php regex nette apigen


【解决方案1】:

最终,highlight.js 是错误的工具,因为 ApiGen 需要源代码的行号,而 hljs 不支持行号。

王牌编辑是门票。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多