|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + +------------------------------------------------------------------------+ |
| 5 | + | Phalcon forum | |
| 6 | + +------------------------------------------------------------------------+ |
| 7 | + | Copyright (c) 2011-2017 Phalcon Team (https://www.phalconphp.com) | |
| 8 | + +------------------------------------------------------------------------+ |
| 9 | + | This source file is subject to the New BSD License that is bundled | |
| 10 | + | with this package in the file LICENSE.txt. | |
| 11 | + | | |
| 12 | + | If you did not receive a copy of the license and are unable to | |
| 13 | + | obtain it through the world-wide-web, please send an email | |
| 14 | + | to [email protected] so we can send you a copy immediately. | |
| 15 | + +------------------------------------------------------------------------+ |
| 16 | + | Authors: Sergii Svyrydenko <[email protected]> | |
| 17 | + +------------------------------------------------------------------------+ |
| 18 | + */ |
| 19 | + |
| 20 | +namespace Phosphorum\Provider\Markdown\Plugins; |
| 21 | + |
| 22 | +use Ciconia\Markdown; |
| 23 | +use Ciconia\Common\Text; |
| 24 | +use Ciconia\Extension\ExtensionInterface; |
| 25 | + |
| 26 | +class SpecSymbolExtension implements ExtensionInterface |
| 27 | +{ |
| 28 | + public function register(Markdown $markdown) |
| 29 | + { |
| 30 | + $markdown->on('inline', [$this, 'escapeText']); |
| 31 | + } |
| 32 | + |
| 33 | + public function escapeText(Text $text) |
| 34 | + { |
| 35 | + $replaceArray = []; |
| 36 | + |
| 37 | + $text->replace('{<code>.*?</code>}m', function (Text $w) use (&$replaceArray) { |
| 38 | + $count = count($replaceArray) + 1; |
| 39 | + $replaceArray[$count] = $w->getString(); |
| 40 | + $w->replaceString($w->getString(), "%%replaced" . $count . "%%"); |
| 41 | + |
| 42 | + return $w; |
| 43 | + }); |
| 44 | + |
| 45 | + $str = htmlspecialchars($text->getString()); |
| 46 | + foreach ($replaceArray as $key => $value) { |
| 47 | + $str = str_replace("%%replaced" . $key . "%%", $value, $str); |
| 48 | + } |
| 49 | + |
| 50 | + $text->setString($str); |
| 51 | + } |
| 52 | + |
| 53 | + public function getName() |
| 54 | + { |
| 55 | + return 'escapeText'; |
| 56 | + } |
| 57 | +} |
0 commit comments