Skip to content

Commit 647477c

Browse files
committed
Merge remote-tracking branch 'origin/master' into sso-exclusive-all
2 parents 6c1e4d2 + 628cd26 commit 647477c

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

src/packages/jupyter/redux/actions.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
7575
public _complete_request?: number;
7676
public store: JupyterStore;
7777
public syncdb: SyncDB;
78-
private labels?: { [label: string]: { tag: string; id: string } };
78+
private labels?: {
79+
math: { [label: string]: { tag: string; id: string } };
80+
fig: { [label: string]: { tag: string; id: string } };
81+
};
7982

8083
public _init(
8184
project_id: string,
@@ -2733,8 +2736,9 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
27332736
value = latexEnvs(value);
27342737

27352738
const labelRegExp = /\s*\\label\{.*?\}\s*/g;
2739+
const figLabelRegExp = /\s*\\figlabel\{.*?\}\s*/g;
27362740
if (this.labels == null) {
2737-
const labels = (this.labels = {});
2741+
const labels = (this.labels = { math: {}, fig: {} });
27382742
// do initial full document scan
27392743
if (this.store == null) {
27402744
return;
@@ -2743,15 +2747,23 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
27432747
if (cells == null) {
27442748
return;
27452749
}
2746-
let n = 0;
2750+
let mathN = 0;
2751+
let figN = 0;
27472752
for (const id of this.store.get_cell_ids_list()) {
27482753
const cell = cells.get(id);
27492754
if (cell?.get("cell_type") == "markdown") {
2750-
const value = cell.get("input") ?? "";
2755+
const value = latexEnvs(cell.get("input") ?? "");
27512756
value.replace(labelRegExp, (labelContent) => {
27522757
const label = extractLabel(labelContent);
2753-
n += 1;
2754-
labels[label] = { tag: `${n}`, id };
2758+
mathN += 1;
2759+
labels.math[label] = { tag: `${mathN}`, id };
2760+
return "";
2761+
});
2762+
value.replace(figLabelRegExp, (labelContent) => {
2763+
const label = extractLabel(labelContent);
2764+
figN += 1;
2765+
labels.fig[label] = { tag: `${figN}`, id };
2766+
return "";
27552767
});
27562768
}
27572769
}
@@ -2760,28 +2772,38 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
27602772
if (labels == null) {
27612773
throw Error("bug");
27622774
}
2763-
const noLabels = value.replace(labelRegExp, (labelContent) => {
2775+
value = value.replace(labelRegExp, (labelContent) => {
2776+
const label = extractLabel(labelContent);
2777+
if (labels.math[label] == null) {
2778+
labels.math[label] = { tag: `${misc.len(labels.math) + 1}`, id };
2779+
} else {
2780+
// in case it moved to a different cell due to cut/paste
2781+
labels.math[label].id = id;
2782+
}
2783+
return `\\tag{${labels.math[label].tag}}`;
2784+
});
2785+
value = value.replace(figLabelRegExp, (labelContent) => {
27642786
const label = extractLabel(labelContent);
2765-
if (labels[label] == null) {
2766-
labels[label] = { tag: `${misc.len(labels) + 1}`, id };
2787+
if (labels.fig[label] == null) {
2788+
labels.fig[label] = { tag: `${misc.len(labels.fig) + 1}`, id };
27672789
} else {
27682790
// in case it moved to a different cell due to cut/paste
2769-
labels[label].id = id;
2791+
labels.fig[label].id = id;
27702792
}
2771-
return `\\tag{${labels[label].tag}}`;
2793+
return ` ${labels.fig[label].tag ?? "?"}`;
27722794
});
27732795
const refRegExp = /\\ref\{.*?\}/g;
2774-
const noRefs = noLabels.replace(refRegExp, (refContent) => {
2796+
value = value.replace(refRegExp, (refContent) => {
27752797
const label = extractLabel(refContent);
2776-
if (labels[label] == null) {
2777-
// nothing to do but strip it
2778-
return "";
2798+
if (labels.fig[label] == null && labels.math[label] == null) {
2799+
// do not know the label
2800+
return "?";
27792801
}
2780-
const { tag, id } = labels[label];
2802+
const { tag, id } = labels.fig[label] ?? labels.math[label];
27812803
return `[${tag}](#id=${id})`;
27822804
});
27832805

2784-
return noRefs;
2806+
return value;
27852807
};
27862808
}
27872809

src/packages/util/latex-envs.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ transformFigures -- dumb parser to turn this:
1818
\begin{figure}
1919
\centering
2020
\centerline{\includegraphics[width=WIDTH]{URL}}
21-
\caption{CAPTION}
21+
\caption{\label{foo}CAPTION}
2222
\end{figure}
2323
2424
...
@@ -31,10 +31,8 @@ into this:
3131
3232
...
3333
34-
<div style="text-align:center"><img src="URL" style="width:WIDTH"/></div>
35-
\begin{equation}
36-
\text{CAPTION}
37-
\end{equation}
34+
<div style="text-align:center"><img src="URL" style="width:WIDTH"/><br/><br/>\figlabel{foo}CAPTION</div>
35+
3836
3937
...
4038
@@ -77,13 +75,18 @@ function transformFigures(content: string): string {
7775
style = `width:${content.slice(w + "width=".length, w2 - 1)}`;
7876
}
7977
const url = content.slice(w2 + 1, w3);
80-
const caption = content.slice(c + "\\caption{".length, c2);
81-
82-
const md = `\n\n<div style="text-align:center"><img src="${url}" style="${style}"/></div>\n\n
83-
\\begin{equation}
84-
\\text{${caption}}
85-
\\end{equation}\n\n`;
78+
let caption = content.slice(c + "\\caption{".length, c2);
79+
const x = caption.indexOf("\\label{");
80+
let figlabel;
81+
if (x != -1) {
82+
const y = caption.indexOf("}", x);
83+
figlabel = `\\figlabel{${caption.slice(x + "\\label{".length, y)}}`;
84+
caption = caption.slice(0, x) + caption.slice(y + 1);
85+
} else {
86+
figlabel = "";
87+
}
8688

89+
const md = `\n\n<div style="text-align:center;margin:20px auto;max-width:750px"><img src="${url}" style="${style}"/><br/><br/><b>Figure${figlabel}:</b> ${caption}</div>\n\n`;
8790
content =
8891
content.slice(0, i) + md + content.slice(j + "\\end{figure}".length);
8992
}

0 commit comments

Comments
 (0)