Skip to content

Commit

Permalink
export feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymon Olewniczak committed May 24, 2013
1 parent 32cbfd6 commit 7f1fa47
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 16 deletions.
93 changes: 93 additions & 0 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,99 @@ class action_plugin_dtable extends DokuWiki_Action_Plugin {
function register(&$controller) {
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'add_php_data');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
$controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'export_dtable');
}
function export_dtable(&$event, $parm)
{
$lines = explode("\n", $event->data[0][1]);
$new_lines = array();
foreach($lines as $line)
{
if(preg_match('/^\[export[\s]+([^\s]+[\s]+dtable|dtable)/', $line))
{

//remove [export
$line = substr($line, 7);
$line = trim($line);

$dtable_str = $line;

$dtable_str = substr($dtable_str, strpos($dtable_str, 'dtable')+7);
$dtable_str = substr($dtable_str, 0, -1);//remove ]

//leave [dtable as code
$new_lines[] = ' [dtable '.$dtable_str.']';

$h_dtable =& plugin_load('helper', 'dtable');
$data = $h_dtable->syntax_parse($dtable_str);

if(strpos($line, 'exttab2') === 0)
{
$new_lines[] = '';
$new_lines[] = '{|';
$new_lines[] = '|+';
foreach($data['fileds']['all'] as $head)
{
$new_lines[] = '!'.$head;
}

$baza = $h_dtable->db_path($data['file']);
$rows = file($baza);
$new_row = '';
foreach($rows as $row)
{
$new_lines[] = '|-';
$new_row = '';
//remove last \n
$row = substr($row, 0, -1);
$row = str_replace('<br>', '\\', $row);
$dane = explode($h_dtable->separator(), $row);

for($i=1;$i<sizeof($dane);$i++)
{
$new_lines[] = '|'.$dane[$i];
}

}
$new_lines[] = '|}';
} else
{
$new_lines[] = '';
$header = '';
foreach($data['fileds']['all'] as $head)
{
$header .= '^'.$head;
}
$header .= '^';
$new_lines[] = $header;

$baza = $h_dtable->db_path($data['file']);
$rows = file($baza);
$new_row = '';
foreach($rows as $row)
{
$new_row = '';
//remove last \n
$row = substr($row, 0, -1);
$row = str_replace('<br>', '', $row);
$dane = explode($h_dtable->separator(), $row);
for($i=1;$i<sizeof($dane);$i++)
{
if(strlen($dane[$i]) <= 0)
$new_row .= '| ';
else
$new_row .= '|'.$dane[$i];
}

$new_lines[] = $new_row.'|';
}
}
} else
{
$new_lines[] = $line;
}
}
$event->data[0][1] = implode("\n", $new_lines);
}

function add_php_data(&$event, $param) {
Expand Down
26 changes: 26 additions & 0 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ function getMethods(){
'params' => array('string' => 'string'),
'return' => array('content' => 'string'),
);
$result[] = array(
'name' => 'syntax_parse',
'desc' => 'save [dtable ] syntax to array. match -> without [dtable ]',
'params' => array('match' => 'string'),
'return' => array('parsed' => 'array'),
);
return $result;
}
function md5_array($array)
Expand Down Expand Up @@ -119,6 +125,26 @@ function parse($string)
$r_str = str_replace('<br>', "\n", str_replace($this->separator_en(), $this->separator(), $string));
return p_render('xhtml',p_get_instructions($r_str),$info);
}
function syntax_parse($match)
{
$special_cols = array('date');

$exploded = explode(' ', $match);
$file = $exploded[0];
preg_match('/"(.*?)"/', $match, $res);
$fileds = array();
preg_match_all('/[[:alnum:]]*\(.*?\)/', $res[1], $fileds_raw);
foreach($fileds_raw[0] as $filed)
{
preg_match('/(.*?)\((.*?)\)/', $filed, $res2);
if(in_array($res2[1], $special_cols))
{
$fileds[$res2[1]][] = $res2[2];
}
$fileds['all'][] = $res2[2];
}
return array('file' => $file, 'fileds' => $fileds);
}
/* function getMethods(){
$result = array();
$result[] = array(
Expand Down
22 changes: 6 additions & 16 deletions syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,14 @@ function connectTo($mode) {
}

function handle($match, $state, $pos, &$handler) {
//remove [dtable
$match = substr($match, 7);
$match = substr($match,0, -1);
$match = trim($match);

$special_cols = array('date');
$dtable =& plugin_load('helper', 'dtable');
return $dtable->syntax_parse($match);

$exploded = explode(' ', $match);
$file = $exploded[1];
preg_match('/"(.*?)"/', $match, $res);
$fileds = array();
preg_match_all('/[[:alnum:]]*\(.*?\)/', $res[1], $fileds_raw);
foreach($fileds_raw[0] as $filed)
{
preg_match('/(.*?)\((.*?)\)/', $filed, $res2);
if(in_array($res2[1], $special_cols))
{
$fileds[$res2[1]][] = $res2[2];
}
$fileds['all'][] = $res2[2];
}
return array('file' => $file, 'fileds' => $fileds);
}

function render($mode, &$renderer, $data) {
Expand Down

0 comments on commit 7f1fa47

Please sign in to comment.