我的博客托管在Blogger上,我经常用C / c# / Java / XML等语言发布代码片段,但我发现这些代码片段“支离破碎”。

是否有任何网站,我可以用来事先解析代码片段,整理格式,将XML“<”转换为“&lt;”等。

在这个领域有很多关于SO的问题,但我找不到任何直接解决这个问题的问题。

编辑:对于@Rich答案,网站声明“要在您的网站上显示格式化的代码,您需要获得此CSS样式表,并在页面的<head>部分中添加对它的引用”。这就是问题所在——你不能在博客AFAIK上这样做。


当前回答

对于我的博客,我使用http://hilite.me/来格式化源代码。它支持许多格式和输出相当干净的html。但如果你有很多代码片段,那么你就必须做很多复制粘贴。对于格式化Python代码,我也使用了pyptions(博客文章)。

其他回答

这个css脚本可能对所有人都有用-它不用于语法高亮显示,但用于以原始格式显示源代码:

 <pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; 
                color: #000000; background-color: #eee;
                font-size: 12px; border: 1px dashed #999999;
                line-height: 14px; padding: 5px; 
                overflow: auto; width: 100%">
       <code style="color:#000000;word-wrap:normal;">

            <<<<<<<YOUR CODE HERE>>>>>>>

       </code>
 </pre>

使用方法:

将此片段粘贴到文本编辑器中, 将代码粘贴到<<<<<<>>>>>>块中。 复制全部和 粘贴到HTML视图在博客(或任何其他)后编辑器。

优点:简单易用,少配置,容易重新配置,不需要额外的软件

我创造了一个工具来完成这项工作。你可以在我的博客上找到它:

免费在线c#语法高亮显示

除了给你的c#代码上色,这个工具还会处理所有的“<”和“>”符号,把它们转换成“&lt;”和“&gt;”制表符被转换为空格,以便在不同的浏览器中看起来相同。你甚至可以让语法高亮显示内联CSS样式,以防你不能或不想在你的博客或网站中插入CSS样式表。

1. 首先,备份你的博客模板 2. 然后打开你的博客模板(在编辑HTML模式下),复制</b:skin>标签之前的所有链接中的css 3.在</head>标签前粘贴以下代码

<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shCore.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCpp.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushDelphi.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJava.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJScript.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushPhp.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushPython.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushRuby.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushSql.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushVb.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushXml.js' type='text/javascript'></script>

4. 将以下代码粘贴在</body>标记之前。

<script language='javascript'>
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

5. 保存博客模板。 6. 现在语法高亮已经可以使用了,你可以使用<pre></pre>标签。

<pre name="code">
...Your html-escaped code goes here...
</pre>

<pre name="code" class="php">
    echo "I like PHP";
</pre>

7. 你可以在这里转义你的代码。 8. 下面是<class>属性支持的语言列表。

http://formatmysourcecode.blogspot.co.uk/ 工作正常,你只需要复制,格式化,粘贴回来。

共享代码最简单的方法是使用公共主旨。只需编写一个并粘贴到嵌入代码。容易peasy。

http://gist.github.com

为了解决搜索引擎的问题,可以在页面上使用隐藏的div,简单如下:

<div style="display:none"> content </div>