DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:syntaxhighlighter

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
plugin:syntaxhighlighter [2009-02-24 14:09] – suggestion about closing tag emanueleplugin:syntaxhighlighter [2020-06-04 16:33] (current) StarArmy
Line 1: Line 1:
 +====== SyntaxHighlighter Plugin ======
  
 +---- plugin ----
 +description: SyntaxHighlighter Plugin ported from SyntaxHighlighter 1.5.1 created by Alex Gorbatchev.
 +author     : David Shin
 +email      : dshin@pimpsmart.com
 +type       : Action, Syntax
 +lastupdate : 2008-12-04
 +compatible : 2008-05-05, !Hogfather
 +similar    : code, code2, code3
 +tags       : code, syntaxhighlight, syntax
 +downloadurl: https://github.com/dshin/dokuwiki-syntaxhighlighter/raw/master/syntaxhighlighter.tar.gz
 +bugtracker : https://github.com/dshin/dokuwiki-syntaxhighlighter/issues
 +sourcerepo : https://github.com/dshin/dokuwiki-syntaxhighlighter
 +----
 +
 +=====Features=====
 +
 +  * it uses [[http://code.google.com/p/syntaxhighlighter/|SyntaxHighlighter]] to do code coloring on the **client side**
 +  * new languages can be added by adding new stylesheets
 +  * configurable start line number and line number toggling (no gutter)
 +  * line numbers do not mess up copy/paste operations
 +  * configurable controls
 +  * collapse code blocks on initial load via configuration option
 +  * plain view mode
 +  * copy all code in a block in one click with clipboard copy button
 +  * show column numbers (somewhat buggy) 
 +
 +===== Download and Installation =====
 +
 +Search and install the plugin using the [[plugin:extension|Extension Manager]]. Refer to [[:Plugins]] on how to install plugins manually.
 +
 +^ Archive   | [[https://github.com/dshin/dokuwiki-syntaxhighlighter/raw/master/syntaxhighlighter.tar.gz|SyntaxHighlighter v1.0 (32Kb, tar.gz)]] |
 +
 +The project is hosted at [[https://github.com/dshin/dokuwiki-syntaxhighlighter/tree|GitHub]]. [[git://github.com/dshin/dokuwiki-syntaxhighlighter.git|Public Git Repository]]
 +
 +===== File Modifications =====
 +
 +I was not able to get the JavaScript files to load properly via the [[devel:action_plugins|Action Plugin]]. To get it to work, I had to modify the **//''_tpl_metaheaders_action''//** function in the **//''inc\template.php''//** file.  The change I made was to use **//'''></$tag>'''//** as the closure tag instead of **//'''/>'''//**.
 +
 +=== inc\template.php:357 ===
 +
 +<code>
 +function _tpl_metaheaders_action($data){
 +  foreach($data as $tag => $inst){
 +    foreach($inst as $attr){
 +      echo '<',$tag,' ',buildAttributes($attr);
 +      if(isset($attr['_data'])){
 +          if($tag == 'script' && $attr['_data']) {
 +                 $attr['_data'] = "<!--//--><![CDATA[//><!--\n".
 +                             $attr['_data'].
 +                             "\n//--><!]]>";
 +               }
 +          echo '>',$attr['_data'],'</',$tag,'>';
 +      }else{
 +          echo '></',$tag,'>';
 +      }
 +      echo "\n";
 +    }
 +  }
 +}
 +</code>
 +
 +> I have had the same problem. The problem seems to a bug of all the browsers except Opera, to cheat, instead of change dokuwiki's functions, you can add an empty "_data" parameter to the array you use to add the js file:
 +> e.g. <code php>
 +      $event->data["script"][] = array ("type" => "text/javascript",
 +   "src" => DOKU_BASE."lib/plugins/syntaxhighlighter/Uncompressed/shCore.js",
 +   "_data" => ""
 +   );
 +</code>
 +> in this way dokuwiki will render also the </$tag> and the problema is gone (at least for me...).
 +
 +=== JavaScript Snippet ===
 +
 +There needs to be a snippet of JavaScript at the end of the template to execute the HighlightAll function.  I've placed the following code at the end of my **//''lib/tpl/default/footer.html''//** file.  If someone can put this in the **//''action.php''//** script, this would be greatly appreciated.  I do not know what action to trigger on to add the JavaScript snippet to the end of the template.
 +
 +<code>
 +<script language="javascript">
 +dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf';
 +dp.SyntaxHighlighter.HighlightAll('code');
 +</script>
 +</code>
 +
 +
 +>I used following solution
 +>add to ''function register(Doku_Event_Handler $controller)'':
 +><code php>    $controller->register_hook('TPL_ACT_RENDER',
 +                               'AFTER',
 +                               $this,
 +                               '_hookjsprocessing');</code>
 +>and add following function:
 +><code php>  /**
 +    Inject the SyntaxHightlighter javascript processing
 +   *
 +    @author David Shin <dshin@pimpsmart.com>
 +    @param $event object target event
 +    @param $param mixed event parameters passed from register_hook
 +   *
 +   */
 +  function _hookjsprocessing (&$event, $param) {
 +    global $ID;
 +    global $INFO;
 +
 +    //this ensures that code will be written only on base page
 +    //not on other inlined wiki pages (e.g. when using monobook template)
 +    if ($ID != $INFO["id"]) return;
 +
 +    ptln("");
 +    ptln("<script language='javascript'>");
 +    ptln("  dp.SyntaxHighlighter.ClipboardSwf = '" .DOKU_BASE. "lib/plugins/syntaxhighlighter/Scripts/clipboard.swf';");
 +    ptln("  dp.SyntaxHighlighter.HighlightAll('code');");
 +    ptln("</script>");
 +
 +
 +  }
 +</code>
 +>with the code change suggested above no other code changes are required
 +>8-) hth Dominik
 +
 +===== Usage =====
 +
 +  * General use (Default highlighting is 'html')
 +''<code> some code </code>''
 +
 +  * Specific language - Python is chosen for this example
 +''<code python> some code </code>''
 +
 +  * Configurable options as specified in the [[plugin:syntaxhighlighter#configuration_options|Configuration Options]] section. First line at 10 and initial collapse
 +''<code firstline[10]:collapse> some code </code>'' \\ 
 +The options are passed together with the alias and are separated by a colon : character. This is how SyntaxHighlight is configured (Also I was too lazy to program it to use spaces instead of ':')
 +
 +=====Configuration Options=====
 +
 +^   Options    ^^
 +|lang           |Language specified by the [[plugin:syntaxhighlighter#language_aliases|Language Alias]].|
 +|nogutter |Will display no gutter.|
 +|nocontrols |Will display no controls at the top.|
 +|collapse |Will collapse the block by default.|
 +|firstline[value] |Will begin line count at value. Default value is 1.|
 +|showcolumns |Will show row columns in the first line. |
 +The options are passed together with the alias and are separated by a colon : character. \\ 
 +
 +**Warning: Do not use** ''nocontrols'' **and** ''collapse'' **together.  You will not be able to expand the code**
 +
 +===== Language Aliases =====
 +
 +^Language ^Aliases      ^
 +|C++ |cpp, c, c++|
 +|C# |c#, c-sharp, csharp|
 +|CSS |css|
 +|Delphi |delphi, pascal|
 +|Java |java|
 +|Java Script |js, jscript, javascript|
 +|PHP |php|
 +|Python |py, python|
 +|Ruby |rb, ruby, rails, ror|
 +|Sql |sql|
 +|VB |vb, vb.net|
 +|XML/HTML |xml, html, xhtml, xslt|
 +
 +Please refer to the [[http://code.google.com/p/syntaxhighlighter/wiki/Languages|language aliases]] page at Google Code for user contributed languages.
 +
 +==== Adding/Removing Languages ====
 +
 +This can be accomplished by modifying the **//''lib/plugins/syntaxhighlighter/action.php''//** file.
 +
 +=== To add a language ===
 +
 +  - Copy the shBrush{NewLanguage}.js file to the **//''lib/plugins/syntaxhighlighter/Uncompressed''//** folder.
 +  - Modify the **//''lib/plugins/syntaxhighlighter/action.php''//** file.
 +  - Add the following code in the **//''_hooksh''//** function:
 +
 +<code php>
 + $event->data["script"][] = array ("type" => "text/javascript",
 +          "src" => DOKU_BASE."lib/plugins/syntaxhighlighter/Uncompressed/shBrush{NewLanguage}.js"
 +                                          );
 +</code>
 +
 +=== To remove a language ===
 +
 +  - Modify the **//''lib/plugins/syntaxhighlighter/action.php''//** file.
 +  - Remove the $event code in the **//''_hooksh''//** function for the language to be removed.

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki