1

我有一段 Java 代码要发布在我的网站上。

代码如下所示:

class Integers {
  public static void main(String[] arguments) {
    int c; //declaring a variable

  /* Using for loop to repeat instruction execution */

    for (c = 1; c <= 10; c++) {
      System.out.println(c);
    }
  }
}

为了将其转换为好看的 HTML 并将其发布在我的网站上,我需要编写以下内容:

<div class="geshifilter">
<pre class="java geshifilter-java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Integers <span style="color: #009900;">{</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">(</span><span style="color: #003399;">String</span><span style="color: #009900;">[</span><span style="color: #009900;">]</span> arguments<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    <span style="color: #000066; font-weight: bold;">int</span> c<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//declaring a variable</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">/* Using for loop to repeat instruction execution */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">(</span>c <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> c <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> c<span style="color: #339933;">++</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">(</span>c<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">}</span>
  <span style="color: #009900;">}</span>
<span style="color: #009900;">}</span></pre></div>

这样做太麻烦了。有没有更简单的方法?

4

2 回答 2

2

我使用 SyntaxHighlighter。它应该完全符合您的要求:http ://alexgorbatchev.com/SyntaxHighlighter/

于 2013-07-29T03:40:31.160 回答
1

将所有代码放入<pre class="prettyprint"><code class="prettyprint">元素中,并将google-code-prettify JavaScript 插件添加到您的网页。这是教程的链接:

http://google-code-prettify.googlecode.com/svn/trunk/README.html

于 2013-07-29T03:42:10.807 回答