1<?php
2/**
3 * DokuWiki Syntax Plugin Gpsies
4 *
5 * Embeds a gpsies track from gpsies.com in a dokuwiki page.
6 *
7 * Syntax:  {{gpsies>[track]}}
8 *
9 *   [gpsies] - the "field" value of the track URL
10 *
11 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
12 * @author  Michael Klier <chi@chimeric.de>
13 */
14// must be run within DokuWiki
15if(!defined('DOKU_INC')) die();
16if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
17
18require_once(DOKU_PLUGIN.'syntax.php');
19require_once(DOKU_INC.'inc/parserutils.php');
20
21/**
22 * All DokuWiki plugins to extend the parser/rendering mechanism
23 * need to inherit from this class
24 */
25class syntax_plugin_gpsies extends DokuWiki_Syntax_Plugin {
26
27
28    /**
29     * General Info
30     */
31    function getInfo(){
32        return array(
33            'author' => 'Michael Klier',
34            'email'  => 'chi@chimeric.de',
35            'date'   => @file_get_contents(DOKU_PLUGIN.'gpsies/VERSION'),
36            'name'   => 'Gpsies',
37            'desc'   => 'Embeds a track from gpsies into a dokuwiki page.',
38            'url'    => 'http://dokuwiki.org/plugin:gpsies'
39        );
40    }
41
42    /**
43     * Syntax Type
44     *
45     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
46     */
47    function getType()  { return 'substition'; }
48    function getPType() { return 'block'; }
49    function getSort()  { return 316; }
50
51    /**
52     * Connect pattern to lexer
53     */
54    function connectTo($mode) {
55        $this->Lexer->addSpecialPattern('\{\{gpsies>.+?\}\}',$mode,'plugin_gpsies');
56    }
57
58    /**
59     * Handler to prepare matched data for the rendering process
60     */
61    function handle($match, $state, $pos, &$handler){
62        $match = substr($match,9,-2); //strip {{gpsies> from start and }} from end
63
64        list($track,$params) = explode('?',$match,2);
65        $data['track'] = $track;
66
67        //get dimensions
68        if(preg_match('/\b(\d+)x(\d+)\b/',$params,$match)){
69            $data['w'] = $match[1];
70            $data['h'] = $match[2];
71        }else{
72            $data['w'] = 500;
73            $data['h'] = 500;
74        }
75
76        return $data;
77    }
78
79    /**
80     * Handles the actual output creation.
81     */
82    function render($mode, &$renderer, $data) {
83        global $lang;
84
85        if($mode == 'xhtml'){
86            // disable caching
87            $renderer->info['cache'] = false;
88            $renderer->doc .= $this->gpsies_xhtml($data);
89            return true;
90        }
91        return false;
92    }
93
94    /**
95     * returns the XHTML output
96     *
97     * @author Michael Klier <chi@chimeric.de>
98     */
99    function gpsies_xhtml($data) {
100        $trackURL            = 'http://gpsies.de/mapOnly.do?fileId=' . $data['track'];
101        $trackMapURL         = 'http://gpsies.com/map.do?fileId=' . $data['track'];
102        $trackMapOnlyURL     = 'http://gpsies.com/mapOnly.do?fileId=' . $data['track'];
103        $trackAltitudeImgURL = 'http://gpsies.de/charts/' . $data['track'] . '_map.png';
104
105        $out  = '<div class="plugin_gpsies">' . DOKU_LF;
106        $out .= '  <div class="plugin_gpsies_header">' . DOKU_LF;
107        $out .= '    <a href="' . $trackMapURL . '" class="plugin_gpsies" title="' . $this->getLang('visit_track') . '">' . $this->getLang('visit_track') . '</a>' . DOKU_LF;
108        $out .= ' &middot; ';
109        $out .= '<a href="' . $trackMapOnlyURL . '" target="_blank" title="' . $this->getLang('fullscreen') . '">' . $this->getLang('fullscreen') . '</a>';
110        $out .= '  </div>' . DOKU_LF;
111        $out .= '    <iframe src="' . $trackURL . '" width="'. $data['w'] . '" height="' . $data['h'] . '" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" title="' . $trackMapURL . '"></iframe>' . DOKU_LF;
112        $out .= '    <div class="clearer"></div>';
113        $out .= '    <img src="' . $trackAltitudeImgURL . '" alt="' . $trackMapURL . '" />' . DOKU_LF;
114        $out .= '  <div class="plugin_gpsies_footer">' . DOKU_LF;
115        $out .= '  </div>' . DOKU_LF;
116        $out .= '</div>' . DOKU_LF;
117        return ($out);
118    }
119}
120
121// vim:ts=4:sw=4:enc=utf-8:
122