1<?php 2 3/** 4 * DokuWiki Plugin simplemap (Syntax Component) 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Michael Große <mic.grosse@googlemail.com> 8 */ 9 10declare(strict_types=1); 11 12use dokuwiki\Extension\SyntaxPlugin; 13 14final class syntax_plugin_simplemap extends SyntaxPlugin 15{ 16 /** 17 * @return string Syntax mode type 18 */ 19 public function getType(): string 20 { 21 return 'substition'; 22 } 23 24 /** 25 * @return string Paragraph type 26 */ 27 public function getPType(): string 28 { 29 return 'block'; 30 } 31 32 /** 33 * @return int Sort order - Low numbers go before high numbers 34 */ 35 public function getSort(): int 36 { 37 return 50; 38 } 39 40 /** 41 * Connect lookup pattern to lexer. 42 * 43 * @param string $mode Parser mode 44 */ 45 public function connectTo($mode): void 46 { 47 $this->Lexer->addSpecialPattern('{{simplemap>.*?}}', $mode, 'plugin_simplemap'); 48 } 49 50 /** 51 * Handle matches of the simplemap syntax 52 * 53 * @param string $match The match of the syntax 54 * @param int $state The state of the handler 55 * @param int $pos The position in the document 56 * @param Doku_Handler $handler The handler 57 * @return array Data for the renderer 58 */ 59 public function handle($match, $state, $pos, Doku_Handler $handler): array 60 { 61 return $this->parseMatch($match); 62 } 63 64 // {{simplemap>osm?lat=50.234&long=13.123}} 65 private function parseMatch(string $match): array 66 { 67 $match = substr($match, strlen('{{simplemap>'), -strlen('}}')); 68 [$type, $query] = explode('?', $match, 2); 69 parse_str($query, $data); 70 $data['type'] = $type; 71 72 return $data; 73 } 74 75 /** 76 * Render xhtml output or metadata 77 * 78 * @param string $mode Renderer mode (supported modes: xhtml) 79 * @param Doku_Renderer $renderer The renderer 80 * @param array $data The data from the handler() function 81 * @return bool If rendering was successful. 82 */ 83 public function render($mode, Doku_Renderer $renderer, $data): bool 84 { 85 if ($mode != 'xhtml') { 86 return false; 87 } 88 89 $long = $data['long']; 90 $lat = $data['lat']; 91 92 $iframeEndpoint = 'https://www.openstreetmap.org/export/embed.html'; 93 $iframeQueryParams = [ 94 'bbox' => ($long - 0.004) . ',' . ($lat - 0.002) . ',' . ($long + 0.004) . ',' . ($lat + 0.002), 95 'layer' => 'mapnik', 96 'marker' => "$lat,$long" 97 ]; 98 $src = $iframeEndpoint . '?' . http_build_query($iframeQueryParams); 99 100 $iframeHtml = '<iframe width="425" height="350" src="' . $src . '"></iframe>'; 101 $linkHref = "https://www.openstreetmap.org/#map=14/$lat/$long"; 102 $link = "<a href=\"$linkHref\" target=\"_blank\">" . $this->getLang('view larger map') . "</a>"; 103 $renderer->doc .= $iframeHtml . '<br>' . $link; 104 105 return true; 106 } 107} 108 109// vim:ts=4:sw=4:et: 110