1<?php
2// must be run within Dokuwiki
3if(!defined('DOKU_INC')) die();
4
5require_once(DOKU_PLUGIN.'formatplus/formatting.php');
6
7class syntax_plugin_formatplus_blockinsertplus extends FormattingPlus_Syntax_Plugin {
8
9  var $disabled = false;
10
11  function _getName() {
12    return 'BlockInsert+';
13  }
14  function _getDesc() {
15    return 'Wraps text in <INS> tags with optional meta-data.
16            Syntax: <ins desc @date>...</ins>';
17  }
18  function _getConfig() {
19    return 'block_ins_del';
20  }
21
22  function _getFormatting() {
23    return array();
24  }
25
26  function getSort() {
27    return 85;
28  }
29
30  function getType() {
31    return 'container';
32  }
33
34  function getAllowedTypes() {
35    return array('container','paragraphs','formatting','substition','disabled','protected');
36  }
37
38  function getPType() {
39    return 'stack';
40  }
41
42  function preConnect() {
43    if ($this->_disabledSyntax($this->_getConfig()))
44      $this->disabled = true;
45  }
46
47  function connectTo($mode) {
48    if (!$this->disabled) {
49      $this->Lexer->addEntryPattern('<ins>(?=.*</ins>)', $mode, 'plugin_formatplus_blockinsertplus');
50      $this->Lexer->addEntryPattern('<ins [^>\r\n]+? @[^>\r\n]+?>(?=.*</ins>)', $mode, 'plugin_formatplus_blockinsertplus');
51      $this->Lexer->addEntryPattern('<ins [^>\r\n]+?>(?=.*</ins>)', $mode, 'plugin_formatplus_blockinsertplus');
52    }
53  }
54
55  function postConnect() {
56    if (!$this->disabled) {
57      $this->Lexer->addExitPattern('</ins>', 'plugin_formatplus_blockinsertplus');
58    }
59  }
60
61  function handle($match, $state, $pos, &$handler){
62    switch ($state) {
63      case DOKU_LEXER_ENTER:
64        $match = substr($match,4,-1);
65        if(!empty($match)){
66          if(preg_match("/^(.*) =(.*?) @(.*?)$/", $match, $matches)){
67            $output = array(trim($matches[1]),trim($matches[2]),trim($matches[3]));
68          }elseif(preg_match("/^(.*) @(.*?) =(.*?)$/", $match, $matches)){
69            $output = array(trim($matches[1]),trim($matches[3]),trim($matches[2]));
70          }elseif(preg_match("/^(.*) =(.*?)$/", $match, $matches)){
71            $output = array(trim($matches[1]),trim($matches[2]),'');
72          }elseif(preg_match("/^(.*) @(.*?)$/", $match, $matches)){
73            $output = array(trim($matches[1]),'',trim($matches[2]));
74          }else{
75            $output = array(trim($match),'','');
76          }
77        }else{
78          $output = array('','','');
79        }
80        break;
81      case DOKU_LEXER_EXIT:
82        $output = '';
83        break;
84      case DOKU_LEXER_UNMATCHED:
85        $output = $match;
86        break;
87    }
88    return array($state,$output);
89  }
90
91  function render($format, &$renderer, $data) {
92    list($state,$output) = $data;
93    if (substr($format,0,5) == 'xhtml'){
94      switch ($state) {
95        case DOKU_LEXER_ENTER:
96          $renderer->doc .= "<ins class=\"block\"";
97          if(!empty($output[0])){
98            $renderer->doc .= ' title="'.$renderer->_xmlEntities($output[0]).'"';
99          }
100          if(!empty($output[1])){
101            $renderer->doc .= ' cite="'.$renderer->_xmlEntities($output[1]).'"';
102          }
103          if(!empty($output[2])){
104            $renderer->doc .= ' datetime="'.$renderer->_xmlEntities($output[2]).'"';
105          }
106          $renderer->doc .= '><div class="inserted">'.DOKU_LF;
107          break;
108        case DOKU_LEXER_EXIT:
109          $renderer->doc .= DOKU_LF."</div></ins>".DOKU_LF;
110          break;
111        case DOKU_LEXER_UNMATCHED:
112          $renderer->doc .= $renderer->_xmlEntities($output);
113          break;
114      }
115      return true;
116    } elseif ($format == 'metadata') {
117      switch ($state) {
118        case DOKU_LEXER_ENTER:
119          $sp = '';
120          $line = '';
121          if(!empty($output[0])){
122            $line .= '['.$output[0].']';
123            $sp = ' ';
124          }
125          if(!empty($output[1])){
126            $line .= $sp.$output[1];
127            $sp = ' ';
128          }
129          if(!empty($output[2])){
130            $line .= $sp.$output[2];
131          }
132          if ($renderer->capture)
133            $renderer->doc .= $line;
134          break;
135        case DOKU_LEXER_EXIT:
136          if ($renderer->capture) {
137            $renderer->doc .= DOKU_LF;
138            if (strlen($renderer->doc) > 250) $renderer->capture = false;
139          }
140          break;
141        case DOKU_LEXER_UNMATCHED:
142          if ($renderer->capture) $renderer->doc .= $output;
143          break;
144      }
145      return true;
146    }
147    return false;
148  }
149
150}
151