| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
namespace MediaWiki\Extension\Math\WikiTexVC\Nodes; |
| 6 |
|
| 7 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\BaseMethods; |
| 8 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLParsingUtil; |
| 9 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util\MMLutil; |
| 10 |
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmi; |
| 11 |
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmn; |
| 12 |
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmo; |
| 13 |
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmpadded; |
| 14 |
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmrow; |
| 15 |
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmstyle; |
| 16 |
use MediaWiki\Extension\Math\WikiTexVC\TexUtil; |
| 17 |
|
| 18 |
class Literal extends TexNode { |
| 19 |
|
| 20 |
|
| 21 |
private $arg; |
| 22 |
|
| 23 |
private $literals; |
| 24 |
|
| 25 |
private $extendedLiterals; |
| 26 |
|
| 27 |
public function __construct( string $arg ) { |
| 28 |
parent::__construct( $arg ); |
| 29 |
$this->arg = $arg; |
| 30 |
$this->literals = array_keys( TexUtil::getInstance()->getBaseElements()['is_literal'] ); |
| 31 |
$this->extendedLiterals = $this->literals; |
| 32 |
array_push( $this->extendedLiterals, '\\infty', '\\emptyset' ); |
| 33 |
} |
| 34 |
|
| 35 |
public function changeUnicodeFontInput( string $input, array &$state ): string { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
if ( isset( $state["double-struck-literals"] ) ) { |
| 42 |
return MMLParsingUtil::mapToDoubleStruckUnicode( $input ); |
| 43 |
} elseif ( isset( $state["calligraphic"] ) ) { |
| 44 |
return MMLParsingUtil::mapToCaligraphicUnicode( $input ); |
| 45 |
} elseif ( isset( $state["fraktur"] ) ) { |
| 46 |
return MMLParsingUtil::mapToFrakturUnicode( $input ); |
| 47 |
} elseif ( isset( $state["bold"] ) ) { |
| 48 |
return MMLParsingUtil::mapToBoldUnicode( $input ); |
| 49 |
} |
| 50 |
return $input; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
public function renderMML( $arguments = [], &$state = [] ) { |
| 55 |
if ( isset( $state["intent-params"] ) ) { |
| 56 |
foreach ( $state["intent-params"] as $intparam ) { |
| 57 |
if ( $intparam == $this->arg ) { |
| 58 |
$arguments["arg"] = $intparam; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if ( isset( $state["intent-params-expl"] ) ) { |
| 64 |
$arguments["arg"] = $state["intent-params-expl"]; |
| 65 |
} |
| 66 |
|
| 67 |
if ( $this->arg === " " ) { |
| 68 |
|
| 69 |
|
| 70 |
return ""; |
| 71 |
} |
| 72 |
|
| 73 |
if ( is_numeric( $this->arg ) ) { |
| 74 |
$mn = new MMLmn( "", $arguments ); |
| 75 |
return $mn->encapsulateRaw( $this->changeUnicodeFontInput( $this->arg, $state ) ); |
| 76 |
} |
| 77 |
|
| 78 |
$foundOperatorContent = MMLutil::initalParseLiteralExpression( $this->arg ); |
| 79 |
if ( !$foundOperatorContent ) { |
| 80 |
$input = $this->arg; |
| 81 |
$operatorContent = null; |
| 82 |
} else { |
| 83 |
$input = $foundOperatorContent[1][0]; |
| 84 |
$operatorContent = [ "foundOC" => $foundOperatorContent[2][0] ]; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if ( str_contains( $input, "\\operatorname" ) ) { |
| 90 |
$mi = new MMLmi(); |
| 91 |
return $mi->encapsulateRaw( $operatorContent["foundOC"] ); |
| 92 |
} |
| 93 |
|
| 94 |
$inputP = $input; |
| 95 |
|
| 96 |
|
| 97 |
$bm = new BaseMethods(); |
| 98 |
$noStretchArgs = $arguments; |
| 99 |
|
| 100 |
$noStretchArgs['stretchy'] ??= 'false'; |
| 101 |
$ret = $bm->checkAndParseOperator( $inputP, $this, $noStretchArgs, $operatorContent, $state, false ); |
| 102 |
if ( $ret ) { |
| 103 |
return $ret; |
| 104 |
} |
| 105 |
|
| 106 |
$bm = new BaseMethods(); |
| 107 |
$ret = $bm->checkAndParseMathCharacter( $inputP, $this, $arguments, $operatorContent, false ); |
| 108 |
if ( $ret ) { |
| 109 |
return $ret; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
$ret = $bm->checkAndParseIdentifier( $inputP, $this, $arguments, $operatorContent, false ); |
| 114 |
if ( $ret ) { |
| 115 |
return $ret; |
| 116 |
} |
| 117 |
|
| 118 |
$ret = $bm->checkAndParseDelimiter( $input, $this, $noStretchArgs, $operatorContent ); |
| 119 |
if ( $ret ) { |
| 120 |
return $ret; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
$ret = BaseMethods::checkAndParse( $inputP, $arguments, |
| 125 |
array_merge( $operatorContent ?? [], $state ?? [] ), |
| 126 |
$this, false ); |
| 127 |
if ( $ret || $ret === '' ) { |
| 128 |
return $ret; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
if ( !( empty( $state['inMatrix'] ) ) && trim( $this->arg ) === '\vline' ) { |
| 133 |
return $this->createVlineElement(); |
| 134 |
} |
| 135 |
|
| 136 |
if ( !( empty( $state['inHBox'] ) ) ) { |
| 137 |
|
| 138 |
return $input; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
$mi = new MMLmi( "", $arguments ); |
| 143 |
return $mi->encapsulateRaw( $this->changeUnicodeFontInput( $input, $state ) ); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function getArg(): string { |
| 150 |
return $this->arg; |
| 151 |
} |
| 152 |
|
| 153 |
public function setArg( string $arg ) { |
| 154 |
$this->arg = $arg; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function getLiterals(): array { |
| 161 |
return $this->literals; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function getExtendedLiterals(): array { |
| 168 |
return $this->extendedLiterals; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
public function extractIdentifiers( $args = null ) { |
| 173 |
return $this->getLiteral( $this->literals, '/^([a-zA-Z\']|\\\\int)$/' ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
public function extractSubscripts() { |
| 178 |
return $this->getLiteral( $this->extendedLiterals, '/^([0-9a-zA-Z+\',-])$/' ); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
public function getModIdent() { |
| 183 |
if ( $this->arg === '\\ ' ) { |
| 184 |
return [ '\\ ' ]; |
| 185 |
} |
| 186 |
return $this->getLiteral( $this->literals, '/^([0-9a-zA-Z\'])$/' ); |
| 187 |
} |
| 188 |
|
| 189 |
private function getLiteral( array $lit, string $regexp ): array { |
| 190 |
$s = trim( $this->arg ); |
| 191 |
if ( preg_match( $regexp, $s ) == 1 ) { |
| 192 |
return [ $s ]; |
| 193 |
} elseif ( in_array( $s, $lit, true ) ) { |
| 194 |
return [ $s ]; |
| 195 |
} else { |
| 196 |
return []; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public function createVlineElement(): string { |
| 204 |
$mrow = new MMLmrow(); |
| 205 |
$mpAdded = new MMLmpadded( "", [ "depth" => "0", "height" => "0" ] ); |
| 206 |
$mStyle = new MMLmstyle( "", [ "mathsize" => "1.2em" ] ); |
| 207 |
$mo = new MMLmo( "", [ "fence" => "false", "stretchy" => "false" ] ); |
| 208 |
return $mrow->encapsulateRaw( $mpAdded->encapsulateRaw( |
| 209 |
$mStyle->encapsulateRaw( $mo->encapsulateRaw( "|" ) ) ) ); |
| 210 |
} |
| 211 |
|
| 212 |
} |