├── .gitignore ├── tests ├── cloud.png ├── test-notugly-expected.png ├── Makefile ├── test.dot └── test-notugly-expected.svg ├── LICENSE ├── README.md ├── arytodot.rb ├── traceviz.rb └── notugly.xsl /.gitignore: -------------------------------------------------------------------------------- 1 | /tests/test.svg 2 | /tests/test-notugly.svg 3 | -------------------------------------------------------------------------------- /tests/cloud.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vidarh/diagram-tools/HEAD/tests/cloud.png -------------------------------------------------------------------------------- /tests/test-notugly-expected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vidarh/diagram-tools/HEAD/tests/test-notugly-expected.png -------------------------------------------------------------------------------- /tests/Makefile: -------------------------------------------------------------------------------- 1 | 2 | .PHONY: clean 3 | 4 | test-notugly.svg: test.svg ../notugly.xsl 5 | xsltproc ../notugly.xsl test.svg >test-notugly.svg 6 | 7 | test.svg: test.dot 8 | dot -Tsvg test.dot >test.svg 9 | 10 | clean: 11 | rm test.svg test-notugly.svg 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2012 Vidar Hokstad 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a 5 | copy of this software and associated documentation files (the "Software"), 6 | to deal in the Software without restriction, including without limitation 7 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | and/or sell copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included 12 | in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 | OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Diagram Tools 3 | 4 | A number of small tools for generating and manipulating diagrams, mostly 5 | relying on Graphviz for output. 6 | 7 | Vidar Hokstad <vidar@hokstad.com> 8 | 9 | Unless otherwise noted, everything here is under the MIT license - see 10 | the LICENSE file. 11 | 12 | ## notugly.xsl ## 13 | 14 | [An XSL transform to pretty up the SVG output from Graphviz](http://www.hokstad.com/making-graphviz-output-pretty-with-xsl.html); 15 | see also [this update](http://www.hokstad.com/making-graphviz-output-pretty-with-xsl-updated.html) 16 | 17 | By Vidar Hokstad and Ryan Shea; Contributions by Jonas Tingborn, Earl Cummings, 18 | Michael Kennedy (Graphviz 2.20.2 compatibility, bug fixes, 19 | testing, lots of gradients); Paul Boddie (refactoring, bug fixes) 20 | 21 | Example output: 22 | 23 | ![expected output](https://raw.githubusercontent.com/vidarh/diagram-tools/master/tests/test-notugly-expected.png) 24 | 25 | ## arytodot.rb ## 26 | 27 | [A script by Vidar Hokstad to visualize Ruby arrays using Graphviz](http://www.hokstad.com/creating-graphviz-graphs-from-ruby-arrays.html) 28 | 29 | ## traceviz.rb ## 30 | 31 | [A script by Vidar Hokstad to visualize traceroute ouput with Graphviz](http://www.hokstad.com/traceviz-visualizing-traceroute-output-with-graphivz.html) 32 | -------------------------------------------------------------------------------- /arytodot.rb: -------------------------------------------------------------------------------- 1 | 2 | # See http://www.hokstad.com/creating-graphviz-graphs-from-ruby-arrays.html 3 | 4 | module ToDot 5 | def self.escape str 6 | str.gsub(/([<>{} |\\])/) { "\""+$1 } 7 | end 8 | end 9 | 10 | class String 11 | def to_dot_label; '\"'+ToDot::escape(self)+'\"'; end 12 | end 13 | 14 | class Array 15 | def to_dot_label; "..."; end 16 | 17 | def to_dot_edge src, shorten 18 | " #{src}" + (shorten ? "" : ":#{object_id}") + " -> #{object_id};\n" 19 | end 20 | 21 | def to_dot_subgraph 22 | return "" if nil 23 | ary = self[0].is_a?(Array) 24 | shorten = !ary && self[1..-1].detect{|o| !o.is_a?(Array)} == nil 25 | s = " #{object_id} [label=\"" 26 | if shorten 27 | s += self[0].to_dot_label + "\", shape=rect];\n" 28 | else 29 | s += collect { |o| "<#{o.object_id}> " + o.to_dot_label }.join("|") 30 | s += "\"];\n" 31 | end 32 | s += collect {|o| o.to_dot_edge(object_id,shorten) }.join 33 | s += collect {|o| o.to_dot_subgraph }.join 34 | s 35 | end 36 | end 37 | 38 | class Object 39 | def to_dot_subgraph; end 40 | def to_dot_edge src, shorten; end 41 | def to_dot_label; ToDot::escape(to_s); end 42 | 43 | def to_dot 44 | s = "digraph G {\n" 45 | s += " node [shape=record style=filled fillcolor=lightblue " 46 | s += "fontname=Verdana height=0.05 fontsize=10.0 ];\n" 47 | s += to_a.to_dot_subgraph 48 | s += "}\n" 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /tests/test.dot: -------------------------------------------------------------------------------- 1 | 2 | digraph test { 3 | size = "7,7"; 4 | 5 | graph [ label = "Test Graph Label" ]; 6 | 7 | node [fontsize=10]; 8 | 9 | foo [ shape = box ]; 10 | bar [ shape = box, style="filled", fillcolor="mediumblue" ]; 11 | 12 | baz [ shape = box, style="rounded,filled", fillcolor="white" ]; 13 | x [ shape = box, style="filled,rounded", fillcolor="crimson" ]; 14 | y [ shape = polygon ]; 15 | z [ shape = polygon, style="filled", fillcolor="aquamarine" ]; 16 | a [ shape = ellipse ]; 17 | b [ shape = ellipse, style="filled", label="Multi line\nLabel" ]; 18 | c [ shape = triangle]; 19 | d [ shape = triangle, style="filled", fillcolor="chocolate" ]; 20 | 21 | e [ shape = house ]; 22 | f [ shape = house, style="filled", fillcolor="orange" ]; 23 | 24 | g [ shape = doublecircle ]; 25 | h [ shape = doublecircle, style="filled", fillcolor="red" ]; 26 | 27 | k [ shape = Mcircle ]; 28 | l [ shape = Mcircle, style="filled", fillcolor="green" ]; 29 | 30 | m [ shape = component ]; 31 | n [ shape = component, style="filled", fillcolor="violet" ]; 32 | 33 | polylink [ shape = box, href="#", label="Poly Link" ]; 34 | pathlink [ shape = box, style="rounded,filled", fillcolor="white", color="green", href="#", label="Path Link" ]; 35 | cloud [ shape = none, image="cloud.png" ]; 36 | 37 | foo -> bar [ label = "foobar" ]; 38 | a -> b -> c; 39 | x -> y -> z; 40 | 41 | k -> l -> m -> n; 42 | 43 | cloud -> polylink -> pathlink; 44 | } 45 | -------------------------------------------------------------------------------- /traceviz.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | 3 | # See http://www.hokstad.com/traceviz-visualizing-traceroute-output-with-graphivz.html 4 | 5 | require 'timeout' 6 | require 'set' 7 | require 'socket' 8 | 9 | TRACEROUTE=`which traceroute`.chomp 10 | TIMES=3 11 | TIMEOUT=60 12 | 13 | def traceroute host 14 | `#{TRACEROUTE} -n #{host}` 15 | end 16 | 17 | 18 | class TraceViz 19 | def initialize(times,timeout) 20 | @times,@timeout = times,timeout 21 | @edges = Set.new 22 | @nodes = Set.new 23 | @targets = Set.new 24 | @this_host = Socket.gethostname 25 | @targets << @this_host 26 | end 27 | 28 | def trace host 29 | @times.times do |i| 30 | STDERR.puts "Trace ##{i+1} for #{host}" 31 | Timeout::timeout(@timeout) do 32 | process_trace(host,traceroute(host)) 33 | end rescue nil 34 | end 35 | end 36 | 37 | def process_trace host,trace 38 | @targets << host 39 | trace = [@this_host] + trace.collect do |line| 40 | line=line.split 41 | line[0].to_i > 0 && line[1] != "*" ? line[1] : nil 42 | end.compact 43 | trace << host 44 | trace.each {|h| @nodes << h } 45 | trace.each_cons(2) {|h1,h2| @edges << [h1,h2] } 46 | end 47 | 48 | def to_dot 49 | res = "digraph G {" 50 | @edges.each { |h1,h2| res << " \"#{h1}\"->\"#{h2}\"\n" } 51 | @nodes.each do |n| 52 | color = @targets.member?(n) ? "lightblue" : "lightgrey" 53 | res << " \"#{n}\" [style=filled fillcolor=#{color}]\n" 54 | end 55 | res << "}" 56 | end 57 | end 58 | 59 | tv = TraceViz.new(TIMES,TIMEOUT) 60 | ARGV.each {|host| tv.trace(host) } 61 | puts tv.to_dot 62 | 63 | -------------------------------------------------------------------------------- /tests/test-notugly-expected.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | testTest Graph Labelfoofoobarbarfoo->barfoobarbazbazxxyyx->yzzy->zaabMulti lineLabela->bccb->cddeeffgghhkkllk->lmml->mnnm->npolylinkPoly LinkpathlinkPath Linkpolylink->pathlinkcloudcloudcloud->polylink 8 | 9 | -------------------------------------------------------------------------------- /notugly.xsl: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | font-size:10px; font-family:sans-serif; 9 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | fill- 219 | 220 | 221 | stop-color:;stop-opacity:1 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | shadow 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 394 | 396 | 398 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | fill: black; stroke: none; fill-opacity:0.3 531 | translate(3,3) 532 | 533 | 534 | 535 | 536 | 537 | 538 | fill: none; stroke: black; stroke-opacity:0.3 539 | translate(3,3) 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 549 | 551 | 553 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 566 | 568 | 570 | black 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | fill: url(#); 597 | 598 | 599 | fill: none; 600 | 601 | 602 | stroke: ; 603 | stroke: ; 604 | stroke: black; 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | fill: url(#fill-); 613 | fill: url(#); 614 | 615 | 616 | 617 | 618 | --------------------------------------------------------------------------------