-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUML.jshell
More file actions
66 lines (54 loc) · 2.21 KB
/
Copy pathUML.jshell
File metadata and controls
66 lines (54 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//%jars /usr/local/bin/plantuml.jar
import io.github.spencerpark.ijava.IJava;
import net.sourceforge.plantuml.*;
import net.sourceforge.plantuml.core.DiagramDescription;
import java.nio.charset.Charset;
import javax.imageio.ImageIO;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import io.github.spencerpark.ijava.IJava;
import io.github.spencerpark.ijava.JavaKernel;
import io.github.spencerpark.jupyter.kernel.magic.registry.UndefinedMagicException;
/**
Render plantUML from cell
*/
IJava.getKernelInstance().getMagics().registerCellMagic("plantUML", (args, body) -> {
//sets the results mimetype
if (args.size()>1) throw new Exception("Max one argument : SVG or PNG");
String fileFormat;
if (args.size()==0) fileFormat="SVG";
else fileFormat=args.get(0);
SourceStringReader reader = new SourceStringReader(body);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
DiagramDescription desc = reader.outputImage(os, new FileFormatOption(FileFormat.valueOf(fileFormat)));
os.close();
Object out;
if (fileFormat.equals("SVG"))
out = new String(os.toByteArray(), Charset.forName("UTF-8"));
else
out= ImageIO.read(new ByteArrayInputStream(os.toByteArray()));
display(out,fileFormat.equals("SVG")?"image/svg+xml":"image/png");
return out;
});
/**
Render plantUML from file
*/
IJava.getKernelInstance().getMagics().registerCellMagic("plantUMLFile", (args, body) -> {
//sets the results mimetype
if (args.size()>1) throw new Exception("Max one argument : SVG or PNG");
String fileFormat;
if (args.size()==0) fileFormat="SVG";
else fileFormat=args.get(0);
List<String> l = new ArrayList<>();
List<Object> outList = new ArrayList<>();
body.lines().forEach(filename-> {
Object out;
try {
out=cellMagic("plantUML",args,Files.readString(Paths.get(filename)));
//display(out,fileFormat.equals("SVG")?"image/svg+xml":"image/png");
outList.add(out);
} catch (java.io.IOException e) {}
});
return outList;
});