<?php
/**
 * datefilter Plugin: Filters lines if a date is found which is in the past - useful for simple calendars
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Michael Arlt <michael.arlt [at] sk-schwanstetten [dot] de>
 */

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_datefilter extends DokuWiki_Syntax_Plugin {

    /**
     * function constructor
     */
    function syntax_plugin_datefilter(){
      // enable direct access to language strings
      $this->setupLocale();
    }

    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Michael Arlt',
            'email'  => 'michael.arlt@sk-schwanstetten.de',
            'date'   => '2020-06-04',
            'name'   => 'datefilter Plugin',
            'desc'   => 'Filters lines if a date is found which is in the past - useful for simple calendars',
            'url'    => 'http://wiki.splitbrain.org/plugin:datefilter',
        );
    }

    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'formatting';
    }

function getPType(){
        return 'normal';
    }

    /**
     * What modes are allowed within our mode?
     */
    function getAllowedTypes() {
        return array('substition','protected','disabled','formatting');
    }

    /**
     * Where to sort in?
     */
    function getSort(){
        return 1;
    }

    function connectTo($mode) {
      $this->Lexer->addSpecialPattern('<'.$this->lang["kwpattern"].'.*?>.*?</'.$this->lang["kwpattern"].'>',$mode,'plugin_datefilter');
    }

    /**
     * Handle the match
     */
    function handle($match, $state, $pos, Doku_Handler $handler)
    {
      $length=strlen($this->lang["kwpattern"])+2;
      $end=strpos($match,">");
      if($end > $length)
      {
        $format=substr($match,$length,$end-$length);
        $error='explicit';
      }
      else
      {
        $format=$this->getConf('default');
        $error='default';
        if($format=="")
        {
          $format=$this->lang["default"];
          $error='language specific default';
        }
      }
      $error='error: '.$error.' datefilter without ';
      $yearpos=strpos($format,$this->lang["year"]);
      if($yearpos===false || substr_count($format,$this->lang["year"])!=2) {$error.='year ';$errormode=true;}
      $monthpos=strpos($format,$this->lang["month"]);
      if($monthpos===false || substr_count($format,$this->lang["month"])!=2) {$error.='month ';$errormode=true;}
      $daypos=strpos($format,$this->lang["day"]);
      if($daypos===false || substr_count($format,$this->lang["day"])!=2) {$error.='day ';$errormode=true;}
      if($errormode)
      {
        $error.="(e.g. not 2-stellig)";
        return array($error);
      }
      $filter=strtr($format,array($this->lang["year"].$this->lang["year"]=>"[0-9][0-9]",$this->lang["month"].$this->lang["month"]=>"((0[1-9])|(1[0-2]))",$this->lang["day"].$this->lang["day"]=>"((0[1-9])|([1-2][0-9])|(3[0-1]))","."=>"\.","/"=>"\/"));
      $nowdate=strftime("%y%m%d");
      foreach (preg_split('/\r\n|\r|\n/',substr($match,$end+1,-$length-1)) as $line)
      {
        if(preg_match("/$filter/",$line,$date))
        {
          $checkdate=substr($date[0],$yearpos,2).substr($date[0],$monthpos,2).substr($date[0],$daypos,2);
          if($checkdate>=$nowdate) { $lines.=$line."\r\n"; }
        }
        else
        {
          $lines.=$line."\r\n";
        }
      }
      return array($lines);
    }

    /**
     * Create output
     */
    function render($mode, Doku_Renderer $renderer, $data) {
        if($mode == 'xhtml'){
          $renderer->doc .= p_render('xhtml',p_get_instructions($data[0]),$info);
          return true;
        }
        return false;
    }
}

//Setup VIM: ex: et ts=4 enc=utf-8 :
