DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:coloured

Coloured Text Plugin

Description

[col red]This is red text[/col]

Plugin

<?php
/**
 * Add coloured text capability to dokuwiki
 * [col red]This is red text[/col]
 * [col blue]This is blue text[/col]
 * [col green]This is green text[/col]
 * [col grey]This is grey text[/col]
 *
 * HIGHLY derived from the work of :
 * Stephane Chamberland <stephane.chamberland@ec.gc.ca> (Side Notes PlugIn)
 * Carl-Christian Salvesen <calle@ioslo.net> (Graphviz plugin)
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Fabian van-de-l'Isle <webmaster@lajzar.co.uk>
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
 
class syntax_plugin_coloured extends DokuWiki_Syntax_Plugin {
 /**
  * return some info
  */
  function getInfo(){
    return array(
      'author' => 'Fabian van-de-l_Isle',
      'email'  => 'webmaster@lajzar.co.uk',
      'date'   => '2005-07-31',
      'name'   => 'Coloured text Plugin',
      'desc'   => 'Add coloured text',
      'url'    => 'http://www.dokuwiki.org/plugin:coloured',
    );
  }
 
 /**
  * Constructor - adds allowed modes
  */
  function syntax_plugin_coloured(){
    global $PARSER_MODES;
    $this->allowedModes = array_merge(
      $PARSER_MODES['container'],
      $PARSER_MODES['paragraphs'],
      $PARSER_MODES['formatting'],
      $PARSER_MODES['substition']
    );
  }
 
 /**
  * What kind of syntax are we?
  */
  function getType(){
    return 'formatting';
  }
 
 /**
  * Paragraph Type
  *
  * Defines how this syntax is handled regarding paragraphs. This is important
  * for correct XHTML nesting. Should return one of the following:
  *
  * 'normal' - The plugin can be used inside paragraphs
  * 'block'  - Open paragraphs need to be closed before plugin output
  * 'stack'  - Special case. Plugin wraps other paragraphs.
  *
  * @see Doku_Handler_Block
  */
  function getPType(){
    return 'normal';
  }
 
 /**
  * Where to sort in?
  */
  function getSort(){
    return 900;
  }
 
 /**
  * Connect pattern to lexer
  */
  function connectTo($mode) {
    $this->Lexer->addEntryPattern('\x5Bcol (?=.*\x5B/col\x5D)',$mode,'plugin_coloured');
  }
  // [] \x5B \x5D
  // <> \x3C \x3E
  function postConnect() {
    $this->Lexer->addExitPattern('\x5B/col\x5D','plugin_coloured');
  }
 
 /**
  * Handle the match
  */
  function handle($match, $state, $pos, &$handler){
    if ( $state == DOKU_LEXER_UNMATCHED) {
      $matches = preg_split('/]/u',$match,2);
      if ( $matches[0] == '' ) { $matches[0] = NULL; }
      return array($state, $matches[0], $matches[1]);
    }
    return array($state);
  }
 
 /**
  * Create output
  */
  function render($mode, &$renderer, $data) {
    if($mode == 'xhtml'){
      if ($data[0] == DOKU_LEXER_UNMATCHED){
        $start = true;
        if ( $data[1] == 'red' || $data[1] == 'ahmar' || $data[1] == 'aka' ) {
          $css_class = "red";
        }
        else if ($data[1] == 'green' || $data[1] == 'ahdar' || $data[1] == 'midori' ) {
          $css_class = "green";
        }
        else if ($data[1] == 'blue' || $data[1] == 'ikhal' || $data[1] == 'ao' ) {
          $css_class = "blue";
        }
        else if ( $data[1] == 'grey' || $data[1] == 'gray' ) {
          $css_class = "grey";
        }
        else {
          $renderer->doc .= $data[1];
          $start = false;
        }
        if ($start) {
          $renderer->doc .= '<span class="' . $css_class . '">' . $data[2];
        }
      }
      else if ($data[0] == DOKU_LEXER_EXIT) {
        $renderer->doc .= '</span>';
      }
      return true;
    }
    return false;
  }
}
//Setup VIM: ex: et ts=4 enc=utf-8 :
?>

PHP

To install, put the following PHP file in /lib/plugins/coloured/syntax.php.

<?php
/**
 * Add coloured text capability to dokuwiki
 * [col red]This is red text[/col]
 * [col blue]This is blue text[/col]
 * [col green]This is green text[/col]
 * [col grey]This is grey text[/col]
 *
 * HIGHLY derived from the work of :
 * Stephane Chamberland <stephane.chamberland@ec.gc.ca> (Side Notes PlugIn)
 * Carl-Christian Salvesen <calle@ioslo.net> (Graphviz plugin)
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Fabian van-de-l'Isle <webmaster@lajzar.co.uk>
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
 
class syntax_plugin_coloured extends DokuWiki_Syntax_Plugin {
 /**
  * return some info
  */
  function getInfo(){
    return array(
      'author' => 'Fabian van-de-l_Isle',
      'email'  => 'webmaster@lajzar.co.uk',
      'date'   => '2005-07-31',
      'name'   => 'Coloured text Plugin',
      'desc'   => 'Add coloured text',
      'url'    => 'http://www.dokuwiki.org/plugin:coloured',
    );
  }
 
 /**
  * Constructor - adds allowed modes
  */
  function syntax_plugin_coloured(){
    global $PARSER_MODES;
    $this->allowedModes = array_merge(
      $PARSER_MODES['container'],
      $PARSER_MODES['paragraphs'],
      $PARSER_MODES['formatting'],
      $PARSER_MODES['substition']
    );
  }
 
 /**
  * What kind of syntax are we?
  */
  function getType(){
    return 'formatting';
  }
 
 /**
  * Paragraph Type
  *
  * Defines how this syntax is handled regarding paragraphs. This is important
  * for correct XHTML nesting. Should return one of the following:
  *
  * 'normal' - The plugin can be used inside paragraphs
  * 'block'  - Open paragraphs need to be closed before plugin output
  * 'stack'  - Special case. Plugin wraps other paragraphs.
  *
  * @see Doku_Handler_Block
  */
  function getPType(){
    return 'normal';
  }
 
 /**
  * Where to sort in?
  */
  function getSort(){
    return 900;
  }
 
 /**
  * Connect pattern to lexer
  */
  function connectTo($mode) {
    $this->Lexer->addEntryPattern('\x5Bcol (?=.*\x5B/col\x5D)',$mode,'plugin_coloured');
  }
  // [] \x5B \x5D
  // <> \x3C \x3E
  function postConnect() {
    $this->Lexer->addExitPattern('\x5B/col\x5D','plugin_coloured');
  }
 
 /**
  * Handle the match
  */
  function handle($match, $state, $pos, &$handler){
    if ( $state == DOKU_LEXER_UNMATCHED) {
      $matches = preg_split('/]/u',$match,2);
      if ( $matches[0] == '' ) { $matches[0] = NULL; }
      return array($state, $matches[0], $matches[1]);
    }
    return array($state);
  }
 
 /**
  * Create output
  */
  function render($mode, &$renderer, $data) {
    if($mode == 'xhtml'){
      if ($data[0] == DOKU_LEXER_UNMATCHED){
        $start = true;
        if ( $data[1] == 'red' || $data[1] == 'ahmar' || $data[1] == 'aka' ) {
          $css_class = "red";
        }
        else if ($data[1] == 'green' || $data[1] == 'ahdar' || $data[1] == 'midori' ) {
          $css_class = "green";
        }
        else if ($data[1] == 'blue' || $data[1] == 'ikhal' || $data[1] == 'ao' ) {
          $css_class = "blue";
        }
        else if ( $data[1] == 'grey' || $data[1] == 'gray' ) {
          $css_class = "grey";
        }
        else {
          $renderer->doc .= $data[1];
          $start = false;
        }
        if ($start) {
          $renderer->doc .= '<span class="' . $css_class . '">' . $data[2];
        }
      }
      else if ($data[0] == DOKU_LEXER_EXIT) {
        $renderer->doc .= '</span>';
      }
      return true;
    }
    return false;
  }
}
//Setup VIM: ex: et ts=4 enc=utf-8 :
?>

CSS

Stick this in your design.css file:

/* ----------- coloured Text ----------- */
.red {
    color: #ff0000;
}
.green {
    color: #009900;
}
.blue {
    color: #0000ff;
}
.grey {
    color: #999999;
}

Changes

2005-7-31 - Created

To Do

Bugs

It doesn't parse nested [col] tags. That is to say, the following code will break:

[col red]Here is some red text, [col blue]with some blue[/col] inside[/col].

The inner set of [col] tags goes unparsed here. The workaround is to make sure you close your tags.

Discussion

There is a similar plugin included with the syntax plugin tutorialChriS 2005-07-31

There's a tutorial? Cool! No one told me :-/ta' Lajzar 2005-07-31 23:44
There is since I finished it today :-D, please send me any comments, positive or negative —ChriS 2005-08-01

Actually, what I'd like to see in a tutorial is an explanation for the pattern matching. The basics I've figured out, and I know generic explanations exist on various PHP sites, but something specific to the opening pattern matches would be nice. I've noticed that the core part of those patterns isn't always the same between plugins, and I don't know why. — ta' Lajzar 2005-08-01 02:35

plugin/coloured.txt · Last modified: 2015-07-13 09:08 by 193.41.205.43