2016年7月17日 星期日

[Blogger] 替程式碼上色

Blogger新開張,先決定程式碼要怎麼貼上來吧
之前在 Xuite blogger 是使用 SyntaxHighlighter,所以這次想試試看別的方法
上網找了一下,發現google有在做 Google Code Prettify


在版面配置→新增小工具→選擇HTML/JavaScript,貼上以下程式碼

  1. <style type='text/css'>
  2.  
  3. /* Main Box */
  4. pre.prettyprint, code.prettyprint {
  5. display: block;
  6. overflow: auto;
  7. min-height: 30px;
  8. max-height: 600px;
  9. /* 圓角屬性 */
  10. border-radius: 8px;
  11. -moz-border-radius: 8px;
  12. -webkit-border-radius: 8px;
  13. }
  14.  
  15. /* font */
  16. pre, code {
  17. font-size: 12px !important;
  18. }
  19.  
  20. li.L0, li.L1, li.L2, li.L3, li.L4,
  21. li.L5, li.L6, li.L7, li.L8, li.L9 {
  22. list-style-type: decimal !important;
  23. margin: 0 !important;
  24. padding-left: 4px !important;
  25. border-left: 2px solid #4CAF50
  26. }
  27. </style>
  28. <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>

這樣預先工作就準備好了,以後只要在文章中有程式碼的部份
就切到HTML編輯,前後夾上
  1. <pre class="prettyprint linenums:1">
  2.     程式碼在這裡
  3. </pre>

指定語言的方式
  1. // <pre class="prettyprint lang-html">
  2. // The lang-* class specifies the language file extensions.
  3. // File extensions supported by default include
  4. // "bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html",
  5. // "java", "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh",
  6. // "xhtml", "xml", "xsl".
  7. // </pre>
  8.  
  9. // 指定 CPP 語言
  10. <pre class="prettyprint linenums:1 lang-cpp">
  11. include <stdio.h>
  12. int main() {
  13. print ("Hello World\n");
  14. }
  15. </pre>

目前先這樣,有遇到問題再補上

沒有留言:

張貼留言