如何突出显示 HTML 中的源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/70169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:16:28  来源:igfitidea点击:

How to highlight source code in HTML?

htmlsyntax-highlighting

提问by Niyaz

I want to highlight C/C++/Java/C# etc source codes in my website.

我想在我的网站中突出显示 C/C++/Java/C# 等源代码。

How can I do this?

我怎样才能做到这一点?

Is it a CPU intensive job to highlight the source code?

突出显示源代码是否是 CPU 密集型工作?

回答by Daniel Spiewak

You can either do this server-side or client-side. It's not very processor intensive, but if you do it client side (using Javascript) there will be a noticeable lag. Most client side solutions revolve around Google Code's syntax highlighting engine. This seems to be the most popular one: SyntaxHighlighter

您可以在服务器端或客户端执行此操作。它不是非常密集的处理器,但是如果您在客户端(使用 Javascript)执行此操作,则会有明显的延迟。大多数客户端解决方案都围绕着 Google Code 的语法高亮引擎。这似乎是最受欢迎的:SyntaxHighlighter

Server-side solutions tend to be more flexible, especially in the way of defining new languages and configuring how they are highlighted (e.g. colors used). I use GeSHi, which is a PHP solution with a moderately nice plugin for Wordpress. There are also a few libraries built for Java, and even some that are based upon VIM (usually requiring a Perl module to be installed from CPAN).

服务器端解决方案往往更加灵活,尤其是在定义新语言和配置它们的突出显示方式(例如使用的颜色)方面。我使用 GeSHi,这是一个 PHP 解决方案,带有一个相当不错的 Wordpress 插件。还有一些为 Java 构建的库,甚至一些基于 VIM 的库(通常需要从 CPAN 安装 Perl 模块)。

In short: you have quite a few options, what are your criteria? It's hard to make a solid recommendation without knowing your requirements.

简而言之:你有很多选择,你的标准是什么?在不了解您的要求的情况下,很难提出可靠的建议。

回答by Paul Dixon

I use GeSHi("Generic Syntax Highlighter") on pastebin.com

我在pastebin.com上使用GeSHi(“通用语法荧光笔”)

pastebin has high traffic, so I do cache the results of the transformation, which certainly reduces the load.

pastebin 的流量很高,所以我缓存了转换的结果,这当然减少了负载。

回答by PhiLho

Personally, I prefer offline tools: I don't see the point of parsing the code (particularly large ones) over and over, for each served page, or even worse, on each browser (for JS libraries), because as pointed above, these libraries often lag (you often see raw source before it is formatted).

就我个人而言,我更喜欢离线工具:我没有看到为每个服务页面一遍又一遍地解析代码(尤其是大代码)的意义,甚至更糟的是,在每个浏览器上(对于 JS 库),因为如上所述,这些库经常滞后(您经常在格式化之前看到原始源代码)。

There are a number of tools to do this job, some pointed above. I just use the export feature of my favorite editor (SciTE) because it just respects the choices of color I carefully set up... :-) And it can output XML, PDF, RTF and LaTeX too.

有许多工具可以完成这项工作,上面提到了一些。我只是使用我最喜欢的编辑器 (SciTE) 的导出功能,因为它只尊重我精心设置的颜色选择... :-) 它也可以输出 XML、PDF、RTF 和 LaTeX。

回答by Kristof Neirynck

I use google-code-prettify. It is the simplest to set up and works great with all C-style languages.

我使用google-code-prettify。它设置起来最简单,并且适用于所有 C 风格的语言。

回答by Stacker

Pygmentis a good Python library to generate HTML, RTF, ANSI (terminal-style) or LaTeX code. It supports a large range of languages(C, C++, Lua, Erlang, ...) and you can even write your own output formatter.

Pygment是一个很好的 Python 库,可以生成 HTML、RTF、ANSI(终端样式)或 LaTeX 代码。它支持多种语言(C、C++、Lua、Erlang 等),您甚至可以编写自己的输出格式化程序。

回答by Javier

If you use jEdit, you might want to use the Code2HTMLplugin.

如果您使用jEdit,您可能需要使用Code2HTML插件。

回答by Brad Wilson

回答by DJ Capelis

Just run it through a tool like: http://www.gnu.org/software/src-highlite/

只需通过以下工具运行它:http: //www.gnu.org/software/src-highlite/

回答by Mark Embling

If you are using PHP, you can use GeSHito highlight many different languages. I've used it before and it works quite well. A quick googling will also uncover GeSHi plugins for wordpressand drupal.

如果您使用的是 PHP,您可以使用GeSHi来突出显示许多不同的语言。我以前用过它,效果很好。快速谷歌搜索还会发现用于wordpressdrupal 的GeSHi 插件。

I wouldn't consider highlighting to be CPU intensive unless you are intending to display megabytes of it all at once. And even then, the CPU load would be minimal and your main problem would be transfer speed for it all.

我不会考虑突出显示是 CPU 密集型的,除非您打算一次显示全部兆字节。即便如此,CPU 负载也会很小,您的主要问题将是传输速度。