2017年4月5日 星期三

Use SyntaxHighlighter on Blogger

Prepare your template to support code formatting


Go to your Blogger's blog online editor and choose "Template" from the menu at the left.




Click on "Edit HTML".

Within the code search for the closing head-tag

</b:template-skin>
    <b:include data='blog' name='google-analytics'/>
  </head>

  <body expr:class='&quot;loading&quot; + data:blog.mobileClass'>
  <b:section class='navbar' id='navbar' maxwidgets='1' name='Navbar' showaddelement='no'>

And copy the following code (only the links to the files you prepared for hosting) right before the end head-tag

<!-- Begin SyntaxHighlighter-->
    <link href='{Host URL}/shCore.css' rel='stylesheet' type='text/css'/> 
    <link href='{Host URL}/shThemeDefault.css' rel='stylesheet' type='text/css'/> 
    <script src='{Host URL}/shCore.js' type='text/javascript'/> 
 <script src='{Host URL}/shBrushCpp.js' type='text/javascript'/> 
    <!--script src='{Host URL}/shBrushCpp.js' type='text/javascript'/--> 
    <!--script src='{Host URL}/shBrushCSharp.js' type='text/javascript'/> 
    <script src='{Host URL}/shBrushCss.js' type='text/javascript'/> 
    <script src='{Host URL}/shBrushJava.js' type='text/javascript'/> 
    <script src='{Host URL}/shBrushJScript.js' type='text/javascript'/> 
    <script src='{Host URL}/shBrushPhp.js' type='text/javascript'/> 
    <script src='{Host URL}/shBrushPython.js' type='text/javascript'/> 
    <script src='{Host URL}/shBrushRuby.js' type='text/javascript'/> 
    <script src='{Host URL}/shBrushSql.js' type='text/javascript'/> 
    <script src='{Host URL}/shBrushVb.js' type='text/javascript'/> 
    <script src='{Host URL}/shBrushXml.js' type='text/javascript'/> 
    <script src='{Host URL}/shBrushPerl.js' type='text/javascript'/--> 
    <script type='text/javascript'>
    window.setTimeout(function() {
        SyntaxHighlighter.config.bloggerMode = true;
        SyntaxHighlighter.all();
    }, 20);
    </script>
<!-- End SyntaxHighlighter-->
</head>

Test it


Let's put some source-code into a blog article to check if it shows up correctly. There are 2 possibilities how to do this:

Method 1:

<script type="syntaxhighlighter" class="brush: cpp"><![CDATA[
// 'Hello World!' program 
 
#include <iostream>
 
int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}
]]></script>

Result:

?
1
2
3
4
5
6
7
8
9
// 'Hello World!' program
  
#include <iostream>
  
int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}


Method 2:

<pre class="brush: cpp">
// 'Hello World!' program 
 
#include &lt;iostream&gt;
 
int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}
</pre>

Result:

?
1
2
3
4
5
6
7
8
9
// 'Hello World!' program
  
#include <iostream>
  
int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}

Brush nameBrush aliasesFile name
ActionScript3as3, actionscript3shBrushAS3.js
Bash/shellbash, shellshBrushBash.js
ColdFusioncf, coldfusionshBrushColdFusion.js
C#c-sharp, csharpshBrushCSharp.js
C++cpp, cshBrushCpp.js
CSScssshBrushCss.js
Delphidelphi, pas, pascalshBrushDelphi.js
Diffdiff, patchshBrushDiff.js
Erlangerl, erlangshBrushErlang.js
GroovygroovyshBrushGroovy.js
JavaScriptjs, jscript, javascriptshBrushJScript.js
JavajavashBrushJava.js
JavaFXjfx, javafxshBrushJavaFX.js
Perlperl, plshBrushPerl.js
PHPphpshBrushPhp.js
Plain Textplain, textshBrushPlain.js
PowerShellps, powershellshBrushPowerShell.js
Pythonpy, pythonshBrushPython.js
Rubyrails, ror, rubyshBrushRuby.js
ScalascalashBrushScala.js
SQLsqlshBrushSql.js
Visual Basicvb, vbnetshBrushVb.js
XMLxml, xhtml, xslt, html, xhtmlshBrushXml.js

References:

  1. http://itsonlycode.blogspot.hk/2015/06/blogger-setup-syntaxhighlighter-for.html
  2. http://alexgorbatchev.com/SyntaxHighlighter/
  3. Embed Code Syntax Highlighting in Blog
  4. How to Host JavaScript or CSS Files on Google Drive
  5. Change Code to HTML friendly

沒有留言:

張貼留言