DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:divalign

divalign plugin

Compatible with DokuWiki

No compatibility info given!

plugin Align content left, right, center, or justify

Last updated on
2008-03-29
Provides
Syntax
Conflicts with
divalign2

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Tagged with style, typography

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

A new plugin based off this one has been added to the Plugins list. divalign2 implements some of the requests and corrections features under Discussion.

Luis Machuca B. 2009/02/14 05:32

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.

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. — Freddy 2005-11-15 14:03
    • By default, text is right aligned for right-to-left languages (Arabic, …) —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

syntax.php
<?php
/**
 * divalign: allows you to align right, left, center, or justify
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Jason Byrne <jbyrne@floridascale.com>
 */
 
// 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 .= '<div style="text-align: '.$align.';">'; }
              break;
 
            case DOKU_LEXER_EXIT : 
              $renderer->doc .= '</div>';
              break;
          }
          return true;
        } // end if ($mode == 'xhtml')
 
        return false;
    }
 
}
 
//Setup VIM: ex: et ts=4 enc=utf-8 :
?>

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 <div> a class name and using a style.css file rather than inline styles.
  • an alternative would be to use the div/span shorthand plugin and define four classes, left, right, center and justify in your style sheet.
  • its not good practice to setup $this->allowedModes values in the constructor. Not all plugins are loaded at that time so you may miss including the syntax modes of other plugins. The ideal method is via the getAllowedTypes() method.

Christopher Smith 2005-09-24 02:20

  • right on–it's taken along time for somebody to do something about text alignment. thanks — Warren Smith
  • Nice Plugin! Thanks! However, I found a problem: when formatting a line with one or more wiki page links in it, a line break is inserted before every page link (so the line is split up). Any idea? — Andreas Kraft
  • Can anyone make this plugin downloadable?? Thank's — Teto
  • Bug: when formatting a line (i tried on center), the line breaks when there is a ' — Teto
  • This plugin isn't XHTML 1.0 compatible. W3 errors are:
    • document type does not allow element “div” here; missing one of “object”, “applet”, “map”, “iframe”, “button”, “ins”, “del” start-tag.
    • document type does not allow element “p” here; missing one of “object”, “applet”, “map”, “iframe”, “button”, “ins”, “del” start-tag.
    • end tag for “p” omitted, but OMITTAG NO was specified.
  • i have a warning when I save my page (“Warning: Cannot modify header information - headers already sent by (output started at /var/www/grim/lib/plugins/divalign/syntax.php:123”), any idea? — Antoine Pigeau 2007-06-15 09:20
    • hi Antoine, I've faced this problem with all plugins that have no installer, looks like it is a post-effect of copying the PHP code intoa file. I pretty much solved it by removing the closing ?> tag in syntax.php. Should work with any hand-coded plugin. Please let me know if this helps. — this comment from Luis Machuca B., a happy DW user (2007-09-29).
  • How about including the Syntax into DokuWiki core? Would probably be nice to have for everyone, not only after cumbersomely installing this plugin via copy&paste…

Christian Marg 2008-07-17 00:04

An XHTML validation 'fix':

I agree about the XHTML compliant coding. So I 'fixed' it.

In the “render” method, look for this line…

    if ($align) { $renderer->doc .= '<div style="text-align: '.$align.';">'; }

Change it to…

    if ($align) { $renderer->doc .= '<div class="divalign-' . $align . '">'; }

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

Walter Torres 2008-08-24 23:52

Is the author still working on this plugin? If not, I would like to incorporate the suggestions to the existing code and release a new version, or a fork, if applicable, as well as package for the Plugin Manager. — Luis Machuca B. 2009/01/22 21:13

Feel free to fork and release as you like, but always acknowledge the contribution of the previous authors. To take over the plugin, send an email to the current author/maintainer offering to take it over. If you get no reply, go with your fork. You are free to edit this page to point people at your fork. On the page you create for your fork include a link back to this page. — Christopher Smith 2009/01/25 02:59
OK, so I did wrote the OA (Jason) and never got an answer, so I took the code base, the corrections that made the last update, and some of the suggestions, and cooked up some code. I've forked this plugin into divalign2 which features Plugin Manager option and code separation. I've followed your advice on links back and forth, Chris. So I hope those among you who were waiting for the improvements can check it and comment. I'll see to provide later a complete list of fixes, featurettes and missing corrections. – — Luis Machuca B. 2009/02/14 05:32
plugin/divalign.txt · Last modified: 2011-06-18 17:42 by ach