<?php
/**
 * Plugin Links: Vytváří odkazy s automatickým načtením titulku stránky
 */
if(!defined('DOKU_INC')) die();

class syntax_plugin_links extends DokuWiki_Syntax_Plugin {
    
    public function getType() {
        return 'substition';
    }
    
    public function getPType() {
        return 'normal';
    }
    
    public function getSort() {
        return 159;
    }

    public function connectTo($mode) {
        $this->Lexer->addSpecialPattern('\{\{link>[^}]*\}\}', $mode, 'plugin_links');
    }

    public function handle($match, $state, $pos, Doku_Handler $handler) {
        $url = substr($match, 7, -2); // odstranění {{link> a }}
        return array($url);
    }

    public function render($mode, Doku_Renderer $renderer, $data) {
        if($mode !== 'xhtml') return false;

        list($url) = $data;
        $url = trim($url);

        try {
            // Přidání protokolu, pokud chybí
            if (!preg_match('~^(?:f|ht)tps?://~i', $url)) {
                $url = 'http://' . $url;
            }

            // Nastavení kontextu pro stream
            $context = stream_context_create([
                'http' => [
                    'method' => 'GET',
                    'header' => [
                        'User-Agent: DokuWiki Links Plugin',
                        'Accept: text/html,application/xhtml+xml,application/xml'
                    ],
 