Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
MathHighlighter | |
0.00% |
0 / 29 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
getStartPos | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
getEndPos | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
getWikiText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | class MathHighlighter { |
4 | |
5 | private const WINDOW_SIZE = 1200; |
6 | |
7 | /** @var string */ |
8 | private $wikiText; |
9 | |
10 | /** |
11 | * @param string $fId |
12 | * @param int $revId |
13 | * @param bool $highlight |
14 | */ |
15 | public function __construct( $fId, $revId, $highlight = true ) { |
16 | $gen = MathIdGenerator::newFromRevisionId( $revId ); |
17 | $unique = $gen->getUniqueFromId( $fId ); |
18 | $wikiText = $gen->getWikiText(); |
19 | $tagPos = strpos( $wikiText, $unique ); |
20 | $startPos = $this->getStartPos( $tagPos, $wikiText ); |
21 | $length = $this->getEndPos( $tagPos, $wikiText ) - $startPos; |
22 | $wikiText = substr( $wikiText, $startPos, $length ); |
23 | $tag = $gen->getTagFromId( $fId ); |
24 | if ( $highlight ) { |
25 | $wikiText = str_replace( $unique, '<span id="mathhighlight" style="background-color: yellow">' |
26 | . $tag[3] . '</span>', $wikiText ); |
27 | } |
28 | foreach ( $gen->getMathTags() as $key => $content ) { |
29 | $wikiText = str_replace( $key, $content[3], $wikiText ); |
30 | } |
31 | $this->wikiText = $wikiText; |
32 | } |
33 | |
34 | /** |
35 | * @param int $tagPos |
36 | * @param string $wikiText |
37 | * @return int |
38 | */ |
39 | private function getStartPos( $tagPos, $wikiText ) { |
40 | $startPos = max( $tagPos - round( self::WINDOW_SIZE / 2 ), 0 ); |
41 | if ( $startPos > 0 ) { |
42 | // Heuristics to find a reasonable cutting point |
43 | $newPos = strpos( $wikiText, "\n", $startPos ); |
44 | if ( $newPos !== false && ( $newPos - $startPos ) < round( self::WINDOW_SIZE / 4 ) ) { |
45 | // only change startPos, if it seems reasonable |
46 | $startPos = $newPos; |
47 | } |
48 | } |
49 | return $startPos; |
50 | } |
51 | |
52 | /** |
53 | * @param int $tagPos |
54 | * @param string $wikiText |
55 | * @return int |
56 | */ |
57 | private function getEndPos( $tagPos, $wikiText ) { |
58 | $halfWindow = round( self::WINDOW_SIZE / 2 ); |
59 | $distance2End = strlen( $wikiText ) - $tagPos; |
60 | if ( $distance2End > $halfWindow ) { |
61 | $newPos = strpos( $wikiText, "\n", $tagPos + $halfWindow ); |
62 | if ( $newPos !== false && ( $newPos - $tagPos ) < round( 3 / 4 * self::WINDOW_SIZE ) ) { |
63 | // only change startPos, if it seems reasonable |
64 | return $newPos; |
65 | } else { |
66 | return $tagPos + $halfWindow; |
67 | } |
68 | } else { |
69 | return strlen( $wikiText ); |
70 | } |
71 | } |
72 | |
73 | /** |
74 | * @return string |
75 | */ |
76 | public function getWikiText() { |
77 | return $this->wikiText; |
78 | } |
79 | |
80 | } |