18ec9a8f2SSungbin Jeon<?php 28ec9a8f2SSungbin Jeon 38ec9a8f2SSungbin Jeon/* 48ec9a8f2SSungbin Jeon * This file is part of the clockoon/dokuwiki-commonmark-plugin package. 58ec9a8f2SSungbin Jeon * 68ec9a8f2SSungbin Jeon * (c) Sungbin Jeon <clockoon@gmail.com> 78ec9a8f2SSungbin Jeon * 88ec9a8f2SSungbin Jeon * Original code based on the followings: 98ec9a8f2SSungbin Jeon * - CommonMark JS reference parser (https://bitly.com/commonmark-js) (c) John MacFarlane 108ec9a8f2SSungbin Jeon * - league/commonmark (https://github.com/thephpleague/commonmark) (c) Colin O'Dell <colinodell@gmail.com> 118ec9a8f2SSungbin Jeon * 128ec9a8f2SSungbin Jeon * For the full copyright and license information, please view the LICENSE 138ec9a8f2SSungbin Jeon * file that was distributed with this source code. 148ec9a8f2SSungbin Jeon */ 158ec9a8f2SSungbin Jeon 168ec9a8f2SSungbin Jeonnamespace DokuWiki\Plugin\Commonmark\Extension\Renderer\Inline; 178ec9a8f2SSungbin Jeon 1894a075eeSSungbin Jeonuse League\CommonMark\Renderer\NodeRendererInterface; 19*b0a36678SSungbin Jeonuse League\CommonMark\Renderer\ChildNodeRendererInterface; 20*b0a36678SSungbin Jeonuse League\CommonMark\Node\Node; 21*b0a36678SSungbin Jeonuse League\CommonMark\Extension\CommonMark\Node\Inline\Image; 22*b0a36678SSungbin Jeonuse League\Config\ConfigurationAwareInterface; 23*b0a36678SSungbin Jeonuse League\Config\ConfigurationInterface; 24*b0a36678SSungbin Jeon 258ec9a8f2SSungbin Jeonuse League\CommonMark\Util\RegexHelper; 268ec9a8f2SSungbin Jeon 2794a075eeSSungbin Jeonfinal class ImageRenderer implements NodeRendererInterface, ConfigurationAwareInterface 288ec9a8f2SSungbin Jeon{ 298ec9a8f2SSungbin Jeon /** 308ec9a8f2SSungbin Jeon * @var ConfigurationInterface 318ec9a8f2SSungbin Jeon */ 328ec9a8f2SSungbin Jeon protected $config; 338ec9a8f2SSungbin Jeon 348ec9a8f2SSungbin Jeon /** 358ec9a8f2SSungbin Jeon * @param Image $inline 36*b0a36678SSungbin Jeon * @param ChildNodeRendererInterface $DWRenderer 378ec9a8f2SSungbin Jeon * 388ec9a8f2SSungbin Jeon * @return string 398ec9a8f2SSungbin Jeon */ 40*b0a36678SSungbin Jeon public function render(Node $node, ChildNodeRendererInterface $DWRenderer): string 418ec9a8f2SSungbin Jeon { 42*b0a36678SSungbin Jeon Image::assertInstanceOf($node); 438ec9a8f2SSungbin Jeon 44*b0a36678SSungbin Jeon $attrs = $node->data->get('attributes'); 458ec9a8f2SSungbin Jeon 468ec9a8f2SSungbin Jeon $forbidUnsafeLinks = !$this->config->get('allow_unsafe_links'); 478ec9a8f2SSungbin Jeon if ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl())) { 488ec9a8f2SSungbin Jeon $attrs['src'] = ''; 498ec9a8f2SSungbin Jeon } else { 50*b0a36678SSungbin Jeon $attrs['src'] = $node->getUrl(); 518ec9a8f2SSungbin Jeon } 528ec9a8f2SSungbin Jeon 53*b0a36678SSungbin Jeon $alt = $DWRenderer->renderNodes($node->children()); 548ec9a8f2SSungbin Jeon $alt = \preg_replace('/\<[^>]*alt="([^"]*)"[^>]*\>/', '$1', $alt); 558ec9a8f2SSungbin Jeon $attrs['alt'] = \preg_replace('/\<[^>]*\>/', '', $alt); 568ec9a8f2SSungbin Jeon 57*b0a36678SSungbin Jeon if (isset($node->data['title'])) { 58*b0a36678SSungbin Jeon $attrs['title'] = $node->data['title']; 598ec9a8f2SSungbin Jeon } 608ec9a8f2SSungbin Jeon 618ec9a8f2SSungbin Jeon $result = '{{' . $attrs['src']; 628ec9a8f2SSungbin Jeon $attrs['alt'] ? $result.= '|' . $attrs['alt'] . '}}' : $result.= '}}'; 638ec9a8f2SSungbin Jeon 648ec9a8f2SSungbin Jeon return $result; 658ec9a8f2SSungbin Jeon } 668ec9a8f2SSungbin Jeon 67*b0a36678SSungbin Jeon public function setConfiguration(ConfigurationInterface $configuration): void 688ec9a8f2SSungbin Jeon { 698ec9a8f2SSungbin Jeon $this->config = $configuration; 708ec9a8f2SSungbin Jeon } 718ec9a8f2SSungbin Jeon} 72