DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:syntaxhighlighter

SyntaxHighlighter Plugin

Compatible with DokuWiki

  • 2024-02-06 "Kaos" unknown
  • 2023-04-04 "Jack Jackrum" unknown
  • 2022-07-31 "Igor" unknown
  • 2020-07-29 "Hogfather" no

plugin SyntaxHighlighter Plugin ported from SyntaxHighlighter 1.5.1 created by Alex Gorbatchev.

Last updated on
2008-12-04
Provides
Syntax, Action
Repository
Source

Features

  • it uses 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 Extension Manager. Refer to Plugins on how to install plugins manually.

The project is hosted at GitHub. Public Git Repository

File Modifications

I was not able to get the JavaScript files to load properly via the 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

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";
    }
  }
}
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.
      $event->data["script"][] = array ("type" => "text/javascript",
	  "src" => DOKU_BASE."lib/plugins/syntaxhighlighter/Uncompressed/shCore.js",
	  "_data" => ""
	  );


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.

<script language="javascript">
dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('code');
</script>
I used following solution
add to function register(Doku_Event_Handler $controller):
    $controller->register_hook('TPL_ACT_RENDER',
                               'AFTER',
                               $this,
                               '_hookjsprocessing');


and add following function:

  /**
   *  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>");
 
 
  }


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 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 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 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

  1. Copy the shBrush{NewLanguage}.js file to the lib/plugins/syntaxhighlighter/Uncompressed folder.
  2. Modify the lib/plugins/syntaxhighlighter/action.php file.
  3. Add the following code in the _hooksh function:
 $event->data["script"][] = array ("type" => "text/javascript",
          "src" => DOKU_BASE."lib/plugins/syntaxhighlighter/Uncompressed/shBrush{NewLanguage}.js"
                                          );

To remove a language

  1. Modify the lib/plugins/syntaxhighlighter/action.php file.
  2. Remove the $event code in the _hooksh function for the language to be removed.
plugin/syntaxhighlighter.txt · Last modified: 2020-06-04 16:33 by StarArmy

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