java中代码块语法_博客中片段的最佳代码语法荧光笔

java中代码块语法

java中代码块语法

I get a few emails a day of folks asking what Syntax Highlighter I use in my blog for my code samples. Specifically, the newer code samples, as some of the old ones sucked as I was experimenting, trying to find the best one to settle on.

我每天都会收到几封电子邮件,询问我在博客中用于代码示例的Syntax Highlighter。 具体来说,较新的代码示例(如我在尝试中的一些旧示例)很烂,试图找到最适合的代码示例。

The tool I use is actually called SyntaxHighlighter and it's from Alex Gorbatchev. The trick is that the syntax highlighter is all javascript on the client side.

我使用的工具实际上称为SyntaxHighlighter 亚历克斯·戈尔巴乔夫( Alex Gorbatchev)的作品。 诀窍是语法高亮器是客户端上所有的javascript。

I was having all sorts of troubles with other code highlighters. First, there were ones that put css classes and stuff all through your code, trying to decorate each keyword. This just bloated my feed and site and made the code look weird in some Feed Readers. Then I tried using images for code, like ScottGu does, but that is just wrong. You can't copy paste the code, you can't search it, it's disrespectful for the blind, etc. Meh.

我在使用其他代码突出显示工具时遇到了种种麻烦。 首先,有一些代码将css类和内容全部放入代码中,试图修饰每个关键字。 这只是使我的提要站点肿,并使某些提要阅读器中的代码看起来很奇怪。 然后我尝试像ScottGu一样使用图像作为代码,但这是错误的。 您无法复制粘贴代码,也无法搜索代码,盲人不敬等。

我如何将代码发布到博客 (How I post code to my blog)

I use Windows Live Writer to post all my blog posts, and it has a great plugin model. I've actually written a WLW plugin for the CueCat...it's really easy. I use a plugin from DasBlog contributor Anthony Bouch called PreCode that directly targets/supports SyntaxHighlighter from within WLW.

我使用Windows Live Writer发布所有博客文章,并且它具有出色的插件模型。 我实际上已经为CueCat编写了WLW插件...这真的很容易。 我使用了来自DasBlog贡献者Anthony Bouch的名为PreCode的插件,该插件直接从WLW中定位/支持SyntaxHighlighter

Screenshot of my plugins in Windows Live Writer

That means I see this from inside Live Writer. I slick Insert PreCode Snippet, and paste in my code.

这意味着我从Live Writer内部看到了这一点。 我打滑Insert PreCode Snippet,然后粘贴我的代码。

If you're reading this blog post from inside an aggregator or feed reader, the next two code snippets look identical to you. However, if you visit my blog, you'll see that one is different.

如果您是从聚合器或供稿阅读器中阅读此博客文章的,那么下面的两个代码片段看起来与您相同。 但是,如果您访问我的博客,则会发现一个不同。

// Hello3.cs
using System;

public class Hello3
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("You entered the following {0} command line arguments:",
args.Length );
for (int i=0; i < args.Length; i++)
{
Console.WriteLine("{0}", args[i]);
}
}
}
// Hello3.cs
using System;

public class Hello3
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("You entered the following {0} command line arguments:",
args.Length );
for (int i=0; i < args.Length; i++)
{
Console.WriteLine("{0}", args[i]);
}
}
}

One looks like this, as HTML:

一个像这样HTML:



See the 'class="c#" name="code"' part? Alex's Javascript SyntaxHighlighter is looking for those and parsing them on the client side. I choose to add breaks, but that's an option in PreCode. Other options for SyntaxHighlighter include line numbering, gutters, copy/paste support, a toolbar and more.

看到“ class =“ c#” name =“ code”部分? Alex的Javascript SyntaxHighlighter正在寻找这些内容,并在客户端进行解析。 我选择添加中断,但这是PreCode中的一个选项。 SyntaxHighlighter的其他选项包括行号,装订线,复制/粘贴支持,工具栏等。

P.S. If you don't use Windows Live Writer (and seriously, stop and ask yourself, WHY NOT?) and use instead a web interface, you can integrate SyntaxHighlighter into your web-based rich text editor. For example, Darren made a SyntaxHighlighter Plugin for the popular FCKeditor. Perhaps we'll put that in DasBlog.

PS如果您不使用Windows Live Writer(认真地说,停下来问问自己,为什么不这样做?),而是使用Web界面,则可以将SyntaxHighlighter集成到基于Web的RTF编辑器中。 例如,Darren为流行的FCKeditor制作了SyntaxHighlighter插件。 也许我们将其放在DasBlog中

将SyntaxHighlighter安装到您的博客 (Installing SyntaxHighlighter to Your Blog)

You install the SyntaxHighlighter by adding it to your blog's template. It doesn't care what blog engine you run, as it doesn't need anything on the server:

您可以通过将SyntaxHighlighter添加到博客模板中来进行安装。 它不在乎您运行什么博客引擎,因为它不需要服务器上的任何内容:









Just add the shCore library and just the languages you require. If you want your blog to feel snappy and you have some control over your server, don't forget to set the files/directories to cache on the client by making them expire far in the future. You don't want your user's browsers to keep asking for these scripts each page view.

只需添加shCore库以及所需的语言即可。 如果您希望博客感觉敏捷,并且可以控制服务器,请不要忘记设置文件或目录以使其在将来过期,从而将其设置在客户端上进行缓存。 您不希望用户的浏览器在每个页面视图中都不断询问这些脚本。

Even better, you can create your own plugins for SyntaxHighlighter if you use a language Alex hasn't supported officially. This guy threw together a Scala SyntaxHighlighter file by editing the Java one and adding a regex.

更好的是,如果您使用Alex官方不支持的语言,则可以为SyntaxHighlighter创建自己的插件。 这个家伙通过编辑Java并添加一个正则表达式,将Scala SyntaxHighlighter文件组合在一起。

There are a few bugs but I think folks forget that Alex is doing this all alone, so I have to give him mad props for the effort. It can be lonely and unforgiving when you do something awesome and either no one cares, or folks only care to complain.

一些错误,但我认为人们会忘记亚历克斯独自一人做这件事,因此我必须给他疯狂的道具。 当您做一些很棒的事情而无人问津,或者人们只在乎抱怨时,这可能是孤独而无情的。

UPDATE: There's some great un-bundled brushes collected here.

更新:这里收集了一些很棒的非捆绑笔刷

翻译自: https://www.hanselman.com/blog/best-code-syntax-highlighter-for-snippets-in-your-blog

java中代码块语法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值