aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/pacien/pandoc/filter/plantuml/Filter.java84
-rw-r--r--src/test/java/org/pacien/pandoc/filter/plantuml/FilterTest.java27
-rw-r--r--src/test/resources/expected.json1
-rw-r--r--src/test/resources/input.json1
4 files changed, 113 insertions, 0 deletions
diff --git a/src/main/java/org/pacien/pandoc/filter/plantuml/Filter.java b/src/main/java/org/pacien/pandoc/filter/plantuml/Filter.java
new file mode 100644
index 0000000..66abc2d
--- /dev/null
+++ b/src/main/java/org/pacien/pandoc/filter/plantuml/Filter.java
@@ -0,0 +1,84 @@
1package org.pacien.pandoc.filter.plantuml;
2
3import com.fasterxml.jackson.databind.JsonNode;
4import com.fasterxml.jackson.databind.ObjectMapper;
5import com.fasterxml.jackson.databind.node.ArrayNode;
6import com.fasterxml.jackson.databind.node.ObjectNode;
7import com.fasterxml.jackson.databind.node.TextNode;
8import net.sourceforge.plantuml.FileFormat;
9import net.sourceforge.plantuml.FileFormatOption;
10import net.sourceforge.plantuml.SourceStringReader;
11
12import java.io.*;
13import java.util.Iterator;
14import java.util.stream.Collectors;
15
16final public class Filter {
17
18 private static final String BEGIN_TAG = "\\begin{tikzpicture}[yscale=-1]";
19 private static final String LINE_SEP = "\n";
20 private static final String TYPE_KEY = "t";
21 private static final String CONTENT_KEY = "c";
22 private static final String CODE_BLOCK_TYPE = "CodeBlock";
23 private static final String RAW_BLOCK_TYPE = "RawBlock";
24 private static final String PLANTUML_TYPE = "puml";
25 private static final String LATEX_TYPE = "latex";
26 private static final int META_INDEX = 0;
27 private static final int META_PROP_INDEX = 1;
28 private static final int META_PROP_TYPE_INDEX = 0;
29 private static final int CONTENT_INDEX = 1;
30
31 private static String plantumlToLatex(String puml) throws IOException {
32 try (ByteArrayOutputStream s = new ByteArrayOutputStream()) {
33 new SourceStringReader(puml).generateImage(s, new FileFormatOption(FileFormat.LATEX_NO_PREAMBLE));
34 try (BufferedReader r = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(s.toByteArray())))) {
35 return BEGIN_TAG + LINE_SEP + r.lines().filter(l -> !l.equals(BEGIN_TAG)).collect(Collectors.joining(LINE_SEP));
36 }
37 }
38 }
39
40 private static void renderPlantumlNode(ObjectNode n) throws IOException {
41 String puml = n.get(CONTENT_KEY).get(CONTENT_INDEX).asText();
42 String tikz = plantumlToLatex(puml);
43
44 n.set(TYPE_KEY, TextNode.valueOf(RAW_BLOCK_TYPE));
45 ((ArrayNode) n.get(CONTENT_KEY)).removeAll()
46 .add(TextNode.valueOf(LATEX_TYPE))
47 .add(TextNode.valueOf(tikz));
48 }
49
50 private static boolean isPlantumlNode(JsonNode n) {
51 return n.path(TYPE_KEY).asText().equals(CODE_BLOCK_TYPE) &&
52 n.path(CONTENT_KEY).path(META_INDEX).path(META_PROP_INDEX).path(META_PROP_TYPE_INDEX).asText().equals(PLANTUML_TYPE);
53 }
54
55 private static void walk(JsonNode n) throws IOException {
56 if (isPlantumlNode(n))
57 renderPlantumlNode((ObjectNode) n);
58 else if (n.isContainerNode())
59 for (Iterator<JsonNode> i = n.elements(); i.hasNext(); ) walk(i.next());
60 }
61
62 public static void filter(InputStream i, OutputStream o) throws IOException {
63 ObjectMapper m = new ObjectMapper();
64 JsonNode t = m.readTree(i);
65 if (t != null) {
66 walk(t);
67 m.writeValue(o, t);
68 }
69 }
70
71 public static void main(String args[]) {
72 try {
73 filter(System.in, System.out);
74 } catch (IOException e) {
75 e.printStackTrace();
76 System.exit(1);
77 }
78 }
79
80 private Filter() {
81 // static class
82 }
83
84}
diff --git a/src/test/java/org/pacien/pandoc/filter/plantuml/FilterTest.java b/src/test/java/org/pacien/pandoc/filter/plantuml/FilterTest.java
new file mode 100644
index 0000000..f720b22
--- /dev/null
+++ b/src/test/java/org/pacien/pandoc/filter/plantuml/FilterTest.java
@@ -0,0 +1,27 @@
1package org.pacien.pandoc.filter.plantuml;
2
3import org.junit.Assert;
4import org.junit.Test;
5
6import java.io.*;
7import java.net.URISyntaxException;
8import java.nio.file.Files;
9import java.nio.file.Paths;
10
11final public class FilterTest {
12
13 private static final String INPUT_FILE = "/input.json";
14 private static final String EXPECTED_FILE = "/expected.json";
15
16 @Test
17 public void filterTest() {
18 try (ByteArrayOutputStream o = new ByteArrayOutputStream()) {
19 byte[] e = Files.readAllBytes(Paths.get(getClass().getResource(EXPECTED_FILE).toURI()));
20 Filter.filter(getClass().getResourceAsStream(INPUT_FILE), o);
21 Assert.assertArrayEquals(o.toByteArray(), e);
22 } catch (IOException | URISyntaxException e) {
23 Assert.fail();
24 }
25 }
26
27}
diff --git a/src/test/resources/expected.json b/src/test/resources/expected.json
new file mode 100644
index 0000000..7131399
--- /dev/null
+++ b/src/test/resources/expected.json
@@ -0,0 +1 @@
[{"unMeta":{}},[{"t":"Header","c":[1,["test-document",[],[]],[{"t":"Str","c":"Test"},{"t":"Space","c":[]},{"t":"Str","c":"document"}]]},{"t":"Para","c":[{"t":"Str","c":"This"},{"t":"Space","c":[]},{"t":"Str","c":"paragraph"},{"t":"Space","c":[]},{"t":"Str","c":"should"},{"t":"Space","c":[]},{"t":"Str","c":"be"},{"t":"Space","c":[]},{"t":"Str","c":"left"},{"t":"Space","c":[]},{"t":"Str","c":"as"},{"t":"Space","c":[]},{"t":"Str","c":"is"},{"t":"Space","c":[]},{"t":"Str","c":"by"},{"t":"Space","c":[]},{"t":"Str","c":"the"},{"t":"Space","c":[]},{"t":"Str","c":"filter."}]},{"t":"CodeBlock","c":[["",[],[]],"This generic code block too.\nOnly PlantUML code blocks should be rendered."]},{"t":"RawBlock","c":["latex","\\begin{tikzpicture}[yscale=-1]\n% generated by Plantuml 8059 \n\\definecolor{plantucolor0000}{RGB}{254,254,206}\n\\definecolor{plantucolor0001}{RGB}{168,0,54}\n\\definecolor{plantucolor0002}{RGB}{173,209,178}\n\\definecolor{plantucolor0003}{RGB}{0,0,0}\n\\definecolor{plantucolor0004}{RGB}{0,0,255}\n\\draw[color=plantucolor0001,fill=plantucolor0000,line width=1.5pt] (87pt,8pt) rectangle (186pt,68.8047pt);\n\\draw[color=plantucolor0001,fill=plantucolor0002,line width=1.0pt] (102pt,24pt) ellipse (11pt and 11pt);\n\\draw[color=black,fill=black] (104.9688pt,29.6406pt) ..controls (104.3906pt,29.9375pt) .. (103.75pt,30.0859pt) ..controls (103.1094pt,30.2344pt) .. (102.4063pt,30.2344pt) ..controls (99.9063pt,30.2344pt) .. (98.5859pt,28.5859pt) ..controls (97.2656pt,26.9375pt) .. (97.2656pt,23.8125pt) ..controls (97.2656pt,20.6875pt) .. (98.5859pt,19.0313pt) ..controls (99.9063pt,17.375pt) .. (102.4063pt,17.375pt) ..controls (103.1094pt,17.375pt) .. (103.7578pt,17.5313pt) ..controls (104.4063pt,17.6875pt) .. (104.9688pt,17.9844pt) -- (104.9688pt,20.7031pt) ..controls (104.3438pt,20.125pt) .. (103.75pt,19.8516pt) ..controls (103.1563pt,19.5781pt) .. (102.5313pt,19.5781pt) ..controls (101.1875pt,19.5781pt) .. (100.5pt,20.6484pt) ..controls (99.8125pt,21.7188pt) .. (99.8125pt,23.8125pt) ..controls (99.8125pt,25.9063pt) .. (100.5pt,26.9766pt) ..controls (101.1875pt,28.0469pt) .. (102.5313pt,28.0469pt) ..controls (103.1563pt,28.0469pt) .. (103.75pt,27.7734pt) ..controls (104.3438pt,27.5pt) .. (104.9688pt,26.9219pt) -- (104.9688pt,29.6406pt);\n\\node at (116pt,17.0156pt)[below right]{Subscriber};\n\\draw[color=plantucolor0001,line width=1.5pt] (88pt,40pt) -- (185pt,40pt);\n\\node at (93pt,44pt)[below right]{subscriberId};\n\\draw[color=plantucolor0001,line width=1.5pt] (88pt,60.8047pt) -- (185pt,60.8047pt);\n\\draw[color=plantucolor0001,fill=plantucolor0000,line width=1.5pt] (6pt,148pt) rectangle (119pt,208.8047pt);\n\\draw[color=plantucolor0001,fill=plantucolor0002,line width=1.0pt] (21pt,164pt) ellipse (11pt and 11pt);\n\\draw[color=black,fill=black] (23.9688pt,169.6406pt) ..controls (23.3906pt,169.9375pt) .. (22.75pt,170.0859pt) ..controls (22.1094pt,170.2344pt) .. (21.4063pt,170.2344pt) ..controls (18.9063pt,170.2344pt) .. (17.5859pt,168.5859pt) ..controls (16.2656pt,166.9375pt) .. (16.2656pt,163.8125pt) ..controls (16.2656pt,160.6875pt) .. (17.5859pt,159.0313pt) ..controls (18.9063pt,157.375pt) .. (21.4063pt,157.375pt) ..controls (22.1094pt,157.375pt) .. (22.7578pt,157.5313pt) ..controls (23.4063pt,157.6875pt) .. (23.9688pt,157.9844pt) -- (23.9688pt,160.7031pt) ..controls (23.3438pt,160.125pt) .. (22.75pt,159.8516pt) ..controls (22.1563pt,159.5781pt) .. (21.5313pt,159.5781pt) ..controls (20.1875pt,159.5781pt) .. (19.5pt,160.6484pt) ..controls (18.8125pt,161.7188pt) .. (18.8125pt,163.8125pt) ..controls (18.8125pt,165.9063pt) .. (19.5pt,166.9766pt) ..controls (20.1875pt,168.0469pt) .. (21.5313pt,168.0469pt) ..controls (22.1563pt,168.0469pt) .. (22.75pt,167.7734pt) ..controls (23.3438pt,167.5pt) .. (23.9688pt,166.9219pt) -- (23.9688pt,169.6406pt);\n\\node at (35pt,157.0156pt)[below right]{AccumUsage};\n\\draw[color=plantucolor0001,line width=1.5pt] (7pt,180pt) -- (118pt,180pt);\n\\node at (12pt,184pt)[below right]{subscriberId};\n\\draw[color=plantucolor0001,line width=1.5pt] (7pt,200.8047pt) -- (118pt,200.8047pt);\n\\draw[color=plantucolor0001,fill=plantucolor0000,line width=1.5pt] (154pt,129pt) rectangle (267pt,228.2188pt);\n\\draw[color=plantucolor0001,fill=plantucolor0002,line width=1.0pt] (178.45pt,145pt) ellipse (11pt and 11pt);\n\\draw[color=black,fill=black] (181.4188pt,150.6406pt) ..controls (180.8406pt,150.9375pt) .. (180.2pt,151.0859pt) ..controls (179.5594pt,151.2344pt) .. (178.8563pt,151.2344pt) ..controls (176.3563pt,151.2344pt) .. (175.0359pt,149.5859pt) ..controls (173.7156pt,147.9375pt) .. (173.7156pt,144.8125pt) ..controls (173.7156pt,141.6875pt) .. (175.0359pt,140.0313pt) ..controls (176.3563pt,138.375pt) .. (178.8563pt,138.375pt) ..controls (179.5594pt,138.375pt) .. (180.2078pt,138.5313pt) ..controls (180.8563pt,138.6875pt) .. (181.4188pt,138.9844pt) -- (181.4188pt,141.7031pt) ..controls (180.7938pt,141.125pt) .. (180.2pt,140.8516pt) ..controls (179.6063pt,140.5781pt) .. (178.9813pt,140.5781pt) ..controls (177.6375pt,140.5781pt) .. (176.95pt,141.6484pt) ..controls (176.2625pt,142.7188pt) .. (176.2625pt,144.8125pt) ..controls (176.2625pt,146.9063pt) .. (176.95pt,147.9766pt) ..controls (177.6375pt,149.0469pt) .. (178.9813pt,149.0469pt) ..controls (179.6063pt,149.0469pt) .. (180.2pt,148.7734pt) ..controls (180.7938pt,148.5pt) .. (181.4188pt,147.9219pt) -- (181.4188pt,150.6406pt);\n\\node at (194.55pt,138.0156pt)[below right]{IpSession};\n\\draw[color=plantucolor0001,line width=1.5pt] (155pt,161pt) -- (266pt,161pt);\n\\node at (160pt,165pt)[below right]{ipAddress};\n\\node at (160pt,177.8047pt)[below right]{specificData};\n\\node at (160pt,190.6094pt)[below right]{sapcOriginStateId};\n\\node at (160pt,203.4141pt)[below right]{apnId};\n\\draw[color=plantucolor0001,line width=1.5pt] (155pt,220.2188pt) -- (266pt,220.2188pt);\n\\draw[color=plantucolor0004,line width=1.0pt] (152.375pt,69.104pt) ..controls (160.988pt,85.167pt) and (171.898pt,105.512pt) .. (181.933pt,124.227pt);\n\\draw[color=plantucolor0004,fill=plantucolor0004,line width=1.0pt] (184.464pt,128.9464pt) -- (183.7353pt,119.1245pt) -- (182.1008pt,124.5401pt) -- (176.6853pt,122.9056pt) -- (184.464pt,128.9464pt) -- cycle;\n\\node at (146.8242pt,76.9968pt)[below right]{1};\n\\node at (156.1855pt,105.9167pt)[below right]{1..*};\n\\draw[color=plantucolor0001,line width=1.0pt] (120.625pt,69.104pt) ..controls (108.92pt,90.933pt) and (92.9726pt,120.673pt) .. (80.7427pt,143.4799pt);\n\\draw[color=plantucolor0001,fill=plantucolor0001,line width=1.0pt] (78.3486pt,147.9445pt) -- (86.1271pt,141.9034pt) -- (80.7116pt,143.5381pt) -- (79.0769pt,138.1226pt) -- (78.3486pt,147.9445pt) -- cycle;\n\\node at (108.5258pt,76.9968pt)[below right]{1};\n\\node at (55.1905pt,124.6557pt)[below right]{0..1};\n\\end{tikzpicture}"]}]] \ No newline at end of file
diff --git a/src/test/resources/input.json b/src/test/resources/input.json
new file mode 100644
index 0000000..0b07e87
--- /dev/null
+++ b/src/test/resources/input.json
@@ -0,0 +1 @@
[{"unMeta":{}},[{"t":"Header","c":[1,["test-document",[],[]],[{"t":"Str","c":"Test"},{"t":"Space","c":[]},{"t":"Str","c":"document"}]]},{"t":"Para","c":[{"t":"Str","c":"This"},{"t":"Space","c":[]},{"t":"Str","c":"paragraph"},{"t":"Space","c":[]},{"t":"Str","c":"should"},{"t":"Space","c":[]},{"t":"Str","c":"be"},{"t":"Space","c":[]},{"t":"Str","c":"left"},{"t":"Space","c":[]},{"t":"Str","c":"as"},{"t":"Space","c":[]},{"t":"Str","c":"is"},{"t":"Space","c":[]},{"t":"Str","c":"by"},{"t":"Space","c":[]},{"t":"Str","c":"the"},{"t":"Space","c":[]},{"t":"Str","c":"filter."}]},{"t":"CodeBlock","c":[["",[],[]],"This generic code block too.\nOnly PlantUML code blocks should be rendered."]},{"t":"CodeBlock","c":[["",["puml"],[]],"@startuml\nclass Subscriber {\n subscriberId\n}\n\nclass AccumUsage {\n subscriberId\n}\n\nclass IpSession {\n ipAddress\n specificData \n sapcOriginStateId \n apnId\n}\n\nSubscriber \"1\" -[#blue]-> \"1..*\" IpSession\nSubscriber \"1\" --> \"0..1\" AccumUsage\n@enduml"]}]] \ No newline at end of file