From 4f3a1cde0a5a059ea28d8a460e5fbb0ec26da5c2 Mon Sep 17 00:00:00 2001
From: Lars-Dominik Braun <lars@6xq.net>
Date: Thu, 2 Dec 2021 11:32:54 +0100
Subject: [PATCH] gnu: Add js-mathjax-3.

* gnu/packages/javascript.scm (js-mathjax-3): New variable.
* gnu/packages/patches/mathjax-disable-webpack.patch,
gnu/packages/patches/mathjax-no-a11y.patch: New patches.
* gnu/local.mk (dist_patch_DATA): Register them.
---
 gnu/local.mk                                  |   2 +
 gnu/packages/javascript.scm                   | 120 ++++++++
 .../patches/mathjax-disable-webpack.patch     |  23 ++
 gnu/packages/patches/mathjax-no-a11y.patch    | 286 ++++++++++++++++++
 4 files changed, 431 insertions(+)
 create mode 100644 gnu/packages/patches/mathjax-disable-webpack.patch
 create mode 100644 gnu/packages/patches/mathjax-no-a11y.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index a5c0486c94..d5d40b8a43 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1452,6 +1452,8 @@ dist_patch_DATA =						\
   %D%/packages/patches/mariadb-CVE-2021-27928.patch		\
   %D%/packages/patches/mars-install.patch			\
   %D%/packages/patches/mars-sfml-2.3.patch			\
+  %D%/packages/patches/mathjax-disable-webpack.patch			\
+  %D%/packages/patches/mathjax-no-a11y.patch			\
   %D%/packages/patches/maxima-defsystem-mkdir.patch		\
   %D%/packages/patches/maven-enforcer-api-fix-old-dependencies.patch	\
   %D%/packages/patches/maven-generate-component-xml.patch		\
diff --git a/gnu/packages/javascript.scm b/gnu/packages/javascript.scm
index 6d4efe2c92..6bcf453bd6 100644
--- a/gnu/packages/javascript.scm
+++ b/gnu/packages/javascript.scm
@@ -26,6 +26,7 @@ (define-module (gnu packages javascript)
   #:use-module (gnu packages)
   #:use-module (gnu packages base)
   #:use-module (gnu packages compression)
+  #:use-module (gnu packages node)
   #:use-module (gnu packages readline)
   #:use-module (gnu packages uglifyjs)
   #:use-module (gnu packages web)
@@ -188,6 +189,125 @@ (define-public js-mathjax
 write web documents that include mathematics and be confident that readers will
 be able to view it naturally and easily.")))
 
+(define-public js-mathjax-3
+  (package
+    (name "js-mathjax")
+    (version "3.2.0")
+    (source
+     (origin
+       (method git-fetch)
+       (uri (git-reference
+              (url "https://github.com/mathjax/MathJax-src")
+              (commit version)))
+       (file-name (git-file-name name version))
+       (sha256
+        (base32
+         "05lm6nw7rzpcc5yz7xsjxi4id9369vvnrksx82nglxrqrpws97wx"))
+       (patches (search-patches "mathjax-disable-webpack.patch"
+                                "mathjax-no-a11y.patch"))))
+    (build-system gnu-build-system)
+    (arguments
+     `(#:phases
+       (modify-phases %standard-phases
+         (add-after 'unpack 'prepare-sources
+           (lambda* (#:key inputs #:allow-other-keys)
+             ;; All a11y components depend on speech-rule-engine, which cannot be
+             ;; built from source. Since this only affects accessibility, remove them.
+             (delete-file-recursively "ts/a11y")
+             (delete-file-recursively "components/src/a11y")
+             (delete-file-recursively "components/src/sre")
+             (delete-file-recursively "components/src/node-main")
+
+             ;; Copy sources of dependencies, so we can create symlinks.
+             (mkdir-p "node_modules")
+             (with-directory-excursion "node_modules"
+               (for-each
+                (lambda (p)
+                 (copy-recursively (assoc-ref inputs (string-append "node-" p)) p))
+                '("mj-context-menu" "mhchemparser")))
+
+             ;; Make sure esbuild can find imports. This way we don’t have to rewrite files.
+             (symlink "ts" "js")
+             (symlink "ts" "node_modules/mj-context-menu/js")))
+         (delete 'configure)
+         (replace 'build
+           (lambda* (#:key inputs outputs #:allow-other-keys)
+             (let ((esbuild (string-append (assoc-ref inputs "esbuild")
+                                           "/bin/esbuild"))
+                   (node (string-append (assoc-ref inputs "node")
+                                        "/bin/node"))
+                   (target (string-append (assoc-ref outputs "out")
+                                          "/share/javascript/mathjax/es5")))
+               ;; Preprocess files and generate lib/ subdirs.
+               (invoke node "components/bin/makeAll")
+               ;; Build components.
+               (apply
+                invoke
+                esbuild
+                "--bundle"
+                "--minify"
+                ;; esbuild cannot transpile some features to ES5, so use ES6 instead.
+                "--target=es6"
+                (string-append "--outdir=" target)
+                "--sourcemap"
+                "--outbase=components/src"
+                "--define:__dirname=\"/\""
+                ;; In the browser the global object is window, see
+                ;; https://developer.mozilla.org/en-US/docs/Glossary/Global_object
+                "--define:global=window"
+                ;; Find all component entry points, which have the same name as their
+                ;; parent directory.
+                (filter
+                 (lambda (f)
+                     (string=?
+                       (basename (dirname f))
+                       (string-drop-right (basename f) 3)))
+                 (find-files "components/src" "\\.js$")))
+               ;; Move all .js files into their parent directory, where MathJax
+               ;; expects them.
+               (for-each
+                 (lambda (f)
+                     (rename-file f (string-append (dirname (dirname f)) "/" (basename f))))
+                 (find-files target "\\.js(\\.map)?$"))
+               ;; Copy font files.
+               (copy-recursively
+                 "ts/output/chtml/fonts/tex-woff-v2"
+                 (string-append target "/output/chtml/fonts/woff-v2")))))
+         (delete 'check)
+         (delete 'install))))
+    (native-inputs
+     `(("esbuild" ,esbuild)
+       ("node" ,node-lts)
+       ("node-mj-context-menu"
+        ,(let ((name "context-menu")
+               (version "0.6.1"))
+           (origin
+             (method git-fetch)
+             (uri (git-reference
+                    (url "https://github.com/zorkow/context-menu.git")
+                    (commit (string-append "v" version))))
+             (file-name (git-file-name name version))
+             (sha256
+              (base32
+               "1q063l6477z285j6h5wvccp6iswvlp0jmb96sgk32sh0lf7nhknh")))))
+       ("node-mhchemparser"
+        ,(let ((name "mhchemparser")
+               ;; Version 4.1.1. There are no tags.
+               (version "b1bd0670df7e9bbd5a724ac642aa2664d6e500b3"))
+           (origin
+             (method git-fetch)
+             (uri (git-reference
+                    (url "https://github.com/mhchem/mhchemParser.git")
+                    (commit version)))
+             (file-name (git-file-name name version))
+             (sha256
+              (base32
+               "1g72kbxzf9f2igidpma3jwk28ln4bp3qhwspmhnm79baf3701dgv")))))))
+    (home-page "https://www.mathjax.org/")
+    (synopsis (package-synopsis js-mathjax))
+    (description (package-description js-mathjax))
+    (license license:asl2.0)))
+
 (define-public js-commander
   (package
     (name "js-commander")
diff --git a/gnu/packages/patches/mathjax-disable-webpack.patch b/gnu/packages/patches/mathjax-disable-webpack.patch
new file mode 100644
index 0000000000..7e3d705c15
--- /dev/null
+++ b/gnu/packages/patches/mathjax-disable-webpack.patch
@@ -0,0 +1,23 @@
+commit 0cfd5b23aed8767f2bcf9583c1feaf4c9a64703a
+Author: Lars-Dominik Braun <lars@6xq.net>
+Date:   Wed Dec 1 14:27:33 2021 +0100
+
+    guix: Disable webpack
+    
+    Guix uses esbuild to “link” the files. We only need the build step.
+
+diff --git a/components/bin/makeAll b/components/bin/makeAll
+index 3e7a8914..5dae9b9b 100755
+--- a/components/bin/makeAll
++++ b/components/bin/makeAll
+@@ -61,8 +61,8 @@ function processList(dirs) {
+   for (const dir of dirs) {
+     const fulldir = path.resolve(dir);
+     processDir(fulldir, buildLib);
+-    processDir(fulldir, webpackLib);
+-    processDir(fulldir, copyLib);
++    //processDir(fulldir, webpackLib);
++    //processDir(fulldir, copyLib);
+   }
+ }
+ 
diff --git a/gnu/packages/patches/mathjax-no-a11y.patch b/gnu/packages/patches/mathjax-no-a11y.patch
new file mode 100644
index 0000000000..40fb514b5a
--- /dev/null
+++ b/gnu/packages/patches/mathjax-no-a11y.patch
@@ -0,0 +1,286 @@
+commit 9bb6f1e11264fb075bfa70fa3684f10a32414594
+Author: Lars-Dominik Braun <lars@6xq.net>
+Date:   Wed Dec 1 14:28:06 2021 +0100
+
+    guix: Remove accessibility.
+    
+    We cannot build speech-rule-engine from source and thus all dependents
+    must be removed. This affects all a11y modules and menu items, which
+    may try to load components.
+
+diff --git a/components/src/dependencies.js b/components/src/dependencies.js
+index f9be6f93..822f8fb6 100644
+--- a/components/src/dependencies.js
++++ b/components/src/dependencies.js
+@@ -16,9 +16,6 @@
+  */
+ 
+ export const dependencies = {
+-  'a11y/semantic-enrich': ['input/mml', '[sre]'],
+-  'a11y/complexity': ['a11y/semantic-enrich'],
+-  'a11y/explorer': ['a11y/semantic-enrich', 'ui/menu'],
+   '[mml]/mml3': ['input/mml'],
+   '[tex]/all-packages': ['input/tex-base'],
+   '[tex]/action': ['input/tex-base', '[tex]/newcommand'],
+@@ -58,7 +55,6 @@ export const dependencies = {
+ export const paths = {
+   tex: '[mathjax]/input/tex/extensions',
+   mml: '[mathjax]/input/mml/extensions',
+-  sre: '[mathjax]/sre/' + (typeof window === 'undefined' ? 'sre-node' : 'sre_browser')
+ };
+ 
+ const allPackages = [
+diff --git a/components/src/mml-chtml/mml-chtml.js b/components/src/mml-chtml/mml-chtml.js
+index c0bd18ee..a283a1ab 100644
+--- a/components/src/mml-chtml/mml-chtml.js
++++ b/components/src/mml-chtml/mml-chtml.js
+@@ -5,5 +5,4 @@ import '../input/mml/mml.js';
+ import '../output/chtml/chtml.js';
+ import '../output/chtml/fonts/tex/tex.js';
+ import '../ui/menu/menu.js';
+-import '../a11y/assistive-mml/assistive-mml.js';
+ import '../startup/startup.js';
+diff --git a/components/src/mml-chtml/preload.js b/components/src/mml-chtml/preload.js
+index e3f202f7..3699570e 100644
+--- a/components/src/mml-chtml/preload.js
++++ b/components/src/mml-chtml/preload.js
+@@ -5,5 +5,5 @@ Loader.preLoad(
+   'core',
+   'input/mml',
+   'output/chtml', 'output/chtml/fonts/tex.js',
+-  'ui/menu', 'a11y/assistive-mml'
++  'ui/menu'
+ );
+diff --git a/components/src/mml-svg/mml-svg.js b/components/src/mml-svg/mml-svg.js
+index 172b2433..23b66e69 100644
+--- a/components/src/mml-svg/mml-svg.js
++++ b/components/src/mml-svg/mml-svg.js
+@@ -5,5 +5,4 @@ import '../input/mml/mml.js';
+ import '../output/svg/svg.js';
+ import '../output/svg/fonts/tex/tex.js';
+ import '../ui/menu/menu.js';
+-import '../a11y/assistive-mml/assistive-mml.js';
+ import '../startup/startup.js';
+diff --git a/components/src/mml-svg/preload.js b/components/src/mml-svg/preload.js
+index 3cb47d42..89f6b9f9 100644
+--- a/components/src/mml-svg/preload.js
++++ b/components/src/mml-svg/preload.js
+@@ -5,5 +5,5 @@ Loader.preLoad(
+   'core',
+   'input/mml',
+   'output/svg', 'output/svg/fonts/tex.js',
+-  'ui/menu', 'a11y/assistive-mml'
++  'ui/menu'
+ );
+diff --git a/components/src/source.js b/components/src/source.js
+index ba6d6835..02ecf18d 100644
+--- a/components/src/source.js
++++ b/components/src/source.js
+@@ -62,12 +62,6 @@ export const source = {
+   'output/chtml/fonts/tex': `${src}/output/chtml/fonts/tex/tex.js`,
+   'output/svg': `${src}/output/svg/svg.js`,
+   'output/svg/fonts/tex': `${src}/output/svg/fonts/tex/tex.js`,
+-  'a11y/assistive-mml': `${src}/a11y/assistive-mml/assistive-mml.js`,
+-  'a11y/semantic-enrich': `${src}/a11y/semantic-enrich/semantic-enrich.js`,
+-  'a11y/complexity': `${src}/a11y/complexity/complexity.js`,
+-  'a11y/explorer': `${src}/a11y/explorer/explorer.js`,
+-  '[sre]': (typeof window === 'undefined' ? `${src}/../../js/a11y/sre-node.js` :
+-            `${src}/../../node_modules/speech-rule-engine/lib/sre_browser.js`),
+   'ui/lazy': `${src}/ui/lazy/lazy.js`,
+   'ui/menu': `${src}/ui/menu/menu.js`,
+   'ui/safe': `${src}/ui/safe/safe.js`,
+diff --git a/components/src/tex-chtml-full/preload.js b/components/src/tex-chtml-full/preload.js
+index 76c81c92..0eb3a8e2 100644
+--- a/components/src/tex-chtml-full/preload.js
++++ b/components/src/tex-chtml-full/preload.js
+@@ -5,5 +5,5 @@ Loader.preLoad(
+   'core',
+   'input/tex-full',
+   'output/chtml', 'output/chtml/fonts/tex.js',
+-  'ui/menu', 'a11y/assistive-mml'
++  'ui/menu'
+ );
+diff --git a/components/src/tex-chtml-full/tex-chtml-full.js b/components/src/tex-chtml-full/tex-chtml-full.js
+index 12291be9..c3f59e6b 100644
+--- a/components/src/tex-chtml-full/tex-chtml-full.js
++++ b/components/src/tex-chtml-full/tex-chtml-full.js
+@@ -5,5 +5,4 @@ import '../input/tex-full/tex-full.js';
+ import '../output/chtml/chtml.js';
+ import '../output/chtml/fonts/tex/tex.js';
+ import '../ui/menu/menu.js';
+-import '../a11y/assistive-mml/assistive-mml.js';
+ import '../startup/startup.js';
+diff --git a/components/src/tex-chtml/preload.js b/components/src/tex-chtml/preload.js
+index 89f110c5..5dae092f 100644
+--- a/components/src/tex-chtml/preload.js
++++ b/components/src/tex-chtml/preload.js
+@@ -5,5 +5,5 @@ Loader.preLoad(
+   'core',
+   'input/tex',
+   'output/chtml', 'output/chtml/fonts/tex.js',
+-  'ui/menu', 'a11y/assistive-mml'
++  'ui/menu'
+ );
+diff --git a/components/src/tex-chtml/tex-chtml.js b/components/src/tex-chtml/tex-chtml.js
+index 8d98865e..4d100a48 100644
+--- a/components/src/tex-chtml/tex-chtml.js
++++ b/components/src/tex-chtml/tex-chtml.js
+@@ -5,5 +5,4 @@ import '../input/tex/tex.js';
+ import '../output/chtml/chtml.js';
+ import '../output/chtml/fonts/tex/tex.js';
+ import '../ui/menu/menu.js';
+-import '../a11y/assistive-mml/assistive-mml.js';
+ import '../startup/startup.js';
+diff --git a/components/src/tex-mml-chtml/preload.js b/components/src/tex-mml-chtml/preload.js
+index 7adb6a3b..3945370f 100644
+--- a/components/src/tex-mml-chtml/preload.js
++++ b/components/src/tex-mml-chtml/preload.js
+@@ -5,5 +5,5 @@ Loader.preLoad(
+   'core',
+   'input/tex', 'input/mml',
+   'output/chtml', 'output/chtml/fonts/tex.js',
+-  'ui/menu', 'a11y/assistive-mml'
++  'ui/menu'
+ );
+diff --git a/components/src/tex-mml-chtml/tex-mml-chtml.js b/components/src/tex-mml-chtml/tex-mml-chtml.js
+index 4167bd59..0b068dc6 100644
+--- a/components/src/tex-mml-chtml/tex-mml-chtml.js
++++ b/components/src/tex-mml-chtml/tex-mml-chtml.js
+@@ -6,5 +6,4 @@ import '../input/mml/mml.js';
+ import '../output/chtml/chtml.js';
+ import '../output/chtml/fonts/tex/tex.js';
+ import '../ui/menu/menu.js';
+-import '../a11y/assistive-mml/assistive-mml.js';
+ import '../startup/startup.js';
+diff --git a/components/src/tex-mml-svg/preload.js b/components/src/tex-mml-svg/preload.js
+index 69d6a2c5..bad39af1 100644
+--- a/components/src/tex-mml-svg/preload.js
++++ b/components/src/tex-mml-svg/preload.js
+@@ -5,5 +5,5 @@ Loader.preLoad(
+   'core',
+   'input/tex', 'input/mml',
+   'output/svg', 'output/svg/fonts/tex.js',
+-  'ui/menu', 'a11y/assistive-mml'
++  'ui/menu'
+ );
+diff --git a/components/src/tex-mml-svg/tex-mml-svg.js b/components/src/tex-mml-svg/tex-mml-svg.js
+index acff78dd..3ca4e6cf 100644
+--- a/components/src/tex-mml-svg/tex-mml-svg.js
++++ b/components/src/tex-mml-svg/tex-mml-svg.js
+@@ -6,5 +6,4 @@ import '../input/mml/mml.js';
+ import '../output/svg/svg.js';
+ import '../output/svg/fonts/tex/tex.js';
+ import '../ui/menu/menu.js';
+-import '../a11y/assistive-mml/assistive-mml.js';
+ import '../startup/startup.js';
+diff --git a/components/src/tex-svg-full/preload.js b/components/src/tex-svg-full/preload.js
+index d4165cf8..8871781a 100644
+--- a/components/src/tex-svg-full/preload.js
++++ b/components/src/tex-svg-full/preload.js
+@@ -5,5 +5,5 @@ Loader.preLoad(
+   'core',
+   'input/tex-full',
+   'output/svg', 'output/svg/fonts/tex.js',
+-  'ui/menu', 'a11y/assistive-mml'
++  'ui/menu'
+ );
+diff --git a/components/src/tex-svg-full/tex-svg-full.js b/components/src/tex-svg-full/tex-svg-full.js
+index 55ce32f2..09c7e65c 100644
+--- a/components/src/tex-svg-full/tex-svg-full.js
++++ b/components/src/tex-svg-full/tex-svg-full.js
+@@ -5,5 +5,4 @@ import '../input/tex-full/tex-full.js';
+ import '../output/svg/svg.js';
+ import '../output/svg/fonts/tex/tex.js';
+ import '../ui/menu/menu.js';
+-import '../a11y/assistive-mml/assistive-mml.js';
+ import '../startup/startup.js';
+diff --git a/components/src/tex-svg/preload.js b/components/src/tex-svg/preload.js
+index 284ce317..3eb02665 100644
+--- a/components/src/tex-svg/preload.js
++++ b/components/src/tex-svg/preload.js
+@@ -5,5 +5,5 @@ Loader.preLoad(
+   'core',
+   'input/tex',
+   'output/svg', 'output/svg/fonts/tex.js',
+-  'ui/menu', 'a11y/assistive-mml'
++  'ui/menu'
+ );
+diff --git a/components/src/tex-svg/tex-svg.js b/components/src/tex-svg/tex-svg.js
+index f4ce644e..5d8caa5c 100644
+--- a/components/src/tex-svg/tex-svg.js
++++ b/components/src/tex-svg/tex-svg.js
+@@ -5,5 +5,4 @@ import '../input/tex/tex.js';
+ import '../output/svg/svg.js';
+ import '../output/svg/fonts/tex/tex.js';
+ import '../ui/menu/menu.js';
+-import '../a11y/assistive-mml/assistive-mml.js';
+ import '../startup/startup.js';
+diff --git a/ts/ui/menu/Menu.ts b/ts/ui/menu/Menu.ts
+index 7553aafc..ca0b9780 100644
+--- a/ts/ui/menu/Menu.ts
++++ b/ts/ui/menu/Menu.ts
+@@ -135,7 +135,7 @@ export class Menu {
+       autocollapse: false,
+       collapsible: false,
+       inTabOrder: true,
+-      assistiveMml: true,
++      assistiveMml: false,
+       explorer: false
+     },
+     jax: {
+@@ -475,7 +475,7 @@ export class Menu {
+           this.command('Reset', 'Reset to defaults', () => this.resetDefaults())
+         ]),
+         this.submenu('Accessibility', 'Accessibility', [
+-          this.checkbox('Activate', 'Activate', 'explorer'),
++          this.checkbox('Activate', 'Activate', 'explorer', {disabled: true}),
+           this.submenu('Speech', 'Speech', [
+             this.checkbox('Speech', 'Speech Output', 'speech'),
+             this.checkbox('Subtitles', 'Speech Subtitles', 'subtitles'),
+@@ -534,11 +534,11 @@ export class Menu {
+             this.checkbox('Prefix', 'Prefix', 'infoPrefix')
+           ], true),
+           this.rule(),
+-          this.checkbox('Collapsible', 'Collapsible Math', 'collapsible'),
++          this.checkbox('Collapsible', 'Collapsible Math', 'collapsible', {disabled: true}),
+           this.checkbox('AutoCollapse', 'Auto Collapse', 'autocollapse', {disabled: true}),
+           this.rule(),
+           this.checkbox('InTabOrder', 'Include in Tab Order', 'inTabOrder'),
+-          this.checkbox('AssistiveMml', 'Include Hidden MathML', 'assistiveMml')
++          this.checkbox('AssistiveMml', 'Include Hidden MathML', 'assistiveMml', {disabled: true})
+         ]),
+         this.submenu('Language', 'Language'),
+         this.rule(),
+@@ -722,6 +722,7 @@ export class Menu {
+    * @param {boolean} mml   True to output hidden Mathml, false to not
+    */
+   protected setAssistiveMml(mml: boolean) {
++    mml = false;
+     this.document.options.enableAssistiveMml = mml;
+     if (!mml || (MathJax._.a11y && MathJax._.a11y['assistive-mml'])) {
+       this.rerender();
+@@ -734,6 +735,7 @@ export class Menu {
+    * @param {boolean} explore   True to enable the explorer, false to not
+    */
+   protected setExplorer(explore: boolean) {
++    explore = false;
+     this.enableExplorerItems(explore);
+     this.document.options.enableExplorer = explore;
+     if (!explore || (MathJax._.a11y && MathJax._.a11y.explorer)) {
+@@ -747,6 +749,7 @@ export class Menu {
+    * @param {boolean} collapse   True to enable collapsible math, false to not
+    */
+   protected setCollapsible(collapse: boolean) {
++    collapse = false;
+     this.document.options.enableComplexity = collapse;
+     if (!collapse || (MathJax._.a11y && MathJax._.a11y.complexity)) {
+       this.rerender(STATE.COMPILED);
+@@ -845,6 +848,8 @@ export class Menu {
+    * @param {string} component   The name of the a11y component to load
+    */
+   public loadA11y(component: string) {
++    console.log ('ignoring load for', component);
++    return;
+     const noEnrich = !STATE.ENRICHED;
+     this.loadComponent('a11y/' + component, () => {
+       const startup = MathJax.startup;
-- 
2.32.0

