|
45 | 45 | # To show TikZ in a GAP jupyter notebook
|
46 | 46 | BindGlobal("JupyterSplashTikZ",
|
47 | 47 | function(tikz)
|
48 |
| - local tmpdir, fn, header, ltx, svgfile, stream, svgdata, tojupyter, b64file, hasbp, img; |
| 48 | + local tmpdir, fn, header, ltx, svgfile, stream, svgdata, tojupyter, hasbp, img, b64file; |
49 | 49 |
|
50 | 50 | hasbp:=PositionSublist(tikz,"begin{tikzpicture}")<>fail;
|
51 | 51 |
|
@@ -77,33 +77,49 @@ function(tikz)
|
77 | 77 |
|
78 | 78 | if not( IsExistingFile( Concatenation(fn, ".pdf") ) ) then
|
79 | 79 | tojupyter := rec( json := true, name := "stdout",
|
80 |
| - data := "No pdf was created; pdflatex is installed in your system?" ); |
81 |
| - else |
82 |
| - svgfile := Concatenation( fn, ".svg" ); |
83 |
| - b64file := Concatenation( fn, ".b64" ); |
| 80 | + data := "No pdf was created; pdflatex is installed in your system?",metadata:=rec() ); |
| 81 | + return JupyterRenderable(tojupyter.data, tojupyter.metadata); |
| 82 | + fi; |
| 83 | + |
| 84 | + svgfile := Concatenation( fn, ".svg" ); |
| 85 | + b64file := Concatenation( fn, ".b64" ); |
| 86 | + if ARCH_IS_MAC_OS_X() then |
| 87 | + ltx := Concatenation( "pdf2svg ", Concatenation( fn, ".pdf" ), " ", |
| 88 | + svgfile, "; base64 -i ", svgfile," >> ", b64file ); |
| 89 | + |
| 90 | + else |
84 | 91 | ltx := Concatenation( "pdf2svg ", Concatenation( fn, ".pdf" ), " ",
|
85 |
| - svgfile, "; base64 -i ", svgfile," >> ", b64file ); |
86 |
| - Exec( ltx ); |
87 |
| - if not( IsExistingFile( svgfile ) ) then |
| 92 | + svgfile); |
| 93 | + fi; |
| 94 | + Exec( ltx ); |
| 95 | + if not( IsExistingFile( svgfile ) ) then |
| 96 | + tojupyter := rec( json := true, name := "stdout", |
| 97 | + data := "No svg was created; pdf2svg is installed in your system?", metadata := rec()); |
| 98 | + return JupyterRenderable(tojupyter.data, tojupyter.metadata); |
| 99 | + fi; |
| 100 | + if ARCH_IS_MAC_OS_X() then |
| 101 | + stream := InputTextFile( b64file ); |
| 102 | + if stream <> fail then |
| 103 | + svgdata := ReadLine( stream ); |
| 104 | + CloseStream( stream ); |
| 105 | + else |
88 | 106 | tojupyter := rec( json := true, name := "stdout",
|
89 |
| - data := "No svg was created; pdf2svg is installed in your system?", metadata := rec()); |
| 107 | + data := Concatenation( "Unable to render ", tikz ), metadata := rec() ); |
90 | 108 | return JupyterRenderable(tojupyter.data, tojupyter.metadata);
|
91 |
| - elif not( IsExistingFile( b64file ) ) then |
| 109 | + fi; |
| 110 | + else |
| 111 | + stream := InputTextFile( svgfile ); |
| 112 | + if stream <> fail then |
| 113 | + svgdata := ReadAll( stream ); |
| 114 | + svgdata:= SubstitutionSublist(Base64String(svgdata),"\n",""); |
| 115 | + CloseStream( stream ); |
| 116 | + else |
92 | 117 | tojupyter := rec( json := true, name := "stdout",
|
93 |
| - data := "No svg was created; base64 is installed in your system?", metadata := rec()); |
| 118 | + data := Concatenation( "Unable to render ", tikz ), metadata := rec() ); |
94 | 119 | return JupyterRenderable(tojupyter.data, tojupyter.metadata);
|
95 |
| - else |
96 |
| - stream := InputTextFile( b64file ); |
97 |
| - if stream <> fail then |
98 |
| - svgdata := ReadLine( stream ); |
99 |
| - CloseStream( stream ); |
100 |
| - else |
101 |
| - tojupyter := rec( json := true, name := "stdout", |
102 |
| - data := Concatenation( "Unable to render ", tikz ), metadata := rec() ); |
103 |
| - return JupyterRenderable(tojupyter.data, tojupyter.metadata); |
104 |
| - fi; |
105 | 120 | fi;
|
106 | 121 | fi;
|
| 122 | + |
107 | 123 | img:=Concatenation("<img src='data:image/svg+xml;base64,", svgdata,"'>");
|
108 | 124 | return Objectify( JupyterRenderableType, rec( data := rec( ("text/html") := img), metadata:=rec() ));
|
109 | 125 | end);
|
|
0 commit comments