mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Math
synced 2024-12-20 19:32:39 +00:00
131 lines
66 KiB
JavaScript
131 lines
66 KiB
JavaScript
|
/* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
|
||
|
/* vim: set ts=2 et sw=2 tw=80: */
|
||
|
|
||
|
/*************************************************************
|
||
|
*
|
||
|
* MathJax/extensions/MathML/content-mathml.js
|
||
|
*
|
||
|
* This file implements an XSLT transform to convert Content-MathML to
|
||
|
* Presentation MathML for processing by MathJax. The transform is
|
||
|
* performed in a pre-filter for the MathML input jax, so that the
|
||
|
* Show Math As menu will still show the Original MathML as Content MathML,
|
||
|
* but the Presentation MathML can be obtained from the main MathML menu.
|
||
|
*
|
||
|
* To load it, include
|
||
|
*
|
||
|
* MathML: {
|
||
|
* extensions: ["content-mathml.js"]
|
||
|
* }
|
||
|
*
|
||
|
* in your configuration.
|
||
|
*
|
||
|
* A portion of this file is taken from ctop.xsl which is
|
||
|
* Copyright (c) David Carlisle 2001, 2002,
|
||
|
* and is used by permission of David Carlisle, who has agreed to allow us
|
||
|
* to release it under the Apache2 license (see below). That portion is
|
||
|
* indicated via comments.
|
||
|
*
|
||
|
* The remainder falls under the copyright that follows.
|
||
|
* ---------------------------------------------------------------------
|
||
|
*
|
||
|
* Copyright (c) 2013 The MathJax Consortium
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
|
||
|
MathJax.Extension["MathML/content-mathml"] = {
|
||
|
version: "2.3"
|
||
|
};
|
||
|
|
||
|
MathJax.Hub.Register.StartupHook("MathML Jax Ready",function () {
|
||
|
|
||
|
var MATHML = MathJax.InputJax.MathML,
|
||
|
PARSE = MATHML.Parse.prototype;
|
||
|
|
||
|
MATHML.prefilterHooks.Add(function (data) {
|
||
|
if (!MATHML.ctopXSLT) return;
|
||
|
|
||
|
// Parse the <math> but use MATHML.Parse's preProcessMath to apply the normal preprocessing.
|
||
|
if (!MATHML.ParseXML) {MATHML.ParseXML = MATHML.createParser()}
|
||
|
var doc = MATHML.ParseXML(PARSE.preProcessMath(data.math));
|
||
|
|
||
|
// Now transform the <math> using the ctop stylesheet.
|
||
|
var newdoc = MATHML.ctopXSLT.transformToDocument(doc);
|
||
|
|
||
|
if ((typeof newdoc) === "string") {
|
||
|
// Internet Explorer returns a string, so just use it.
|
||
|
data.math = newdoc;
|
||
|
} else if (window.XMLSerializer) {
|
||
|
// Serialize the <math> again. We could directly provide the DOM content
|
||
|
// but other prefilterHooks may assume data.math is still a string.
|
||
|
var serializer = new XMLSerializer();
|
||
|
data.math = serializer.serializeToString(newdoc.documentElement, doc);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
/*
|
||
|
* The following is taken from ctop.xsl (http://www.w3.org/Math/XSL/ctop.xsl)
|
||
|
* which is Copyright (c) David Carlisle 2001, 2002. It is used by permission
|
||
|
* of David Carlisle, who has agreed to allow it to be released under the
|
||
|
* Apache2 licesnse.
|
||
|
*/
|
||
|
var ctopStylesheet = '<x:stylesheet version="1.0" xmlns:x="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:e="http://exslt.org/strings" xmlns:c="http://exslt.org/common" exclude-result-prefixes="m e c"> <!-- Copyright David Carlisle 2001, 2002, 2008, 2009. Use and distribution of this code are permitted under the terms of the<a href="http://www.w3.org/Consortium/Legal/copyright-software-19980720" >W3C Software Notice and License</a>. 2001-2002 MathML2 version 2008-2009 Updates for MathML3 --> <x:output method="xml" /> <x:template match="/"><x:apply-templates/></x:template> <x:template match="*"><x:copy><x:copy-of select="@*"/><x:apply-templates/></x:copy></x:template> <x:template match="m:cn"><mn><x:apply-templates/></mn></x:template> <x:template match="m:cn[@type=\'complex-cartesian\']"><mrow><mn><x:apply-templates select="text()[1]"/></mn><mo>+</mo><mn><x:apply-templates select="text()[2]"/></mn><mo>⁢</mo><mi>i</mi></mrow></x:template> <x:template match="m:apply[*[1][self::m:csymbol=\'complex_cartesian\']]"><mrow><mn><x:apply-templates select="*[2]"/></mn><mo>+</mo><mn><x:apply-templates select="*[3]"/></mn><mo>⁢</mo><mi>i</mi></mrow></x:template> <x:template match="m:cn[@type=\'rational\']"><mrow><mn><x:apply-templates select="text()[1]"/></mn><mo>/</mo><mn><x:apply-templates select="text()[2]"/></mn></mrow></x:template> <x:template match="m:apply[*[1][self::m:csymbol=\'rational\']]"><mrow><mn><x:apply-templates select="*[2]"/></mn><mo>/</mo><mn><x:apply-templates select="*[3]"/></mn></mrow></x:template> <x:template match="m:cn[not(@type) or @type=\'integer\']"><x:choose><x:when test="not(@base) or @base=10"><mn><x:apply-templates/></mn></x:when><x:otherwise><msub><mn><x:apply-templates/></mn><mn><x:value-of select="@base"/></mn></msub></x:otherwise></x:choose></x:template> <x:template match="m:cn[@type=\'complex-polar\']"><mrow><mn><x:apply-templates select="text()[1]"/></mn><mo>⁢</mo><msup><mi>e</mi><mrow><mi>i</mi><mo>⁢</mo><mn><x:apply-templates select="text()[2]"/></mn></mrow></msup></mrow></x:template> <x:template match="m:apply[*[1][self::m:csymbol=\'complex_polar\']]"><mrow><x:apply-templates select="*[2]"/><mo>⁢</mo><msup><mi>e</mi><mrow><mi>i</mi><mo>⁢</mo><x:apply-templates select="*[3]"/></mrow></msup></mrow></x:template> <x:template match="m:cn[@type=\'e-notation\']"><mn><x:apply-templates select="m:sep/preceding-sibling::node()"/><x:text>E</x:text><x:apply-templates select="m:sep/following-sibling::node()"/></mn></x:template> <x:template match="m:cn[@type=\'hexdouble\']"><mn><x:text>0x</x:text><x:apply-templates/></mn></x:template> <x:template match="m:ci/text()"><mi><x:value-of select="."/></mi></x:template> <x:template match="m:ci"><mrow><x:apply-templates/></mrow></x:template> <x:template match="m:csymbol/text()"><mi><x:value-of select="."/></mi></x:template> <x:template match="m:csymbol"><mrow><x:apply-templates/></mrow></x:template> <x:template match="m:apply|m:reln"><mrow><x:choose><x:when test="*[1]/*/*"><mfenced separators=""> <x:apply-templates select="*[1]"> <x:with-param name="p" select="10"/> </x:apply-templates></mfenced></x:when><x:otherwise><x:apply-templates select="*[1]"> <x:with-param name="p" select="10"/></x:apply-templates></x:otherwise></x:choose><mo>⁡</mo><mfenced open="(" close=")" separators=","><x:apply-templates select="*[position()>1]"/></mfenced></mrow></x:template> <x:template match="m:bind"><mrow><x:choose><x:when test="*[1]/*/*"><mfenced separators=""> <x:apply-templates select="*[1]"> <x:with-param name="p" select="10"/> </x:apply-templates></mfenced></x:when><x:otherwise><x:apply-templates select="*[1]"> <x:with-param name="p" select="10"/></x:apply-templates></x:otherwise></x:choose><x:apply-templates select="bvar/*"/><mo>.</mo><x:apply-templates select="*[position()>1][not(self::m:bvar)]"/></mrow></x:template> <x:template match="m:fn"><mrow><x:apply-templates/></mrow></x:template> <x:template m
|
||
|
/*
|
||
|
* End of ctop.xsl material.
|
||
|
*/
|
||
|
|
||
|
var ctop;
|
||
|
if (window.XSLTProcessor) {
|
||
|
// standard method: just use an XSLTProcessor and parse the stylesheet
|
||
|
if (!MATHML.ParseXML) {MATHML.ParseXML = MATHML.createParser()}
|
||
|
MATHML.ctopXSLT = new XSLTProcessor();
|
||
|
MATHML.ctopXSLT.importStylesheet(MATHML.ParseXML(ctopStylesheet));
|
||
|
} else if (MathJax.Hub.Browser.isMSIE) {
|
||
|
// nonstandard methods for Internet Explorer
|
||
|
if (MathJax.Hub.Browser.versionAtLeast("9.0")) {
|
||
|
// For Internet Explorer >= 9, use createProcessor
|
||
|
ctop = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
|
||
|
ctop.loadXML(ctopStylesheet);
|
||
|
var xslt = new ActiveXObject("Msxml2.XSLTemplate");
|
||
|
xslt.stylesheet = ctop;
|
||
|
MATHML.ctopXSLT = {
|
||
|
ctop: xslt.createProcessor(),
|
||
|
transformToDocument: function(doc) {
|
||
|
this.ctop.input = doc;
|
||
|
this.ctop.transform();
|
||
|
return this.ctop.output;
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
// For Internet Explorer <= 8, use transformNode
|
||
|
ctop = MATHML.createMSParser();
|
||
|
ctop.async = false;
|
||
|
ctop.loadXML(ctopStylesheet);
|
||
|
MATHML.ctopXSLT = {
|
||
|
ctop: ctop,
|
||
|
transformToDocument: function(doc) {
|
||
|
return doc.documentElement.transformNode(this.ctop);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
// No XSLT support. Do not change the <math> content.
|
||
|
MATHML.ctopXSLT = null;
|
||
|
}
|
||
|
|
||
|
MathJax.Hub.Startup.signal.Post("MathML content-mathml Ready");
|
||
|
});
|
||
|
|
||
|
MathJax.Ajax.loadComplete("[MathJax]/extensions/MathML/content-mathml.js");
|