Skip to content

Commit 1d70815

Browse files
committed
Restructured the project and removed a compatibility issue
Restructured the project to fit to be easier to be edited. Compatibility issues with the versions 1.36.1 Class Autoloader resolved.
1 parent 54ce0d5 commit 1d70815

File tree

8 files changed

+169
-176
lines changed

8 files changed

+169
-176
lines changed

extension.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"type": "validextensionclass",
99
"manifest_version": 1,
1010
"AutoloadClasses": {
11-
"MultiCodeBlock": "includes/MultiCodeBlock.php"
11+
"MultiCodeBlockHooks": "includes/hooks/MultiCodeBlockHooks.php"
1212
},
1313
"Hooks": {
14-
"ParserFirstCallInit": "MultiCodeBlock::onParserFirstCallInit"
14+
"ParserFirstCallInit": "MultiCodeBlockHooks::onParserFirstCallInit"
1515
},
1616
"ResourceModules": {
1717
"ext.multicodeblock.styles": {

includes/MultiCodeBlock.php

Lines changed: 118 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,134 @@
11
<?php
2+
23
/**
34
* This file is part of MultiCodeBlock.
45
*
56
* For the full copyright and license information, please view the LICENSE
67
* file that was distributed with this source code.
78
*/
89

10+
require_once __DIR__ . '/vendor/autoload.php';
11+
12+
require_once 'class/Description.php';
13+
require_once 'class/Code.php';
14+
require_once 'class/LanguageBlock.php';
15+
16+
require_once 'utils/HTMLFramework.php';
17+
require_once 'utils/getData.php';
18+
919
/**
10-
* Protect against register_globals vulnerabilities.
11-
* This line must be present before any global variable is referenced.
20+
* Returns a string based on the MultiCodeBlock HTML-Element
21+
*
22+
* @param string $input The content of the MultiCodeBlock HTML-Element
23+
* @param array $args The arguments of the MultiCodeBlock HTML-Element
24+
* @param Parser $parser The MediaWiki syntax parser
25+
* @param PPFrame $frame MediaWiki frame
1226
*/
13-
if( !defined( 'MEDIAWIKI' ) ) {
14-
echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
15-
die( -1 );
27+
function createMultiCodeBlock(string &$input, Parser &$parser) {
28+
$out = $parser->getOutput();
29+
$out->addModuleStyles(['ext.multicodeblock.styles']);
30+
$out->addModules(['ext.multicodeblock.js']);
31+
32+
$code = findCodeBlocks($input);
33+
34+
$replaced = str_replace($code, 'test', $input);
35+
$dom = getDOM($replaced);
36+
37+
$codevariants = $dom->getElementsbyTagName('codeblock');
38+
39+
$descriptions = [];
40+
foreach ($codevariants as $codevariant) {
41+
array_push($descriptions, $codevariant->getElementsbyTagName('desc'));
42+
}
43+
$codeArr = [];
44+
foreach ($codevariants as $codevariant) {
45+
array_push($codeArr, $codevariant->getElementsbyTagName('code'));
46+
}
47+
48+
$size = sizeof($codevariants);
49+
$return = "";
50+
$languages = array();
51+
52+
$h1 = new \Highlight\Highlighter();
53+
54+
$last = 0;
55+
for ($i = 0; $i < $size; ++$i) {
56+
$length = sizeof($codeArr[$i]);
57+
$codeBlocks = array_slice($code, $last, $length);
58+
59+
$last += $length;
60+
61+
$codeblock = createCodeBlock($codeBlocks, $descriptions[$i], $codevariants[$i]->getAttribute('lang'), $parser, $h1);
62+
$return .= createTab($codeblock[0], $i);
63+
array_push($languages, $codeblock[1]);
64+
}
65+
66+
return array(createFrame($languages, $return), 'markerType' => 'nowiki');
1667
}
1768

18-
require_once 'require.php';
69+
/**
70+
* Returns a human-readable-version of the language.
71+
*
72+
* @param string $lang The specific language token
73+
*
74+
* @return string The replaced language.
75+
*/
76+
function replaceLang(string $lang) {
77+
$file = file_get_contents(__DIR__ . '/languages/languages.json');
78+
$languages = json_decode($file, true);
79+
80+
return $languages[$lang];
81+
}
1982

2083
/**
21-
* The main class for the MultiCodeBlock MediaWiki-Extension.
84+
* Returns the combined version of the code and the description.
85+
*
86+
* @param string $code The code inside the `<code>` element
87+
* @param Description $desc The description as a Description object
88+
* @param Parser $parser The parser object by MediaWiki
2289
*
23-
* @author QuickWrite
90+
* @return string A combined version of the code and the description with the MediaWiki syntax.
2491
*/
25-
class MultiCodeBlock {
26-
/**
27-
* Sets a hook for the MediaWiki parser to be able to use the <multicodeblock>-Tag in the MediaWiki syntax.
28-
*
29-
* @param Parser &$parser The Parser Element as a reference.
30-
*/
31-
public static function onParserFirstCallInit( Parser &$parser ) {
32-
$parser->setHook( 'multicodeblock', [ self::class, 'renderMultiCodeBlock' ] );
33-
}
34-
35-
/**
36-
* Returns a string based on the MultiCodeBlock HTML-Element
37-
*
38-
* @param string $input The content of the MultiCodeBlock HTML-Element
39-
* @param array $args The arguments of the MultiCodeBlock HTML-Element
40-
* @param Parser $parser The MediaWiki syntax parser
41-
* @param PPFrame $frame MediaWiki frame
42-
*/
43-
public static function renderMultiCodeBlock( string $input, array $args, Parser $parser, PPFrame $frame ) {
44-
$out = $parser->getOutput();
45-
$out->addModuleStyles( [ 'ext.multicodeblock.styles' ] );
46-
$out->addModules( [ 'ext.multicodeblock.js' ] );
47-
48-
$code = findCodeBlocks($input);
49-
50-
$replaced = str_replace($code, 'test', $input);
51-
$dom = getDOM($replaced);
52-
53-
$codevariants = $dom->getElementsbyTagName('codeblock');
54-
55-
$descriptions = [];
56-
foreach($codevariants as $codevariant) {
57-
array_push($descriptions, $codevariant->getElementsbyTagName('desc'));
58-
}
59-
$codeArr = [];
60-
foreach($codevariants as $codevariant) {
61-
array_push($codeArr, $codevariant->getElementsbyTagName('code'));
62-
}
63-
64-
$size = sizeof($codevariants);
65-
$return = "";
66-
$languages = array();
67-
68-
$h1 = new \Highlight\Highlighter();
69-
70-
$last = 0;
71-
for($i = 0; $i < $size; ++$i) {
72-
$length = sizeof($codeArr[$i]);
73-
$codeBlocks = array_slice($code, $last, $length);
74-
75-
$last += $length;
76-
77-
$codeblock = createCodeBlock($codeBlocks, $descriptions[$i], $codevariants[$i]->getAttribute('lang'), $parser, $h1);
78-
$return .= createTab($codeblock[0], $i);
79-
array_push($languages, $codeblock[1]);
80-
}
81-
82-
return array(createFrame($languages, $return), 'markerType' => 'nowiki');
83-
}
84-
}
92+
function &combineCodeDescription(string $code, Description &$desc, Parser &$parser) {
93+
$arr = explode("\n", $code);
94+
$size = sizeof($arr);
95+
96+
$keysSize = sizeof($desc->keys);
97+
98+
$return = '<table class="code-table">
99+
<tr class="table-header">
100+
<th>Code</th>
101+
<th>Description</th>
102+
</tr>
103+
';
104+
105+
$isFirst = ($arr[0] === '' ? true : false);
106+
107+
for ($i = (!$isFirst ? 0 : 1), $j = 0; $i < $size; ++$j) {
108+
$return .= '<tr><th class="first"><pre><ol start="' . ($i + 1 - $isFirst) . '">';
109+
110+
$nextKey = 0;
111+
if ($keysSize > $j + 1) {
112+
$nextKey = $desc->keys[$j + 1] - 1 + $isFirst;
113+
114+
if ($nextKey > $size) {
115+
$nextKey = $size;
116+
}
117+
} else {
118+
$nextKey = $size;
119+
}
120+
121+
while ($i < $nextKey) {
122+
if (!($i + 1 == $size && $arr[$i] === ''))
123+
$return .= '<li><span class="line">' . ($arr[$i] !== '' ? $arr[$i] : '&nbsp;') . '</span></li>';
124+
125+
$i++;
126+
}
127+
128+
$return .= '</pre></ol></th><th class="second mw-body-content">' . $parser->recursiveTagParse($desc->texts[$j]) . '</td>';
129+
}
130+
131+
$return .= '</table>';
132+
133+
return $return;
134+
}

includes/MultiCodeBlockBuilder.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

includes/class/Code.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
* @param string $lang The language the code should be highlighted in.
1818
*/
1919
class Code {
20-
public $code;
21-
public $lang;
20+
public string $code = '';
21+
public string $lang = '';
2222

23-
public function __construct(&$code, $lang) {
23+
public function __construct(string &$code, string $lang) {
2424
$this->setCode($code, $lang);
2525
}
2626

@@ -31,7 +31,7 @@ public function __construct(&$code, $lang) {
3131
* @return string The highlighted version the the $code.
3232
*/
3333
public function &highlight(\Highlight\Highlighter &$hl) {
34-
$highlighted;
34+
$highlighted = null;
3535

3636
try {
3737
$highlighted = $hl->highlight($this->lang, $this->code);
@@ -48,7 +48,7 @@ public function &highlight(\Highlight\Highlighter &$hl) {
4848
* @param string $code The code that should be inserted into the class
4949
* @param string $lang The language the code is written in.
5050
*/
51-
public function setCode($code, $lang) {
51+
public function setCode(string $code, string &$lang) {
5252
$this->code = $code;
5353
$this->lang = $lang;
5454
}

includes/class/Description.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
* file that was distributed with this source code.
77
*/
88

9-
require_once __DIR__ . '/../vendor/autoload.php';
10-
119
/**
1210
* Stores the description of the code.
1311
*
@@ -16,14 +14,13 @@
1614
* @global array $texts An array of all the different descriptions.
1715
* @global array $keys The lines where the descriptions should start.
1816
*
19-
* @param string $dom A string that has the content of the <desc>-element.
17+
* @param DOMDocument $dom A string that has the content of the <desc>-element.
2018
*/
2119
class Description {
2220
public $texts = array();
2321
public $keys = array();
24-
private $parser;
2522

26-
public function __construct($dom = null) {
23+
public function __construct(DOMDocument $dom = null) {
2724
if($dom === null) {
2825
array_push($this->texts, '');
2926
array_push($this->keys, 1);
@@ -50,9 +47,9 @@ public function getNext($index) {
5047
/**
5148
* Inserts the description into the attrributes.
5249
*
53-
* @param string $dom A string that has the content of the <desc>-element.
50+
* @param DOMDocument $dom A string that has the content of the <desc>-element.
5451
*/
55-
public function setTexts(&$dom) {
52+
public function setTexts(DOMDocument &$dom) {
5653
$positions = $dom->getElementsByTagName('position');
5754
$numberOfPos = sizeof($positions);
5855

0 commit comments

Comments
 (0)