1<?php
2/**
3 * PDF-Tools
4 *
5 * @license    MIT
6 * @author     Gero Gothe <practical@medizin-lernen.de>
7 */
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_pdftools extends DokuWiki_Syntax_Plugin {
19
20    private $dw2pdf_inst = false;
21
22    function getType(){return 'substition';}
23
24    function getSort(){return 59;}
25
26  	# Checks if dw2pdf is installed/activated
27  	function __construct() {
28  		$list = plugin_list();
29  		if(in_array('dw2pdf',$list)) {
30  			$this->dw2pdf_inst = true;
31  		}
32  	}
33
34    # Returns list of installed dw2pdf-templates
35    function templateList(){
36        if ($this->dw2pdf_inst) {
37
38            $path = DOKU_PLUGIN."/dw2pdf/tpl/";
39
40            $dirs = array();
41
42            // directory handle
43            $dir = dir($path);
44
45            while (false !== ($entry = $dir->read())) {
46                if ($entry != '.' && $entry != '..') {
47                    if (is_dir($path . '/' .$entry)) {
48                        $dirs[] = $entry;
49                    }
50                }
51            }
52
53            return $dirs;
54        }
55        return false;
56    }
57
58    # Add patterns
59    function connectTo($mode) {
60        $this->Lexer->addEntryPattern('<pdf (?=.*?>)',$mode,'plugin_pdftools');
61        $this->Lexer->addSpecialPattern('<etikett>',$mode,'plugin_pdftools');
62		$this->Lexer->addSpecialPattern('<abstand1>',$mode,'plugin_pdftools');
63		$this->Lexer->addSpecialPattern('<abstand2>',$mode,'plugin_pdftools');
64		$this->Lexer->addSpecialPattern('<abstand3>',$mode,'plugin_pdftools');
65		$this->Lexer->addSpecialPattern('<quer1>',$mode,'plugin_pdftools');
66		$this->Lexer->addSpecialPattern('<quer2>',$mode,'plugin_pdftools');
67		$this->Lexer->addSpecialPattern('<quer3>',$mode,'plugin_pdftools');
68    }
69
70
71    function postConnect() {
72      $this->Lexer->addExitPattern('>','plugin_pdftools');
73    }
74
75
76    /* Handle the match */
77    function handle($match, $state, $pos, Doku_Handler $handler){
78		return array($state,$match);
79    }
80
81
82    /* Create output */
83    function render($format, Doku_Renderer $renderer, $data) {
84        global $ID;
85        if($format != 'xhtml') return false;
86
87		$match = $data[0];
88		list($state, $match) = $data;
89
90		if ($match=='<abstand1>') {$renderer->doc .= '<img src="'.DOKU_BASE.'lib/plugins/pdftools/img/line.php?h=1">';return true;}
91		if ($match=='<abstand2>') {$renderer->doc .= '<img src="'.DOKU_BASE.'lib/plugins/pdftools/img/line.php?h=3">';return true;}
92		if ($match=='<abstand3>') {$renderer->doc .= '<img src="'.DOKU_BASE.'lib/plugins/pdftools/img/line.php?h=5">';return true;}
93
94		if ($match=='<quer1>') {$renderer->doc .= '<img src="'.DOKU_BASE.'lib/plugins/pdftools/img/line.php?h=1&q">';return true;}
95		if ($match=='<quer2>') {$renderer->doc .= '<img src="'.DOKU_BASE.'lib/plugins/pdftools/img/line.php?h=3&q">';return true;}
96		if ($match=='<quer3>') {$renderer->doc .= '<img src="'.DOKU_BASE.'lib/plugins/pdftools/img/line.php?h=5&q">';return true;}
97
98
99        if ($match=='<etikett>') {
100          $renderer->doc .= '<img src="'.DOKU_BASE.'lib/plugins/pdftools/img/etikett.png">';
101          return true;
102        }
103
104        if (!$this->dw2pdf_inst) {
105    			$renderer->doc .= '<i class="noprint">Plugin <u>dw2pdf</u> benötigt.</i><br>';
106    		} else {
107          if ($state == DOKU_LEXER_UNMATCHED) {
108              $t=$this->templateList();
109
110              # The tag "quer" creates landscape orientation
111              if (strpos($match,"quer")>0) {
112				  $quer = '&orientation=landscape';
113				  $match = str_replace("quer","",$match);
114				  $match = trim($match);
115			  } else $quer = '';
116
117              if (!in_array($match,$t)) {
118                  $msg = "Vorlage '<u>$match</u>' existiert nicht. Bitte einer der folgende Vorlagen verwenden:<br>";
119                  $msg .= '<b>'.implode(", ",$t).'</b>';
120
121                  $renderer->doc .= "<div class='noprint boxed'>$msg</div>";
122              } else {
123                  $renderer->doc .= "<div class='noprint'>
124                                    <a href='doku.php?id=$ID&do=export_pdf&toc=0&tpl=$match$quer&rev=".($_GET['rev'])."'>";
125                  $renderer->doc .= '<img src="'.DOKU_BASE.'lib/plugins/pdftools/img/pdfbutton.php?text='.$match.'"></a></div>';
126              }
127          }
128
129        }
130
131        return true;
132
133    } # function render
134
135
136}
137
138//Setup VIM: ex: et ts=4 enc=utf-8 :
139