9

我目前正在尝试使用 Jekyll。大多数事情看起来都很好,但是 Jekyll 处理代码高亮的方式似乎有问题。

我使用 pygments。

然后 Jekyll 似乎使用了以下部分:

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

def wer(r, h):
    """
{% endhighlight %}

生成类似的代码

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

看起来像

在此处输入图像描述

在此处输入图像描述

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 错误,所以我添加了问题 1801

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

4

2 回答 2

4

这个问题是由 Jekyll 的模板引擎 Liquid 引起的(参见Liquid 的第 216期和 Jekyll 的第 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)
于 2013-12-11T23:44:19.470 回答
-1

这是与样式表相关的。

我能够使用默认设置在我的测试环境中构建您的示例页面,没有任何问题*。

尝试将以下内容添加到style.css

/* standard */
 .post pre {
  border: 1px solid #ddd;
  background-color: #eef;
  padding: 0 .4em;
}

*我唯一的问题是它抱怨以下行

<a href="http://jekyllrb.com/">Jekyll</a> is a static blog generator.

我通过将行包装在段落标签中来解决。

于 2013-12-10T13:43:24.027 回答