我在SyntaxHighlighter
上使用Blogger
。问题是意外行已添加到我的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>
被添加到输出中。
为什么会发生这种情况以及如何防止这种行为?
答案 0 :(得分:0)
好的,所以我不知道为什么会这样,以及如何预防。但是我提出了一个简单的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()
}
}
};
});