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