lexxy_math_symbols 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,148 @@
1
+ // Symbol sets: a palette that shows only some of the catalog (a pupil's
2
+ // editor), from the Lexxy preset's `mathSymbols`:
3
+ //
4
+ // configure({ uczen: { mathSymbols: { categories: ["basic"], symbols: ["\\le"] } } })
5
+ //
6
+ // A set is
7
+ //
8
+ // { categories, selected, pool, search, recent, label, name, signature }
9
+ //
10
+ // `categories` the catalog's categories it shows, in the order given;
11
+ // `selected` the entries of `symbols` they do not show (one more tab); `pool`
12
+ // every entry it may insert, in catalog order (the search's and the recent
13
+ // list's); `search` and `recent` whether those show; `label` the selected
14
+ // tab's name; `name` what its recent list is stored under (null: the old,
15
+ // shared list); `signature` tells one set from another.
16
+ import { CARET, CATEGORIES, ITEMS } from "lexxy_math_symbols/catalog";
17
+
18
+ // No set: the whole catalog, as before there were sets.
19
+ export const FULL_SET = Object.freeze({
20
+ categories: CATEGORIES,
21
+ selected: [],
22
+ pool: ITEMS,
23
+ search: true,
24
+ recent: true,
25
+ label: null,
26
+ name: null,
27
+ signature: "",
28
+ });
29
+
30
+ // The same LaTeX however it is spaced, without the caret mark: "\left( \right)"
31
+ // and "\left(‸\right)" are one entry. A space between two letters stays
32
+ // (\alpha x), as does the one of the "\ " command.
33
+ function canonical(latex) {
34
+ return latex
35
+ .replace(CARET, " ")
36
+ .replace(/\s+/g, " ")
37
+ .trim()
38
+ .replace(/(?<!\\) (?![a-zA-Z])|(?<![a-zA-Z\\]) /g, "");
39
+ }
40
+
41
+ let lookup = null;
42
+
43
+ // The entries by every way `symbols` may name them, made the first time a
44
+ // set is read.
45
+ function lookupTables() {
46
+ if (lookup) return lookup;
47
+ const exact = new Map();
48
+ const spaced = new Map();
49
+ const aliases = new Map();
50
+ const commands = new Map();
51
+ for (const item of ITEMS) {
52
+ exact.set(item.insert, item);
53
+ if (!spaced.has(canonical(item.insert))) spaced.set(canonical(item.insert), item);
54
+ for (const alias of item.aliases) {
55
+ if (!aliases.has(canonical(alias))) aliases.set(canonical(alias), item);
56
+ }
57
+ // A template by its command alone: \frac for \frac{}{}. Where one command
58
+ // starts several (\sqrt{}, \sqrt[]{}), the one made of empty {} only.
59
+ const match = canonical(item.insert).match(/^(\\[a-zA-Z]+\*?)([[{_^].*)$/);
60
+ if (match) {
61
+ const [, command, rest] = match;
62
+ if (!commands.has(command)) commands.set(command, []);
63
+ commands.get(command).push({ item, plain: /^(\{\})+$/.test(rest) });
64
+ }
65
+ }
66
+ lookup = { exact, spaced, aliases, commands };
67
+ return lookup;
68
+ }
69
+
70
+ // The catalog's entry for one of `symbols`, undefined if none: by its insert
71
+ // exactly, then spaced or marked otherwise, then by an alias (\le for \leq),
72
+ // then by its command alone where that names one template only.
73
+ export function findSymbol(latex) {
74
+ if (typeof latex !== "string") return undefined;
75
+ const { exact, spaced, aliases, commands } = lookupTables();
76
+ const key = canonical(latex);
77
+ const found = exact.get(latex) || spaced.get(key) || aliases.get(key);
78
+ if (found) return found;
79
+ const candidates = commands.get(key) || [];
80
+ if (candidates.length === 1) return candidates[0].item;
81
+ const plain = candidates.filter((each) => each.plain);
82
+ return plain.length === 1 ? plain[0].item : undefined;
83
+ }
84
+
85
+ const WARNED = new Set();
86
+
87
+ // The set of a `mathSymbols` value (`name`: its preset's). Nothing, null or
88
+ // false: the whole catalog. Neither `categories` nor `symbols`: every
89
+ // category, with the other options. An unknown category or symbol is left
90
+ // out with one warning per set; the rest works.
91
+ export function symbolSet(config, name = "default") {
92
+ if (!config || typeof config !== "object") return FULL_SET;
93
+ const signature = `${name}:${JSON.stringify(config)}`;
94
+ const given = config.categories !== undefined || config.symbols !== undefined;
95
+ const keys = given ? [config.categories || []].flat() : CATEGORIES.map((each) => each.key);
96
+ const unknown = [];
97
+
98
+ const categories = [];
99
+ for (const key of keys) {
100
+ const category = CATEGORIES.find((each) => each.key === key);
101
+ if (!category) unknown.push(`category ${JSON.stringify(key)}`);
102
+ else if (!categories.includes(category)) categories.push(category);
103
+ }
104
+ const inCategories = new Set(categories.flatMap((category) => category.items));
105
+
106
+ const selected = [];
107
+ for (const latex of [config.symbols || []].flat()) {
108
+ const item = findSymbol(latex);
109
+ if (!item) unknown.push(`symbol ${JSON.stringify(latex)}`);
110
+ // An entry the catalog keeps for the search (\dfrac{}{}) shows here when
111
+ // named, even where one of the set's categories hides it.
112
+ else if ((!inCategories.has(item) || item.searchOnly) && !selected.includes(item)) {
113
+ selected.push(item);
114
+ }
115
+ }
116
+
117
+ if (unknown.length && !WARNED.has(signature)) {
118
+ WARNED.add(signature);
119
+ console.warn(
120
+ `lexxy_math_symbols: mathSymbols of preset "${name}" leaves out what the catalog does not have: ${unknown.join(", ")}`,
121
+ );
122
+ }
123
+
124
+ const inSet = new Set([...inCategories, ...selected]);
125
+ return {
126
+ categories,
127
+ selected,
128
+ pool: ITEMS.filter((item) => inSet.has(item)),
129
+ search: config.search !== false,
130
+ recent: config.recent !== false,
131
+ label: typeof config.label === "string" && config.label ? config.label : null,
132
+ name,
133
+ signature,
134
+ };
135
+ }
136
+
137
+ // The set of a Lexxy editor (`<lexxy-editor>`): its preset's `mathSymbols`,
138
+ // with the `math-symbols` attribute over it. Only the top key is read:
139
+ // Lexxy's config.get("a.b") throws where `a` is missing.
140
+ export function editorSymbolSet(editor) {
141
+ let config;
142
+ try {
143
+ config = editor?.config?.get?.("mathSymbols");
144
+ } catch {
145
+ config = undefined;
146
+ }
147
+ return symbolSet(config, editor?.preset || "default");
148
+ }
@@ -0,0 +1,5 @@
1
+ # The gem's own modules, subfolders too: "lexxy_math_symbols" (index.js) and
2
+ # "lexxy_math_symbols/catalog", "/palette", "/sets", "/icons", "/i18n", "/locales/pl",
3
+ # "/locales/en". The host pins what they import: "lexxy" (from the lexxy gem)
4
+ # and "lexxy-math".
5
+ pin_all_from LexxyMathSymbols::Engine.root.join("app/javascript/lexxy_math_symbols"), under: "lexxy_math_symbols", to: "lexxy_math_symbols"
@@ -0,0 +1,17 @@
1
+ module LexxyMathSymbols
2
+ # Puts app/assets and app/javascript on the host's asset load path, so
3
+ # Propshaft serves lexxy_math_symbols/palette.css and the extension's
4
+ # modules, and adds the gem's pins to an importmap host.
5
+ class Engine < ::Rails::Engine
6
+ initializer "lexxy_math_symbols.assets" do |app|
7
+ app.config.assets.paths << root.join("app/javascript") if app.config.respond_to?(:assets)
8
+ end
9
+
10
+ initializer "lexxy_math_symbols.importmap", before: "importmap" do |app|
11
+ if app.config.respond_to?(:importmap)
12
+ app.config.importmap.paths << root.join("config/importmap.rb")
13
+ app.config.importmap.cache_sweepers << root.join("app/javascript")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module LexxyMathSymbols
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,20 @@
1
+ require "pathname"
2
+ require "lexxy_math_symbols/version"
3
+ # The engine only inside a Rails app: `require "lexxy_math_symbols"` on its
4
+ # own (a build script asking where the gem is) must not boot railties.
5
+ require "lexxy_math_symbols/engine" if defined?(Rails::Engine)
6
+
7
+ # A palette of every LaTeX symbol KaTeX renders, for the math editor of
8
+ # lexxy-math inside Lexxy. README.md has how a host takes it in.
9
+ module LexxyMathSymbols
10
+ def self.root
11
+ Pathname.new(File.expand_path("..", __dir__))
12
+ end
13
+
14
+ # The extension's entry point, for a host that bundles with esbuild: its
15
+ # config resolves `import … from "lexxy_math_symbols"` (and the
16
+ # `lexxy_math_symbols/*` modules) to this directory.
17
+ def self.javascript_path
18
+ root.join("app/javascript/lexxy_math_symbols/index.js")
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lexxy_math_symbols
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michał Krzysteczko
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2026-12-04 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: railties
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '7.1'
26
+ description: 'Click a symbol and it goes into the formula: every symbol KaTeX can
27
+ draw, in tabs, with search in Polish and English, templates with blanks, and symbol
28
+ sets per Lexxy preset.'
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - README.md
35
+ - app/assets/stylesheets/lexxy_math_symbols/palette.css
36
+ - app/javascript/lexxy_math_symbols/catalog.js
37
+ - app/javascript/lexxy_math_symbols/i18n.js
38
+ - app/javascript/lexxy_math_symbols/icons.js
39
+ - app/javascript/lexxy_math_symbols/index.js
40
+ - app/javascript/lexxy_math_symbols/locales/en.js
41
+ - app/javascript/lexxy_math_symbols/locales/pl.js
42
+ - app/javascript/lexxy_math_symbols/palette.js
43
+ - app/javascript/lexxy_math_symbols/sets.js
44
+ - config/importmap.rb
45
+ - lib/lexxy_math_symbols.rb
46
+ - lib/lexxy_math_symbols/engine.rb
47
+ - lib/lexxy_math_symbols/version.rb
48
+ homepage: https://github.com/Xeross99/lexxy_math_symbols
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/Xeross99/lexxy_math_symbols
53
+ source_code_uri: https://github.com/Xeross99/lexxy_math_symbols
54
+ bug_tracker_uri: https://github.com/Xeross99/lexxy_math_symbols/issues
55
+ rubygems_mfa_required: 'true'
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '3.3'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.6.2
71
+ specification_version: 4
72
+ summary: A math symbol palette for formulas in the Lexxy editor, built on lexxy-math
73
+ test_files: []