@unified-latex/unified-latex-util-parse 1.0.9 → 1.0.12

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.
Files changed (3) hide show
  1. package/README.md +170 -0
  2. package/index.cjs +203 -0
  3. package/package.json +14 -14
package/README.md ADDED
@@ -0,0 +1,170 @@
1
+ <!-- DO NOT MODIFY -->
2
+ <!-- This file was autogenerated by build-docs.ts -->
3
+ <!-- Edit the docstring in index.ts and regenerate -->
4
+ <!-- rather than editing this file directly. -->
5
+ # unified-latex-util-parse
6
+
7
+ ## What is this?
8
+
9
+ Functions parse strings to a `unified-latex` Abstract Syntax Tree (AST).
10
+
11
+ ## When should I use this?
12
+
13
+ If you have a string that you would like to parse to a `unified-latex` `Ast.Ast`, or
14
+ if you are building a plugin for `unified()` that manipulates LaTeX.
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ npm install @unified-latex/unified-latex-util-parse
20
+ ```
21
+
22
+ This package contains both esm and commonjs exports. To explicitly access the esm export,
23
+ import the `.js` file. To explicitly access the commonjs export, import the `.cjs` file.
24
+
25
+ # Plugins
26
+
27
+ ## `unifiedLatexAstComplier`
28
+
29
+ Unified complier plugin that passes through a LaTeX AST without modification.
30
+
31
+ ### Usage
32
+
33
+ `unified().use(unifiedLatexAstComplier)`
34
+
35
+ ### Type
36
+
37
+ `Plugin<void[], Ast.Root, Ast.Root>`
38
+
39
+ ```typescript
40
+ function unifiedLatexAstComplier(): void;
41
+ ```
42
+
43
+ ## `unifiedLatexFromString`
44
+
45
+ Parse a string to a LaTeX AST.
46
+
47
+ ### Usage
48
+
49
+ `unified().use(unifiedLatexFromString[, options])`
50
+
51
+ #### options
52
+
53
+ ```typescript
54
+ { mode?: "math" | "regular"; macros?: Ast.MacroInfoRecord; environments?: Ast.EnvInfoRecord; }
55
+ ```
56
+
57
+ ### Type
58
+
59
+ `Plugin<{ mode?: "math" | "regular"; macros?: Ast.MacroInfoRecord; environments?: Ast.EnvInfoRecord; }[], string, Ast.Root>`
60
+
61
+ ```typescript
62
+ function unifiedLatexFromString(options: {
63
+ mode?: "math" | "regular";
64
+ macros?: Ast.MacroInfoRecord;
65
+ environments?: Ast.EnvInfoRecord;
66
+ }): void;
67
+ ```
68
+
69
+ ## `unifiedLatexFromStringMinimal`
70
+
71
+ Parse a string to a LaTeX AST with no post processing. For example,
72
+ no macro arguments will be attached, etc.
73
+
74
+ ### Usage
75
+
76
+ `unified().use(unifiedLatexFromStringMinimal[, options])`
77
+
78
+ #### options
79
+
80
+ ```typescript
81
+ PluginOptions
82
+ ```
83
+
84
+ ### Type
85
+
86
+ `Plugin<PluginOptions[], string, Ast.Root>`
87
+
88
+ ```typescript
89
+ function unifiedLatexFromStringMinimal(options: PluginOptions): void;
90
+ ```
91
+
92
+ # Functions
93
+
94
+ ## `parse(str)`
95
+
96
+ Parse the string into an AST.
97
+
98
+ ```typescript
99
+ function parse(str: String): Ast.Root;
100
+ ```
101
+
102
+ **Parameters**
103
+
104
+ | Param | Type |
105
+ | :---- | :------- |
106
+ | str | `String` |
107
+
108
+ ## `parseMath(str)`
109
+
110
+ Parse `str` into an AST. Parsing starts in math mode and a list of
111
+ nodes is returned (instead of a "root" node).
112
+
113
+ ```typescript
114
+ function parseMath(str: string | Ast.Ast): Ast.Node[];
115
+ ```
116
+
117
+ **Parameters**
118
+
119
+ | Param | Type |
120
+ | :---- | :------------------ |
121
+ | str | `string \| Ast.Ast` |
122
+
123
+ ## `parseMathMinimal(str)`
124
+
125
+ Parse `str` to an AST with minimal processing. E.g., macro
126
+ arguments are not attached to macros, etc. when parsed with this
127
+ function.
128
+
129
+ The parsing assumes a math-mode context, so, for example, `^` and `_` are
130
+ parsed as macros (even though arguments are not attached to them).
131
+
132
+ ```typescript
133
+ function parseMathMinimal(str: String): Ast.Node[];
134
+ ```
135
+
136
+ **Parameters**
137
+
138
+ | Param | Type |
139
+ | :---- | :------- |
140
+ | str | `String` |
141
+
142
+ ## `parseMinimal(str)`
143
+
144
+ Parse `str` to an AST with minimal processing. E.g., macro
145
+ arguments are not attached to macros, etc. when parsed with this
146
+ function.
147
+
148
+ ```typescript
149
+ function parseMinimal(str: String): Ast.Root;
150
+ ```
151
+
152
+ **Parameters**
153
+
154
+ | Param | Type |
155
+ | :---- | :------- |
156
+ | str | `String` |
157
+
158
+ # Types
159
+
160
+ ## `PluginOptions`
161
+
162
+ ```typescript
163
+ export type PluginOptions =
164
+ | {
165
+ mode?: "math" | "regular";
166
+ macros?: MacroInfoRecord;
167
+ environments?: EnvInfoRecord;
168
+ }
169
+ | undefined;
170
+ ```
package/index.cjs ADDED
@@ -0,0 +1,203 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // index.ts
20
+ var unified_latex_util_parse_exports = {};
21
+ __export(unified_latex_util_parse_exports, {
22
+ parse: () => parse,
23
+ parseMath: () => parseMath,
24
+ parseMathMinimal: () => parseMathMinimal,
25
+ parseMinimal: () => parseMinimal,
26
+ unifiedLatexAstComplier: () => unifiedLatexAstComplier,
27
+ unifiedLatexFromString: () => unifiedLatexFromString,
28
+ unifiedLatexFromStringMinimal: () => unifiedLatexFromStringMinimal
29
+ });
30
+ module.exports = __toCommonJS(unified_latex_util_parse_exports);
31
+
32
+ // libs/compiler-ast.ts
33
+ var unifiedLatexAstComplier = function unifiedLatexAstComplier2() {
34
+ Object.assign(this, { Compiler: (x) => x });
35
+ };
36
+
37
+ // libs/plugin-from-string.ts
38
+ var import_unified = require("unified");
39
+ var import_unified_latex_ctan = require("@unified-latex/unified-latex-ctan");
40
+ var import_unified_latex_util_trim = require("@unified-latex/unified-latex-util-trim");
41
+
42
+ // libs/parse-minimal.ts
43
+ var import_unified_latex_util_pegjs = require("@unified-latex/unified-latex-util-pegjs");
44
+ function parseMinimal(str) {
45
+ return import_unified_latex_util_pegjs.LatexPegParser.parse(str);
46
+ }
47
+ function parseMathMinimal(str) {
48
+ return import_unified_latex_util_pegjs.LatexPegParser.parse(str, { startRule: "math" });
49
+ }
50
+
51
+ // libs/plugin-from-string-minimal.ts
52
+ var unifiedLatexFromStringMinimal = function unifiedLatexFromStringMinimal2(options) {
53
+ const parser2 = (str) => {
54
+ if ((options == null ? void 0 : options.mode) === "math") {
55
+ return {
56
+ type: "root",
57
+ content: parseMathMinimal(str),
58
+ _renderInfo: { inMathMode: true }
59
+ };
60
+ }
61
+ return parseMinimal(str);
62
+ };
63
+ Object.assign(this, { Parser: parser2 });
64
+ };
65
+
66
+ // libs/process-macros-and-environments.ts
67
+ var import_unified_latex_util_visit2 = require("@unified-latex/unified-latex-util-visit");
68
+ var import_unified_latex_util_match2 = require("@unified-latex/unified-latex-util-match");
69
+ var import_unified_latex_util_print_raw2 = require("@unified-latex/unified-latex-util-print-raw");
70
+
71
+ // libs/reparse-math.ts
72
+ var import_unified_latex_util_match = require("@unified-latex/unified-latex-util-match");
73
+ var import_unified_latex_util_print_raw = require("@unified-latex/unified-latex-util-print-raw");
74
+ var import_unified_latex_util_visit = require("@unified-latex/unified-latex-util-visit");
75
+ function unifiedLatexReparseMathConstructPlugin({
76
+ mathEnvs,
77
+ mathMacros
78
+ }) {
79
+ const isMathEnvironment = import_unified_latex_util_match.match.createEnvironmentMatcher(mathEnvs);
80
+ const isMathMacro = import_unified_latex_util_match.match.createMacroMatcher(mathMacros);
81
+ return (tree) => {
82
+ (0, import_unified_latex_util_visit.visit)(tree, (node) => {
83
+ if (import_unified_latex_util_match.match.anyMacro(node)) {
84
+ for (const arg of node.args || []) {
85
+ if (arg.content.length > 0 && !wasParsedInMathMode(arg.content)) {
86
+ arg.content = parseMathMinimal((0, import_unified_latex_util_print_raw.printRaw)(arg.content));
87
+ }
88
+ }
89
+ }
90
+ if (import_unified_latex_util_match.match.anyEnvironment(node)) {
91
+ if (!wasParsedInMathMode(node.content)) {
92
+ node.content = parseMathMinimal((0, import_unified_latex_util_print_raw.printRaw)(node.content));
93
+ }
94
+ }
95
+ }, {
96
+ test: (node) => isMathEnvironment(node) || isMathMacro(node)
97
+ });
98
+ };
99
+ }
100
+ function wasParsedInMathMode(nodes) {
101
+ return !nodes.some((node) => import_unified_latex_util_match.match.anyString(node) && node.content.length > 1 || import_unified_latex_util_match.match.string(node, "^") || import_unified_latex_util_match.match.string(node, "_"));
102
+ }
103
+
104
+ // libs/process-macros-and-environments.ts
105
+ var import_unified_latex_util_arguments = require("@unified-latex/unified-latex-util-arguments");
106
+ var import_unified_latex_util_environments = require("@unified-latex/unified-latex-util-environments");
107
+ var unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse = function unifiedLatexAttachMacroArguments(options) {
108
+ const { environments = {}, macros = {} } = options || {};
109
+ const mathMacros = Object.fromEntries(Object.entries(macros).filter(([_, info]) => {
110
+ var _a;
111
+ return ((_a = info.renderInfo) == null ? void 0 : _a.inMathMode) === true;
112
+ }));
113
+ const mathEnvs = Object.fromEntries(Object.entries(environments).filter(([_, info]) => {
114
+ var _a;
115
+ return ((_a = info.renderInfo) == null ? void 0 : _a.inMathMode) === true;
116
+ }));
117
+ const mathReparser = unifiedLatexReparseMathConstructPlugin({
118
+ mathEnvs: Object.keys(mathEnvs),
119
+ mathMacros: Object.keys(mathMacros)
120
+ });
121
+ const isRelevantEnvironment = import_unified_latex_util_match2.match.createEnvironmentMatcher(environments);
122
+ const isRelevantMathEnvironment = import_unified_latex_util_match2.match.createEnvironmentMatcher(mathEnvs);
123
+ return (tree) => {
124
+ (0, import_unified_latex_util_visit2.visit)(tree, {
125
+ enter: (nodes) => {
126
+ if (!Array.isArray(nodes)) {
127
+ return;
128
+ }
129
+ (0, import_unified_latex_util_arguments.attachMacroArgsInArray)(nodes, mathMacros);
130
+ },
131
+ leave: (node) => {
132
+ if (!isRelevantMathEnvironment(node)) {
133
+ return;
134
+ }
135
+ const envName = (0, import_unified_latex_util_print_raw2.printRaw)(node.env);
136
+ const envInfo = environments[envName];
137
+ if (!envInfo) {
138
+ throw new Error(`Could not find environment info for environment "${envName}"`);
139
+ }
140
+ (0, import_unified_latex_util_environments.processEnvironment)(node, envInfo);
141
+ }
142
+ }, { includeArrays: true });
143
+ mathReparser(tree);
144
+ (0, import_unified_latex_util_visit2.visit)(tree, {
145
+ enter: (nodes) => {
146
+ if (!Array.isArray(nodes)) {
147
+ return;
148
+ }
149
+ (0, import_unified_latex_util_arguments.attachMacroArgsInArray)(nodes, macros);
150
+ },
151
+ leave: (node) => {
152
+ if (!isRelevantEnvironment(node)) {
153
+ return;
154
+ }
155
+ const envName = (0, import_unified_latex_util_print_raw2.printRaw)(node.env);
156
+ const envInfo = environments[envName];
157
+ if (!envInfo) {
158
+ throw new Error(`Could not find environment info for environment "${envName}"`);
159
+ }
160
+ (0, import_unified_latex_util_environments.processEnvironment)(node, envInfo);
161
+ }
162
+ }, { includeArrays: true });
163
+ };
164
+ };
165
+
166
+ // libs/plugin-from-string.ts
167
+ var unifiedLatexFromString = function unifiedLatexFromString2(options) {
168
+ const {
169
+ mode = "regular",
170
+ macros = {},
171
+ environments = {}
172
+ } = options || {};
173
+ const allMacroInfo = Object.assign({}, macros, ...Object.values(import_unified_latex_ctan.macroInfo));
174
+ const allEnvInfo = Object.assign({}, environments, ...Object.values(import_unified_latex_ctan.environmentInfo));
175
+ const fullParser = (0, import_unified.unified)().use(unifiedLatexFromStringMinimal, { mode }).use(unifiedLatexProcessMacrosAndEnvironmentsWithMathReparse, {
176
+ macros: allMacroInfo,
177
+ environments: allEnvInfo
178
+ }).use(import_unified_latex_util_trim.unifiedLatexTrimEnvironmentContents).use(import_unified_latex_util_trim.unifiedLatexTrimRoot).use(unifiedLatexAstComplier);
179
+ const parser2 = (str) => {
180
+ const file = fullParser.processSync({ value: str });
181
+ return file.result;
182
+ };
183
+ Object.assign(this, { Parser: parser2 });
184
+ };
185
+
186
+ // libs/parse.ts
187
+ var import_unified2 = require("unified");
188
+ var parser = (0, import_unified2.unified)().use(unifiedLatexFromString).freeze();
189
+ function parse(str) {
190
+ return parser.parse(str);
191
+ }
192
+
193
+ // libs/parse-math.ts
194
+ var import_unified3 = require("unified");
195
+ var import_unified_latex_util_print_raw3 = require("@unified-latex/unified-latex-util-print-raw");
196
+ function parseMath(str) {
197
+ if (typeof str !== "string") {
198
+ str = (0, import_unified_latex_util_print_raw3.printRaw)(str);
199
+ }
200
+ const file = (0, import_unified3.unified)().use(unifiedLatexFromString, { mode: "math" }).use(unifiedLatexAstComplier).processSync({ value: str });
201
+ return file.result.content;
202
+ }
203
+ //# sourceMappingURL=index.cjs.map
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@unified-latex/unified-latex-util-parse",
3
- "version": "1.0.9",
3
+ "version": "1.0.12",
4
4
  "description": "Tools for manipulating unified-latex ASTs",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "dependencies": {
8
- "@unified-latex/unified-latex-ctan": "^1.0.9",
9
- "@unified-latex/unified-latex-types": "^1.0.7",
10
- "@unified-latex/unified-latex-util-arguments": "^1.0.8",
11
- "@unified-latex/unified-latex-util-environments": "^1.0.8",
12
- "@unified-latex/unified-latex-util-match": "^1.0.8",
13
- "@unified-latex/unified-latex-util-pegjs": "^1.0.8",
14
- "@unified-latex/unified-latex-util-print-raw": "^1.0.8",
15
- "@unified-latex/unified-latex-util-trim": "^1.0.8",
16
- "@unified-latex/unified-latex-util-visit": "^1.0.8",
8
+ "@unified-latex/unified-latex-ctan": "^1.0.12",
9
+ "@unified-latex/unified-latex-types": "^1.0.12",
10
+ "@unified-latex/unified-latex-util-arguments": "^1.0.12",
11
+ "@unified-latex/unified-latex-util-environments": "^1.0.12",
12
+ "@unified-latex/unified-latex-util-match": "^1.0.12",
13
+ "@unified-latex/unified-latex-util-pegjs": "^1.0.12",
14
+ "@unified-latex/unified-latex-util-print-raw": "^1.0.12",
15
+ "@unified-latex/unified-latex-util-trim": "^1.0.12",
16
+ "@unified-latex/unified-latex-util-visit": "^1.0.12",
17
17
  "unified": "^10.1.2"
18
18
  },
19
19
  "repository": {
@@ -36,8 +36,8 @@
36
36
  "homepage": "https://github.com/siefkenj/unified-latex#readme",
37
37
  "exports": {
38
38
  ".": {
39
- "import": "index.js",
40
- "require": "index.cjs"
39
+ "import": "./index.js",
40
+ "require": "./index.cjs"
41
41
  },
42
42
  "./*js": "./*js",
43
43
  "./*": {
@@ -51,8 +51,8 @@
51
51
  }
52
52
  },
53
53
  "files": [
54
- "**/*.ts",
55
- "**/*.js",
54
+ "**/*ts",
55
+ "**/*js",
56
56
  "**/*.map",
57
57
  "**/*.json"
58
58
  ]