【问题标题】:SyntaxHighlighter on Blogger - unexpected line added to Python snippetBlogger 上的 SyntaxHighlighter - Python 片段中添加了意外的行
【发布时间】:2015-10-18 19:03:03
【问题描述】:

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

代码包含在:

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

所以最后一行&lt;/module&gt;&lt;/module&gt;&lt;/stdin&gt; 被添加到输出中。

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

【问题讨论】:

    标签: python blogger syntaxhighlighter


    【解决方案1】:

    好的,所以我不知道为什么会发生这种情况以及如何防止它。但我想出了一个简单的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()
            }
          }
        };
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多