1<?php
2/**
3 * Replace regular images with lightbox pop-ups
4 *
5 * @license CC 2.5
6 * @author Dustin Butler <dustin@intrcomm.net>
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_lightbox extends DokuWiki_Syntax_Plugin {
18    /**
19     * return some info
20     */
21    function getInfo(){
22      return array(
23        'author' => 'Dustin Butler',
24        'email'  => 'dustin@intrcomm.net',
25        'date'   => '2010-11-17',
26        'name'   => 'Lightbox For Images',
27        'desc'   => 'Pop-up Image Using Lightbox JS',
28        'url'    => 'http://www.roundporch.com/wiki/doku.php?id=dokuwiki:lightbox',
29      );
30    }
31
32    /**
33     * What kind of syntax are we?
34     */
35    function getType(){
36      return 'substition';
37    }
38
39    /**
40     * What about paragraphs? (optional)
41     */
42     function getPType(){
43      return 'block';
44     }
45
46    /**
47     * Where to sort in?
48     */
49    function getSort(){
50        return 300;
51    }
52
53    /**
54     * Connect pattern to lexer
55     */
56    function connectTo($mode) {
57      $this->Lexer->addSpecialPattern('{{[^{}]*\.[pjg][npi][gf][^{}]*}}', $mode, 'plugin_lightbox');
58    }
59
60    /**
61     * Handle the match
62     */
63    function handle($match, $state, $pos, Doku_Handler $handler){
64
65      if ($state != DOKU_LEXER_SPECIAL) return array();
66      // $match will equal our full matching pattern, need to pull the image and size out
67      if (preg_match('/{{(.*\.[pjg][npi][gf])\??([0-9]*)x?([0-9]*)\|?(.*)}}/', html_entity_decode($match), $parts)) {
68        if (! $parts[2]) {
69          $parts[2] = 150;
70          $parts[3] = 150;
71        }
72        return $parts;
73      }
74      return array();
75		  /*
76      switch ($state) {
77        case DOKU_LEXER_ENTER :
78          break;
79        case DOKU_LEXER_MATCHED :
80          break;
81        case DOKU_LEXER_UNMATCHED :
82          break;
83        case DOKU_LEXER_EXIT :
84          break;
85        case DOKU_LEXER_SPECIAL :
86          break;
87      }
88		  */
89
90    }
91
92    /**
93     * Create output
94     */
95    function render($mode, Doku_Renderer $renderer, $data) {
96      if ($mode == 'xhtml' or $mode == 'htmldoc') {
97        list($all, $src, $width, $height, $title) = $data;
98
99        //$renderer->doc .= "<!-- $src -->";
100
101        if (! preg_match('/^https?:/', $src)) {
102          $src = DOKU_BASE . "lib/exe/fetch.php?media=$src";
103        }
104
105        // Sized display
106        $renderer->doc .= "<a href=\"$src\" rel=\"lightbox\"><img src=\"$src\" class=\"media\" width=\"$width\" height=\"$height\" alt=\"$title\" title=\"$title\" /></a>";
107
108        //$renderer->doc .= "<div style=\"margin: 3px;border: 1px solid grey;display: inline-block; overflow: hidden; width: ${width}px; height: ${height}px\"><a href=\"$src\" rel=\"lightbox\"><img src=\"$src\" class=\"media\" alt=\"$title\" title=\"$title\" /></a></div>";
109        return true;
110      }
111      return false;
112    }
113}
114