【问题标题】:Jekyll Pygments ProcessingJekyll Pygments 处理
【发布时间】:2026-02-14 12:15:01
【问题描述】:

我已经和 Jekyll 和 Pygments 高亮打了一段时间了。我安装了 pygments 并生成了 css 文件,但是当我运行 Jekyll 生成站点时,代码突出显示似乎无法正确生成。

这里是我用来处理的一些示例代码

{% highlight php lineos %}
/**
 * Passing by reference
 *
 * Outputs
 *
 * 10 - before add() call
 * 10 - after add() call
 * 15 - after addref() call
 */
$a = 10;
echo $a;
add($a);
echo $a;
addref($a);
echo $a;

function addref(&$a)
{
    $a += 5;
}

function add($a)
{
    $a += 5;
}
{% endhighlight %}

这是 Jekyll 构建我的网站后的样子。

<div class="highlight"><pre><code class="php"><span class="x">/**</span>
<span class="x"> * Passing by reference</span>
<span class="x"> *</span>
<span class="x"> * Outputs</span>
<span class="x"> *</span>
<span class="x"> * 10 - before add() call</span>
<span class="x"> * 10 - after add() call</span>
<span class="x"> * 15 - after addref() call</span>
<span class="x"> */</span>
<span class="x">$a = 10;</span>
<span class="x">echo $a;</span>
<span class="x">add($a);</span>
<span class="x">echo $a;</span>
<span class="x">addref($a);</span>
<span class="x">echo $a;</span>
<span class="x"> </span>
<span class="x">function addref(&amp;$a)</span>
<span class="x">{</span>
<span class="x">    $a += 5;</span>
<span class="x">}</span>
<span class="x"> </span>
<span class="x">function add($a)</span>
<span class="x">{</span>
<span class="x">    $a += 5;</span>
<span class="x">}</span>
</code></pre>
</div>

如您所见,Jekyll 似乎将每一行都标记为class="x",我不太清楚为什么。

我尝试过使用 Github repos 中的液体和 jekyll,我什至尝试过使用 redcarpet,即使它与液体模板处理无关。我已经尝试了几乎所有我能想到的东西,但似乎无法让它发挥作用。

这是我查看网站时的实际样子

http://i.stack.imgur.com/kCvLN.png

我正在运行以下版本。

Ruby:ruby 1.9.3p327(2012-11-10 修订版 37606)[x86_64-darwin11.4.2]
rdiscount: rdiscount (1.6.8)
红地毯:红地毯(2.2.2) pygments:pygments.rb (0.2.13)
液体:液体 (2.4.1)
杰基尔:杰基尔(0.11.2)

我刚刚使用了redcarpet_markdown.rb 插件并将配置设置设置为使用 redcarpet2,并设置了 redcarpet 的扩展。

markdown: redcarpet2
redcarpet:
  extensions: ["no_intra_emphasis", "fenced_code_blocks", "autolink", "strikethrough", "superscript", "with_toc_data"]

一旦到位,我将代码突出显示更改为这样

```php
/**
* Passing by reference
*
* Outputs
*
* 10 - before add() call
* 10 - after add() call
* 15 - after addref() call
*/
$a = 10;
echo $a;
add($a);
echo $a;
addref($a);
echo $a;

function addref(&$a)
{
    $a += 5;
}

function add($a)
{
    $a += 5;
}
```

然后我尝试再次生成该站点并得到相同的结果。我不确定这是 Jekyll 还是 Pygments 造成的,但过去 2 天我一直在与此作斗争。但我现在知道它不是降价处理器。

如果您有任何想法,我非常愿意尝试任何事情。

【问题讨论】:

    标签: highlighting jekyll pygments


    【解决方案1】:

    如果你想避免&lt;?php标签,你可以指定Pygment选项startinline

    {% highlight php startinline %}
    
    phpinfo();
    
    {% endhighlight %}
    

    这样它应该可以正确渲染(它对我有用)。

    【讨论】:

    • 感谢您的回复。它应该是公认的答案。
    • 绝对是正确的答案,为我节省了很多时间。
    • 奇怪的是,在 Ubuntu 14.04 上安装了默认的 jekyll,这对我不起作用。 (通过&lt;?php 标签仍然有效)
    【解决方案2】:

    看来您不仅必须包含代码块的开始标记,而且使用 PHP 您还必须包含

    ```php
    <?php
    /**
    * Passing by reference
    *
    * Outputs
    *
    * 10 - before add() call
    * 10 - after add() call
    * 15 - after addref() call
    */
    $a = 10;
    echo $a;
    add($a);
    echo $a;
    addref($a);
    echo $a;
    
    function addref(&$a)
    {
        $a += 5;
    }
    
    function add($a)
    {
        $a += 5;
    }
    ```
    

    【讨论】: