程式碼高亮顯示 -- google-code-prettify

 Google的code-prettify 網頁程式碼高亮

加入script到文件裡

<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>

code則是放在<pre class="prettyprint> </pre><code class="prettyprint> </code>之間

<pre class="prettyprint">
    ...code
</pre>

prettify.js能支援多種程式,在 lang- 後加上想要使用的語言

<pre class="prettyprint lang-html">
    The lang-* class specifies the language file extensions.
    File extensions supported by default include:
      "bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html", "java",
      "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh", "xhtml", "xml",
      "xsl".
</pre>

linenums顯示行數,若不是從1開始就在後加上  :起始行數

<pre class="prettyprint linenums:4">


實際的效果如下

<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
<pre class="prettyprint linenums:1 lang-c">
#ifdef __MAIN_H__
#include <stdio.h>
void main()
{
    printf("Hello world!);
}
#endif
</pre>
#ifdef __MAIN_H__
#include 
#define true 	1
#define false 	0

void main()
{
	printf("Hello world!);
}
#endif

留言