Skip to content

Commit

Permalink
Merge pull request #10 from lpaulsen93/master
Browse files Browse the repository at this point in the history
Fix and cleanup
  • Loading branch information
Klap-in authored Oct 16, 2021
2 parents 8752219 + c37c776 commit 3a1b04e
Showing 1 changed file with 82 additions and 45 deletions.
127 changes: 82 additions & 45 deletions syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,41 @@
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_meta extends DokuWiki_Syntax_Plugin {
function getType() { return 'substition'; }
function getSort() { return 99; }
function connectTo($mode) { $this->Lexer->addSpecialPattern('~~META:.*?~~',$mode,'plugin_meta');}
class syntax_plugin_meta extends DokuWiki_Syntax_Plugin
{
function getType()
{
return 'substition';
}

function getSort()
{
return 99;
}

function connectTo($mode)
{
$this->Lexer->addSpecialPattern('~~META:.*?~~',$mode,'plugin_meta');
}

/**
* Handle the match
*/
function handle($match, $state, $pos, Doku_Handler $handler){
$match = substr($match,7,-2); //strip ~~META: from start and ~~ from end
public function handle($match, $state, $pos, Doku_Handler $handler)
{
// strip ~~META: from start and ~~ from end
$match = substr($match,7,-2);

$data = array();
$pairs = explode('&', $match);
foreach ($pairs as $pair) {
list($key, $value) = explode('=', $pair, 2);
list($key, $subkey) = explode(' ', $key, 2);
if (trim($subkey)) $data[trim($key)][trim($subkey)] = trim($value);
else $data[trim($key)] = trim($value);
if (trim($subkey)) {
$data[trim($key)][trim($subkey)] = trim($value);
} else {
$data[trim($key)] = trim($value);
}
}
$data = array_change_key_case($data, CASE_LOWER);

Expand All @@ -40,19 +57,24 @@ function handle($match, $state, $pos, Doku_Handler $handler){
/**
* Create output
*/
function render($mode, Doku_Renderer $renderer, $data) {
public function render($mode, Doku_Renderer $renderer, $data)
{
if ($mode == 'xthml') {
return true; // don't output anything
// don't output anything
return true;
} elseif ($mode == 'metadata') {
/** @var Doku_Renderer_metadata $renderer */

// do some validation / conversion for date metadata
if (isset($data['date'])) {
if (is_array($data['date'])) {
foreach ($data['date'] as $key => $date) {
$date = $this->_convertDate(trim($date));
if (!$date) unset($data['date'][$key]);
else $data['date'][$key] = $date;
$date = $this->convertDate(trim($date));
if (!$date) {
unset($data['date'][$key]);
} else {
$data['date'][$key] = $date;
}
}
} else {
unset($data['date']);
Expand All @@ -73,21 +95,20 @@ function render($mode, Doku_Renderer $renderer, $data) {
Therefore the only thing we can do is setting this internal value by calling _firstimage.
This fails if there has already been a first image saved. */
$renderer->_firstimage($subvalue);
} else { // for everything else assume that we have a page id
} else {
// for everything else assume that we have a page id
$renderer->meta[$key][$subkey][cleanID($subvalue)] = page_exists($subvalue);
}
}

// be careful with some senisitive arrays of $meta
} elseif (in_array($key, $protected)) {
if (array_key_exists($key, $renderer->meta)) {
$renderer->meta[$key] = array_merge($renderer->meta[$key], $value);
// be careful with some sensitive arrays of $meta
if (is_array($renderer->meta) && array_key_exists($key, $renderer->meta)) {
$renderer->meta[$key] = array_merge($renderer->meta[$key], array($value));
} else {
$renderer->meta[$key] = $value;
}

// no special treatment for the rest
} else {
// no special treatment for the rest
$renderer->meta[$key] = $value;
}
}
Expand All @@ -97,69 +118,85 @@ function render($mode, Doku_Renderer $renderer, $data) {
/**
* converts YYYY-MM-DD[ hh:mm:ss][ -> [YYYY-MM-DD ]hh:mm:ss] to PHP timestamps
*/
function _convertDate($date) {
private function convertDate($date)
{
list($start, $end) = explode('->', $date, 2);

// single date
if (!$end) {
// single date
list($date, $time) = explode(' ', trim($start), 2);
if (!preg_match('/\d{4}\-\d{2}\-\d{2}/', $date)) return false;
$time = $this->_autocompleteTime($time);
if (!preg_match('/\d{4}\-\d{2}\-\d{2}/', $date)) {
return false;
}
$time = $this->autocompleteTime($time);
return strtotime($date.' '.$time);

// duration
} else {
// duration

// start
list($startdate, $starttime) = explode(' ', trim($start), 2);
$startdate = $this->_autocompleteDate($startdate);
if (!$startdate) return false;
$starttime = $this->_autocompleteTime($starttime);
$startdate = $this->autocompleteDate($startdate);
if (!$startdate) {
return false;
}
$starttime = $this->autocompleteTime($starttime);

// end
list($enddate, $endtime) = explode(' ', trim($end), 2);
if (!trim($endtime)) { // only time given
$end_date = $this->_autocompleteDate($enddate, true);
if (!trim($endtime)) {
// only time given
$end_date = $this->autocompleteDate($enddate, true);
if (!$end_date) {
$endtime = $this->_autocompleteTime($enddate, true);
$endtime = $this->autocompleteTime($enddate, true);
$enddate = $startdate;
} else { // only date given
} else {
// only date given
$enddate = $end_date;
$endtime = '23:59:59';
}
} else {
$enddate = $this->_autocompleteDate($enddate, true);
if (!$enddate) $enddate = $startdate;
$endtime = $this->_autocompleteTime($endtime, true);
$enddate = $this->autocompleteDate($enddate, true);
if (!$enddate) {
$enddate = $startdate;
}
$endtime = $this->autocompleteTime($endtime, true);
}

$start = strtotime($startdate.' '.$starttime);
$end = strtotime($enddate.' '.$endtime);
if (!$start || !$end) return false;
if (!$start || !$end) {
return false;
}
return array('start' => $start, 'end' => $end);
}
}

function _autocompleteDate($date, $end=false) {
private function autocompleteDate($date, $end=false)
{
if (!preg_match('/^\d{4}\-\d{2}\-\d{2}$/', $date)) {
if (preg_match('/^\d{4}\-\d{2}$/', $date))
if (preg_match('/^\d{4}\-\d{2}$/', $date)) {
// we don't know which month
return ($end) ? $date.'-28' : $date.'-01';
elseif (preg_match('/^\d{4}$/', $date))
} elseif (preg_match('/^\d{4}$/', $date)) {
return ($end) ? $date.'-12-31' : $date.'-01-01';
else return false;
} else {
return false;
}
} else {
return $date;
}
}

function _autocompleteTime($time, $end=false) {
private function autocompleteTime($time, $end=false)
{
if (!preg_match('/^\d{2}:\d{2}:\d{2}$/', $time)) {
if (preg_match('/^\d{2}:\d{2}$/', $time))
if (preg_match('/^\d{2}:\d{2}$/', $time)) {
return ($end) ? $time.':59' : $time.':00';
elseif (preg_match('/^\d{2}$/', $time))
} elseif (preg_match('/^\d{2}$/', $time)) {
return ($end) ? $time.':59:59': $time.':00:00';
else return ($end) ? '23:59:59' : '00:00:00';
} else {
return ($end) ? '23:59:59' : '00:00:00';
}
} else {
return $time;
}
Expand Down

0 comments on commit 3a1b04e

Please sign in to comment.