Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F7403434
MMLParsingUtil.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
11 KB
Referenced Files
None
Subscribers
None
MMLParsingUtil.php
View Options
<?php
namespace
MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util
;
use
MediaWiki\Extension\Math\WikiTexVC\MMLmappings\TexConstants\Tag
;
use
MediaWiki\Extension\Math\WikiTexVC\MMLmappings\TexConstants\TexClass
;
use
MediaWiki\Extension\Math\WikiTexVC\MMLmappings\TexConstants\Variants
;
use
MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmo
;
use
MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmpadded
;
use
MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmrow
;
use
MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmtext
;
/**
* This class contains functionalities for MML-node
* parsing which can be extracted and are used
* for multiple functions.
*/
class
MMLParsingUtil
{
public
static
function
renderApplyFunction
()
{
$mo
=
new
MMLmo
();
return
$mo
->
encapsulateRaw
(
"⁡"
);
}
public
static
function
getFontArgs
(
string
$name
,
?
string
$variant
,
?
array
$passedArgs
):
array
{
$args
=
[];
switch
(
$name
)
{
case
"cal"
:
case
"mathcal"
:
$args
=
[
Tag
::
MJXVARIANT
=>
"-tex-calligraphic"
,
"mathvariant"
=>
Variants
::
SCRIPT
];
break
;
case
"it"
:
case
"mathit"
:
$args
=
[
Tag
::
MJXVARIANT
=>
$variant
,
"mathvariant"
=>
Variants
::
ITALIC
];
break
;
case
"bf"
:
case
"mathbf"
:
$args
=
[
"mathvariant"
=>
$variant
];
break
;
// Sstatements from here come from other fct ok ? otherwise create second fct
case
"textit"
:
$args
=
[
"mathvariant"
=>
Variants
::
ITALIC
];
break
;
case
"textbf"
:
$args
=
[
"mathvariant"
=>
Variants
::
BOLD
];
break
;
case
"textsf"
:
$args
=
[
"mathvariant"
=>
Variants
::
SANSSERIF
];
break
;
case
"texttt"
:
$args
=
[
"mathvariant"
=>
Variants
::
MONOSPACE
];
break
;
case
"textrm"
:
break
;
case
"emph"
:
// Toggle by passed args in emph
if
(
isset
(
$passedArgs
[
"mathvariant"
]
)
)
{
if
(
$passedArgs
[
"mathvariant"
]
===
Variants
::
ITALIC
)
{
$args
=
[
"mathvariant"
=>
Variants
::
NORMAL
];
}
}
else
{
$args
=
[
"mathvariant"
=>
Variants
::
ITALIC
];
}
break
;
default
:
$args
=
[
"mathvariant"
=>
$variant
];
}
return
$args
;
}
/**
* Parses an expression that defines a color; this is usually an argument in Literal.
* Example expression is: "\definecolor {ultramarine}{rgb}{0,0.12549019607843,0.37647058823529}"
* @param string $input tex-string, which contains the expression
* @return array|null either an array which contains hex of parsed expression or null if not parsable
*/
public
static
function
parseDefineColorExpression
(
string
$input
):
?
array
{
$returnObj
=
null
;
$matches
=
[];
$matched
=
preg_match_all
(
'/
\{
(.*?)
\}
/'
,
$input
,
$matches
);
if
(
!
$matched
)
{
return
null
;
}
$ctr
=
count
(
$matches
[
1
]
??
[]
);
if
(
$ctr
==
3
&&
$matches
[
1
][
1
]
===
"rgb"
)
{
$returnObj
=
[];
$rgbValues
=
explode
(
","
,
$matches
[
1
][
2
]
);
$r
=
round
(
floatval
(
$rgbValues
[
0
]
)
*
255
);
$g
=
round
(
floatval
(
$rgbValues
[
1
]
)
*
255
);
$b
=
round
(
floatval
(
$rgbValues
[
2
]
)
*
255
);
$color
=
sprintf
(
"#%02x%02x%02x"
,
$r
,
$g
,
$b
);
$returnObj
[
"name"
]
=
$matches
[
1
][
0
];
$returnObj
[
"type"
]
=
"rgb"
;
$returnObj
[
"hex"
]
=
$color
;
}
return
$returnObj
;
}
/**
* Creates a negation block in MathML, usually preceding the negated statement
* @return string negation block as MathML
*/
public
static
function
createNot
()
{
$mmlMrow
=
new
MMLmrow
(
TexClass
::
REL
);
$mpadded
=
new
MMLmpadded
(
""
,
[
"width"
=>
"0"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
'𝓆'
,
'r'
=>
'𝓇'
,
's'
=>
'𝓈'
,
't'
=>
'𝓉'
,
'u'
=>
'𝓊'
,
'v'
=>
'𝓋'
,
'w'
=>
'𝓌'
,
'x'
=>
'𝓍'
,
'y'
=>
'𝓎'
,
'z'
=>
'𝓏'
];
return
self
::
matchAlphanumeric
(
$inputString
,
$map
);
}
public
static
function
mapToFrakturUnicode
(
string
$inputString
):
string
{
$res
=
''
;
$specialCases
=
[
'C'
=>
'ℭ'
,
'H'
=>
'ℌ'
,
'I'
=>
'ℑ'
,
'R'
=>
'ℜ'
,
'Z'
=>
'ℤ'
];
foreach
(
mb_str_split
(
$inputString
)
as
$chr
)
{
// see https://www.w3.org/TR/mathml-core/#fraktur-mappings
if
(
isset
(
$specialCases
[
$chr
]
)
)
{
$res
.=
$specialCases
[
$chr
];
continue
;
}
if
(
$chr
>=
'A'
&&
$chr
<=
'Z'
)
{
$code
=
self
::
addToChr
(
$chr
,
'1D4C3'
);
$res
.=
'&#x'
.
$code
.
';'
;
}
elseif
(
$chr
>=
'a'
&&
$chr
<=
'z'
)
{
$code
=
self
::
addToChr
(
$chr
,
'1D4BD'
);
$res
.=
'&#x'
.
$code
.
';'
;
}
else
{
$res
.=
$chr
;
}
}
return
$res
;
}
public
static
function
mapToBoldUnicode
(
string
$inputString
):
string
{
$res
=
''
;
foreach
(
mb_str_split
(
$inputString
)
as
$chr
)
{
// see https://www.w3.org/TR/mathml-core/#bold-mappings
if
(
$chr
>=
'A'
&&
$chr
<=
'Z'
)
{
$code
=
self
::
addToChr
(
$chr
,
'1D3BF'
);
$res
.=
'&#x'
.
$code
.
';'
;
}
elseif
(
$chr
>=
'a'
&&
$chr
<=
'z'
)
{
$code
=
self
::
addToChr
(
$chr
,
'1D3B9'
);
$res
.=
'&#x'
.
$code
.
';'
;
}
elseif
(
$chr
>=
'0'
&&
$chr
<=
'9'
)
{
$code
=
self
::
addToChr
(
$chr
,
'1D79E'
);
$res
.=
'&#x'
.
$code
.
';'
;
}
else
{
$res
.=
$chr
;
}
}
return
$res
;
}
private
static
function
addToChr
(
string
$chr
,
string
$base
):
string
{
return
strtoupper
(
dechex
(
mb_ord
(
$chr
)
+
hexdec
(
$base
)
)
);
}
public
static
function
matchAlphanumeric
(
string
$inputString
,
array
$map
):
string
{
// Replace each character in the input string with its caligraphic Unicode equivalent
return
preg_replace_callback
(
'/[A-Za-z0-9]/u'
,
static
function
(
$matches
)
use
(
$map
)
{
return
$map
[
$matches
[
0
]]
??
$matches
[
0
];
},
$inputString
);
}
public
static
function
getIntentContent
(
?
string
$input
):
?
string
{
if
(
!
$input
)
{
return
null
;
}
$matchesInt
=
[];
$matchInt
=
preg_match
(
"/intent=[
\'\"
](.*)[
\'\"
]/"
,
$input
,
$matchesInt
);
if
(
$matchInt
&&
count
(
$matchesInt
)
>=
2
)
{
return
$matchesInt
[
1
];
}
return
null
;
}
public
static
function
getIntentParams
(
?
string
$intentContent
):
?
array
{
if
(
!
$intentContent
)
{
return
null
;
}
$matchesParams
=
[];
// tbd eventually not only alphanumerical chars valid in intent params
$matchParams
=
preg_match_all
(
"/
\\\$
([a-zA-Z]+)/"
,
$intentContent
,
$matchesParams
);
if
(
$matchParams
&&
count
(
$matchesParams
)
>=
2
)
{
return
$matchesParams
[
1
];
}
return
null
;
}
public
static
function
getIntentArgs
(
?
string
$input
):
?
string
{
if
(
!
$input
)
{
return
null
;
}
$matchesArgs
=
[];
$matchArg
=
preg_match
(
"/arg
\s
*=
\s
*[
\'\"
](.*?)[
\'\"
]/"
,
$input
,
$matchesArgs
);
if
(
$matchArg
&&
count
(
$matchesArgs
)
>=
2
)
{
return
$matchesArgs
[
1
];
}
return
null
;
}
/**
* Converts a rendered MathML string to a XML tree and adds the attributes from input
* to the top-level element.Valid attributes for adding are "arg" and "intent.
* It overwrites pre-existing attributes in the top-level element.
* TBD: currently contains a hacky way to remove xml header in the output string
* example:" <msup intent="_($op,_of,$arg)">" intent attributes comes from input variables
* @param string $renderedMML defines input MathML string
* @param array $intentContentAtr defines attributes to add
* @return string MML with added attributes
*/
public
static
function
forgeIntentToTopElement
(
string
$renderedMML
,
$intentContentAtr
)
{
if
(
!
$intentContentAtr
||
!
$renderedMML
)
{
return
$renderedMML
;
}
return
self
::
addAttributesToMML
(
$renderedMML
,
$intentContentAtr
,
""
);
}
/**
* Add parameters from aattributes to the MML string
* @param string $renderedMML defines input MathML string
* @param array $intentContentAtr defines attributes to add
* @param string $elementTag element tag when using foundNodes
* @param bool $useFoundNodes use found nodes
* @return string MML with added attributes
*/
public
static
function
addAttributesToMML
(
string
$renderedMML
,
array
$intentContentAtr
,
string
$elementTag
,
bool
$useFoundNodes
=
false
):
string
{
$xml
=
simplexml_load_string
(
$renderedMML
);
if
(
!
$xml
)
{
return
""
;
}
if
(
$useFoundNodes
)
{
$foundNodes
=
$xml
->
xpath
(
$elementTag
);
if
(
!(
$foundNodes
!==
null
&&
count
(
$foundNodes
)
>=
1
)
)
{
return
$renderedMML
;
}
}
if
(
isset
(
$intentContentAtr
[
"intent"
]
)
)
{
if
(
isset
(
$xml
[
"intent"
]
)
)
{
$xml
[
"intent"
]
=
$intentContentAtr
[
"intent"
];
}
elseif
(
$intentContentAtr
[
"intent"
]
!=
null
&&
is_string
(
$intentContentAtr
[
"intent"
]
)
)
{
$xml
->
addAttribute
(
"intent"
,
$intentContentAtr
[
"intent"
]
);
}
}
if
(
isset
(
$intentContentAtr
[
"arg"
]
)
)
{
if
(
isset
(
$xml
[
"arg"
]
)
)
{
$xml
[
"arg"
]
=
$intentContentAtr
[
"arg"
];
}
elseif
(
$intentContentAtr
[
"arg"
]
!=
null
&&
is_string
(
$intentContentAtr
[
"arg"
]
)
)
{
$xml
->
addAttribute
(
"arg"
,
$intentContentAtr
[
"arg"
]
);
}
}
$hackyXML
=
str_replace
(
"<?xml version=
\"
1.0
\"
?>"
,
""
,
$xml
->
asXML
()
);
return
str_replace
(
"
\n
"
,
""
,
$hackyXML
);
}
public
static
function
forgeIntentToSpecificElement
(
string
$renderedMML
,
array
$intentContentAtr
,
string
$elementTag
):
string
{
if
(
!
$intentContentAtr
||
!
$renderedMML
||
!
$elementTag
)
{
return
$renderedMML
;
}
return
self
::
addAttributesToMML
(
$elementTag
,
$intentContentAtr
,
$elementTag
,
true
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Nov 21, 18:37 (21 h, 59 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
3f/25/262eeefbddc38dfc9c52c18b79b7
Default Alt Text
MMLParsingUtil.php (11 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment