DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:localdate

Definition Localdate

Compatible with DokuWiki

No compatibility info given!

plugin Display date/time in client's regional format

Last updated on
2005-07-27
Provides
Syntax

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Tagged with !experimental, date, time

experimental

Description

With this plugin the syntax of DokuWiki is extended to allow display of local date on client machine. The syntax to use this plugin is:

  <date>year,month,day</date>
  <date>year,month,day,hour,minute</date>

Plugin will use JavaScript to display date/time on client's side using regional date setting. For example using

<date>2005,7,16</date>

will display in some browser June 16th, 2005 00:00 or 05/07/16 00:00 AM on another, etc., depending on client's regional setting.

Plugin

Put the following PHP file in /lib/plugins/localdate/syntax.php.

<?php
/**
 * Local-date Plugin: outputs date in client's locale. Syntax is <date>year,month,day[,hour,minute]</date>,
 * output is something like "January 01th, 2005 00:00", or "05/01/01 03:45 PM" depending on user's regional settings.
 * Time is not mandatory. If not specified, '00:00' will be displayed.
 * Uses JavaScript function Date.toLocaleString().
 * Issues: It always outputs time as well, sometimes we don't want this. No solution yet.
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     pavel Vitis <pavel [dot] vitis [at] seznam [dot] cz>
 */
 
if(!defined('DOKU_INC'))
  define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN'))
  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_localdate extends DokuWiki_Syntax_Plugin {
 
    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Pavel Vitis',
            'email'  => 'pavel.vitis@seznam.cz',
            'date'   => '2005-07-27',
            'name'   => 'Local-date Plugin',
            'desc'   => 'Prints date in client\'s locale format',
            'url'    => 'http://www.dokuwiki.org/plugin:localdate',
        );
    }
 
    /**
     * Constructor - adds allowed modes
     */
    function syntax_plugin_localdate(){
        global $PARSER_MODES;
        /*
        $this->allowedModes = array_merge (
                $PARSER_MODES['substition']
            );
        */
    }
 
    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }
 
    /**
     * Where to sort in?
     */
    function getSort(){
        return 108;
    }
 
    /**
    * Connect pattern to lexer
    */
    function connectTo($mode) {
      $this->Lexer->addEntryPattern('<date>(?=.*</date>)',$mode,'plugin_localdate');
    }
 
    function postConnect() {
      $this->Lexer->addExitPattern('</date>','plugin_localdate');
    }
 
    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        return array($match, $state);
    }
 
    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml'){
            if ($data[1] == DOKU_LEXER_ENTER){
                $renderer->doc .= 
		    "\x3Cscript language=\"javascript\"\x3E\n".
		    "\x3C!--\n".
		    "document.write((new Date(";
            } else if ($data[1] == DOKU_LEXER_UNMATCHED){
        	$dateStr = trim($data[0]);
        	if (strlen($dateStr > 0)) {
        	    $elements = explode(",", $dateStr);
        	    if (count($elements)>2) {
                	$renderer->doc .= $elements[0]; //year
                	$renderer->doc .= ",".($elements[1]*1-1); //month
                	$renderer->doc .= ",".$elements[2]; //day
          		if (count($elements)>=5) {
                    	    $renderer->doc .= ",".$elements[3]; //hour
                    	    $renderer->doc .= ",".$elements[4]; //minute
          		}
            	    }
		}
            } else if ($data[1] == DOKU_LEXER_EXIT){
                $renderer->doc .= 
		    ")).toLocaleString());\n".
		    "//--\x3E\n".
		    "\x3C/script\x3E";
            }
            return true;
        }
        return false;
    }
 
}
 
//Setup VIM: ex: et ts=4 enc=utf-8 :
?>

Comments

You can see this in action on my site
Pavel Vitis

A couple of things. Are you sure this plugin is of type formatting, it seems more like protected or substition to me. Does the <date> syntax allow other DokuWiki markup to be nested within it? Your description at the top seems to say not, however your plugin says it allows other substition, formatting & disabled modes within it. That implies <date>**2005**,//9//,%%7%%</date> is legitimate markup. — ChrisS 2005-07-27
Yes that's right. I fixed it in above code. As well month has been displayed incorrectly, I forgot to subtract 1 from month, since JavaScript is numbering months starting from 0. Should be OK now. Thanks for your tip.
Pavel Vitis
You have left 'substition' as an allowed mode type. I don't think there should be any. If there is any markup for a 'substition' mode (e.g. smiley or acronym) your plugin won't get to see that data. Your plugin won't get to see the data that belongs to any other mode, not before or after that mode processes it. — ChrisS 2005-07-28
plugin/localdate.txt · Last modified: 2013-03-06 15:44 by Klap-in