@drghaliasri/butex 6.1.3 → 6.1.4

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.
@@ -110,11 +110,6 @@ function butexFontDisplayTex(macro, text) {
110
110
  function butexMaghribiDisplayTex(text) {
111
111
  return butexFontDisplayTex("\\butexmaghribi", text);
112
112
  }
113
- function butexMaghribiDisplayLatex(text) {
114
- return `\\[
115
- ${butexMaghribiDisplayTex(text)}
116
- \\]`;
117
- }
118
113
  var BASMALA_LINES = [BUTEX_BASMALA_LINE1, BUTEX_BASMALA_LINE2, BUTEX_BASMALA_SALAWAT];
119
114
  function butexBasmalaDisplayTex() {
120
115
  return BASMALA_LINES.map((line) => butexMaghribiDisplayTex(line)).join(" \\\\ ");
@@ -9677,7 +9672,43 @@ function styledTextLatex(text, style) {
9677
9672
  }
9678
9673
  return output;
9679
9674
  }
9680
- function tokenLatex(token) {
9675
+ function semanticRefPrefix(kind) {
9676
+ if (kind === "fig") {
9677
+ return "\u0627\u0644\u0634\u0643\u0644";
9678
+ }
9679
+ if (kind === "tab") {
9680
+ return "\u0627\u0644\u062C\u062F\u0648\u0644";
9681
+ }
9682
+ return "\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629";
9683
+ }
9684
+ function semanticRefNames(kind) {
9685
+ if (kind === "fig") {
9686
+ return ["\u0627\u0644\u0634\u0643\u0644", "\u0634\u0643\u0644", "Figure", "figure"];
9687
+ }
9688
+ if (kind === "tab") {
9689
+ return ["\u0627\u0644\u062C\u062F\u0648\u0644", "\u062C\u062F\u0648\u0644", "Table", "table"];
9690
+ }
9691
+ return ["\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629", "\u0645\u0639\u0627\u062F\u0644\u0629", "Equation", "equation"];
9692
+ }
9693
+ function previousTextAlreadyNamesKind(previousToken, kind) {
9694
+ if (previousToken?.kind !== "text") {
9695
+ return false;
9696
+ }
9697
+ const text = previousToken.text.trimEnd();
9698
+ return semanticRefNames(kind).some((name) => text.endsWith(name));
9699
+ }
9700
+ function refTokenLatexForDocument(token, labelKinds, previousToken) {
9701
+ const reference = refTokenLatex(token.keys, token.refCommand);
9702
+ if (!labelKinds || token.keys.length !== 1) {
9703
+ return reference;
9704
+ }
9705
+ const kind = labelKinds.get(token.keys[0]);
9706
+ if (!kind || previousTextAlreadyNamesKind(previousToken, kind)) {
9707
+ return reference;
9708
+ }
9709
+ return `${semanticRefPrefix(kind)}~${reference}`;
9710
+ }
9711
+ function tokenLatex(token, labelKinds, previousToken) {
9681
9712
  if (token.kind === "text") {
9682
9713
  return styledTextLatex(token.text, token.style);
9683
9714
  }
@@ -9685,7 +9716,7 @@ function tokenLatex(token) {
9685
9716
  return citeTokenLatex(token.keys);
9686
9717
  }
9687
9718
  if (token.kind === "ref") {
9688
- return refTokenLatex(token.keys, token.refCommand);
9719
+ return refTokenLatexForDocument(token, labelKinds, previousToken);
9689
9720
  }
9690
9721
  if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
9691
9722
  const body = mathBodyLatex(token);
@@ -9702,11 +9733,11 @@ ${body}
9702
9733
  }
9703
9734
  return renderMathNodeLatex(token.math);
9704
9735
  }
9705
- function inlineFieldLatex(field) {
9706
- return field.tokens.map((token) => tokenLatex(token)).join("");
9736
+ function inlineFieldLatexWithLabelKinds(field, labelKinds) {
9737
+ return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1])).join("");
9707
9738
  }
9708
- function textBlockLatex(block) {
9709
- const content = inlineFieldLatex(block.field);
9739
+ function textBlockLatex(block, labelKinds) {
9740
+ const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds);
9710
9741
  const body = block.command === "\\paragraph" ? `\\par
9711
9742
  ${content}` : `${block.command}{${content}}`;
9712
9743
  if (block.command === "\\paragraph" && block.centered) {
@@ -9719,38 +9750,61 @@ ${body}
9719
9750
  function indent(value) {
9720
9751
  return value.split("\n").map((line) => ` ${line}`).join("\n");
9721
9752
  }
9722
- function listBlockLatex(block, references) {
9753
+ function listBlockLatex(block, references, labelKinds, fixedFloats) {
9723
9754
  const items = block.items.map((item) => {
9724
- const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references))).join("");
9725
- return ` \\item ${inlineFieldLatex(item.field)}${nested}`;
9755
+ const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats))).join("");
9756
+ return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds)}${nested}`;
9726
9757
  }).join("\n");
9727
9758
  return `${block.command}
9728
9759
  ${items}
9729
9760
  ${block.closing}`;
9730
9761
  }
9731
- function tabularInnerLatex(block) {
9732
- const rows = block.rows.map((row) => ` ${row.map((cell) => inlineFieldLatex(cell)).join(" & ")}`).join(" \\\\\n");
9762
+ function defaultGridColumnSpec(block) {
9763
+ const compact = block.columns.replace(/\s+/g, "");
9764
+ const columnCount = block.rows[0]?.length ?? 0;
9765
+ if (columnCount === 0 || compact.length !== columnCount || !/^[lcr]+$/.test(compact)) {
9766
+ return null;
9767
+ }
9768
+ return `|${compact.split("").join("|")}|`;
9769
+ }
9770
+ function tabularInnerLatex(block, labelKinds) {
9771
+ const rows = block.rows.map(
9772
+ (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds)).join(" & ")}`
9773
+ );
9774
+ const gridColumns = defaultGridColumnSpec(block);
9775
+ if (gridColumns) {
9776
+ const griddedRows = rows.map((row) => `${row} \\\\ \\hline`).join("\n");
9777
+ return `${block.command}{${gridColumns}}
9778
+ \\hline
9779
+ ${griddedRows}
9780
+ ${block.closing}`;
9781
+ }
9733
9782
  return `${block.command}{${block.columns}}
9734
- ${rows}
9783
+ ${rows.join(" \\\\\n")}
9735
9784
  ${block.closing}`;
9736
9785
  }
9737
- function floatCaptionLabelLines(block) {
9786
+ function floatCaptionLabelLines(block, counter) {
9738
9787
  const lines = [];
9739
- if (block.captionEnabled && block.caption.trim().length > 0) {
9788
+ const hasCaption = block.captionEnabled && block.caption.trim().length > 0;
9789
+ if (hasCaption) {
9740
9790
  lines.push(` \\caption{${block.caption.trim()}}`);
9741
9791
  }
9742
9792
  if (block.labelEnabled && block.label.trim().length > 0) {
9793
+ if (!hasCaption) {
9794
+ lines.push(` \\refstepcounter{${counter}}`);
9795
+ }
9743
9796
  lines.push(` \\label{${block.label.trim()}}`);
9744
9797
  }
9745
9798
  return lines;
9746
9799
  }
9747
- function tableBlockLatex(block) {
9748
- const lines = ["\\begin{table}[htbp]"];
9800
+ function tableBlockLatex(block, labelKinds, fixedFloats) {
9801
+ const placement = fixedFloats ? "H" : "htbp";
9802
+ const lines = [`\\begin{table}[${placement}]`];
9749
9803
  if (block.centered) {
9750
9804
  lines.push(" \\centering");
9751
9805
  }
9752
- lines.push(...floatCaptionLabelLines(block));
9753
- lines.push(indent(tabularInnerLatex(block)));
9806
+ lines.push(...floatCaptionLabelLines(block, "table"));
9807
+ lines.push(indent(tabularInnerLatex(block, labelKinds)));
9754
9808
  lines.push("\\end{table}");
9755
9809
  return lines.join("\n");
9756
9810
  }
@@ -9769,28 +9823,29 @@ function imageOptionsLatex(block, constrainToColumn) {
9769
9823
  }
9770
9824
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
9771
9825
  }
9772
- function imageBlockLatex(block) {
9773
- const lines = ["\\begin{figure}[htbp]"];
9826
+ function imageBlockLatex(block, fixedFloats) {
9827
+ const placement = fixedFloats ? "H" : "htbp";
9828
+ const lines = [`\\begin{figure}[${placement}]`];
9774
9829
  if (block.centered) {
9775
9830
  lines.push(" \\centering");
9776
9831
  }
9777
9832
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
9778
- lines.push(...floatCaptionLabelLines(block));
9833
+ lines.push(...floatCaptionLabelLines(block, "figure"));
9779
9834
  lines.push("\\end{figure}");
9780
9835
  return lines.join("\n");
9781
9836
  }
9782
- function blockLatex(block, references) {
9837
+ function blockLatex(block, references, labelKinds, fixedFloats) {
9783
9838
  if (block.kind === "textBlock") {
9784
- return textBlockLatex(block);
9839
+ return textBlockLatex(block, labelKinds);
9785
9840
  }
9786
9841
  if (block.kind === "list") {
9787
- return listBlockLatex(block, references);
9842
+ return listBlockLatex(block, references, labelKinds, fixedFloats);
9788
9843
  }
9789
9844
  if (block.kind === "table") {
9790
- return tableBlockLatex(block);
9845
+ return tableBlockLatex(block, labelKinds, fixedFloats);
9791
9846
  }
9792
9847
  if (block.kind === "image") {
9793
- return imageBlockLatex(block);
9848
+ return imageBlockLatex(block, fixedFloats);
9794
9849
  }
9795
9850
  if (block.kind === "bibliography") {
9796
9851
  const width = String(Math.max(references.length, 9));
@@ -9832,12 +9887,29 @@ ${meta.abstract.trim()}
9832
9887
  }
9833
9888
  return parts;
9834
9889
  }
9890
+ function documentLabelKindMap(document2) {
9891
+ const map = /* @__PURE__ */ new Map();
9892
+ for (const entry of collectDocument2Labels(document2)) {
9893
+ if (!map.has(entry.key)) {
9894
+ map.set(entry.key, entry.kind);
9895
+ }
9896
+ }
9897
+ return map;
9898
+ }
9899
+ function closingHamdalaLatex() {
9900
+ return `\\begin{center}
9901
+ \\maghribi{${BUTEX_CLOSING_HAMDALA}}
9902
+ \\end{center}`;
9903
+ }
9835
9904
  function document2Latex(document2, options = {}) {
9836
9905
  const meta = document2.meta ?? emptyDocument2Meta();
9837
9906
  const twocolumn = options.twocolumn === true;
9838
9907
  const front = articleFrontMatterLatex(meta);
9839
- const bodyBlocks = document2.blocks.map((block) => blockLatex(block, document2.references));
9840
- const closing = butexMaghribiDisplayLatex(BUTEX_CLOSING_HAMDALA);
9908
+ const labelKinds = documentLabelKindMap(document2);
9909
+ const bodyBlocks = document2.blocks.map(
9910
+ (block) => blockLatex(block, document2.references, labelKinds, twocolumn)
9911
+ );
9912
+ const closing = closingHamdalaLatex();
9841
9913
  const body = [...front, ...bodyBlocks, closing].filter((part) => part.length > 0).join("\n\n");
9842
9914
  const wrapDocument = options.wrapDocument !== false;
9843
9915
  if (!wrapDocument) {
@@ -9846,10 +9918,11 @@ function document2Latex(document2, options = {}) {
9846
9918
 
9847
9919
  ${body}` : body;
9848
9920
  }
9921
+ const floatPackage = twocolumn ? "\n\\usepackage{float}\n" : "\n";
9849
9922
  const preamble = getArabicXeLatexPreamble({
9850
9923
  digitsMapping: options.digitsMapping ?? digitsMappingFromDigitForm(options.digitForm),
9851
9924
  twocolumn
9852
- }) + "\n" + articleMaketitlePreamble(meta, twocolumn);
9925
+ }) + floatPackage + articleMaketitlePreamble(meta, twocolumn);
9853
9926
  return `${preamble}
9854
9927
  \\begin{document}
9855
9928