Skip to content

Commit e93503b

Browse files
committed
Added comments
Added comments for readability and maintainability of the code.
1 parent 4cccdb3 commit e93503b

File tree

5 files changed

+116
-5
lines changed

5 files changed

+116
-5
lines changed

includes/MultiCodeBlock.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
require_once 'highlight/Highlighter.php';
1212

13+
/**
14+
* The main class for the MultiCodeBlock MediaWiki-Extension.
15+
*
16+
* @author QuickWrite
17+
*/
1318
class MultiCodeBlock {
1419
public static function onParserFirstCallInit( Parser &$parser ) {
1520
$parser->setHook( 'multicodeblock', [ self::class, 'renderMultiCodeBlock' ] );
@@ -20,6 +25,14 @@ public static function onBeforePageDisplay( OutputPage &$out ) {
2025
$out->addModules( [ 'ext.multicodeblock.js' ] );
2126
}
2227

28+
/**
29+
* Returns a string based on the MultiCodeBlock HTML-Element
30+
*
31+
* @param string $input The content of the MultiCodeBlock HTML-Element
32+
* @param array $args The arguments of the MultiCodeBlock HTML-Element
33+
* @param Parser $parser The MediaWiki syntax parser
34+
* @param PPFrame $frame MediaWiki frame
35+
*/
2336
public static function renderMultiCodeBlock( $input, array $args, Parser $parser, PPFrame $frame ) {
2437
$code = findCodeBlocks($input);
2538

includes/highlight/Code.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
<?php
22

3+
/**
4+
* Saves the code and the language as well as highlights the code.
5+
*
6+
* @author QuickWrite
7+
* @global string $code The code stored in raw text format.
8+
* @global string $lang The language that was inputted.
9+
*
10+
* @param string $code The code that should be highlighted.
11+
* @param string $lang The language the code should be highlighted in.
12+
*/
313
class Code {
414
public $code;
515
public $lang;
6-
16+
717
public function __construct(&$code, $lang) {
818
$this->setCode($code, $lang);
919
}
1020

21+
/**
22+
* Returns a highlighted version of the code.
23+
*
24+
* @param Highlighter $h1 The highlighter class og Highlight.php to highlight the code.
25+
* @return string The highlighted version the the $code.
26+
*/
1127
public function &highlight(\Highlight\Highlighter &$hl) {
1228
$highlighted;
1329

@@ -20,6 +36,12 @@ public function &highlight(\Highlight\Highlighter &$hl) {
2036
return $highlighted;
2137
}
2238

39+
/**
40+
* Inserts the code into the attributes.
41+
*
42+
* @param string $code The code that should be inserted into the class
43+
* @param string $lang The language the code is written in.
44+
*/
2345
public function setCode($code, $lang) {
2446
$this->code = $code;
2547
$this->lang = $lang;

includes/highlight/CustomOptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22
require_once __DIR__ . '/../vendor/autoload.php';
33

4+
/**
5+
* Returns specific options for the HTML-Parser.
6+
*
7+
* @return \PHPHtmlParser\Options The options as a PHPHtmlParser class.
8+
*/
49
function getOptions() {
510
$options = new \PHPHtmlParser\Options();
611
$options->setPreserveLineBreaks(true);

includes/highlight/Description.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
use PHPHtmlParser\Dom;
66

7+
/**
8+
* Stores the description of the code.
9+
*
10+
* @author QuickWrite
11+
*
12+
* @global array $texts An array of all the different descriptions.
13+
* @global array $keys The lines where the descriptions should start.
14+
*
15+
* @param string $dom A string that has the content of the <desc>-element.
16+
*/
717
class Description {
818
public $texts = array();
919
public $keys = array();
@@ -19,13 +29,25 @@ public function __construct($dom = null) {
1929
$this->setTexts($dom);
2030
}
2131

32+
/**
33+
* Returns the last line the current description.
34+
*
35+
* @param int $index The index of the textblock.
36+
*
37+
* @return int The last line of the textblock (based on $index).
38+
*/
2239
public function getNext($index) {
2340
if($index + 1 > sizeof($this->keys))
2441
return null;
2542

2643
return $this->keys[$index] - 1;
2744
}
2845

46+
/**
47+
* Inserts the description into the attrributes.
48+
*
49+
* @param string $dom A string that has the content of the <desc>-element.
50+
*/
2951
public function setTexts(&$dom) {
3052

3153
$content = new Dom();

includes/highlight/Highlighter.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,29 @@
77

88
use PHPHtmlParser\Dom;
99

10+
/**
11+
* Returns the DOM-Parser with custom options and the HTML-Tree
12+
*
13+
* @param string $content The content that should be parsed by the DOM-Parser
14+
*
15+
* @return PHPHtmlParser\Dom The DOM-Element that has the HTML-Tree
16+
*/
1017
function getDOM(&$content) {
1118
$dom = new Dom();
1219
$dom->loadStr($content, getOptions());
1320

1421
return $dom;
1522
}
1623

17-
function createFrame($lang, &$code) {
24+
/**
25+
* Returns the MultiCodeBlock as a whole.
26+
*
27+
* @param array $lang All of the languages of the different codeblocks.
28+
* @param string $code The codeblocks as a whole.
29+
*
30+
* @return string The whole MultiCodeBlock.
31+
*/
32+
function createFrame(array $lang, string &$code) {
1833
$size = sizeof($lang);
1934
$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>';
2035

@@ -32,13 +47,31 @@ function createFrame($lang, &$code) {
3247
return $return;
3348
}
3449

35-
function replaceLang($lang) {
50+
/**
51+
* Returns a human-readable-version of the language.
52+
*
53+
* @param string $lang The specific language token
54+
*
55+
* @return string The replaced language.
56+
*/
57+
function replaceLang(string $lang) {
3658
$file = file_get_contents(__DIR__ . '/languages/languages.json');
3759
$languages = json_decode($file, true);
3860

3961
return $languages[$lang];
4062
}
4163

64+
/**
65+
* Returns a single codeblock
66+
*
67+
* @param DOMElement $codevariant The content of the `<codevariant>` element.
68+
* @param string $code The code inside the `<codevariant>` block.
69+
* @param DOMElement $description The content of the `<desc>` element.
70+
* @param Parser $parser The parser object by MediaWiki
71+
* @param Highlighter $h1 The highlighter object
72+
*
73+
* @return array The codeblock as the first element and the language as the second element.
74+
*/
4275
function createCodeBlock(&$codevariant, &$code, &$description, Parser &$parser, \Highlight\Highlighter &$h1) {
4376

4477
$lang = $codevariant->getAttribute("lang");
@@ -60,7 +93,16 @@ function createCodeBlock(&$codevariant, &$code, &$description, Parser &$parser,
6093
return array(combineCodeDescription(($isObject ? $highlight->value : $highlight), $desc, $parser), ($isObject ? replaceLang($highlight->language) : $lang));
6194
}
6295

63-
function combineCodeDescription($code, Description &$desc, Parser &$parser) {
96+
/**
97+
* Returns the combined version of the code and the description.
98+
*
99+
* @param string $code The code inside the `<code>` element
100+
* @param Description $desc The description as a Description object
101+
* @param Parser $parser The parser object by MediaWiki
102+
*
103+
* @return string A combined version of the code and the description with the MediaWiki syntax.
104+
*/
105+
function combineCodeDescription(string $code, Description &$desc, Parser &$parser) {
64106
$arr = explode("\n", $code);
65107
$size = sizeof($arr);
66108

@@ -99,7 +141,14 @@ function combineCodeDescription($code, Description &$desc, Parser &$parser) {
99141
return $return;
100142
}
101143

102-
function findCodeBlocks(&$codeblock) {
144+
/**
145+
* Returns the code in the `<code>` tags.
146+
*
147+
* @param string $codeblock The object where all of the codeblocks should be removed.
148+
*
149+
* @return array An array of the code.
150+
*/
151+
function findCodeBlocks(string &$codeblock) {
103152
preg_match_all("(< *code *>((.|\n)*?)<\/code *>)", $codeblock, $array);
104153

105154
return $array[1];

0 commit comments

Comments
 (0)