1<?php
2/**
3 * Plugin Block:  Create sized boxes with borders,font and color selection, and screen positoning
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Myron Turner <turnermm02@shaw.ca>
7 */
8
9
10
11// must be run within Dokuwiki
12if(!defined('DOKU_INC')) die();
13
14if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15require_once(DOKU_PLUGIN.'syntax.php');
16
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21
22/*
23 *  <block width:alignment:background-color,text-color;border;font-family/font-size>
24 *   text
25 *  </block>
26 *   width:  percent of window size
27 *   align:  number of pixels from left margin, (0 or 'c' centers block), 'r' floats right
28 *   border: CSS style: width type color
29 *   font-family:  font name
30 *   font size: specify points or pixels
31 *
32 *   Example:
33 *  <block 80:0:rgb(102, 51, 255);rgb(255, 255, 153);2px dashed rgb(255, 102, 255);Comic Sans MS /10pt>
34 *
35 *  This creates a block 80% of page width,centered on page, background of block is rgb(102, 51, 255),
36 *  text-color is rgb(255, 255, 153); the box border is 2px wide, dashed, its color is rgb(255, 102, 255).
37 *  The font is 10pt Comic Sans MS.
38 *
39 *  Only width and alignment are required.  In this case, if alignment is 'c' or 'r',
40 *  then you must follow it with a colon:  'c:'
41*/
42
43class syntax_plugin_block extends DokuWiki_Syntax_Plugin {
44
45    var $edit = false;
46
47    function getType(){ return 'formatting'; }
48    function getAllowedTypes() { return array('formatting','baseonly', 'substition','container', 'paragraphs'); }
49    function getSort(){ return 160; }
50    function connectTo($mode) { $this->Lexer->addEntryPattern('<block.*?>(?=.*?</block>)',$mode,'plugin_block'); }
51    function postConnect() { $this->Lexer->addExitPattern('</block>','plugin_block'); }
52    function getPType() {  return "stack"; }
53
54    /**
55     * Handle the match
56     */
57    function handle($match, $state, $pos, Doku_Handler $handler){
58
59        $padding = 0;
60        $width = "100%";
61        $margin = ' margin: auto ';
62        $bgcolor = 'white';
63        $text_color = "";
64        $border = "";
65        $font = "";
66        $face = "";
67        $font_size = "";
68        $rounded_corners = " class='blocks_round' ";
69		$class = "";
70        global $INPUT;
71        $this->edit = $INPUT->str('edit');
72
73        switch ($state) {
74          case DOKU_LEXER_ENTER :
75
76                list($type, $val) = preg_split("/\s+/u", substr($match, 1, -1), 2);
77
78                if(!isset($type)) return array($state, '');
79                if(!isset($val)) {
80                      $this->edit = false;
81                      return array($state, '<div>');
82                }
83				if(preg_match("/rounded\s*$/",$val)) {
84					$val = preg_replace("/rounded\s*$/","",$val);
85					$class = $rounded_corners;
86				}
87
88                if(preg_match('/(\d+)\%?:(\d+|r|c):(.*)/',$val, $matches)) {
89                   $width =   $matches[1] .'%';
90
91                   if($matches[2] == 'r') {
92                        $margin = ' float: right ';
93                   }
94
95                   elseif($matches[2]> 0) {
96                        $margin = ' margin-left: ' . $matches[2] . 'px';
97                   }
98                   $color = $matches[3];
99
100                   if($color) {
101                        list($bg,$fg,$border,$font) = explode(';',$color);
102                        if($bg) $bgcolor = "$bg;";
103                        if($fg) $text_color = "color: $fg;";
104                        if($border) $border = "border: $border;";
105                        if($font) {
106                          list($face,$size) = explode('/',$font);
107                          if($face) {
108                             $face = " font-family: $face; ";
109                          }
110                          if($size) {
111                             $font_size =" font-size: $size; ";
112                          }
113                          $font = $face . $font_size;
114                        }
115                   }
116                }
117                elseif(preg_match('/(\d+)\%?:(\d+)/',$val, $matches)) {
118                   $width = $matches[1] .'%';
119                   if($matches[2]> 0) $margin = ' margin-left: ' . $matches[2] . 'px';
120               }
121
122
123                  if($this->edit){
124                    return array($state,
125                     "<blockquote $class style='$border width:$width;display:block;padding:8px; $margin;"
126                     . "  background-color:$bgcolor $text_color $font'>" );
127                  }
128
129                  return array($state,
130                     "<div $class style='$border width:$width; padding:8px; $margin;"
131                     . "  background-color:$bgcolor $text_color $font'>");
132
133
134          return array($state, $match);
135
136          case DOKU_LEXER_UNMATCHED :  return array($state, $match);
137          case DOKU_LEXER_EXIT :       return array($state,$match);
138        }
139        return array();
140    }
141
142    /**
143     * Create output
144     */
145    function render($mode, Doku_Renderer $renderer, $data) {
146        if($mode == 'xhtml'){
147            list($state, $match) = $data;
148
149            switch ($state) {
150              case DOKU_LEXER_ENTER :
151
152                $renderer->doc .= $match;
153                break;
154
155              case DOKU_LEXER_UNMATCHED :  $renderer->doc .= $renderer->_xmlEntities($match); break;
156              case DOKU_LEXER_EXIT :
157                      if($this->edit){
158                         $renderer->doc .= "</blockquote></br>";
159                      }
160                      else {
161                         $renderer->doc .= "</div><div  style=' padding: 4px; clear:both;'></div>";
162                      }
163                       break;
164            }
165            return true;
166        }
167        return false;
168    }
169
170     function _isValid($c) {
171        $c = trim($c);
172
173        $pattern = "/
174            ([a-zA-z]+)|                                #colorname - not verified
175            (\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))|        #colorvalue
176            (rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\))     #rgb triplet
177            /x";
178
179        if (preg_match($pattern, $c)) return $c;
180
181        return "";
182    }
183
184function write_debug($data) {
185
186  if (!$handle = fopen('block_check.txt', 'a')) {
187    return;
188    }
189
190    // Write $somecontent to our opened file.
191    fwrite($handle, "$data\n");
192    fclose($handle);
193
194}
195}
196?>
197