Example #1
0
    /**
     * Handle reference-style labeled images: ![alt text][id]
     *
     * @param Text  $text
     * @param array $options
     */
    public function processReferencedImage(Text $text, array $options = array())
    {
        if (!$text->contains('![')) {
            return;
        }
        /** @noinspection PhpUnusedParameterInspection */
        $text->replace('{
                #(               # wrap whole match in $1
                  !\\[
                    (.*?)       # alt text = $2
                  \\]

                  [ ]?          # one optional space
                  (?:\\n[ ]*)?   # one optional newline followed by spaces

                  \\[
                    (.*?)       # id = $3
                  \\]
                #)
            }xs', function (Text $whole, Text $alt, Text $id = null) use($options) {
            $result = null;
            if ($id->lower() == '') {
                $id->setString($alt);
            }
            $this->markdown->emit('escape.special_chars', [$alt->replace('/(?<!\\\\)_/', '\\\\_')]);
            $attr = array('alt' => $alt->replace('/"/', '&quot;'));
            if ($this->markdown->getUrlRegistry()->exists($id)) {
                $url = new Text($this->markdown->getUrlRegistry()->get($id));
                $url->escapeHtml();
                if ($this->markdown->getTitleRegistry()->exists($id)) {
                    $title = new Text($this->markdown->getTitleRegistry()->get($id));
                    $attr['title'] = $title->escapeHtml();
                }
                return $this->getRenderer()->renderImage($url, array('attr' => $attr));
            } else {
                if ($options['strict']) {
                    throw new SyntaxError(sprintf('Unable to find id "%s" in Reference-style image', $id), $this, $whole, $this->markdown);
                }
                return $whole;
            }
        });
    }
Example #2
0
    /**
     * Handle reference-style links: [link text] [id]
     *
     * @param Text  $text
     * @param array $options
     */
    public function processReferencedLink(Text $text, array $options = array())
    {
        if (!$text->contains('[')) {
            return;
        }
        /** @noinspection PhpUnusedParameterInspection */
        $text->replace('{
            #(                   # wrap whole match in $1
              \\[
                (' . $this->getNestedBrackets() . ')    # link text = $2
              \\]

              [ ]?              # one optional space
              (?:\\n[ ]*)?       # one optional newline followed by spaces

              \\[
                (.*?)       # id = $3
              \\]
            #)
        }xs', function (Text $whole, Text $linkText, Text $id = null) use($options) {
            if (is_null($id) || (string) $id == '') {
                $id = new Text($linkText);
            }
            $id->lower();
            if ($this->markdown->getUrlRegistry()->exists($id)) {
                $url = new Text($this->markdown->getUrlRegistry()->get($id));
                $url->escapeHtml()->replace('/(?<!\\\\)_/', '\\\\_');
                $this->markdown->emit('escape.special_chars', [$url]);
                $linkOptions = ['href' => $url->getString()];
                if ($this->markdown->getTitleRegistry()->exists($id)) {
                    $title = new Text($this->markdown->getTitleRegistry()->get($id));
                    $linkOptions['title'] = $title->escapeHtml()->getString();
                }
                return $this->getRenderer()->renderLink($linkText->getString(), $linkOptions);
            } else {
                if ($options['strict']) {
                    throw new SyntaxError(sprintf('Unable to find id "%s" in Reference-style link', $id), $this, $whole, $this->markdown);
                }
                return $whole;
            }
        });
    }