File: dot2ruby.g

package info (click to toggle)
ruby-graphviz 1.2.5-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,216 kB
  • sloc: ruby: 7,685; xml: 26; makefile: 17
file content (185 lines) | stat: -rw-r--r-- 4,850 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
// Copyright (c) 2010 Gregoire Lejeune <gregoire.lejeune@free.fr>
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
//
// Usage :
//   gvpr -f dot2ruby.g [-a <output type>] <dot script>

BEGIN {
  int g_strict; int g_direct;
  graph_t cluster;
  node_t cnode;
  edge_t cedge;
  string attr; string attrv;
  graph_t subgraph; graph_t pgraph;
  graph_t ofgraph;
  graph_t sub_graph;
  graph_t tmp_g;

  string xOut;
  if( ARGC == 0 ) {
    xOut = "_";
  } else {
    xOut = tolower(ARGV[0]);
  }
  
  printf( "# This code was generated by dot2ruby.g\n\n" );

  string rubyfy( string s ) {
    string out;
    out = tolower( s );
    out = gsub( out, "[!a-zA-Z0-9_]", "_" );
    return( out );
  }

  string stringify( string s ) {
    string sout;
    sout = gsub(s, "\"", "\\\"");
    sout = gsub(sout, "@", "\\@");
    sout = gsub(sout, "$", "\\$");
    return( sout );
  }

  void load_sub_graph( graph_t p ) {
    if(p == NULL) {
      return;
    }
    sub_graph = fstsubg(p);
    while( sub_graph != NULL ) {
      pgraph = sub_graph.parent;
      printf ( "  graph_%s = graph_%s.add_graph( \"%s\" )\n", rubyfy(sub_graph.name), rubyfy(pgraph.name), rubyfy(sub_graph.name) );

      // ATTRS
      attr = fstAttr(sub_graph, "G");
      while( attr != "" ) {
        attrv = aget( sub_graph, attr );
        if( attrv != "" ) {
          printf( "  graph_%s[:%s] = '%s'\n", rubyfy(sub_graph.name), attr, attrv );
        }
        attr = nxtAttr( sub_graph, "G", attr );
      }
      tmp_g = sub_graph;
      load_sub_graph(sub_graph);
      sub_graph = nxtsubg( tmp_g );

    }
  }

  graph_t get_node_graph(node_t n, graph_t p) {
    sub_graph = fstsubg(p);
    while( sub_graph != NULL ) {
      if( isSubnode( sub_graph, n) != 0 ) {
        return get_node_graph(n, sub_graph);
      }    
      sub_graph = nxtsubg( sub_graph );
    }
    return p;
  }

  graph_t get_edge_graph(edge_t e, graph_t p) {
    sub_graph = fstsubg(p);
    while( sub_graph != NULL ) {
      if( isSubedge( sub_graph, e) != 0 ) {      
        return get_edge_graph(e, sub_graph);
      }    
      sub_graph = nxtsubg( sub_graph );
    }
    return p;
  }


}

BEG_G {
  printf( "require 'rubygems'\nrequire 'graphviz'\n");
  // Directed 
  g_direct = isDirect($);
  if( g_direct == 0 ) {
    printf( "graph_%s = GraphViz.graph( \"%s\"", rubyfy($.name), stringify($.name) );
  } else {
    printf( "graph_%s = GraphViz.digraph( \"%s\"", rubyfy($.name), stringify($.name) );
  }
  // Strict
  g_strict = isStrict($);
  if( g_strict != 0 ) {
    printf( ", :strict => true" );
  }
  printf( " ) { |graph_%s|\n", rubyfy($.name) );
  
  // Attributs of G
  attr = fstAttr($, "G");
  while( attr != "" ) {
    attrv = aget( $, attr );
    if( attrv != "" ) {
      printf( "  graph_%s[:%s] = '%s'\n", rubyfy($.name), attr, attrv );
    }
    attr = nxtAttr( $, "G", attr );
  }
  
  load_sub_graph($);
}

N {
  pgraph = $.root;
  ofgraph = get_node_graph($, pgraph);
  
  printf( "  node_%s = graph_%s.add_nodes( \"%s\"", rubyfy($.name), rubyfy(ofgraph.name), stringify($.name) );

  // Attributs of N
    attr = fstAttr($G, "N");
    while( attr != "" ) {
      attrv = aget( $, attr );
      if( attrv != "" ) {
        printf( ", :%s => '%s'", attr, gsub( attrv, "'", "\\'" ) );
      } else {
        printf( ", :%s => ''", attr );
      }
      attr = nxtAttr( $G, "N", attr );
    }

    printf( " )\n");
  }

E {
   pgraph = $.root;
    ofgraph = get_edge_graph($, $.root);  

  printf( "  graph_%s.add_edges( \"%s\", \"%s\"", rubyfy(ofgraph.name), stringify($.tail.name), stringify($.head.name) );

// Attributs of E
    attr = fstAttr($G, "E");
    while( attr != "" ) {
      attrv = aget( $, attr );
      if( attrv != "" ) {
        printf( ", :%s => '%s'", attr, gsub( attrv, "'", "\\'" ) );
      } else {
        printf( ", :%s => ''", attr );
      }
      attr = nxtAttr( $G, "E", attr );
    }

    printf( " )\n" );
  }

END_G {
  printf( "}\n" );
  if( xOut != "_" ) {
    if( xOut == "-" ) {
      printf( "@_graph_eval = graph_%s\n", rubyfy($.name) );
    } else {
  printf( "graph_%s.output( :%s => \"%s.%s\" )\n", rubyfy($.name), xOut, gsub($F, ".*/", ""), xOut );
    }
  }
}