Sunday, July 28, 2019

How to display code in blogger

Using Google Code Prettify

Method 1: Using the HTML/Javascript Gadget feature
1. Layout->Add a Gadget>HTML/Javascript

2. Copy the following context,

  1. <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>

3. In each blog HTML, place before the code,

  1. <pre class = "prettyprint">

and place after the code

  1. </pre>

4. Google Code Prettify has different Skin choices,just need to modify the script,
  1. <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?skin=desert"></script>

Method 2: Modify the Theme HTML
1: Find </head> inside the HTML;

2: Before the </head>, copy the following lines:

  1. <script src='https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js'/>

3. In each blog HTML, place before the code,

  1. <pre  class = "prettyprint">

and place after the code

  1. </pre>

Done.

Bonus:

1: If you want to show the code line number, add the following lines before </head> in the Theme HTML,

  1. <style>
  2. li.L0, li.L1, li.L2, li.L3,
  3. li.L5, li.L6, li.L7, li.L8
  4. { list-style-type: decimal !important }
  5. </style>

2: If you want to modify the code background and other parameters, try to place the following lines in the same place,

  1. <style>
  2. CODE {
  3. display: block; /* fixes a strange ie margin bug */
  4. font-family: Calibri (Body);
  5. font-size: 12pt;
  6. overflow:auto;
  7. background: #f0f0f0 url(http://klcintw.images.googlepages.com/Code_BG.gif) left top repeat-y;
  8. border: 1px solid #ccc;
  9. padding: 10px 10px 10px 21px;
  10. max-height:4000px;
  11. line-height: 1.2em;
  12. }
  13. </style>

3. In each blog HTML, place before the code,

  1. <pre class = "prettyprint linenums code">

and place after the code

  1. </pre>

Done.

No comments:

Post a Comment