source: documentation/trunk/packages/dokuwiki-2011-05-25a/lib/plugins/tablewidth/action.php@ 30098

Last change on this file since 30098 was 25027, checked in by jmt12, 12 years ago

Adding the packages directory, and within it a configured version of dokuwiki all ready to run

  • Property svn:executable set to *
File size: 2.8 KB
Line 
1<?php
2
3/**
4 * Plugin TableWidth
5 *
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Mykola Ostrovskyy <[email protected]>
8 */
9
10/* Must be run within Dokuwiki */
11if(!defined('DOKU_INC')) die();
12
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
14require_once(DOKU_PLUGIN . 'action.php');
15
16class action_plugin_tablewidth extends DokuWiki_Action_Plugin {
17
18 /**
19 * Register callbacks
20 */
21 function register(&$controller) {
22 $controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, 'replaceComments');
23 }
24
25 /**
26 * Replace table-width comments by HTML
27 */
28 function replaceComments(&$event, $param) {
29 if ($event->data[0] == 'xhtml') {
30 $pattern = '/(<!-- table-width [^\n]+? -->\n)([^\n]*<table.*?>)(\s*<t)/';
31 $flags = PREG_SET_ORDER | PREG_OFFSET_CAPTURE;
32 if (preg_match_all($pattern, $event->data[1], $match, $flags) > 0) {
33 $start = 0;
34 $html = '';
35 foreach ($match as $data) {
36 $html .= substr($event->data[1], $start, $data[0][1] - $start);
37 $html .= $this->_processTable($data);
38 $start = $data[0][1] + strlen($data[0][0]);
39 }
40 $event->data[1] = $html . substr($event->data[1], $start);;
41 }
42 }
43 }
44
45 /**
46 * Convert table-width comments and table mark-up into final HTML
47 */
48 function _processTable($data) {
49 preg_match('/<!-- table-width ([^\n]+?) -->/', $data[1][0], $match);
50 $width = preg_split('/\s+/', $match[1]);
51 $tableWidth = array_shift($width);
52 if ($tableWidth != '-') {
53 $table = $this->_styleTable($data[2][0], $tableWidth);
54 }
55 else {
56 $table = $data[2][0];
57 }
58 return $table . $this->_renderColumns($width) . $data[3][0];
59 }
60
61 /**
62 * Add width style to the table
63 */
64 function _styleTable($html, $width) {
65 preg_match('/^([^\n]*<table)(.*?)(>)$/', $html, $match);
66 $entry = $match[1];
67 $attributes = $match[2];
68 $exit = $match[3];
69 if (preg_match('/(.*?style\s*=\s*(["\']).*?)(\2.*)/', $attributes, $match) == 1) {
70 $attributes = $match[1] . ';width: ' . $width . ';' . $match[3];
71 }
72 else {
73 $attributes .= ' style="width: ' . $width . ';"';
74 }
75 return $entry . $attributes . $exit;
76 }
77
78 /**
79 * Render column tags
80 */
81 function _renderColumns($width) {
82 $html = DOKU_LF;
83 foreach ($width as $w) {
84 if ($w != '-') {
85 $html .= '<col style="width: ' . $w . '" />';
86 }
87 else {
88 $html .= '<col />';
89 }
90 }
91 return $html;
92 }
93}
Note: See TracBrowser for help on using the repository browser.