9

I'm currently experimenting with Jekyll. Most things look fine, but the way Jekyll processes code highlighting seems to be buggy.

I use pygments.

Then Jekyll seems to use pieces like:

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

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

to generate code like

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

which looks like

enter image description here

enter image description here

The problem is whitespace between code and pre:

enter image description here

How can I tell Jekyll not to put whitespace between those tags?

Bug hunting

  • My Jekyll version is jekyll 1.3.1.
  • With gem environment I found that my gems are at /var/lib/gems/1.9.1.
  • With grep -rn "highlight" --exclude-dir=site --exclude-dir=test * I found that the highlight tag gets parsed in /var/lib/gems/1.9.1/gems/jekyll-1.3.1/lib/jekyll/tags/highlight.rb
  • As this might be a Jekyll bug, I've added issue 1801

Now comes the strange part: highlight.rb seems not to add any whitespace between <pre> and <code>.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • I looked at your [source code](https://raw.github.com/MartinThoma/MartinThoma.github.io/source/_posts/2013-12-09-jekyll-and-git.markdown). Just out of curiosity, why are you using HTML tags inside your markdown file? Why aren't you using Markdown? – Garrett Hyde Dec 10 '13 at 19:37
  • I'm currently trying to migrate from WordPress to Jekyll. So the reason is simply that I had HTML before. I'm currently not sure if I want to switch. An advantage of using HTML is that I know the syntax. – Martin Thoma Dec 10 '13 at 19:43
  • Jekyll does support HTML files. If you're going to use pure HTML in your posts, I would recommend changing your file extensions from `.markdown` to `.html`. This way your posts won't be sent through the Markdown processor. – Garrett Hyde Dec 10 '13 at 20:12
  • Thank you for mentioning that. As markdown might be another source for errors, I've switched to `.html` files. The problem is still there. – Martin Thoma Dec 11 '13 at 10:07
  • I also tried changing the file extension to `.html`, and that didn't fix anything. I also converted your post to markdown, and that still produced the same error. Since the spaces were added between the `
    ` and `` tags in both cases, I think it's an issue with the processor and the `{% highlight bash %}` tag.
    – Garrett Hyde Dec 11 '13 at 16:30
  • Just out of curiosity, could you check if it can be fixed with CSS-only by using code from this article: http://css-tricks.com/snippets/css/make-pre-text-wrap/ – Teo Dragovic Dec 16 '13 at 20:57
  • CSS answers cannot work for this problem. Not every leading space is bad. Sometimes I need indentation. – Martin Thoma Dec 16 '13 at 21:33

2 Answers2

4

This problem is caused by Liquid, the templating engine of Jekyll (see Issue 216 of Liquid and Issue 1806 of Jekyll).

The current (12.12.2013) answer to this question is: You cannot keep Jekyll from adding those whitespaces.

But a fix to the underlying problem is to remove the whitespaces after all pages have been compiled. I've written the following Python script to do so:

#!/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)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
-1

This is stylesheet related.

I was able to build your sample page on my test environment using the default without any problem*.

Try adding the following to style.css

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

* Only problem I had was it complained about the following line

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

Which I resolved by wrapping the line in a paragraph tag.

RobertKenny
  • 3,556
  • 2
  • 19
  • 17
  • 1
    No, this does not fix the problem (see latest build) – Martin Thoma Dec 10 '13 at 14:58
  • OK. I will try a few more tweaks. It is definitely a CSS issue rather than a Jekyll issue though. – RobertKenny Dec 10 '13 at 15:16
  • 1
    You can see that added whitespace is the problem when you remove whitespace betweeen `
    ` and ``. Something adds this whitespace and I think it is Jekyll. Please delete your "answer" because it did not answer my question.
    – Martin Thoma Dec 10 '13 at 15:29
  • I am not seeing any extraneous whitespace when I view source on the sample page. Also, as I have said the page builds without any problem on my test installation using default style. – RobertKenny Dec 10 '13 at 15:43
  • 1
    I've added an image of the lines of code. Please take a look at it. It's definitely there. – Martin Thoma Dec 10 '13 at 19:25
  • Even if the problem of intendation was a stylesheet related (but I'm very sure it is not) I've asked how to remove the whitespace. You did not answer this question. So please delete this or answer my question. – Martin Thoma Dec 11 '13 at 10:10