1<?php
2/**
3 * datefilter Plugin: Filters lines if a date is found which is in the past - useful for simple calendars
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Michael Arlt <michael.arlt [at] sk-schwanstetten [dot] de>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class syntax_plugin_datefilter extends DokuWiki_Syntax_Plugin {
18
19    /**
20     * function constructor
21     */
22    function syntax_plugin_datefilter(){
23      // enable direct access to language strings
24      $this->setupLocale();
25    }
26
27    /**
28     * return some info
29     */
30    function getInfo(){
31        return array(
32            'author' => 'Michael Arlt',
33            'email'  => 'michael.arlt@sk-schwanstetten.de',
34            'date'   => '2020-06-04',
35            'name'   => 'datefilter Plugin',
36            'desc'   => 'Filters lines if a date is found which is in the past - useful for simple calendars',
37            'url'    => 'http://wiki.splitbrain.org/plugin:datefilter',
38        );
39    }
40
41    /**
42     * What kind of syntax are we?
43     */
44    function getType(){
45        return 'formatting';
46    }
47
48function getPType(){
49        return 'normal';
50    }
51
52    /**
53     * What modes are allowed within our mode?
54     */
55    function getAllowedTypes() {
56        return array('substition','protected','disabled','formatting');
57    }
58
59    /**
60     * Where to sort in?
61     */
62    function getSort(){
63        return 1;
64    }
65
66    function connectTo($mode) {
67      $this->Lexer->addSpecialPattern('<'.$this->lang["kwpattern"].'.*?>.*?</'.$this->lang["kwpattern"].'>',$mode,'plugin_datefilter');
68    }
69
70    /**
71     * Handle the match
72     */
73    function handle($match, $state, $pos, Doku_Handler $handler)
74    {
75      $length=strlen($this->lang["kwpattern"])+2;
76      $end=strpos($match,">");
77      if($end > $length)
78      {
79        $format=substr($match,$length,$end-$length);
80        $error='explicit';
81      }
82      else
83      {
84        $format=$this->getConf('default');
85        $error='default';
86        if($format=="")
87        {
88          $format=$this->lang["default"];
89          $error='language specific default';
90        }
91      }
92      $error='error: '.$error.' datefilter without ';
93      $yearpos=strpos($format,$this->lang["year"]);
94      if($yearpos===false || substr_count($format,$this->lang["year"])!=2) {$error.='year ';$errormode=true;}
95      $monthpos=strpos($format,$this->lang["month"]);
96      if($monthpos===false || substr_count($format,$this->lang["month"])!=2) {$error.='month ';$errormode=true;}
97      $daypos=strpos($format,$this->lang["day"]);
98      if($daypos===false || substr_count($format,$this->lang["day"])!=2) {$error.='day ';$errormode=true;}
99      if($errormode)
100      {
101        $error.="(e.g. not 2-stellig)";
102        return array($error);
103      }
104      $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]))","."=>"\.","/"=>"\/"));
105      $nowdate=strftime("%y%m%d");
106      foreach (preg_split('/\r\n|\r|\n/',substr($match,$end+1,-$length-1)) as $line)
107      {
108        if(preg_match("/$filter/",$line,$date))
109        {
110          $checkdate=substr($date[0],$yearpos,2).substr($date[0],$monthpos,2).substr($date[0],$daypos,2);
111          if($checkdate>=$nowdate) { $lines.=$line."\r\n"; }
112        }
113        else
114        {
115          $lines.=$line."\r\n";
116        }
117      }
118      return array($lines);
119    }
120
121    /**
122     * Create output
123     */
124    function render($mode, Doku_Renderer $renderer, $data) {
125        if($mode == 'xhtml'){
126          $renderer->doc .= p_render('xhtml',p_get_instructions($data[0]),$info);
127          return true;
128        }
129        return false;
130    }
131}
132
133//Setup VIM: ex: et ts=4 enc=utf-8 :
134