1<?php
2/**
3 * Plugin Outdent: Removes one level of indenting.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Christopher Smith <chris@jalakai.co.uk>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class syntax_plugin_outdent extends DokuWiki_Syntax_Plugin {
18
19    function getInfo(){
20      return array(
21        'author' => 'Christopher Smith',
22        'email'  => 'chris@jalakai.co.uk',
23        'date'   => '2008-08-13',
24        'name'   => 'Outdent Plugin',
25        'desc'   => 'Remove one level of indenting
26                     Syntax: ==',
27        'url'    => 'http://wiki.splitbrain.org/plugin:outdent',
28      );
29    }
30
31    function getType() { return 'baseonly'; }
32    function getSort() { return 50; }                       /* same as header */
33
34    function connectTo($mode) {
35        $this->Lexer->addSpecialPattern('\n[ \t]*==[ \t]*(?=\n)',$mode,'plugin_outdent');
36    }
37
38    function handle($match, $state, $pos, &$handler){
39      $level=0;
40      if ($state == DOKU_LEXER_SPECIAL) {
41        $level = $this->_getLevel($handler->calls);
42        if ($level > 1) {
43            $handler->_addCall('section_close', array(), $pos);
44            $handler->_addCall('section_open', array($level-1), $pos);
45        }
46      }
47
48      return false;
49    }
50
51    function render($mode, &$renderer, $data) {
52
53      return false;
54    }
55
56    function _getLevel(&$calls) {
57
58      for ($i=count($calls); $i >= 0; $i--) {
59          if ($calls[$i][0] == 'header') return $calls[$i][1][1];
60        if ($calls[$i][0] == 'section_open') return $calls[$i][1][0];
61      }
62
63      return 0;
64    }
65}