sub import {
hook(type => "getsetup", id => "graphviz", call => \&getsetup);
hook(type => "needsbuild", id => "version", call => \&needsbuild);
- hook(type => "preprocess", id => "graph", call => \&graph);
+ hook(type => "preprocess", id => "graph", call => \&graph, scan => 1);
}
sub getsetup () {
sub graph (@) {
my %params=@_;
- $params{src} = "" unless defined $params{src};
+
+ if (exists $params{file}) {
+ if (! exists $pagesources{$params{file}}) {
+ error gettext("cannot find file");
+ }
+ $params{src} = readfile(srcfile($params{file}));
+ add_depends($params{page}, $params{file});
+ }
+
+ # Support wikilinks in the graph source.
+ my $src=$params{src};
+ $src="" unless defined $src;
+ $src=IkiWiki::linkify($params{page}, $params{destpage}, $params{src});
+ return unless defined wantarray; # scan mode short-circuit
+ if ($src ne $params{src}) {
+ # linkify makes html links, but graphviz wants plain
+ # urls. This is, frankly a hack: Process source as html,
+ # throw out everything inside tags that is not a href.
+ my $s;
+ my $nested=0;
+ use HTML::Parser;
+ error $@ if $@;
+ my $p=HTML::Parser->new(api_version => 3);
+ $p->handler(start => sub {
+ my %attrs=%{shift()};
+ if (exists $attrs{href}) {
+ if ($s=~/href\s*=\s*"$/) {
+ $s.=$attrs{href};
+ }
+ elsif ($s=~/href\s*=\s*$/) {
+ $s.="\"$attrs{href}\"";
+ }
+ else {
+ $s.="href=\"$attrs{href}\"";
+ }
+ }
+ $nested++;
+ }, "attr");
+ $p->handler(end => sub {
+ $nested--;
+ });
+ $p->handler(default => sub {
+ $s.=join("", @_) unless $nested;
+ }, "text");
+ $p->parse($src);
+ $p->eof;
+ $s=~s/\[ href= \]//g; # handle self-links
+ $params{src}=$s;
+ }
+ else {
+ $params{src}=$src;
+ }
+
$params{type} = "digraph" unless defined $params{type};
$params{prog} = "dot" unless defined $params{prog};
error gettext("prog not a valid graphviz program") unless $graphviz_programs{$params{prog}};