======divalign plugin======
---- plugin ----
description: Align content left, right, center, or justify
author : Jason Byrne
email : jbyrne@floridascale.com
type : syntax
lastupdate : 2008-03-29
compatible :
depends :
conflicts :
similar :
tags : align, div
----
As a DokuWiki newbie, I'm pretty proud to be contributing with my first plugin (I only started using DokuWiki two days ago!). I think this makes a pretty nice, unintrusive, and intuitive way to align things without going against what I think is a core principle of not adding in html or html-like code to the native wiki format.
==== Updated ====
I've updated the source listing below to remove the security issue. Any user of this plugin should upgrade to the new version. To upgrade simply replace the contents of your lib/plugins/divalign/syntax.php file with the code shown below.
The update also includes other changes and improvements:
* use getAllowedTypes() method rather the constructor to set allowed modes.
* increase allowed modes to all sensible modes. Plugin can now wrap around tables, lists, quotes, code and a host of other syntax modes.
* use core "cdata" instruction to handle content between this plugin's open and closing tags. This allows this plugin to behave sensibly with other renderers.
--- //[[chris@jalakai.co.uk|Christopher Smith]] 2008/03/30 01:25//
====Syntax====
Align Left:
#;;
This would be aligned left.
#;;
Align Right:
;;#This is aligned right.;;#
Align Center:
;#;
This is aligned center.
;#;
Align Justify:
###Justify me baby!###
=====Notes=====
* The justify doesn't seem to work (at least in Firefox), but I think that's just because the browser doesn't support the style="text-align: justify" or maybe I'm doing it wrong--who knows.
* The align left doesn't really do anything since everything's already aligned left.
* No, that depends on the chosen style at design.css - maybe there are freaks centering their pages... ;) at least it helps to break from justify to left. --- //[[freddyw@gmx.net|Freddy]] 2005-11-15 14:03//
* By default, text is right aligned for right-to-left languages (arabic, ...) ---//[[viktro@bigfoot.com|Viktor]] 2006-10-14 18:32//
* As shown in the syntax above, you can do it all in one line or you can break it up. It just creates a big div around it so you could nest other things in it... whatever.
=====The Code=====
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
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_divalign extends DokuWiki_Syntax_Plugin {
function getInfo(){
return array(
'author' => 'Jason Byrne',
'email' => 'jbyrne@floridascale.com',
'date' => '2008-03-29',
'name' => 'divalign',
'desc' => 'Add alignment',
'url' => 'http://www.dokuwiki.org/plugin:divalign',
);
}
function getSort() { return 157; }
function getType() { return 'container'; }
function getAllowedTypes() { return array('container','substition','protected','disabled','formatting','paragraphs'); }
function getPType(){ return 'block';}
function connectTo($mode) {
$this->Lexer->addEntryPattern(';;#(?=.*;;#)',$mode,'plugin_divalign');
$this->Lexer->addEntryPattern('#;;(?=.*#;;)',$mode,'plugin_divalign');
$this->Lexer->addEntryPattern(';#;(?=.*;#;)',$mode,'plugin_divalign');
$this->Lexer->addEntryPattern('###(?=.*###)',$mode,'plugin_divalign');
}
function postConnect() {
$this->Lexer->addExitPattern(';;#','plugin_divalign');
$this->Lexer->addExitPattern('#;;','plugin_divalign');
$this->Lexer->addExitPattern(';#;','plugin_divalign');
$this->Lexer->addExitPattern('###','plugin_divalign');
}
function handle($match, $state, $pos, &$handler){
switch ( $state ) {
case DOKU_LEXER_ENTER:
switch ( $match ) {
case '#;;' : $align = 'left'; break;
case ';;#' : $align = 'right'; break;
case ';#;' : $align = 'center'; break;
case '###' : $align = 'justify'; break;
default : $align = '';
}
return array($align,$state,$pos);
case DOKU_LEXER_UNMATCHED:
$handler->_addCall('cdata', array($match), $pos);
break;
}
return array('',$state,'');
}
function render($mode, &$renderer, $data) {
if ($mode == 'xhtml'){
list($align,$state,$pos) = $data;
switch ($state) {
case DOKU_LEXER_ENTER:
if ($align) { $renderer->doc .= '
===== Discussion =====
Hi, and welcome to the Dokuwiki community :-)
Some notes on your plugin:
* multiple exit patterns are probably not a good idea. The first exit pattern encountered will generate a DOKU_LEXER_EXIT state irregardless of which pattern entered. Admittedly if the syntax is followed there won't be any issues but in poorly formed pages the results could be unexpected. You may want to consider one exit pattern or four separate component plugins, ''divalign/syntax/left.php'', ''divalign/syntax/right.php'', ''divalign/syntax/center.php'', ''divalign/syntax/justify.php''
* in handle(), when the state is DOKU_LEXER_ENTER, the match can only be three characters. You would be better off doing an equate than an ereg().
* you may want to consider assigning each ''
if ($align) { $renderer->doc .= ''; }
Change it to...
if ($align) { $renderer->doc .= ''; }
Also, remove the ''?>'' at the end of the file, it causes trouble down stream.
Then create a ''style.css'' file in your ''divalign'' plugin folder and drop this code in there:
/* plugin:divalign */
div.divalign-left {
text-align: left;
}
div.divalign-right {
text-align: right;
}
div.divalign-center {
text-align: center;
}
div.divalign-justify {
text-align: justify;
}
/* end plugin:divalign */
The plugin is now xHTML complicate!
--- //[[walter@torres.ws|Walter Torres]] 2008-08-24 23:52//