diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/pacien/pandoc/filter/plantuml/Filter.java | 84 |
1 files changed, 84 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 @@ | |||
1 | package org.pacien.pandoc.filter.plantuml; | ||
2 | |||
3 | import com.fasterxml.jackson.databind.JsonNode; | ||
4 | import com.fasterxml.jackson.databind.ObjectMapper; | ||
5 | import com.fasterxml.jackson.databind.node.ArrayNode; | ||
6 | import com.fasterxml.jackson.databind.node.ObjectNode; | ||
7 | import com.fasterxml.jackson.databind.node.TextNode; | ||
8 | import net.sourceforge.plantuml.FileFormat; | ||
9 | import net.sourceforge.plantuml.FileFormatOption; | ||
10 | import net.sourceforge.plantuml.SourceStringReader; | ||
11 | |||
12 | import java.io.*; | ||
13 | import java.util.Iterator; | ||
14 | import java.util.stream.Collectors; | ||
15 | |||
16 | final 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 | } | ||