#lang racket
(require srfi/1 srfi/13 srfi/14)
(define *wizard-nodes* '((living-room (you are in the living-room.
a wizard is snoring loudly on the couch.))
(garden (you are in a beautiful garden.
there is a well in front of you.))
(attic (you are in the attic.
there is a giant welding torch in the corner.))))
(define *wizard-edges* '((living-room (garden west door)
(attic upstairs ladder))
(garden (living-room east door))
(attic (living-room downstairs ladder))))
(regexp-replace #px"[^[:alnum:]]+" (format "~a" exp) "_"))
(define *max-label-length* 30)
""
(let ((s (string-delete (char-set #\tab #\newline) (format "~a" exp))))
(if (> (string-length s) *max-label-length*)
(string-append (substring s 0 (- *max-label-length* 3)) "...")
s))))
(define (nodes->dot nodes)
(for-each (lambda (node)
(newline)
(display (dot-name (car node)))
(display "[label=\"")
(display (dot-label node))
(display "\"];"))
nodes))
(define (edges->dot edges)
(for-each (lambda(node)
(for-each (lambda (edge)
(newline)
(display (dot-name (car node)))
(display "->")
(display (dot-name (car edge)))
(display "[label=\"")
(display (dot-label (cdr edge)))
(display "\"];"))
(cdr node)))
edges))
(define (graph->dot nodes edges)
(display "digraph{")
(nodes->dot nodes)
(edges->dot edges)
(display "}"))
(define (uedges->dot edges)
(pair-for-each (lambda (lst)
(for-each (lambda (edge)
(unless (assq (car edge) (cdr lst))
(newline)
(display (dot-name (caar lst)))
(display "--")
(display (dot-name (car edge)))
(display "[label=\"")
(display (dot-label (cdr edge)))
(display "\"];")))
(cdar lst)))
edges))
(define (ugraph->dot nodes edges)
(display "graph{")
(nodes->dot nodes)
(uedges->dot edges)
(display "}"))
(define (dot->png fname thunk)
(with-output-to-file fname
#:exists 'replace
thunk)
(system (string
-append
"dot -Tpng -O " fname
)))
(define (graph->png fname nodes edges)
(dot->png fname
(lambda ()
(graph->dot nodes edges))))
(define (ugraph->png fname nodes edges)
(dot->png fname
(lambda ()
(ugraph->dot nodes edges))))
;;(define (run)
;; (ugraph->png "wizard" *nodes* *edges*))