Generating Templates from a Simple Input Model

There are many cases where we have an internal data structure, such as a tree or network graph, that we’d like to visualize for documentation or debugging purposes. This is a perfect opportunity to flex our code generation muscles. We can generate graphical descriptions using the DOT DSL of Graphviz, an open source graph visualization tool.

As an example application, let’s visualize the link interconnections between web pages. It’ll teach us the basics of creating output templates from an input model. From a list of Link objects, we’ll generate a series of from->to edge definitions in a DOT file surrounded by some bookkeeping definitions.

digraph testgraph {
node [shape=plaintext, fontsize=12, ...

Get Language Implementation Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.