Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / tidy / RaggettWrapper.php
1 <?php
2 namespace MediaWiki\Tidy;
3
4 use ReplacementArray;
5 use ParserOutput;
6 use Parser;
7
8 /**
9 * Class used to hide mw:editsection tokens from Tidy so that it doesn't break them
10 * or break on them. This is a bit of a hack for now, but hopefully in the future
11 * we may create a real postprocessor or something that will replace this.
12 * It's called wrapper because for now it basically takes over MWTidy::tidy's task
13 * of wrapping the text in a xhtml block
14 *
15 * This re-uses some of the parser's UNIQ tricks, though some of it is private so it's
16 * duplicated. Perhaps we should create an abstract marker hiding class.
17 *
18 * @ingroup Parser
19 */
20 class RaggettWrapper {
21
22 /**
23 * @var ReplacementArray
24 */
25 protected $mTokens;
26
27 protected $mMarkerIndex;
28
29 public function __construct() {
30 $this->mTokens = null;
31 }
32
33 /**
34 * @param string $text
35 * @return string
36 */
37 public function getWrapped( $text ) {
38 $this->mTokens = new ReplacementArray;
39 $this->mMarkerIndex = 0;
40
41 // Replace <mw:editsection> elements with placeholders
42 $wrappedtext = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
43 array( &$this, 'replaceCallback' ), $text );
44 // ...and <mw:toc> markers
45 $wrappedtext = preg_replace_callback( '/\<\\/?mw:toc\>/',
46 array( &$this, 'replaceCallback' ), $wrappedtext );
47 // ... and <math> tags
48 $wrappedtext = preg_replace_callback( '/\<math(.*?)\<\\/math\>/s',
49 array( &$this, 'replaceCallback' ), $wrappedtext );
50 // Modify inline Microdata <link> and <meta> elements so they say <html-link> and <html-meta> so
51 // we can trick Tidy into not stripping them out by including them in tidy's new-empty-tags config
52 $wrappedtext = preg_replace( '!<(link|meta)([^>]*?)(/{0,1}>)!', '<html-$1$2$3', $wrappedtext );
53
54 // Wrap the whole thing in a doctype and body for Tidy.
55 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' .
56 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>' .
57 '<head><title>test</title></head><body>' . $wrappedtext . '</body></html>';
58
59 return $wrappedtext;
60 }
61
62 /**
63 * @param array $m
64 *
65 * @return string
66 */
67 public function replaceCallback( $m ) {
68 $marker = Parser::MARKER_PREFIX . "-item-{$this->mMarkerIndex}" . Parser::MARKER_SUFFIX;
69 $this->mMarkerIndex++;
70 $this->mTokens->setPair( $marker, $m[0] );
71 return $marker;
72 }
73
74 /**
75 * @param string $text
76 * @return string
77 */
78 public function postprocess( $text ) {
79 // Revert <html-{link,meta}> back to <{link,meta}>
80 $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
81
82 // Restore the contents of placeholder tokens
83 $text = $this->mTokens->replace( $text );
84
85 return $text;
86 }
87
88 }
89 ?>