Skip to content

Commit 7de585c

Browse files
committed
Split up the code.
1 parent 448adf2 commit 7de585c

File tree

5 files changed

+151
-123
lines changed

5 files changed

+151
-123
lines changed

includes/MultiCodeBlock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
die( -1 );
1616
}
1717

18-
require_once 'MultiCodeBlockBuilder.php';
18+
require_once 'require.php';
1919

2020
/**
2121
* The main class for the MultiCodeBlock MediaWiki-Extension.

includes/MultiCodeBlockBuilder.php

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

9-
require_once __DIR__ . '/vendor/autoload.php';
10-
11-
require_once 'class/Description.php';
12-
require_once 'class/Code.php';
13-
require_once 'class/LanguageBlock.php';
14-
15-
/**
16-
* Returns the DOM-Parser with custom options and the HTML-Tree
17-
*
18-
* @param string $content The content that should be parsed by the DOM-Parser
19-
*
20-
* @return PHPHtmlParser\Dom The DOM-Element that has the HTML-Tree
21-
*/
22-
function getDOM(&$content) {
23-
$dom = new DOMDocument();
24-
25-
$dom->validateOnParse = false;
26-
27-
libxml_use_internal_errors(true);
28-
$dom->loadHTML($content);
29-
libxml_clear_errors();
30-
31-
return $dom;
32-
}
33-
34-
/**
35-
* Creates an HTML-Element to warp around the content.
36-
*
37-
* @param string $code The code where the Element should wrap around
38-
* @param int $index The index of the element
39-
* @param string $extra Another class to be added to the element.
40-
*
41-
* @return string The full object.
42-
*/
43-
function createTab(string &$code, int $index, string $extra = 'outer') {
44-
return '<div class="'.$extra.' tab-content '.($index == 0 ? 'tc-active' : '').'" data-tab="'.$index.'">'.$code.'</div>';
45-
}
46-
47-
/**
48-
* Returns the MultiCodeBlock as a whole.
49-
*
50-
* @param array $lang All of the languages of the different codeblocks.
51-
* @param string $code The codeblocks as a whole.
52-
* @param bool $addCopy If a copy button should be added to the element.
53-
* @param string $extra Another class to be added to the element.
54-
*
55-
* @return string The whole MultiCodeBlock.
56-
*/
57-
function createFrame(array &$lang, string &$code, bool $addCopy = true, string $extra = 'outer') {
58-
$size = sizeof($lang);
59-
$copyIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0z" fill="none"/><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>';
60-
61-
$return = '<div class="multicodeblock">
62-
'.($addCopy ? '<div class="copy" title="Copy code to clipboard">'.$copyIcon.'<span class="tooltip">Failed to copy!</span></div>': '').'
63-
<div class="'.$extra.' tabs">
64-
<div class="'.$extra.' tab-sidebar">
65-
';
66-
for($i = 0; $i < $size; $i++) {
67-
$return .= '<button class="'.$extra.' tab-button '.($i == 0 ? 'tb-active' : '').'" data-for-tab="'.$i.'">'.$lang[$i].'</button>';
68-
}
69-
$return .= '</div>'.$code.'</div></div>';
70-
71-
return $return;
72-
}
9+
require_once 'require.php';
7310

7411
/**
7512
* Returns a human-readable-version of the language.
@@ -85,51 +22,6 @@ function replaceLang(string $lang) {
8522
return $languages[$lang];
8623
}
8724

88-
/**
89-
* Returns a single codeblock
90-
*
91-
* @param string $codeTags The code inside the `<codevariant>` block.
92-
* @param DOMElement $descriptions The content of the `<desc>` element.
93-
* @param string $lang The language of the `<codevariant>`.
94-
* @param Parser $parser The parser object by MediaWiki.
95-
* @param Highlighter $h1 The highlighter object.
96-
*
97-
* @return array The codeblock as the first element and the language as the second element.
98-
*/
99-
function createCodeBlock(array &$codeTags, DOMNodeList &$descriptions, $lang, Parser &$parser, \Highlight\Highlighter &$h1) {
100-
if($lang == null) {
101-
return array('<span style="color: red; font-size: 700;">No Lang Attribute</span>', 'No lang');
102-
}
103-
104-
$lang = strtolower($lang);
105-
106-
$languageBlock = new LanguageBlock($codeTags, $descriptions, $lang);
107-
108-
$return = '';
109-
110-
$versions = [];
111-
112-
for($i = 0;$i < $languageBlock->size; ++$i) {
113-
if($languageBlock->code[$i] === null) {
114-
continue;
115-
}
116-
117-
$highlight = $languageBlock->code[$i]->highlight($h1);
118-
119-
$isObject = true;
120-
if(!isset($highlight->value)) {
121-
$isObject = false;
122-
}
123-
124-
array_push($versions, 'Version #'.($i + 1));
125-
$return .= createTab(combineCodeDescription(($isObject ? $highlight->value : $highlight), $languageBlock->getDescription($i), $parser), $i, 'inner');
126-
}
127-
128-
$return = createFrame($versions, $return, false, 'inner');
129-
130-
return array($return, replaceLang($lang));
131-
}
132-
13325
/**
13426
* Returns the combined version of the code and the description.
13527
*
@@ -182,16 +74,3 @@ function &combineCodeDescription(string $code, Description &$desc, Parser &$pars
18274

18375
return $return;
18476
}
185-
186-
/**
187-
* Returns the code in the `<code>` tags.
188-
*
189-
* @param string $codeblock The object where all of the codeblocks should be removed.
190-
*
191-
* @return array An array of the code.
192-
*/
193-
function findCodeBlocks(string &$codeblock) {
194-
preg_match_all("(< *code *>((.|\n)*?)<\/code *>)", $codeblock, $array);
195-
196-
return $array[1];
197-
}

includes/require.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* This file is part of MultiCodeBlock.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
require_once __DIR__ . '/vendor/autoload.php';
10+
11+
require_once 'class/Description.php';
12+
require_once 'class/Code.php';
13+
require_once 'class/LanguageBlock.php';
14+
15+
require_once 'utils/HTMLFramework.php';
16+
require_once 'utils/getData.php';
17+
18+
require_once 'MultiCodeBlockBuilder.php';

includes/utils/HTMLFramework.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* This file is part of MultiCodeBlock.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
/**
10+
* Creates an HTML-Element to warp around the content.
11+
*
12+
* @param string $code The code where the Element should wrap around
13+
* @param int $index The index of the element
14+
* @param string $extra Another class to be added to the element.
15+
*
16+
* @return string The full object.
17+
*/
18+
function createTab(string &$code, int $index, string $extra = 'outer') {
19+
return '<div class="'.$extra.' tab-content '.($index == 0 ? 'tc-active' : '').'" data-tab="'.$index.'">'.$code.'</div>';
20+
}
21+
22+
/**
23+
* Returns the MultiCodeBlock as a whole.
24+
*
25+
* @param array $lang All of the languages of the different codeblocks.
26+
* @param string $code The codeblocks as a whole.
27+
* @param bool $addCopy If a copy button should be added to the element.
28+
* @param string $extra Another class to be added to the element.
29+
*
30+
* @return string The whole MultiCodeBlock.
31+
*/
32+
function createFrame(array &$lang, string &$code, bool $addCopy = true, string $extra = 'outer') {
33+
$size = sizeof($lang);
34+
$copyIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0z" fill="none"/><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>';
35+
36+
$return = '<div class="multicodeblock">
37+
'.($addCopy ? '<div class="copy" title="Copy code to clipboard">'.$copyIcon.'<span class="tooltip">Failed to copy!</span></div>': '').'
38+
<div class="'.$extra.' tabs">
39+
<div class="'.$extra.' tab-sidebar">
40+
';
41+
for($i = 0; $i < $size; $i++) {
42+
$return .= '<button class="'.$extra.' tab-button '.($i == 0 ? 'tb-active' : '').'" data-for-tab="'.$i.'">'.$lang[$i].'</button>';
43+
}
44+
$return .= '</div>'.$code.'</div></div>';
45+
46+
return $return;
47+
}
48+
49+
/**
50+
* Returns a single codeblock
51+
*
52+
* @param string $codeTags The code inside the `<codevariant>` block.
53+
* @param DOMElement $descriptions The content of the `<desc>` element.
54+
* @param string $lang The language of the `<codevariant>`.
55+
* @param Parser $parser The parser object by MediaWiki.
56+
* @param Highlighter $h1 The highlighter object.
57+
*
58+
* @return array The codeblock as the first element and the language as the second element.
59+
*/
60+
function createCodeBlock(array &$codeTags, DOMNodeList &$descriptions, $lang, Parser &$parser, \Highlight\Highlighter &$h1) {
61+
if($lang == null) {
62+
return array('<span style="color: red; font-size: 700;">No Lang Attribute</span>', 'No lang');
63+
}
64+
65+
$lang = strtolower($lang);
66+
67+
$languageBlock = new LanguageBlock($codeTags, $descriptions, $lang);
68+
69+
$return = '';
70+
71+
$versions = [];
72+
73+
for($i = 0;$i < $languageBlock->size; ++$i) {
74+
if($languageBlock->code[$i] === null) {
75+
continue;
76+
}
77+
78+
$highlight = $languageBlock->code[$i]->highlight($h1);
79+
80+
$isObject = true;
81+
if(!isset($highlight->value)) {
82+
$isObject = false;
83+
}
84+
85+
array_push($versions, 'Version #'.($i + 1));
86+
$return .= createTab(combineCodeDescription(($isObject ? $highlight->value : $highlight), $languageBlock->getDescription($i), $parser), $i, 'inner');
87+
}
88+
89+
$return = createFrame($versions, $return, false, 'inner');
90+
91+
return array($return, replaceLang($lang));
92+
}

includes/utils/getData.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* This file is part of MultiCodeBlock.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
/**
10+
* Returns the DOM-Parser with custom options and the HTML-Tree
11+
*
12+
* @param string $content The content that should be parsed by the DOM-Parser
13+
*
14+
* @return PHPHtmlParser\Dom The DOM-Element that has the HTML-Tree
15+
*/
16+
function getDOM(&$content) {
17+
$dom = new DOMDocument();
18+
19+
$dom->validateOnParse = false;
20+
21+
libxml_use_internal_errors(true);
22+
$dom->loadHTML($content);
23+
libxml_clear_errors();
24+
25+
return $dom;
26+
}
27+
28+
/**
29+
* Returns the code in the `<code>` tags.
30+
*
31+
* @param string $codeblock The object where all of the codeblocks should be removed.
32+
*
33+
* @return array An array of the code.
34+
*/
35+
function findCodeBlocks(string &$codeblock) {
36+
preg_match_all("(< *code *>((.|\n)*?)<\/code *>)", $codeblock, $array);
37+
38+
return $array[1];
39+
}

0 commit comments

Comments
 (0)