Skip to content

Instantly share code, notes, and snippets.

Created December 2, 2021 22:56
What would you like to do?
Python script to convert CSAcademy Graph Editor Markup to TikZ
import json
before = """\\begin{center}
\\begin{tikzpicture}[every node/.style={draw,circle,minimum size=25pt}]
after = """;
markup = input("Enter markup ( ")
# Parse markup
nodes = markup.split("nodes=")[1].split("edges=")[0].strip()
edges = markup.split("edges=")[1].split("/>")[0].strip()
# JSONify
nodes = '{"nodes":' + nodes[1:].replace('label:', '"label":').replace('center:', '"center":').replace('x:', '"x":').replace('y:', '"y":')
edges = '{"edges":' + edges[1:].replace('source:', '"source":').replace('target:', '"target":')
nodes = json.loads(nodes)
edges = json.loads(edges)
# Convert to TikZ
tikz = before
for i,node in enumerate(nodes["nodes"]):
tikz += f"""
for edge in edges["edges"]:
tikz += """
({source}) -- ({target})""".format(**edge)
tikz += after
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment