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 ] : ' ' ) . '</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+ }
0 commit comments