diff --git a/README.md b/README.md index f2b944b..bb12415 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,10 @@ This plugin uses the [SpaceAPI](http://spaceapi.net/) of your hackerspace to dis ## Install -The best way is to put [wiyh] into the page :sidebar, but any other place is fine too. +The best way is to put `[wiyh 'hackerspace API path/url here']` into the page :sidebar, but any other place is fine too. + +Else you can edit the `dokuwiki/lib/plugins/whoisinyourhackspace/conf/default.php` and put the API path in +`$conf['api_path'] = '';` and then just put `[wiyh]` in any page. If you install this plugin manually, make sure it is installed in lib/plugins/whoisinyourhackspace/ - if the folder is called different it diff --git a/lang/de/lang.php b/lang/de/lang.php index 89583ed..288ad60 100644 --- a/lang/de/lang.php +++ b/lang/de/lang.php @@ -10,6 +10,8 @@ $lang['wiyh_heading'] = 'Raumstatus'; $lang['wiyh_open'] = 'ist geöffnet.'; $lang['wiyh_closed'] = 'ist geschlossen.'; $lang['wiyh_stats'] = 'Raumöffnungsstatistiken'; +$lang['wiyh_no_api_path'] = 'Es wurde kein API-Pfad angegeben.'; +$lang['wiyh_fetch_error'] = 'Der API-Pfad konnte nicht abgerufen werden.'; diff --git a/lang/en/lang.php b/lang/en/lang.php index d7de60a..7e72678 100644 --- a/lang/en/lang.php +++ b/lang/en/lang.php @@ -10,7 +10,8 @@ $lang['wiyh_heading'] = 'State'; $lang['wiyh_open'] = 'is open.'; $lang['wiyh_closed'] = 'is closed.'; $lang['wiyh_stats'] = 'Stats'; - +$lang['wiyh_no_api_path'] = 'No API path was given.'; +$lang['wiyh_fetch_error'] = 'Could not fetch API path.'; //Setup VIM: ex: et ts=4 : diff --git a/syntax.php b/syntax.php index 669082b..452086f 100644 --- a/syntax.php +++ b/syntax.php @@ -16,19 +16,6 @@ if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once DOKU_PLUGIN.'syntax.php'; class syntax_plugin_whoisinyourhackspace extends DokuWiki_Syntax_Plugin { - /** - * Check if a given option has been given, and remove it from the initial string - * @param string $match The string match by the plugin - * @param string $pattern The pattern which activate the option - * @param $varAffected The variable which will memorise the option - * @param $valIfFound the value affected to the previous variable if the option is found - */ - private function _checkOption(&$match, $pattern, &$varAffected, $valIfFound){ - if ( preg_match($pattern, $match, $found) ){ - $varAffected = $valIfFound; - $match = str_replace($found[0], '', $match); - } - } // _checkOption public function getType() { return 'substition'; @@ -42,46 +29,71 @@ class syntax_plugin_whoisinyourhackspace extends DokuWiki_Syntax_Plugin { return 0; } - public function connectTo($mode) { - $this->Lexer->addSpecialPattern('\[wiyh\]',$mode,'plugin_whoisinyourhackspace'); + $this->Lexer->addSpecialPattern("(?:\[wiyh '.*?'\]|\[wiyh\])", $mode,'plugin_whoisinyourhackspace'); } - public function render($mode, &$renderer, $data) { - global $conf; + public function handle($match, $state, $pos, Doku_Handler $handler) + { + if (DOKU_LEXER_SPECIAL !== $state) { + return []; + } - if($mode != 'xhtml') return false; + $matches = []; - $api_path = $this->getConf('api_path'); + if (false !== preg_match("/wiyh '(.*?)'/", $match, $matches)) { + return [$state, $match, $matches[1] ?? null]; + } - $file = file_get_contents($api_path); + return [$state, $match]; + } - $api = json_decode($file); + public function render($format, Doku_Renderer $renderer, $data) { + global $conf; - $content = ''; - $content .= '
'; - $content .= "

" . $this->getLang('wiyh_heading') . "

"; + [$state, $match, $url] = $data; - if ($api->state->open) { - $content .= "state->icon->open}\" alt=\"{$api->space} ist besetzt.\" title=\"{$api->space} ist besetzt.\" />"; - $content .= "

{$api->space} " . $this->getLang('wiyh_open') . "

"; - } else { - $content .= "state->icon->closed}\" alt=\"{$api->space} ist geschlossen.\" title=\"{$api->space} ist geschlossen.\" />"; - $content .= "

{$api->space} " . $this->getLang('wiyh_closed') . "

"; - } + if($format != 'xhtml') return false; - $date = new DateTime(); - $date->setTimeStamp($api->lastchange); - $content .= "

seit " . $date->format('d.m.Y H:i') . " Uhr

"; + $api_path = $url ?: $this->getConf('api_path'); - $content .= '
'; + if (empty ($api_path)) { + $renderer->doc .= "
{$this->getLang('wiyh_no_api_path')}
"; - $content .= sprintf('

'.$this->getLang('wiyh_stats').'

',strtolower($api->space)); - $content .= '
'; - - $renderer->doc .= $content; return true; - } + } + + if (false === $file = file_get_contents($api_path)) { + $renderer->doc .= "
{$this->getLang('wiyh_fetch_error')}
"; + + return true; + } + + $api = json_decode($file); + + $content = ''; + $content .= '
'; + $content .= "

" . $this->getLang('wiyh_heading') . "

"; + + if ($api->state->open) { + $content .= $api->state->icon->open ? "state->icon->open}\" alt=\"{$api->space} ist besetzt.\" title=\"{$api->space} ist besetzt.\" />" : ''; + $content .= "

{$api->space} " . $this->getLang('wiyh_open') . "

"; + } else { + $content .= $api->state->icon->open ? "state->icon->closed}\" alt=\"{$api->space} ist geschlossen.\" title=\"{$api->space} ist geschlossen.\" />" : ''; + $content .= "

{$api->space} " . $this->getLang('wiyh_closed') . "

"; + } + + $date = new DateTime(); + $date->setTimeStamp($api->state->lastchange); + $content .= "

seit " . $date->format('d.m.Y H:i') . " Uhr

"; + + $content .= '
'; + $content .= '
'; + + $renderer->doc .= $content; + + return true; + } } // vim:ts=4:sw=4:et: