1<?php
2
3	if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
4	if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
5	require_once(DOKU_PLUGIN.'syntax.php');
6
7
8	class syntax_plugin_epub extends DokuWiki_Syntax_Plugin {
9		protected $title;
10		private $helper;
11		function getInfo() {
12			return array(
13            'author' => 'Myron Turner',
14            'email'  => 'turnermm02@shaw.ca',
15            'date'   => '2011-07-1',
16            'name'   => 'epub',
17            'desc'   => 'ebook creator',
18            'url'    => 'http://www.dokuwiki.org/plugin:epub');
19		}
20
21		function getType(){ return 'container'; }
22		function getPType(){ return 'block'; }
23		function getAllowedTypes() {
24			return array();
25		}
26		function getSort(){ return 25; }
27
28		function connectTo($mode) {
29			$this->Lexer->addEntryPattern('<epub.*?>(?=.*?</epub>)',$mode,'plugin_epub');
30
31		}
32		function postConnect() {
33			$this->Lexer->addExitPattern('</epub>','plugin_epub');
34
35		}
36		function __construct() {
37		    $this->helper =& plugin_load('helper', 'epub');
38		}
39		function handle($match, $state, $pos, Doku_Handler $handler) {
40            $match = str_replace(';','&#59;',$match);
41		    $match = str_replace(',','&#44;',$match);
42
43			switch ($state) {
44				case DOKU_LEXER_ENTER :
45				$title =  substr($match, 6, -1);
46				if($title)
47				$this->title = $title;
48				else
49				$this->title="Dokuwiki EBook";
50				return array($state, trim($title));
51
52				case DOKU_LEXER_UNMATCHED :
53				return array($state, $match);
54				case DOKU_LEXER_EXIT :
55				return array($state,$match);
56
57				default:
58
59				return array($state,$match);
60			}
61		}
62
63		function render($mode, Doku_Renderer $renderer, $data) {
64			global $INFO;
65
66			if($mode == 'xhtml'){
67				$renderer->nocache();
68				list($state, $match) = $data;
69
70				switch ($state) {
71					case DOKU_LEXER_ENTER :
72
73				    $this->helper->writeCache($INFO['id']);
74					$renderer->doc .= '<div>';
75					break;
76
77					case DOKU_LEXER_UNMATCHED :
78					$id = $INFO['id'];
79					$renderer->doc .= "\n<SCRIPT  type='text/javascript'>\n//<![CDATA[\n" ;
80					$renderer->doc .= "\nvar book_id = '$id';";
81					$renderer->doc .= "\nvar epub_wikilink = new Array();\nvar epub_id = new Array();\n";
82					$files = explode("\n",$match);
83
84					for($i=0;$i<count($files);$i++) {
85	                    $file = trim($files[$i],'][');
86		                list($file,$rest) = explode('|',$file);
87						$file=trim($file);
88						$file=trim($file,'/');
89						if(!$file) continue;
90 						if(!auth_quickaclcheck($file)) {
91							continue;
92						}
93						$renderer->doc .= "epub_id[$i]='" . str_replace('/',':',$file) . "';\n"	;
94                        $rest = trim($rest," ][");
95
96                        if(!$rest) {
97                          $ar = explode(':',$file);
98                          $n = count($ar) -1;
99                          $rest = $ar[$n];
100                         }
101
102                        $rest=hsc($rest);
103                        $rest =  str_replace(':','&#58;',$rest);
104                        $renderer->doc .= "epub_wikilink[$i]='" . str_replace('/',':',$rest) . "';\n"	;
105					}
106
107					$renderer->doc .= 'epub_title="' . $this->title . '";';
108					$renderer->doc .= "\n// ]]>\n</SCRIPT>\n";
109
110
111					break;
112
113					case DOKU_LEXER_EXIT :
114				    $throbber = DOKU_BASE . 'lib/plugins/epub/throbber.gif';
115				    $renderer->doc .= '<div id="epub_throbber" style="display:none;"><center><img src="' . $throbber .'"></center><br /><span id="epub_progress">progress</span></div>';
116				    $renderer->doc .= "\n</div>";
117				    break;
118				}
119				return true;
120
121			}
122			// unsupported $mode
123			return false;
124		}
125
126		function write_debug($what) {
127			return;
128	       if(is_array($what))   $what = print_r($what,true);
129			$handle = fopen('epub.txt', 'a');
130			fwrite($handle,"$what\n");
131			fclose($handle);
132		}
133	}
134
135	//Setup VIM: ex: et ts=4 enc=utf-8 :
136?>
137