@drghaliasri/butex 8.0.1 → 8.0.3

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.
@@ -237,7 +237,9 @@ ${this.closing}` + this.latexScripts();
237
237
  }
238
238
  const formattedLines = this.lines.map((line) => ` ${line.arabicLatex()}`);
239
239
  const content = formattedLines.join(" \\\\\n");
240
- const fullContent = `${this.opening}
240
+ const fullContent = this.opening === "\\begin{cases}" && this.closing === "\\end{cases}" ? `\\left.\\begin{array}{rl}
241
+ ${content}
242
+ \\end{array}\\right\\}` : `${this.opening}
241
243
  ${content}
242
244
  ${this.closing}`;
243
245
  return this.arabicLatexScripts(sup, sub, fullContent);
@@ -2702,7 +2704,7 @@ function createAccentNode(accentId) {
2702
2704
  }
2703
2705
  function createGridEnvNode(envName, rows, columns, columnAlignments) {
2704
2706
  const safeRows = Math.max(1, Math.min(12, Math.trunc(rows)));
2705
- const safeColumns = Math.max(1, Math.min(12, Math.trunc(columns)));
2707
+ const safeColumns = envName === "cases" ? 2 : Math.max(1, Math.min(12, Math.trunc(columns)));
2706
2708
  const matrixStyles = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
2707
2709
  const isMatrix = matrixStyles.has(envName) && envName !== "array" && envName !== "aligned";
2708
2710
  return {
@@ -3006,6 +3008,9 @@ function renderMatrixRowsArabic(node, options) {
3006
3008
  const spec = (node.columnAlignments ?? []).slice(0, node.matrixRows[0]?.length ?? 0).reverse().map(reverseAlignmentForArabic).join("");
3007
3009
  return `\\begin{array}{${spec || "c"}}${rows.join(" \\\\\n")}\\end{array}`;
3008
3010
  }
3011
+ if (envName === "cases" && options?.casesPresentation) {
3012
+ return `\\left.\\begin{array}{rl}${rows.join(" \\\\\n")}\\end{array}\\right\\}`;
3013
+ }
3009
3014
  return `\\begin{${envName}}${rows.join(" \\\\\n")}\\end{${envName}}`;
3010
3015
  }
3011
3016
  function renderAccentEnglish(command, accent, base, options) {
@@ -3027,6 +3032,10 @@ function renderAccentArabic(command, accent, base, options) {
3027
3032
  function escapeText(s) {
3028
3033
  return s.replace(/\\/g, "\\textbackslash{}").replace(/[{}]/g, "\\$&");
3029
3034
  }
3035
+ function escapeAtomicLiteral(text) {
3036
+ const unescaped = text.replace(/\\([%_&#{}$])/g, "$1");
3037
+ return escapeText(unescaped).replace(/[%_&#$]/g, "\\$&");
3038
+ }
3030
3039
  function renderCharContent(node, options) {
3031
3040
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3032
3041
  const font = node.characterFont ?? "default";
@@ -3073,7 +3082,7 @@ function renderNodeEnglish(node, options) {
3073
3082
  return `${body}${latinScripts(node, options)}`;
3074
3083
  }
3075
3084
  if (node.kind === "atomicOperatorCommand") {
3076
- const body = atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3085
+ const body = node.sourceLatex === "\\le" || node.sourceLatex === "\\ge" ? node.sourceLatex : atomicOperatorCommandSpec(node.expr)?.Tex ?? node.expr;
3077
3086
  return `${body}${latinScripts(node, options)}`;
3078
3087
  }
3079
3088
  if (node.kind === "standardCommand") {
@@ -3090,7 +3099,8 @@ function renderNodeEnglish(node, options) {
3090
3099
  if (node.wrapperCommand) {
3091
3100
  const expr = formatDigits(node.expr, selectedDigitForm(options));
3092
3101
  const wrapper = node.wrapperCommand === "\\boldarabic" ? "\\mathbf" : node.wrapperCommand;
3093
- return `${wrapper}{${escapeText(expr)}}${latinScripts(node, options)}`;
3102
+ const escaped = wrapper === "\\operatorname" || wrapper === "\\text" ? escapeAtomicLiteral(expr) : escapeText(expr);
3103
+ return `${wrapper}{${escaped}}${latinScripts(node, options)}`;
3094
3104
  }
3095
3105
  return `${renderCharContent(node, options)}${latinScripts(node, options)}`;
3096
3106
  }
@@ -3151,7 +3161,8 @@ function renderNodeArabic(node, options) {
3151
3161
  const content = renderCharContent(node, options);
3152
3162
  if (node.wrapperCommand) {
3153
3163
  const wrapper = node.wrapperCommand === "\\mathbf" ? "\\boldarabic" : node.wrapperCommand;
3154
- return arabicScripts(node, `${wrapper}{${content}}`, options);
3164
+ const literal = wrapper === "\\operatorname" || wrapper === "\\text" ? escapeAtomicLiteral(expr) : content;
3165
+ return arabicScripts(node, `${wrapper}{${literal}}`, options);
3155
3166
  }
3156
3167
  return arabicScripts(node, (node.characterFont ?? "default") === "default" ? `\\text{${escapeText(expr)}}` : content, options);
3157
3168
  }
@@ -3180,6 +3191,9 @@ function renderEnglishLatexFromSession(session) {
3180
3191
  function renderArabicLatexFromSession(session) {
3181
3192
  return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western" });
3182
3193
  }
3194
+ function renderArabicPreviewLatexFromSession(session) {
3195
+ return renderChainArabic(session.arabicTree, { digitForm: session.currentDigitForm ?? "western", casesPresentation: true });
3196
+ }
3183
3197
 
3184
3198
  // src/editor/state.ts
3185
3199
  function appendDebug(session, command, note) {
@@ -3529,6 +3543,11 @@ var MATRIX_ENV_CSS = `
3529
3543
  transform-origin: center;
3530
3544
  }
3531
3545
 
3546
+ .matrix-env--cases .matrix-env__wrapper:empty {
3547
+ min-width: 0;
3548
+ padding: 0;
3549
+ }
3550
+
3532
3551
  .matrix-env__grid {
3533
3552
  display: inline-grid;
3534
3553
  direction: ltr;
@@ -4045,8 +4064,9 @@ function commandIdForTex(tex) {
4045
4064
  return null;
4046
4065
  }
4047
4066
  function operatorCommandIdForTex(tex) {
4067
+ const normalized = tex === "\\le" ? "\\leq" : tex === "\\ge" ? "\\geq" : tex;
4048
4068
  for (const spec of Object.values(ATOMIC_OPERATOR_COMMANDS)) {
4049
- if (spec.Tex === tex) {
4069
+ if (spec.Tex === normalized) {
4050
4070
  return spec.id;
4051
4071
  }
4052
4072
  }
@@ -4077,7 +4097,7 @@ function attachScripts(astNode, editorNode) {
4077
4097
  }
4078
4098
  var MATRIX_ENV_NAMES = /* @__PURE__ */ new Set(["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"]);
4079
4099
  function isGridEnvName(value) {
4080
- return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned";
4100
+ return MATRIX_ENV_NAMES.has(value) || value === "array" || value === "aligned" || value === "cases";
4081
4101
  }
4082
4102
  function isMatrixEnvName(value) {
4083
4103
  return MATRIX_ENV_NAMES.has(value);
@@ -4124,6 +4144,9 @@ function envToEditorNode(node) {
4124
4144
  return { editable: false, reason: `\u0625\u063A\u0644\u0627\u0642 \u0628\u064A\u0626\u0629 \u0631\u064A\u0627\u0636\u064A\u0629 \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645 \u062D\u0627\u0644\u064A\u0627: ${node.closing}` };
4125
4145
  }
4126
4146
  const rowCells = node.lines.map(splitEnvLine);
4147
+ if (parsed.envName === "cases" && (rowCells.length === 0 || rowCells.length > 12 || rowCells.some((row) => row.length !== 2))) {
4148
+ return { editable: false, reason: "\u0628\u064A\u0626\u0629 cases \u062A\u062A\u0637\u0644\u0628 \u062A\u0639\u0628\u064A\u0631\u0627 \u0648\u0634\u0631\u0637\u0627 \u0641\u064A \u0643\u0644 \u0635\u0641 (\u0639\u0645\u0648\u062F\u0627\u0646)" };
4149
+ }
4127
4150
  const rows = Math.max(1, rowCells.length);
4128
4151
  const columns = Math.max(1, ...rowCells.map((row) => row.length));
4129
4152
  const env = createGridEnvNode(parsed.envName, rows, columns, parsed.alignments);
@@ -4206,14 +4229,19 @@ function wrappedAtomicCommandId(node) {
4206
4229
  return commandIdForTex(`${node.name}{${child.expr}}`);
4207
4230
  }
4208
4231
  function editableWrapperCommand(node) {
4209
- if (node.name !== "\\mathtt" && node.name !== "\\mathsf" && node.name !== "\\unit" && node.name !== "\\mathbf" && node.name !== "\\boldarabic") {
4232
+ if (node.name !== "\\mathtt" && node.name !== "\\operatorname" && node.name !== "\\text" && node.name !== "\\mathsf" && node.name !== "\\unit" && node.name !== "\\mathbf" && node.name !== "\\boldarabic") {
4233
+ return null;
4234
+ }
4235
+ if (node.name === "\\text" && node.sourceLatex && !node.sourceLatex.startsWith("\\text{")) {
4210
4236
  return null;
4211
4237
  }
4212
4238
  const child = singleCommandChar(node);
4239
+ if (node.name === "\\text" && child?.sourceLatex && !node.sourceLatex) return null;
4213
4240
  let editorNode;
4214
4241
  if (child) {
4215
4242
  const normalized = normalizeCharImportExpr(child.expr);
4216
4243
  editorNode = createEditorNode("char", normalized.expr);
4244
+ if (child.sourceLatex) editorNode.sourceLatex = child.sourceLatex;
4217
4245
  if (normalized.characterFont !== "default") {
4218
4246
  editorNode.characterFont = normalized.characterFont;
4219
4247
  }
@@ -4225,7 +4253,7 @@ function editableWrapperCommand(node) {
4225
4253
  if (!nested?.editable) return null;
4226
4254
  editorNode = nested.node;
4227
4255
  }
4228
- editorNode.wrapperCommand = node.name === "\\mathtt" ? "\\mathtt" : node.name === "\\mathbf" ? "\\mathbf" : node.name === "\\boldarabic" ? "\\boldarabic" : "\\unit";
4256
+ editorNode.wrapperCommand = node.name === "\\mathsf" ? "\\unit" : node.name;
4229
4257
  const scripts = attachScripts(node, editorNode);
4230
4258
  if (scripts) {
4231
4259
  return scripts;
@@ -4359,6 +4387,7 @@ function commandToEditorNode(node) {
4359
4387
  const operatorId = operatorCommandIdForTex(node.name);
4360
4388
  if (operatorId && node.optionalArgs.length === 0 && node.mandatoryArgs.length === 0) {
4361
4389
  const atomicOperator = createEditorNode("atomicOperatorCommand", operatorId);
4390
+ if (node.name === "\\le" || node.name === "\\ge") atomicOperator.sourceLatex = node.name;
4362
4391
  const scripts = attachScripts(node, atomicOperator);
4363
4392
  if (scripts) {
4364
4393
  return scripts;
@@ -4526,8 +4555,8 @@ function mathTokenToEditorSession(math, side = "arabic") {
4526
4555
  result.session.sync = buildInitialSyncMap(result.session.englishTree, result.session.arabicTree);
4527
4556
  return result;
4528
4557
  }
4529
- function mathSourceFromSession(session, side, opening = "$", closing = "$") {
4530
- const inner = side === "english" ? renderEnglishLatexFromSession(session) : renderArabicLatexFromSession(session);
4558
+ function mathSourceFromSession(session, side, opening = "$", closing = "$", presentation = false) {
4559
+ const inner = side === "english" ? renderEnglishLatexFromSession(session) : presentation ? renderArabicPreviewLatexFromSession(session) : renderArabicLatexFromSession(session);
4531
4560
  if (opening === "$" || opening === "\\(") {
4532
4561
  return opening + inner + closing;
4533
4562
  }
@@ -4540,11 +4569,11 @@ ${closing}`;
4540
4569
  ${inner}
4541
4570
  ${closing}`;
4542
4571
  }
4543
- function mathTokenSourceForSide(token, side, digitForm) {
4572
+ function mathTokenSourceForSide(token, side, digitForm, presentation = false) {
4544
4573
  if (!token.math || token.sourceOwner === "raw") {
4545
4574
  return token.source;
4546
4575
  }
4547
- if (token.sourceSide === side && digitForm === void 0) {
4576
+ if (!presentation && token.sourceSide === side && digitForm === void 0) {
4548
4577
  return token.source;
4549
4578
  }
4550
4579
  const result = mathTokenToEditorSession(token.math, side);
@@ -4552,12 +4581,12 @@ function mathTokenSourceForSide(token, side, digitForm) {
4552
4581
  return token.source;
4553
4582
  }
4554
4583
  const session = digitForm === void 0 ? result.session : setCurrentDigitForm(result.session, digitForm);
4555
- return mathSourceFromSession(session, side, token.opening, token.closing);
4584
+ return mathSourceFromSession(session, side, token.opening, token.closing, presentation);
4556
4585
  }
4557
4586
 
4558
4587
  // src/document/previewModel.ts
4559
4588
  function mathTex(token, equationSide, digitForm) {
4560
- return mathTokenSourceForSide(token, equationSide, digitForm);
4589
+ return mathTokenSourceForSide(token, equationSide, digitForm, true);
4561
4590
  }
4562
4591
  function previewInlines(field, islands, output, equationSide, document2, previewOptions) {
4563
4592
  return field.tokens.map((token) => {