如何在HTML中突出源代码?

时间:2022-01-29 15:54:23

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密集型的工作吗?

9 个解决方案

#1


27  

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)使用它,将会有明显的延迟。大多数客户端解决方案都围绕谷歌代码的语法突出显示引擎。这似乎是最受欢迎的一个: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.

简而言之:你有很多选择,你的标准是什么?如果不了解您的需求,就很难做出可靠的推荐。

#2


11  

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具有高通信量,所以我确实缓存了转换的结果,这肯定会减少负载。

#3


6  

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。

#4


5  

Pygment is 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、…),甚至可以编写自己的输出格式化程序。

#5


5  

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

我使用google-code-prettify。它是最简单的设置和工作与所有c风格的语言。

#6


1  

If you use jEdit, you might want to use the Code2HTML plugin.

如果您使用jEdit,您可能希望使用Code2HTML插件。

#7


0  

I use SyntaxHighligher on my blog.

我在我的博客上使用SyntaxHighligher。

#8


0  

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

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

#9


0  

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

如果您正在使用PHP,您可以使用GeSHi来突出显示许多不同的语言。我以前用过,效果很好。谷歌一下也会发现wordpress和drupal的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的负载也会很小,你的主要问题就是传输速度。

#1


27  

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)使用它,将会有明显的延迟。大多数客户端解决方案都围绕谷歌代码的语法突出显示引擎。这似乎是最受欢迎的一个: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.

简而言之:你有很多选择,你的标准是什么?如果不了解您的需求,就很难做出可靠的推荐。

#2


11  

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具有高通信量,所以我确实缓存了转换的结果,这肯定会减少负载。

#3


6  

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。

#4


5  

Pygment is 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、…),甚至可以编写自己的输出格式化程序。

#5


5  

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

我使用google-code-prettify。它是最简单的设置和工作与所有c风格的语言。

#6


1  

If you use jEdit, you might want to use the Code2HTML plugin.

如果您使用jEdit,您可能希望使用Code2HTML插件。

#7


0  

I use SyntaxHighligher on my blog.

我在我的博客上使用SyntaxHighligher。

#8


0  

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

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

#9


0  

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

如果您正在使用PHP,您可以使用GeSHi来突出显示许多不同的语言。我以前用过,效果很好。谷歌一下也会发现wordpress和drupal的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的负载也会很小,你的主要问题就是传输速度。