Blogger 上的 SyntaxHighlighter - 添加到 Python 片段的意外行

SyntaxHighlighter on Blogger - unexpected line added to Python snippet

我在 Blogger 上使用 SyntaxHighlighter。问题是在我的 Python 代码片段中添加了 意外行

代码附在:

<pre class="brush:bash;">
  ...
</pre>

我的代码是:

(venv) dm@Z580:~/workspace/venv/greeter$ python
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from greeter import app
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dm/workspace/venv/greeter/greeter/app.py", line 7, in <module>
    from effects.dashed import add_dashes
ImportError: No module named 'effects'

实际呈现的内容:

(venv) dm@Z580:~/workspace/venv/greeter$ python
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from greeter import app
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dm/workspace/venv/greeter/greeter/app.py", line 7, in <module>
    from effects.dashed import add_dashes
ImportError: No module named 'effects'
</module></module></stdin>

所以最后一行 </module></module></stdin> 被添加到输出中。

为什么会发生这种情况以及如何防止这种行为?

好的,所以我不知道为什么会发生这种情况以及如何预防。但是我想出了一个简单的 jQuery 脚本来删除这些不需要的行。

$(window).load(function() {
    // line number regex
    var lineNumberRegex = /number\d+/;
    // </some_string> regex
    var unvantedOutputRegex = /^<\/.*>$/;

    // code for removing unvanted last lines added by SyntaxHighlighter to a Python code snippet
    // removes last lines like: </module></module></stdin>, i.e. starting with '</' and ending with '>'
    var syntaxHighlighters = $('.syntaxhighlighter');
    for (var i = 0; i < syntaxHighlighters.length; i++) {
      var syntaxHighlighter = syntaxHighlighters.eq(i);

      var lastLine = syntaxHighlighter.find('.line').last();
      var lastLineText = lastLine.text();

      if (unvantedOutputRegex.test(lastLineText)){
        var lastLineClasses = lastLine.attr('class');
        var lineToRemove = lastLineClasses.match(lineNumberRegex)[0];

        var targetLines = syntaxHighlighter.find('.' + lineToRemove);
        for (var y = 0; y < targetLines.length; y++) {
          targetLines[y].remove()
        }
      }
    };
});