Skip to content

Commit e3c1bc7

Browse files
committed
jupyter: add support for latex figures
- I'm definitely not going to support anything else besides this; even this is probably a really bad idea I'll regret. - I tried for a while using regexp, but it never worked well, so instead this brittle parsing...
1 parent 473a9e0 commit e3c1bc7

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

src/packages/jupyter/redux/actions.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2777,10 +2777,95 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
27772777
const { tag, id } = labels[label];
27782778
return `[${tag}](#id=${id})`;
27792779
});
2780-
return noRefs;
2780+
2781+
const figures = transformFigures(noRefs);
2782+
2783+
return figures;
27812784
};
27822785
}
27832786

2787+
/*
2788+
Turn this:
2789+
2790+
---
2791+
2792+
...
2793+
2794+
\begin{figure}
2795+
\centering
2796+
\centerline{\includegraphics[width=WIDTH]{URL}}
2797+
\caption{CAPTION}
2798+
\end{figure}
2799+
2800+
...
2801+
2802+
---
2803+
2804+
into this:
2805+
2806+
---
2807+
2808+
...
2809+
2810+
<div style="text-align:center"><img src="URL" style="width:WIDTH"/></div>
2811+
\begin{equation}
2812+
\text{CAPTION}
2813+
\end{equation}
2814+
2815+
...
2816+
2817+
---
2818+
2819+
There can be lots of figures.
2820+
2821+
*/
2822+
2823+
function transformFigures(content: string): string {
2824+
while (true) {
2825+
const i = content.indexOf("\\begin{figure}");
2826+
if (i == -1) {
2827+
return content;
2828+
}
2829+
const j = content.indexOf("\\end{figure}");
2830+
if (j == -1) {
2831+
return content;
2832+
}
2833+
const k = content.indexOf("\\includegraphics");
2834+
if (k == -1) {
2835+
return content;
2836+
}
2837+
const c = content.indexOf("\\caption{");
2838+
if (c == -1) {
2839+
return content;
2840+
}
2841+
const c2 = content.lastIndexOf("}", j);
2842+
if (c2 == -1) {
2843+
return content;
2844+
}
2845+
2846+
const w = content.indexOf("width=");
2847+
const w2 = content.indexOf("{", k);
2848+
const w3 = content.indexOf("}", k);
2849+
if (w2 == -1 || w3 == -1) {
2850+
return content;
2851+
}
2852+
let style = "";
2853+
if (w != -1) {
2854+
style = `width:${content.slice(w + "width=".length, w2 - 1)}`;
2855+
}
2856+
const url = content.slice(w2 + 1, w3);
2857+
const caption = content.slice(c + "\\caption{".length, c2);
2858+
2859+
const md = `\n\n<div style="text-align:center"><img src="${url}" style="${style}"/></div>\n\n
2860+
\\begin{equation}
2861+
\\text{${caption}}
2862+
\\end{equation}\n\n`;
2863+
2864+
content =
2865+
content.slice(0, i) + md + content.slice(j + "\\end{figure}".length);
2866+
}
2867+
}
2868+
27842869
function extractLabel(content: string): string {
27852870
const i = content.indexOf("{");
27862871
const j = content.lastIndexOf("}");

0 commit comments

Comments
 (0)