Debian Codegraph @

extensions/Math/maintenance/GenerateDoc.php

raw symbols SCIP
1 <?php
2
3 require_once __DIR__ . '/../../../maintenance/Maintenance.php';
4
5 use MediaWiki\Extension\Math\WikiTexVC\TexUtil;
6
7 class GenerateDoc extends Maintenance {
8
9 private array $baseElements;
10 private array $letterMods;
11 private array $literals;
12 private array $omitElements;
13 private array $sets = [
14 'big_literals',
15 'box_functions',
16 'color_function',
17 'declh_function',
18 'definecolor_function',
19 'fun_ar1',
20 'fun_ar1nb',
21 'fun_ar1opt',
22 'fun_ar2',
23 'fun_ar2nb',
24 'fun_infix',
25 'fun_mhchem',
26 'hline_function',
27 'latex_function_names',
28 'left_function',
29 'mediawiki_function_names',
30 'mhchem_bond',
31 'mhchem_macro_1p',
32 'mhchem_macro_2p',
33 'mhchem_macro_2pc',
34 'mhchem_macro_2pu',
35 'mhchem_single_macro',
36 'nullary_macro',
37 'nullary_macro_in_mbox',
38 'other_delimiters1',
39 'other_delimiters2',
40 'right_function',
41 ];
42 private array $argCounts = [
43 'big_literals' => 1,
44 'box_functions' => 1,
45 'color_function' => 1,
46 'definecolor_function' => 1,
47 'fun_ar1' => 1,
48 'fun_ar1nb' => 1,
49 'fun_ar1opt' => 1,
50 'fun_ar2' => 2,
51 'fun_ar2nb' => 5,
52 'fun_infix' => 1,
53 'fun_mhchem' => 1,
54 'left_function' => 1,
55 'mhchem_bond' => 1,
56 'mhchem_macro_1p' => 1,
57 'mhchem_macro_2p' => 2,
58 'mhchem_macro_2pu' => 1,
59 'right_function' => 1,
60 ];
61 private array $sampleArgs = [
62 'big_literals' => '(',
63 'color_function' => '{red}{red}',
64 'definecolor_function' => '{mycolor}{cmyk}{.4,1,1,0}',
65 'fun_ar2nb' => '{_1^2}{_3^4}\sum',
66 'left_function' => '( \right.',
67 'mhchem_bond' => '{-}',
68 'mhchem_macro_2pc' => '{red}{red}',
69 'right_function' => ')',
70 ];
71
72 public function __construct() {
73 parent::__construct();
74 $this->baseElements = TexUtil::getInstance()->getBaseElements();
75 $this->letterMods = array_keys( $this->baseElements['is_letter_mod'] );
76 $this->literals = array_keys( $this->baseElements['is_literal'] );
77 $this->omitElements = array_merge( array_keys( $this->baseElements['mhchemtexified_required'] ),
78 array_keys( $this->baseElements['intent_required'] ) );
79 }
80
81 private function printSample( string $set, string $elem ): string {
82 $count = $this->argCounts[$set] ?? 0;
83 if ( in_array( $elem, $this->omitElements ) ) {
84 // no output for special mhchem implementation
85 return '';
86 }
87
88 $textString = str_replace( '\\', '\\textbackslash ', $elem );
89
90 if ( $set === 'fun_infix' ) {
91 return "\\texttt{{$textString}} applied on \$ x, y\$ is rendered as \$x{$elem} y\$";
92 }
93 if ( $set === 'hline_function' ) {
94 return "\\texttt{{$textString}} applied in a table is rendered as \$"
95 . "\\begin{matrix} x_{11} & x_{12} \\\\ \hline \\end{matrix}\$";
96 }
97 if ( $set === 'mediawiki_function_names' ) {
98 return "\\texttt{{$textString}} is rendered as \$\\operatorname{" . substr( $elem, 1 ) . "} y\$";
99 }
100 if ( $set === 'right_function' ) {
101 return "\\texttt{{$textString}} is rendered as \$\\left. \\right)\$";
102 }
103
104 if ( in_array( $elem, [ '\\limits', '\\nolimits' ] ) ) {
105 return "\\texttt{{$textString}} is rendered for example as \$\\mathop\\cap{$elem}_a^b\$";
106 }
107 if ( $elem === '\\pagecolor' ) {
108 return '\\texttt{\\textbackslash pagecolor} is not rendered.';
109 }
110 if ( $elem === '\\ca' ) {
111 return '\\texttt{\\textbackslash ca} was never used. \\newline ' .
112 ' \\url{https://phabricator.wikimedia.org/T323878}';
113 }
114
115 $args = $this->sampleArgs[$set] ?? str_repeat( '{x}', $count );
116 $argDesc = $count > 1 ? "applied on \${$args}\$ " : '';
117 $rendering = strpos( $set, 'mhchem' ) === 0 ? "\\ce{{$elem}{$args}}" : $elem . $args;
118
119 return "\\texttt{{$textString}} {$argDesc}is rendered as \${$rendering}\$";
120 }
121
122 private function writeFile( string $filename, string $content ): void {
123 $filepath = __DIR__ . '/../doc/' . $filename . '.tex';
124 if ( file_put_contents( $filepath, $content ) === false ) {
125 echo "Error saving document" . PHP_EOL;
126 } else {
127 echo "The file \"{$filename}\" was saved!" . PHP_EOL;
128 }
129 }
130
131 private function printMod( string $x ): string {
132 $x_str = str_replace( '\\', '\\textbackslash ', $x );
133
134 return "\\texttt{{$x_str}} applied on \$x,X\$ is rendered as \${$x}{x},{$x}{X}\$\n\n";
135 }
136
137 private function printLiteral( string $x ): string {
138 $x_str = str_replace( '\\', '\\textbackslash ', $x );
139 return "\\texttt{{$x_str}} is rendered as \${$x}\$\n\n";
140 }
141
142 public function execute() {
143 $this->writeFile( 'commands', implode( "\n",
144 array_map( [ $this, 'printMod' ], $this->letterMods ) ) );
145 $this->writeFile( 'literals', implode( "\n",
146 array_map( [ $this, 'printLiteral' ], $this->literals ) ) );
147 $this->writeFile( 'groups', implode( "\n\n",
148 array_map( function ( $set ) {
149 return "\\section{ Group \\texttt{" .
150 str_replace( '_', '\\textunderscore ', $set )
151 . "}}\n\n" .
152 implode( "\n\n",
153 array_map( fn ( $elem ) => $this->printSample( $set, $elem ),
154 array_keys( $this->baseElements[$set] ) ) );
155 }, $this->sets ) ) );
156 }
157 }
158 $maintClass = GenerateDoc::class;
159 require_once RUN_MAINTENANCE_IF_MAIN;