@drghaliasri/butex 6.1.2 → 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.
@@ -6837,7 +6837,43 @@ function styledTextLatex(text, style) {
6837
6837
  }
6838
6838
  return output;
6839
6839
  }
6840
- function tokenLatex(token) {
6840
+ function semanticRefPrefix(kind) {
6841
+ if (kind === "fig") {
6842
+ return "\u0627\u0644\u0634\u0643\u0644";
6843
+ }
6844
+ if (kind === "tab") {
6845
+ return "\u0627\u0644\u062C\u062F\u0648\u0644";
6846
+ }
6847
+ return "\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629";
6848
+ }
6849
+ function semanticRefNames(kind) {
6850
+ if (kind === "fig") {
6851
+ return ["\u0627\u0644\u0634\u0643\u0644", "\u0634\u0643\u0644", "Figure", "figure"];
6852
+ }
6853
+ if (kind === "tab") {
6854
+ return ["\u0627\u0644\u062C\u062F\u0648\u0644", "\u062C\u062F\u0648\u0644", "Table", "table"];
6855
+ }
6856
+ return ["\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629", "\u0645\u0639\u0627\u062F\u0644\u0629", "Equation", "equation"];
6857
+ }
6858
+ function previousTextAlreadyNamesKind(previousToken, kind) {
6859
+ if (previousToken?.kind !== "text") {
6860
+ return false;
6861
+ }
6862
+ const text = previousToken.text.trimEnd();
6863
+ return semanticRefNames(kind).some((name) => text.endsWith(name));
6864
+ }
6865
+ function refTokenLatexForDocument(token, labelKinds, previousToken) {
6866
+ const reference = refTokenLatex(token.keys, token.refCommand);
6867
+ if (!labelKinds || token.keys.length !== 1) {
6868
+ return reference;
6869
+ }
6870
+ const kind = labelKinds.get(token.keys[0]);
6871
+ if (!kind || previousTextAlreadyNamesKind(previousToken, kind)) {
6872
+ return reference;
6873
+ }
6874
+ return `${semanticRefPrefix(kind)}~${reference}`;
6875
+ }
6876
+ function tokenLatex(token, labelKinds, previousToken) {
6841
6877
  if (token.kind === "text") {
6842
6878
  return styledTextLatex(token.text, token.style);
6843
6879
  }
@@ -6845,7 +6881,7 @@ function tokenLatex(token) {
6845
6881
  return citeTokenLatex(token.keys);
6846
6882
  }
6847
6883
  if (token.kind === "ref") {
6848
- return refTokenLatex(token.keys, token.refCommand);
6884
+ return refTokenLatexForDocument(token, labelKinds, previousToken);
6849
6885
  }
6850
6886
  if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
6851
6887
  const body = mathBodyLatex(token);
@@ -6862,11 +6898,14 @@ ${body}
6862
6898
  }
6863
6899
  return renderMathNodeLatex(token.math);
6864
6900
  }
6901
+ function inlineFieldLatexWithLabelKinds(field, labelKinds) {
6902
+ return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1])).join("");
6903
+ }
6865
6904
  function inlineFieldLatex(field) {
6866
- return field.tokens.map((token) => tokenLatex(token)).join("");
6905
+ return inlineFieldLatexWithLabelKinds(field);
6867
6906
  }
6868
- function textBlockLatex(block) {
6869
- const content = inlineFieldLatex(block.field);
6907
+ function textBlockLatex(block, labelKinds) {
6908
+ const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds);
6870
6909
  const body = block.command === "\\paragraph" ? `\\par
6871
6910
  ${content}` : `${block.command}{${content}}`;
6872
6911
  if (block.command === "\\paragraph" && block.centered) {
@@ -6879,38 +6918,61 @@ ${body}
6879
6918
  function indent(value) {
6880
6919
  return value.split("\n").map((line) => ` ${line}`).join("\n");
6881
6920
  }
6882
- function listBlockLatex(block, references) {
6921
+ function listBlockLatex(block, references, labelKinds, fixedFloats) {
6883
6922
  const items = block.items.map((item) => {
6884
- const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references))).join("");
6885
- return ` \\item ${inlineFieldLatex(item.field)}${nested}`;
6923
+ const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats))).join("");
6924
+ return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds)}${nested}`;
6886
6925
  }).join("\n");
6887
6926
  return `${block.command}
6888
6927
  ${items}
6889
6928
  ${block.closing}`;
6890
6929
  }
6891
- function tabularInnerLatex(block) {
6892
- const rows = block.rows.map((row) => ` ${row.map((cell) => inlineFieldLatex(cell)).join(" & ")}`).join(" \\\\\n");
6930
+ function defaultGridColumnSpec(block) {
6931
+ const compact = block.columns.replace(/\s+/g, "");
6932
+ const columnCount = block.rows[0]?.length ?? 0;
6933
+ if (columnCount === 0 || compact.length !== columnCount || !/^[lcr]+$/.test(compact)) {
6934
+ return null;
6935
+ }
6936
+ return `|${compact.split("").join("|")}|`;
6937
+ }
6938
+ function tabularInnerLatex(block, labelKinds) {
6939
+ const rows = block.rows.map(
6940
+ (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds)).join(" & ")}`
6941
+ );
6942
+ const gridColumns = defaultGridColumnSpec(block);
6943
+ if (gridColumns) {
6944
+ const griddedRows = rows.map((row) => `${row} \\\\ \\hline`).join("\n");
6945
+ return `${block.command}{${gridColumns}}
6946
+ \\hline
6947
+ ${griddedRows}
6948
+ ${block.closing}`;
6949
+ }
6893
6950
  return `${block.command}{${block.columns}}
6894
- ${rows}
6951
+ ${rows.join(" \\\\\n")}
6895
6952
  ${block.closing}`;
6896
6953
  }
6897
- function floatCaptionLabelLines(block) {
6954
+ function floatCaptionLabelLines(block, counter) {
6898
6955
  const lines = [];
6899
- if (block.captionEnabled && block.caption.trim().length > 0) {
6956
+ const hasCaption = block.captionEnabled && block.caption.trim().length > 0;
6957
+ if (hasCaption) {
6900
6958
  lines.push(` \\caption{${block.caption.trim()}}`);
6901
6959
  }
6902
6960
  if (block.labelEnabled && block.label.trim().length > 0) {
6961
+ if (!hasCaption) {
6962
+ lines.push(` \\refstepcounter{${counter}}`);
6963
+ }
6903
6964
  lines.push(` \\label{${block.label.trim()}}`);
6904
6965
  }
6905
6966
  return lines;
6906
6967
  }
6907
- function tableBlockLatex(block) {
6908
- const lines = ["\\begin{table}[htbp]"];
6968
+ function tableBlockLatex(block, labelKinds, fixedFloats) {
6969
+ const placement = fixedFloats ? "H" : "htbp";
6970
+ const lines = [`\\begin{table}[${placement}]`];
6909
6971
  if (block.centered) {
6910
6972
  lines.push(" \\centering");
6911
6973
  }
6912
- lines.push(...floatCaptionLabelLines(block));
6913
- lines.push(indent(tabularInnerLatex(block)));
6974
+ lines.push(...floatCaptionLabelLines(block, "table"));
6975
+ lines.push(indent(tabularInnerLatex(block, labelKinds)));
6914
6976
  lines.push("\\end{table}");
6915
6977
  return lines.join("\n");
6916
6978
  }
@@ -6929,28 +6991,29 @@ function imageOptionsLatex(block, constrainToColumn) {
6929
6991
  }
6930
6992
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
6931
6993
  }
6932
- function imageBlockLatex(block) {
6933
- const lines = ["\\begin{figure}[htbp]"];
6994
+ function imageBlockLatex(block, fixedFloats) {
6995
+ const placement = fixedFloats ? "H" : "htbp";
6996
+ const lines = [`\\begin{figure}[${placement}]`];
6934
6997
  if (block.centered) {
6935
6998
  lines.push(" \\centering");
6936
6999
  }
6937
7000
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
6938
- lines.push(...floatCaptionLabelLines(block));
7001
+ lines.push(...floatCaptionLabelLines(block, "figure"));
6939
7002
  lines.push("\\end{figure}");
6940
7003
  return lines.join("\n");
6941
7004
  }
6942
- function blockLatex(block, references) {
7005
+ function blockLatex(block, references, labelKinds, fixedFloats) {
6943
7006
  if (block.kind === "textBlock") {
6944
- return textBlockLatex(block);
7007
+ return textBlockLatex(block, labelKinds);
6945
7008
  }
6946
7009
  if (block.kind === "list") {
6947
- return listBlockLatex(block, references);
7010
+ return listBlockLatex(block, references, labelKinds, fixedFloats);
6948
7011
  }
6949
7012
  if (block.kind === "table") {
6950
- return tableBlockLatex(block);
7013
+ return tableBlockLatex(block, labelKinds, fixedFloats);
6951
7014
  }
6952
7015
  if (block.kind === "image") {
6953
- return imageBlockLatex(block);
7016
+ return imageBlockLatex(block, fixedFloats);
6954
7017
  }
6955
7018
  if (block.kind === "bibliography") {
6956
7019
  const width = String(Math.max(references.length, 9));
@@ -6992,12 +7055,29 @@ ${meta.abstract.trim()}
6992
7055
  }
6993
7056
  return parts;
6994
7057
  }
7058
+ function documentLabelKindMap(document2) {
7059
+ const map = /* @__PURE__ */ new Map();
7060
+ for (const entry of collectDocument2Labels(document2)) {
7061
+ if (!map.has(entry.key)) {
7062
+ map.set(entry.key, entry.kind);
7063
+ }
7064
+ }
7065
+ return map;
7066
+ }
7067
+ function closingHamdalaLatex() {
7068
+ return `\\begin{center}
7069
+ \\maghribi{${BUTEX_CLOSING_HAMDALA}}
7070
+ \\end{center}`;
7071
+ }
6995
7072
  function document2Latex(document2, options = {}) {
6996
7073
  const meta = document2.meta ?? emptyDocument2Meta();
6997
7074
  const twocolumn = options.twocolumn === true;
6998
7075
  const front = articleFrontMatterLatex(meta);
6999
- const bodyBlocks = document2.blocks.map((block) => blockLatex(block, document2.references));
7000
- const closing = butexMaghribiDisplayLatex(BUTEX_CLOSING_HAMDALA);
7076
+ const labelKinds = documentLabelKindMap(document2);
7077
+ const bodyBlocks = document2.blocks.map(
7078
+ (block) => blockLatex(block, document2.references, labelKinds, twocolumn)
7079
+ );
7080
+ const closing = closingHamdalaLatex();
7001
7081
  const body = [...front, ...bodyBlocks, closing].filter((part) => part.length > 0).join("\n\n");
7002
7082
  const wrapDocument = options.wrapDocument !== false;
7003
7083
  if (!wrapDocument) {
@@ -7006,10 +7086,11 @@ function document2Latex(document2, options = {}) {
7006
7086
 
7007
7087
  ${body}` : body;
7008
7088
  }
7089
+ const floatPackage = twocolumn ? "\n\\usepackage{float}\n" : "\n";
7009
7090
  const preamble = getArabicXeLatexPreamble({
7010
7091
  digitsMapping: options.digitsMapping ?? digitsMappingFromDigitForm(options.digitForm),
7011
7092
  twocolumn
7012
- }) + "\n" + articleMaketitlePreamble(meta, twocolumn);
7093
+ }) + floatPackage + articleMaketitlePreamble(meta, twocolumn);
7013
7094
  return `${preamble}
7014
7095
  \\begin{document}
7015
7096