简体   繁体   中英

How to style a code block to make all the colours look pretty like my IDE and here in stackoverflow?

When putting code samples in my blog I would like the code to have all the appropriate colours. How can I do that?

Basically if I write:

foreach (ListItem item in items)
{
    item.Text = "something";
}

I want it to appear in the correct colours like it does in my Visual Studio or here at stack overflow .

I'm bored of styling the colours of each key word manually. Is there a pre-existing javascript library I can use?

EDIT: I'm using blogger as my blogging platform if that helps

A common approach is to use a JavaScript library to style up the code samples. You can use prettify to do syntax highlighting on a webapage, the README explains exactly how to do it.

您还可以使用托管代码的位置(例如http : //gist.github.com,http://snipplr.com/以及其他各种“ pastebin”服务),然后仅使用它们提供的嵌入脚本。

Google "syntax highlighter". There are a bunch of libraries out there, many use javascript.

Try this syntax highlighter . If you are using a particular CMS, there are probably modules already created for this.

I used Windows Live Writer for a while, and there is a plugin to paste from Visual Studio , that does the colour coding for you. I then coded that content into my Blogger/Wordpress post.

Now I use a java based plug-in for Wordpress SyntaxHighlighter Evolved , which provides line wrapping.

UPDATED:

link tutorial

You can also use vim's export as html, and then copy&paste in blogger. No the best solution, but probably the simplest.

Really I'd like to have jballs comment as the answer... but I can't. He was right though - SyntaxHighlighter seems to be the easiest to get/use and there is a lot of support for it too.

I implemented it really quickly. Tested it and explained my steps here: Colouring code with Syntax Highlighter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM