From 3fef3f84e7d13b2341f15557f3bbe415cb862e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20J=C3=A4derberg?= Date: Wed, 7 Jun 2017 13:13:00 +0200 Subject: [PATCH] Override dot block generation * Produce complete dot source. * Inject styling when writing the dot source. * Use block defition and attributes understood by asciidoctor-diagram. --- .../org/neo4j/doc/tools/GraphVizConfig.java | 101 ++++++++++++++++++ .../neo4j/doc/tools/RESTDocsGenerator.java | 2 +- .../doc/tools/SpatialGraphVizHelper.java | 82 ++++++++++++++ 3 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/neo4j/doc/tools/GraphVizConfig.java create mode 100644 src/test/java/org/neo4j/doc/tools/SpatialGraphVizHelper.java diff --git a/src/test/java/org/neo4j/doc/tools/GraphVizConfig.java b/src/test/java/org/neo4j/doc/tools/GraphVizConfig.java new file mode 100644 index 000000000..971519b57 --- /dev/null +++ b/src/test/java/org/neo4j/doc/tools/GraphVizConfig.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2002-2017 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.doc.tools; + +public class GraphVizConfig { + + String nodeFontSize = "10"; + String edgeFontSize = nodeFontSize; + String nodeFontColor = "#1c2021"; // darker grey + String edgeFontColor = nodeFontColor; + String edgeColor = "#2e3436"; // dark grey + String boxColor = edgeColor; + String edgeHighlight = "#a40000"; // dark red + String nodeFillColor = "#ffffff"; + String nodeHighlight = "#fcee7d"; // lighter yellow + String nodeHighlight2 = "#fcc574"; // lighter orange + String nodeShape = "box"; + + // Commandline args in the shell script, in order + String fontPath; + // We are not calling the dot command from here at the moment, so we don't need the filename + // String targetImage; + String colorSet; + String graphAttrs; + + String graphSettings = "graph [size=\"7.0,9.0\" fontpath=\"" + fontPath + "\"]"; + String nodeStyle = "filled,rounded"; + String nodeSep = "0.4"; + + String textNode = "shape=plaintext,style=diagonals,height=0.2,margin=0.0,0.0"; + + String arrowHead = "vee"; + String arrowSize = "0.75"; + + String inData; + + String graphFont = "FreeSans"; + String nodeFont = graphFont; + String edgeFont = graphFont; + + public GraphVizConfig(String inData, String fontPath, String colorSet, String graphAttrs) { + this.fontPath = fontPath; + this.colorSet = colorSet; + this.graphAttrs = graphAttrs; + + handleColorSet(colorSet); + + this.inData = handleInData(inData); + } + + public String get() { + String prepend = String.format("digraph g{ %s ", graphSettings) + + String.format("node [shape=\"%s\" penwidth=1.5 fillcolor=\"%s\" color=\"%s\" ", nodeShape, nodeFillColor, boxColor) + + String.format("fontcolor=\"%s\" style=\"%s\" fontsize=%s fontname=\"%s\"] ", nodeFontColor, nodeStyle, nodeFontSize, nodeFont) + + String.format("edge [color=\"%s\" penwidth=2 arrowhead=\"%s\" arrowtail=\"%s\" ", boxColor, arrowHead, arrowHead) + + String.format("arrowSize=%s fontcolor=\"%s\" fontsize=%s fontname=\"%s\"] ", arrowSize, edgeFontColor, edgeFontSize, edgeFont) + + String.format("nodesep=%s fontname=\"%s\"", nodeSep, graphFont) + + graphAttrs; + + return prepend + this.inData + "}"; + } + + private final void handleColorSet(String colorSet) { + if (colorSet.equals("meta")) { + this.nodeFillColor = "#fadcad"; + this.nodeHighlight = "#a8e270"; + this.nodeHighlight2 = "#95bbe3"; + } else if (colorSet.equals("neoviz")) { + this.nodeShape = "Mrecord"; + this.nodeFontSize = "8"; + this.edgeFontSize = this.nodeFontSize; + } + } + + private final String handleInData(String in) { + return in.replace("NODEHIGHLIGHT", nodeHighlight) + .replace("NODE2HIGHLIGHT", nodeHighlight2) + .replace("EDGEHIGHLIGHT", edgeHighlight) + .replace("BOXCOLOR", boxColor) + .replace("TEXTNODE", textNode); + } + + +} diff --git a/src/test/java/org/neo4j/doc/tools/RESTDocsGenerator.java b/src/test/java/org/neo4j/doc/tools/RESTDocsGenerator.java index c395154ad..d37d0f4ae 100644 --- a/src/test/java/org/neo4j/doc/tools/RESTDocsGenerator.java +++ b/src/test/java/org/neo4j/doc/tools/RESTDocsGenerator.java @@ -522,7 +522,7 @@ protected void document( final DocumentationData data ) { fw.append( org.neo4j.doc.tools.AsciiDocGenerator.dumpToSeparateFile( dir, name + ".graph", - AsciidocHelper.createGraphVizWithNodeId( "Final Graph", + SpatialGraphVizHelper.createGraphVizWithNodeId( "Final Graph", graph, title ) ) ); line(fw, "" ); } diff --git a/src/test/java/org/neo4j/doc/tools/SpatialGraphVizHelper.java b/src/test/java/org/neo4j/doc/tools/SpatialGraphVizHelper.java new file mode 100644 index 000000000..b35e60876 --- /dev/null +++ b/src/test/java/org/neo4j/doc/tools/SpatialGraphVizHelper.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2002-2017 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.doc.tools; + +import org.neo4j.graphdb.GraphDatabaseService; +import org.neo4j.graphdb.Transaction; +import org.neo4j.visualization.graphviz.AsciiDocStyle; +import org.neo4j.visualization.graphviz.GraphStyle; +import org.neo4j.visualization.graphviz.GraphvizWriter; +import org.neo4j.walk.Walker; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +public class SpatialGraphVizHelper extends org.neo4j.visualization.asciidoc.AsciidocHelper { + + private static final String ILLEGAL_STRINGS = "[:\\(\\)\t;&/\\\\]"; + + public static String createGraphVizWithNodeId( + String title, GraphDatabaseService graph, String identifier + ) { + return createGraphViz( + title, graph, identifier, AsciiDocStyle.withAutomaticRelationshipTypeColors(), "" + ); + } + + public static String createGraphViz(String title, GraphDatabaseService graph, String identifier, GraphStyle graphStyle, String graphvizOptions) { + try (Transaction tx = graph.beginTx()) { + GraphvizWriter writer = new GraphvizWriter( graphStyle ); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try { + writer.emit( out, Walker.fullGraph( graph ) ); + } catch (IOException e) { + e.printStackTrace(); + } + + String safeTitle = title.replaceAll( ILLEGAL_STRINGS, "" ); + + tx.success(); + + String fontsDir = "target/tools/bin/fonts"; + String colorSet = "neoviz"; + String graphAttrs = ""; + + try { + String result = "." + title + "\n[graphviz, " + + (safeTitle + "-" + identifier).replace( " ", "-" ) + + ", svg]\n" + + "----\n" + + new GraphVizConfig( + out.toString( StandardCharsets.UTF_8.name()), + fontsDir, + colorSet, graphAttrs + ).get() + "\n" + + "----\n"; + System.out.println(result); + return result; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + } +}