isync has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I am using Graph::Easy to render a link graph as boxart. Works very well, except when I am using the boxart in a browser and the nodes are containing html markup.

The problem arises as Graph::Easy usually handles only textual node labels. Now adding, as I do, HTML and smuggling in a href, the result is a discrepancy in length between the textual representation and the length of the label string (which Graph::Easy uses to compute the node-box dimensions.

The dirty hack I use to get around this is supplying Graph::Easy with two different strings, depending on the state of its layout processing - by overriding Graph::Easy::Node::dimensions:
# compensate discrepancy between textual length and rendered-html leng +th sub Graph::Easy::Node::dimensions { # Returns the minimum dimensions of the node/cell derived from the # label or name, in characters. my $self = shift; use HTML::Strip; my $hs = HTML::Strip->new(); my $align = $self->attribute('align'); my ($lines,$aligns) = $self->_aligned_label($align); my $w = 0; my $h = scalar @$lines; foreach my $line (@$lines){ $line = $hs->parse($line); # $line =~ s/\s+/ /g; $w = length($line); # if length($line) > $w; } ($w,$h); }


The problem now is that Graph::Easy still thinks the label is wider and leaves the box open, as this:
┌─────────────────────────┐  aaaaa bbbbbb ccccccc   ┌──────────────────────┐
│ xxxxxxxxxxxxxxxxxxxxxxx │ ──────────────────────> │ xxxxxxxxxxxxxxxxxxx
└─────────────────────────┘                         └──────────────────────┘

Anyone inclined to look into Graph::Easy for me as I am incapable of figuring it out any better.

Replies are listed 'Best First'.
Re: Help with HTML-izing Graph::Easy
by CountZero (Bishop) on Jan 31, 2010 at 15:54 UTC
    Why don't you use the as_html_file(); method on your graph-object?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      The _html routines in Graph::Easy are quite simple. They just wrap some tags around the rendered ascii, boxart, etc. But they do not actually fix problems arising from the discrepancy between source code and rendered html when href's are used as labels. (See my example and patched code, where I use the label string with HTML in it and without, to feed it to Graph::Easy layouting)

      I use the outputted graph boxart on a webpage.
Re: Help with HTML-izing Graph::Easy
by gmargo (Hermit) on Feb 01, 2010 at 18:34 UTC

    This was a very interesting problem. Since the ascii output routine of Graph::Easy creates a fixed X*Y grid (known as a framebuffer), and lays the text out on it, it cannot handle the length difference problem between text and a link.

    However, if you don't mind some pre- and post- processing, I came up with a method to get what you want.

    After the graph is parsed, it is inspected and all the node labels are replaced with unique placeholders of the proper length. After the ascii is generated, it is post-processed to replace the unique labels with the link text.

    This code only handles the Node objects, but it could easily be extended to other objects. There are probably some hidden caveats in here that will show up in a more complex example, but I've no idea what your graph data looks like.

    I hope this helps you in some way.

    #!/usr/bin/perl -w use strict; use warnings; use Graph::Easy; my $graph = Graph::Easy->new(); my $bonn = $graph->add_node('Bonn'); my $berlin = $graph->add_node('Berlin'); $graph->add_edge ($bonn, $berlin, 'car'); $berlin->set_attributes({ link => "http://www.visit-berlin.org/", labe +l => "berlin"}); my $asc = $graph->as_ascii(); print "Original:\n"; print $asc."\n"; my $labelmap = pre_process_graph($graph); my $asc2 = $graph->as_ascii(); print "After pre-process:\n"; print $asc2."\n"; my $asc3 = post_process_ascii($labelmap, $asc2); print "After post-process:\n"; print $asc3."\n"; ###################################################################### +##### # Scan graph object and replace labels on nodes. ###################################################################### +##### sub pre_process_graph { my ($graph) = @_; my $labelmap = {}; # Just look at Node text for starters. foreach my $node ($graph->nodes()) { my $name = $node->name(); my $label = $node->attribute('label'); my $labeltext = defined $label ? $label : $name; $label = "undef" if !defined $label; #print "node: name=$name label=$label labeltext=$labeltext\n"; my $key = "$name,$label"; $labelmap->{$key} = { node => $node, name => $name, labeltext => $labeltext, }; } # How many labels have we found? my $count = scalar(keys %$labelmap); # Return early if nothing to do. return $labelmap if $count == 0; # How many digits identifier do we need? my $digits = length("$count"); # Create printf format: "_%01d", "_%02d" my $fmt = "_%0"."$digits"."d"; # label format is _01, _02 etc # so the min length is 3 if $digits == 2; (2 if $digits == 1) my $minlen = $digits + 1; my $labelnum = 1; foreach my $key (keys %$labelmap) { my $lmap = $labelmap->{$key}; my $textlen = length $lmap->{labeltext}; # Copy above default format string my $fmt = $fmt; # Pad string with underscores to maintain correct length. $fmt .= '_' x ($textlen - $minlen) if $textlen > $minlen; # Create new label. my $newlabel = sprintf $fmt, $labelnum; $lmap->{labelnum} = $labelnum; $lmap->{newlabel} = $newlabel; $lmap->{node}->set_attribute('label', $newlabel); $labelnum++; } return $labelmap; } ###################################################################### +##### # Scan ascii string and replace labels on nodes. ###################################################################### +##### sub post_process_ascii { my ($labelmap, $asc) = @_; foreach my $key (keys %$labelmap) { my $lmap = $labelmap->{$key}; my $newlabel = $lmap->{newlabel}; my $labeltext = $lmap->{labeltext}; my $link = $lmap->{node}->link(); # Pad out the old label if it was too short, # since we forced a minimum of 2 characters. my $minlen = length($newlabel); my $lablen = length($labeltext); $labeltext .= ' ' x ($minlen - $lablen) if $minlen > $lablen; if (defined $link && $link ne '') { my $href = "<a href=\"$link\">$labeltext</a>"; # If a link, replace text with link. $asc =~ s/\b$newlabel\b/$href/s; } else { # If not a link, restore original label. $asc =~ s/\b$newlabel\b/$labeltext/s; } } return $asc; } __END__ Original: +------+ car +--------+ | Bonn | -----> | berlin | +------+ +--------+ After pre-process: +------+ car +--------+ | _1__ | -----> | _2____ | +------+ +--------+ After post-process: +------+ car +--------+ | Bonn | -----> | <a href="http://www.visit-berlin.org/">berlin</a> | +------+ +--------+
      Thanks a lot! I had already given up hope that someone cares - and than such an amount of code!
      Works great.