Joe*_*nin 7 html css python syntax-highlighting pre
是否有任何库允许我在<pre>
标签中显示代码并根据语言突出显示语法?我想象的是这样的:
<pre class="python">
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
</pre>
Run Code Online (Sandbox Code Playgroud)
... CSS pre.python
将适当地突出显示Python代码.
这样的事情存在吗?
Gio*_*rje 14
<pre class="brush: python">
# python code here
</pre>
Run Code Online (Sandbox Code Playgroud)
还有highlight.js,它可以选择自动检测语法并适当地突出显示它; 但是,您需要使用两个<pre><code>
标记来包装代码.
如果你正在寻找一个服务器端的例子,那就是Python 的GeSHi或Pygments.
我更喜欢highlight.js。它支持112种语言。
使用浏览器控制台中的代码注入预览您的页面:
// Highlight 22 popular code types. TODO: Inline for speed and security.
function loadjscssfile(filename, filetype){ // http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
if(filetype=="js"){
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if(filetype=="css"){
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if(typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref)
}
loadjscssfile("//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/vs.min.css", "css")
loadjscssfile("//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js", "js")
setTimeout("var a = document.querySelectorAll('.code'); for(var i=0; i < a.length; ++i) hljs.highlightBlock(a[i])", 600)
Run Code Online (Sandbox Code Playgroud)