1<?php 2 3/* 4 * This file is part of the clockoon/dokuwiki-commonmark-plugin package. 5 * 6 * (c) Sungbin Jeon <clockoon@gmail.com> 7 * 8 * Original code based on the followings: 9 * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane 10 * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com> 11 * 12 * For the full copyright and license information, please view the LICENSE 13 * file that was distributed with this source code. 14 */ 15 16namespace DokuWiki\Plugin\Commonmark\Extension\Renderer\Inline; 17 18use League\CommonMark\Inline\Renderer\InlineRendererInterface; 19use League\CommonMark\ElementRendererInterface; 20use League\CommonMark\Inline\Element\AbstractInline; 21use League\CommonMark\Inline\Element\Image; 22use League\CommonMark\Util\ConfigurationAwareInterface; 23use League\CommonMark\Util\ConfigurationInterface; 24use League\CommonMark\Util\RegexHelper; 25 26final class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterface 27{ 28 /** 29 * @var ConfigurationInterface 30 */ 31 protected $config; 32 33 /** 34 * @param Image $inline 35 * @param ElementRendererInterface $DWRenderer 36 * 37 * @return string 38 */ 39 public function render(AbstractInline $inline, ElementRendererInterface $DWRenderer) 40 { 41 if (!($inline instanceof Image)) { 42 throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline)); 43 } 44 45 $attrs = $inline->getData('attributes', []); 46 47 $forbidUnsafeLinks = !$this->config->get('allow_unsafe_links'); 48 if ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl())) { 49 $attrs['src'] = ''; 50 } else { 51 $attrs['src'] = $inline->getUrl(); 52 } 53 54 $alt = $DWRenderer->renderInlines($inline->children()); 55 $alt = \preg_replace('/\<[^>]*alt="([^"]*)"[^>]*\>/', '$1', $alt); 56 $attrs['alt'] = \preg_replace('/\<[^>]*\>/', '', $alt); 57 58 if (isset($inline->data['title'])) { 59 $attrs['title'] = $inline->data['title']; 60 } 61 62 $result = '{{' . $attrs['src']; 63 $attrs['alt'] ? $result.= '|' . $attrs['alt'] . '}}' : $result.= '}}'; 64 65 return $result; 66 } 67 68 public function setConfiguration(ConfigurationInterface $configuration) 69 { 70 $this->config = $configuration; 71 } 72} 73