From 8a80ffe53a7e436b5e74165c0e818a691dd4f147 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 15 Nov 2020 20:22:28 +1000 Subject: [PATCH] add rule to copy mathjax from node_modules --- qt/aqt/clayout.py | 2 +- qt/aqt/data/web/js/BUILD.bazel | 8 +-- .../js/{vendor/mathjax/conf.js => mathjax.js} | 0 qt/aqt/data/web/js/vendor/BUILD.bazel | 5 ++ qt/aqt/data/web/js/vendor/mathjax/.gitignore | 2 - qt/aqt/data/web/js/vendor/mathjax/BUILD.bazel | 6 ++ qt/aqt/previewer.py | 2 +- qt/aqt/reviewer.py | 2 +- ts/BUILD.bazel | 16 ----- ts/mathjax.bzl | 70 +++++++++++++++++++ 10 files changed, 86 insertions(+), 27 deletions(-) rename qt/aqt/data/web/js/{vendor/mathjax/conf.js => mathjax.js} (100%) create mode 100644 qt/aqt/data/web/js/vendor/BUILD.bazel delete mode 100644 qt/aqt/data/web/js/vendor/mathjax/.gitignore create mode 100644 qt/aqt/data/web/js/vendor/mathjax/BUILD.bazel create mode 100644 ts/mathjax.bzl diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index 1769e9d96..1281face9 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -304,7 +304,7 @@ class CardLayout(QDialog): jsinc = [ "js/vendor/jquery.js", "js/vendor/browsersel.js", - "js/vendor/mathjax/conf.js", + "js/mathjax.js", "js/vendor/mathjax/tex-chtml.js", "js/reviewer.js", ] diff --git a/qt/aqt/data/web/js/BUILD.bazel b/qt/aqt/data/web/js/BUILD.bazel index 14efa367e..a64fd61a7 100644 --- a/qt/aqt/data/web/js/BUILD.bazel +++ b/qt/aqt/data/web/js/BUILD.bazel @@ -26,16 +26,12 @@ filegroup( output_group = "es5_sources", ) -filegroup( - name = "vendor", - srcs = glob(["vendor/**"]), -) - filegroup( name = "js", srcs = [ "aqt_es5", - "vendor", + "mathjax.js", + "//qt/aqt/data/web/js/vendor", ], visibility = ["//qt:__subpackages__"], ) diff --git a/qt/aqt/data/web/js/vendor/mathjax/conf.js b/qt/aqt/data/web/js/mathjax.js similarity index 100% rename from qt/aqt/data/web/js/vendor/mathjax/conf.js rename to qt/aqt/data/web/js/mathjax.js diff --git a/qt/aqt/data/web/js/vendor/BUILD.bazel b/qt/aqt/data/web/js/vendor/BUILD.bazel new file mode 100644 index 000000000..46dd6ff9b --- /dev/null +++ b/qt/aqt/data/web/js/vendor/BUILD.bazel @@ -0,0 +1,5 @@ +filegroup( + name = "vendor", + srcs = glob(["**"]) + ["//qt/aqt/data/web/js/vendor/mathjax"], + visibility = ["//qt:__subpackages__"], +) diff --git a/qt/aqt/data/web/js/vendor/mathjax/.gitignore b/qt/aqt/data/web/js/vendor/mathjax/.gitignore deleted file mode 100644 index f79a09ee2..000000000 --- a/qt/aqt/data/web/js/vendor/mathjax/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!conf.js diff --git a/qt/aqt/data/web/js/vendor/mathjax/BUILD.bazel b/qt/aqt/data/web/js/vendor/mathjax/BUILD.bazel new file mode 100644 index 000000000..78649d7a8 --- /dev/null +++ b/qt/aqt/data/web/js/vendor/mathjax/BUILD.bazel @@ -0,0 +1,6 @@ +load("//ts:mathjax.bzl", "copy_mathjax") + +copy_mathjax( + name = "mathjax", + visibility = ["//visibility:public"], +) diff --git a/qt/aqt/previewer.py b/qt/aqt/previewer.py index 7ce48e3e5..779d9eaf0 100644 --- a/qt/aqt/previewer.py +++ b/qt/aqt/previewer.py @@ -114,7 +114,7 @@ class Previewer(QDialog): jsinc = [ "js/vendor/jquery.js", "js/vendor/browsersel.js", - "js/vendor/mathjax/conf.js", + "js/mathjax.js", "js/vendor/mathjax/tex-chtml.js", "js/reviewer.js", ] diff --git a/qt/aqt/reviewer.py b/qt/aqt/reviewer.py index 346f146ff..9f1f7e50c 100644 --- a/qt/aqt/reviewer.py +++ b/qt/aqt/reviewer.py @@ -155,7 +155,7 @@ class Reviewer: js=[ "js/vendor/jquery.js", "js/vendor/browsersel.js", - "js/vendor/mathjax/conf.js", + "js/mathjax.js", "js/vendor/mathjax/tex-chtml.js", "js/reviewer.js", ], diff --git a/ts/BUILD.bazel b/ts/BUILD.bazel index 5340860a5..91f8ac7c5 100644 --- a/ts/BUILD.bazel +++ b/ts/BUILD.bazel @@ -13,19 +13,3 @@ exports_files([ ".eslintrc.js", "licenses.json", ]) - -filegroup( - name = "mathjax", - srcs = glob( - include = glob([ - 'node_modules/mathjax/es5/tex-chtml.js', - 'node_modules/mathjax/es5/input/tex/extensions/**/*', - 'node_modules/mathjax/es5/output/chtml/fonts/woff-v2/**/*', - 'node_modules/mathjax/es5/a11y/**/*', - 'node_modules/mathjax/es5/sre/**/*', - ]), - exclude = [ - 'node_modules/mathjax/es5/sre/mathmaps/mathmaps_ie.js', - ], - ), -) diff --git a/ts/mathjax.bzl b/ts/mathjax.bzl new file mode 100644 index 000000000..f1c7d9372 --- /dev/null +++ b/ts/mathjax.bzl @@ -0,0 +1,70 @@ +"Rule to copy mathjax subset from node_modules to vendor folder." + +_exclude = [ + "mathmaps_ie.js", +] + +_include = [ + "es5/tex-chtml.js", + "es5/input/tex/extensions", + "es5/output/chtml/fonts/woff-v2", + "es5/a11y", + "es5/sre", +] + +_unwanted_prefix = "external/npm/node_modules/mathjax/es5/" + +def _copy_files(ctx, files): + cmds = [] + inputs = [] + outputs = [] + for (src, dst) in files: + inputs.append(src) + dst = ctx.actions.declare_file(dst) + outputs.append(dst) + cmds.append("cp -f {} {}".format(src.path, dst.path)) + + shell_fname = ctx.label.name + "-cp.sh" + shell_file = ctx.actions.declare_file(shell_fname) + ctx.actions.write( + output = shell_file, + content = "#!/bin/bash\nset -e\n" + "\n".join(cmds), + is_executable = True, + ) + ctx.actions.run( + inputs = inputs, + executable = "bash", + tools = [shell_file], + arguments = [shell_file.path], + outputs = outputs, + mnemonic = "CopyFile", + use_default_shell_env = True, + ) + + return [DefaultInfo(files = depset(outputs))] + +def _copy_mathjax_impl(ctx): + wanted = [] + for f in ctx.attr.mathjax.files.to_list(): + path = f.path + want = True + for substr in _exclude: + if substr in path: + want = False + continue + if not want: + continue + + for substr in _include: + if substr in path: + output = path.replace(_unwanted_prefix, "") + wanted.append((f, output)) + + return _copy_files(ctx, wanted) + +copy_mathjax = rule( + implementation = _copy_mathjax_impl, + attrs = { + "mathjax": attr.label(default = "@npm//mathjax:mathjax__files"), + }, +)