如何让Jekyll不要在突出显示中添加空格?

Mar*_*oma 9 pygments liquid jekyll

我正在试验杰基尔.大多数东西看起来很好,但Jekyll处理代码突出显示的方式似乎有些错误.

我使用pygments.

然后Jekyll似乎使用如下部分:

{% highlight python %}
#!/usr/bin/env python

def wer(r, h):
    """
{% endhighlight %}
Run Code Online (Sandbox Code Playgroud)

生成像这样的代码

<div class="highlight">
   <pre>
      <code class="python"><span class="c">#!/usr/bin/env python</span>

<span class="k">def</span> <span class="nf">wer</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">h</span><span class="p">):</span>
    <span class="sd">"""</span>
<span class="sd">        Calculation of WER with Levenshtein distance.</span>
<span class="sd">        Works only for iterables up to 254 elements (uint8).</span>
<span class="sd">        O(nm) time ans space complexity.</span>
[...]
    <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>
</code>
   </pre>
</div>
Run Code Online (Sandbox Code Playgroud)

看起来像

在此输入图像描述

在此输入图像描述

问题是code和之间的空白pre:

在此输入图像描述

我如何告诉Jekyll不要在这些标签之间放置空格?

寻找虫子

  • 我的Jekyll版本是jekyll 1.3.1.
  • 随着gem environment我发现我的宝石在/var/lib/gems/1.9.1.
  • 随着grep -rn "highlight" --exclude-dir=site --exclude-dir=test *我发现亮点标签获取解析/var/lib/gems/1.9.1/gems/jekyll-1.3.1/lib/jekyll/tags/highlight.rb
  • 由于这可能是一个Jekyll bug,我添加了问题1801

现在出现了奇怪的部分:highlight.rb似乎没有在<pre>和之间添加任何空格<code>.

Mar*_*oma 4

该问题是由 Jekyll 的模板引擎 Liquid 引起的(参见Liquid 的Issue 216和 Jekyll 的Issue 1806)。

该问题的当前(12.12.2013)答案是:您无法阻止 Jekyll 添加这些空格。

但解决根本问题的方法是在编译所有页面后删除空格。为此,我编写了以下 Python 脚本:

#!/usr/bin/env python
import re, fnmatch, os

def removeWhitespace(file_path):
    #read file content
    with open(file_path) as f:
        content = f.read()

    #replace whitespace
    openingTag = re.compile('<pre>\s*<code', re.DOTALL)
    closingTag = re.compile('</code>\s*</pre>', re.DOTALL)
    if re.findall(openingTag, content):
        content = re.sub(openingTag, '<pre><code', content)
        content = re.sub(closingTag, '</code></pre>', content)

    #write without whitespace
    with open(file_path,'w') as f:
        f.write(content)

# Get all HTML files
files = []
for root, dirnames, filenames in os.walk('.'):
  for filename in fnmatch.filter(filenames, '*.html'):
      files.append(os.path.join(root, filename))

for filename in files:
    removeWhitespace(filename)
Run Code Online (Sandbox Code Playgroud)