How to highlight source code in HTML? [closed]

2019-01-13 15:50发布

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

How can I do this?

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

9条回答
霸刀☆藐视天下
2楼-- · 2019-01-13 16:04

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

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-13 16:14

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

查看更多
神经病院院长
4楼-- · 2019-01-13 16:15

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.

查看更多
【Aperson】
5楼-- · 2019-01-13 16:15

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

查看更多
唯我独甜
6楼-- · 2019-01-13 16:16

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.

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.

查看更多
▲ chillily
7楼-- · 2019-01-13 16:20

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

查看更多
登录 后发表回答