【问题标题】:GitHub Pages mangling syntax highlighting after upgrade to Jekyll 3升级到 Jekyll 3 后 GitHub Pages 修改语法突出显示
【发布时间】:2016-02-10 18:38:18
【问题描述】:

我将 Github Pages 用于我的个人网站。他们正在从 Jekyll 2 升级到 Jekyll 3 并发送弃用警告。我听从了他们的警告,从 redcarpet 切换到 kramdown,从 pygments 切换到 rouge。当我在本地构建(使用bundle exec jekyll serve)时,一切正常。但是,当我推送更改时,无论我的代码块中有linenos,语法高亮都会被破坏。

这是代码块:

{% highlight python linenos %}
'''
scrape lyrics from vagalume.com.br
(author: thiagomarzagao.com)
'''

import json
import time
import pickle
import requests
from bs4 import BeautifulSoup

# get each genre's URL
basepath = 'http://www.vagalume.com.br'
r = requests.get(basepath + '/browse/style/')
soup = BeautifulSoup(r.text)
genres = [u'Rock']
          u'Ax\u00E9',
          u'Forr\u00F3',
          u'Pagode',
          u'Samba',
          u'Sertanejo',
          u'MPB',
          u'Rap']
genre_urls = {}
for genre in genres:
    genre_urls[genre] = soup.find('a', class_ = 'eA', text = genre).get('href')

# get each artist's URL, per genre
artist_urls = {e: [] for e in genres}
for genre in genres:
    r = requests.get(basepath + genre_urls[genre])
    soup = BeautifulSoup(r.text)
    counter = 0
    for artist in soup.find_all('a', class_ = 'top'):
        counter += 1
        print 'artist {} \r'.format(counter)
        artist_urls[genre].append(basepath + artist.get('href'))
    time.sleep(2) # don't reduce the 2-second wait (here or below) or you get errors

# get each lyrics, per genre
api = 'http://api.vagalume.com.br/search.php?musid='
genre_lyrics = {e: {} for e in genres}
for genre in artist_urls:
    print len(artist_urls[genre])
    counter = 0
    artist1 = None
    for url in artist_urls[genre]:
        success = False
        while not success: # foor loop in case your connection flickers
            try:
                r = requests.get(url)
                success = True
            except:
                time.sleep(2)
        soup = BeautifulSoup(r.text)
        hrefs = soup.find_all('a')
        for href in hrefs:
            if href.has_attr('data-song'):
                song_id = href['data-song']
                print song_id
                time.sleep(2)
                success = False
                while not success:
                    try:
                        song_metadata = requests.get(api + song_id).json()
                        success = True
                    except:
                        time.sleep(2)
                if 'mus' in song_metadata:
                    if 'lang' in song_metadata['mus'][0]: # discard if no language info
                        language = song_metadata['mus'][0]['lang']
                        if language == 1: # discard if language != Portuguese
                            if 'text' in song_metadata['mus'][0]: # discard if no lyrics
                                artist2 = song_metadata['art']['name']
                                if artist2 != artist1:
                                    if counter > 0:
                                        print artist1.encode('utf-8') # change as needed
                                        genre_lyrics[genre][artist1] = artist_lyrics
                                    artist1 = artist2
                                    artist_lyrics = []
                                lyrics = song_metadata['mus'][0]['text']
                                artist_lyrics.append(lyrics)
                                counter += 1
                                print 'lyrics {} \r'.format(counter)

    # serialize
    with open(genre + '.json', mode = 'wb') as fbuffer:
        json.dump(genre_lyrics[genre], fbuffer)
{% endhighlight %}

这是我在本地看到的:

这是我在 Github 页面上看到的:

(没有linenos,语法高亮工作正常。)

会发生什么?

【问题讨论】:

  • 请将您的代码发布为text,而不是image。有一种方法可以显示两个代码块:有和没有linenos,但我们需要查看您的代码来尝试修复它!
  • 很抱歉。现已发布。
  • 没问题! :) 我会尝试重现您的代码块并给您答复,好吗?

标签: jekyll github-pages rouge


【解决方案1】:

我想我明白了!

您的代码块似乎没问题。没问题。

确保您已将此添加到您的_config.yml

highlighter: rouge
markdown: kramdown
kramdown:
  input: GFM

您缺少的可能是kramdown input: GFM,不是吗?

嗯,我在本地进行了测试并且工作正常。当uploaded to GitHub 时,效果也很好。应该也适合你。

告诉我进展如何,好吗? :)


更新!

将此添加到您的样式表并检查它的运行情况:

.lineno { width: 35px; }

看起来是您的 CSS 样式破坏了布局!继续调整你的 CSS,你会没事的!

【讨论】:

  • 这是path 我的“测试”存储库,如果你想检查源代码。
  • 我查看了您的存储库,并在您的 _config.yml 中看到了引起我注意的两件事:(1)那里仍然没有 input: GFM - 不要忘记添加它。 (2):您使用插件jekyll-paginate。 Jekyll 3.x 默认不包含它,因此您需要将其添加到您的 Gemfile gem 'jekyll-paginate'
  • 嘿,看:您的本地浏览器图像在一个字符空间中显示行号!而不是10,你可以看到它是1,然后是0;对于11,您会看到1,然后再次看到1,依此类推。我很确定是您的 CSS 导致了您的问题!检查浏览器上的元素并更改此表格列的宽度以检查是否是它!对我来说,这会在行号列中添加一些空格:.lineno { width: 35px; }。测试一下!
  • 哈!那是正确的:我在我的 syntax.css 文件中添加了.lineno { width: 35px; },现在行号不再是一个字符,所以它们显示正确。不过,代码块仍然没有完全解开——前 10 行左右和最后 10 行左右没有编号,水平不适合代码块的行被换行。但我想这是一个不断调整 syntax.css 直到我做对的问题。非常感谢! (另外:善于观察模式!我盯着这些行号看了好几个小时,却没有意识到它们是 10、11、12 等)。
  • 哦,关于kramdown: input: GFM,我已经添加了它,但仅在本地提供(我通过从bundler 安装rougekramdown 设法在本地重现了修改)。
猜你喜欢
  • 2021-12-31
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多