1<?php
2/**
3 * DokuWiki Plugin mtg (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Jamin Collins <jamin.collins@gmail.com>
7 *
8 * 2018/06/07 : Initial Release
9 */
10
11// must be run within Dokuwiki
12if (!defined('DOKU_INC')) {
13    die();
14}
15
16class syntax_plugin_mtg extends DokuWiki_Syntax_Plugin
17{
18    /**
19     * @return string Syntax mode type
20     */
21    public function getType()
22    {
23        return 'substition';
24    }
25
26    /**
27     * @return string Paragraph type
28     */
29    public function getPType()
30    {
31        return 'normal';
32    }
33
34    /**
35     * @return int Sort order - Low numbers go before high numbers
36     */
37    public function getSort()
38    {
39        return 25; // Internal link is 300, AFAIK
40    }
41
42    /**
43     * Connect lookup pattern to lexer.
44     *
45     * @param string $mode Parser mode
46     */
47    public function connectTo($mode)
48    {
49        // TODO -- come up with syntax for matching cards???
50        $this->Lexer->addSpecialPattern('{{mtg:.*?}}', $mode, 'plugin_mtg');
51    }
52
53    /**
54     * Handle matches of the mtg syntax
55     *
56     * @param string       $match   The match of the syntax
57     * @param int          $state   The state of the handler
58     * @param int          $pos     The position in the document
59     * @param Doku_Handler $handler The handler
60     *
61     * @return array Data for the renderer
62     */
63    public function handle($match, $state, $pos, Doku_Handler $handler)
64    {
65        // get card name from the match
66        preg_match("~{{mtg:(.*)}}~", $match, $matches);
67        list($card_name, $edition) = explode("|", $matches[1]);
68
69        // get image uri from Scryfall's API
70        $url = "https://api.scryfall.com/cards/named?exact=" . urlencode($card_name);
71        if ($edition != null) {
72            $url .= "&set=" . $edition;
73        }
74        $resp = file_get_contents($url);
75        $obj = json_decode($resp, true);
76
77        $image_uri = $obj['image_uris']['normal'];
78        $data = array($card_name, $pos, $image_uri);
79
80        return $data;
81    }
82
83    /**
84     * Render xhtml output or metadata
85     *
86     * @param string        $mode     Renderer mode (supported modes: xhtml)
87     * @param Doku_Renderer $renderer The renderer
88     * @param array         $data     The data from the handler() function
89     *
90     * @return bool If rendering was successful.
91     */
92    public function render($mode, Doku_Renderer $renderer, $data)
93    {
94        if ($mode !== 'xhtml') {
95            return false;
96        }
97
98        $card_name = $data[0];
99        $pos = $data[1];
100        $image_uri = $data[2];
101
102        // create a unique id based on the card name and the position within the document
103        $id = "mtg-" . str_replace(" ", "_", strtolower($card_name)) . "-$pos";
104        $gatherer_url = "http://gatherer.wizards.com/Pages/Card/Details.aspx?name=" . urlencode($card_name);
105        $transparent_pixel = "lib/plugins/mtg/images/transparent.png";
106
107        $output = "<a href='$gatherer_url' class='mtg urlextern' ";
108        $output .= "onmouseover=\"document.getElementById('$id').src='$image_uri'; document.getElementById('$id').width='300';\" ";
109        $output .= "onmouseout=\"document.getElementById('$id').src='$transparent_pixel'; document.getElementById('$id').width=0\"";
110        $output .= ">$card_name<img class='mtg' src='$transparent_pixel' id='$id' /></a>";
111
112        $renderer->doc .= $output;
113
114        return true;
115    }
116}
117
118