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
  
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
  
219
  
220
  
221
  
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
  
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
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
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
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
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
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
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
  
597
  
598
  
599
  
600
  
601
  
602
  
603
  
604
  
605
  
606
  
607
  
608
  
609
  
610
  
611
  
612
  
613
  
614
  
615
  
616
  
617
  
618
  
619
  
620
  
621
  
622
  
623
  
624
  
625
  
626
  
627
  
628
  
629
  
630
  
631
  
632
  
633
  
634
  
635
  
636
  
637
  
638
  
639
  
640
  
641
  
642
  
643
  
644
  
645
  
646
  
647
  
648
  
649
  
650
  
651
  
652
  
653
  
654
  
655
  
656
  
657
  
658
  
659
  
660
  
661
  
662
  
663
  
664
  
665
  
666
  
667
  
668
  
669
  
670
  
671
  
672
  
673
  
674
  
675
  
676
  
677
  
678
  
679
  
680
  
681
  
682
  
683
  
684
  
685
  
686
  
687
  
688
  
689
  
690
  
691
  
692
  
693
  
694
  
695
  
696
  
697
  
698
  
699
  
700
  
701
  
702
  
703
  
704
  
705
  
706
  
707
  
708
  
709
  
710
  
711
  
712
  
713
  
714
  
715
  
716
  
717
  
718
  
719
  
720
  
721
  
722
  
723
  
724
  
725
  
726
  
727
  
728
  
729
  
730
  
731
  
732
  
733
  
734
  
735
  
736
  
737
  
738
  
739
  
740
  
741
  
742
  
743
  
744
  
745
  
746
  
747
  
748
  
749
  
750
  
751
  
752
  
753
  
754
  
755
  
756
  
757
  
758
  
759
  
760
  
761
  
762
  
763
  
764
  
765
  
766
  
767
  
768
  
769
  
770
  
771
  
772
  
773
  
774
  
775
  
// This module is the most high-level. 
// Some functions to apply to the XML tree after extraction, too, for 
// reference resolving etc. 
 
static inherit Parser.XML.Tree; 
static inherit "module.pmod"; 
 
#define DEB werror("###%s:%d\n", __FILE__, __LINE__); 
static private void processError(string message, mixed ... args) { 
  throw (AutoDocError(0, "ProcessXML", sprintf(message, @args))); 
} 
 
//======================================================================== 
// From source file to XML 
//======================================================================== 
 
static object makeWrapper(array(string) modules, object|void child) 
{ 
  foreach(reverse(modules) + ({ "" }), string n) { 
    object m = .PikeObjects.Module(); 
    m->name = n; 
    if (child) 
      m->AddChild(child); 
    child = m; 
  } 
  return child; 
} 
 
// filename       The file to extract from. 
// pikeMode       Non-zero if it is a Pike file. If the file contains 
//                  C-style doc comments, C-mode is used despite 
//                  pikeMode != 0. 
//   The following parameters used only when pikeMode != 0: 
// type           "class" or "module". 
// name           The name of the class/module. 
// parentModules  The ancestors of the class/module. 
// 
// EXAMPLE: To extract doc for Foo.Bar.Ippa: 
//   string xml = extractXML("lib/modules/Foo.pmod/Bar.pmod/Ippa.pike", 1, 
//     "class", "Ippa", ({ "Foo", "Bar" })); 
// 
string extractXML(string filename, int|void pikeMode, string|void type, 
                  string|void name, array(string)|void parentModules) 
{ 
  werror("extracting file %O\n", filename); 
  // extract the file... 
  // check if there are C style doc comments in it, 
  // because if there are, the CExtractor should be 
  // used instead of the PikeExtractor 
  int styleC = !pikeMode; 
  int stylePike = pikeMode; 
 
  string contents = Stdio.File(filename)->read(); 
 
  if (pikeMode) { 
    if (search("/*!", contents)) { 
      array(string) a = Parser.Pike.split(contents); 
      // check if it _really_ is a C-style doc comment ... 
      foreach (a, string s) 
        if (strlen(s) >= strlen("/*!*/") && s[0..2] == "/*!") 
          styleC = 1; 
        else if (isDocComment(s)) 
          stylePike = 1; 
      if (stylePike && styleC) 
        processError("both C and Pike style doc comments in file " + filename); 
    } 
  } 
  if (styleC) { 
    object m = .CExtractor.extract(contents, filename); 
    return m->xml(); 
  } 
  else { 
    object m = (type == "module") 
      ? .PikeExtractor.extractModule(contents, filename, name) 
      : .PikeExtractor.extractClass(contents, filename, name); 
    return makeWrapper(parentModules, m)->xml(); 
  } 
} 
 
//======================================================================== 
// BUILDING OF THE BIG TREE 
//======================================================================== 
 
static int isClass(Node node) { return node->get_any_name() == "class"; } 
static int isModule(Node node) { return node->get_any_name() == "module"; } 
static int isDoc(Node node) { return node->get_any_name() == "doc"; } 
 
static string getName(Node node) { return node->get_attributes()["name"]; } 
 
// dest and source are two <module> or <class> nodes. 
// Puts all children of source into the tree dest, in their right 
// place hierarchically. 
// Used when we have several root nodes, which occurs when there 
// are several lib root dirs, and when C files are involved (each 
// C extraction generates a separate module tree with a root) 
void mergeTrees(Node dest, Node source) { 
  mapping(string : Node) dest_children = ([]); 
  int dest_has_doc = 0; 
  foreach(dest->get_children(), Node node) 
    if (isClass(node) || isModule(node)) 
      dest_children[getName(node)] = node; 
    else if (isDoc(node)) 
      dest_has_doc = 1; 
  foreach(source->get_children(), Node node) 
    switch(node->get_any_name()) { 
      case "class": 
      case "module": 
        { 
        string name = getName(node); 
        Node n = dest_children[name]; 
        if (n) { 
          if (node->get_any_name() != n->get_any_name()) 
            processError("entity '" + name + 
                         "' used both as a class and a module"); 
          mergeTrees(n, node); 
        } 
        else 
          dest->add_child(node); 
        } 
        break; 
      case "inherit": 
      case "modifiers": 
        // these shouldn't be duplicated.. 
        break; 
      case "doc": 
        if (dest_has_doc) { 
          if (isClass(dest)) 
            processError("duplicate documentation for class " + getName(dest)); 
          else if (isModule(dest)) 
            processError("duplicate documentation for module " + 
                         getName(dest)); 
          processError("duplicate documentation"); 
        } 
        // fall through 
      default: 
        dest->add_child(node); 
    } 
} 
 
static void reportError(string filename, mixed ... args) { 
  werror("[%s]\t%s\n", filename, sprintf(@args)); 
} 
 
//======================================================================== 
// HANDLING @appears directives 
//======================================================================== 
 
static Node findNode(Node root, array(string) ref) { 
  Node n = root; 
  // top:: is just an anchor to the root 
  if (sizeof(ref) && ref[0] == "top::") 
    ref = ref[1..]; 
  if (!sizeof(ref)) 
    return root; 
  while (sizeof(ref)) { 
    array(Node) children = n->get_children(); 
    Node found = 0; 
    foreach (children, Node child) { 
      string tag = child->get_any_name(); 
      if (tag == "module" || tag == "class") { 
        string name = child->get_attributes()["name"]; 
        if (name && name == ref[0]) { 
          found = child; 
          break; 
        } 
      } 
    } 
    if (!found) 
      return 0; 
    n = found; 
    ref = ref[1..]; 
  } 
  return n; 
} 
 
static class ReOrganizeTask { 
  array(string) belongsRef = 0; 
  string newName = 0; 
  Node n; 
} 
 
static array(ReOrganizeTask) tasks; 
 
// current is a <module>, <class> or <docgroup> node 
static void recurseAppears(Node root, Node current) { 
  mapping attr = current->get_attributes() || ([]); 
  if (attr["appears"] || attr["belongs"] || attr["global"]) { 
    ReOrganizeTask t = ReOrganizeTask(); 
    if (string attrRef = attr["appears"]) { 
      array(string) a = splitRef(attrRef); 
      t->belongsRef = a[0 .. sizeof(a) - 2]; 
      t->newName = a[-1]; 
    } 
    else if (string belongsRef = attr["belongs"]) { 
      array(string) a = splitRef(belongsRef); 
      t->belongsRef = a; 
    } 
    t->n = current; 
    tasks += ({ t }); 
  } 
  if ( (<"class", "module">)[ current->get_any_name() ] ) 
    foreach (current->get_children(), Node child) 
      if ((<"module", "class", "docgroup">)[child->get_any_name()]) 
        recurseAppears(root, child); 
} 
 
// Tke care of all the @appears and @belongs directives everywhere, 
// and rearrange the nodes in the tree accordingly 
void handleAppears(Node root) { 
  tasks = ({ }); 
  recurseAppears(root, root); 
  tasks = Array.sort_array( 
    tasks, 
    lambda (ReOrganizeTask task1, ReOrganizeTask task2) { 
      return sizeof(task1->belongsRef) > sizeof(task2->belongsRef); 
    } ); 
  foreach (tasks, ReOrganizeTask task) { 
    Node n = task->n; 
    string type = n->get_any_name(); 
    array(string) belongsRef = task->belongsRef; 
    string newName = task->newName; 
    Node belongsNode = findNode(root, belongsRef); 
    if (!belongsNode) 
      werror("couldn't find the node: %O\n", belongsRef); 
    if (type == "docgroup") { 
      if (newName) 
        foreach (n->get_children(), Node child) 
          if (child->get_node_type() == XML_ELEMENT) { 
            mapping attributes = child->get_attributes(); 
            if (attributes["name"]) 
              // this ought to happen only once in this loop... 
              attributes["name"] = newName; 
          } 
    } 
    else if (type == "class" || type == "module") { 
      if (newName) 
        n->get_attributes()["name"] = newName; 
    } 
    Node parent = n->get_parent(); 
    if (parent) 
      parent->remove_child(n); 
    belongsNode->add_child(n); 
  } 
} 
 
//======================================================================== 
// RESOLVING REFERENCES 
//======================================================================== 
 
// Rather DWIM splitting of a string into a chain of identifiers: 
// "Module.Class->funct(int i)" ==> ({"Module","Class","func"}) 
static array(string) splitRef(string ref) { 
  array(string) a = Parser.Pike.split(ref); 
  string scope = ""; 
  array result = ({}); 
  if (sizeof(a) && (a[0] == "lfun" || a[0] == "predef" || a[0] == "top")) { 
    scope += a[0]; 
    a = a[1..]; 
  } 
  if (sizeof(a) && a[0] == "::") { 
    scope += a[0]; 
    a = a[1..]; 
  } 
  if (strlen(scope)) 
    result = ({ scope }); 
  for (;;) { 
    if (!sizeof(a)) 
      return result; 
    string token = a[0]; 
    a = a[1..]; 
    if (isIdent(token)) 
      result += ({ token }); 
    else 
      return result; 
    token = sizeof(a) ? a[0] : ""; 
    if ((<".", "->">)[token]) 
      a = a[1..]; 
    else 
      return result; 
  } 
} 
 
static string mergeRef(array(string) ref) { 
  string s = ""; 
  if (sizeof(ref) && search(ref[0], "::") >= 0) { 
    s = ref[0]; 
    ref = ref[1..]; 
  } 
  s += ref * "."; 
  return s; 
} 
 
static class Scope(string|void type, string|void name) { 
  multiset(string) idents = (<>); 
} 
 
static class ScopeStack { 
  /*static*/ array(Scope) scopeArr = ({ 0 }); // sentinel 
 
  void enter(string|void type, string|void name) { 
    //werror("entering scope type(%O), name(%O)\n", type, name); 
    scopeArr = ({ Scope(type, name) }) + scopeArr; 
  } 
  void leave() { scopeArr = scopeArr[1..]; } 
 
  void addName(string sym) { scopeArr[0]->idents[sym] = 1; } 
 
  mapping resolveRef(string ref) { 
    //werror("[[[[ resolving %O\n", ref); 
    array(string) idents = splitRef(ref); 
    if (!sizeof(idents)) 
      ref = ""; 
    else { 
      if (idents[0] == "top::") 
        // top:: is an anchor to the root. 
        ref = mergeRef(idents[1..]); 
      else 
        ref = mergeRef(idents); 
      string firstIdent = idents[0]; 
      for(int i = 0; ; ++i) { 
        Scope s = scopeArr[i]; 
        if (!s) 
          break;  // end of array 
        if (s->idents[firstIdent]) 
          if (s->type == "params") { 
            return ([ "param" : ref ]); 
          } 
          else { 
            //werror("[[[[ found in type(%O) name(%O)\n", s->type, s->name); 
            string res = ""; 
            // work our way from the bottom of the stack 
            for (int j = sizeof(scopeArr) - 1; j >= i; --j) { 
              string name = 0; 
              if (scopeArr[j]) 
                name = scopeArr[j]->name; 
              if (name && name != "") 
                res += name + "."; 
              //werror("[[[[ name == %O\n", name); 
            } 
            return ([ "resolved" : res + ref ]); 
          } 
        if (s->type == "module") 
          break; // reached past the innermost module... 
      } 
    } 
    // TODO: should we check that the symbol really DOES appear 
    // on the top level too? 
    return ([ "resolved" : ref ]); // we consider it to be an absolute reference 
  } 
} 
 
static void fixupRefs(ScopeStack scopes, Node node) { 
  node->walk_preorder( 
    lambda(Node n) { 
      if (n->get_node_type() == XML_ELEMENT) { 
        // More cases than just "ref" ?? 
        // Add them here if we decide that also references in e.g. 
        // object types should be resolved. 
        if (n->get_any_name() == "ref") { 
          mapping m = n->get_attributes(); 
          if (m["resolved"]) 
            return; 
          string ref = n->value_of_node(); 
          //werror("¤¤¤¤ resolving reference %O\n", ref); 
          //foreach (scopes->scopeArr, Scope s) 
          //  werror("¤¤¤¤    %O\n", s ? s->name : "NULL"); 
          mapping resolved = scopes->resolveRef(n->value_of_node()); 
          //werror("¤¤¤¤ resolved to: %O\n", resolved); 
          //werror("¤¤¤¤ m == %O\n", m); 
          foreach (indices(resolved), string i) 
            m[i] = resolved[i]; 
        } 
      } 
    } 
  ); 
} 
 
// expects a <module> or <class> node 
static void resolveFun(ScopeStack scopes, Node node) { 
  // first collect the names of all things inside the scope 
  foreach (node->get_children(), Node child) 
    if (child->get_node_type() == XML_ELEMENT) { 
      switch (child->get_any_name()) { 
        case "docgroup": 
          // add the names of all things documented 
          // in the docgroup. 
          foreach (child->get_children(), Node thing) 
            if (child->get_node_type() == XML_ELEMENT) { 
              mapping attributes = thing->get_attributes() || ([]); 
              string name = attributes["name"]; 
              if (name) 
                scopes->addName(name); 
            } 
          break; 
        case "module": 
        case "class": 
          // add the name of the submodule/subclass to the scope 
          string name = child->get_attributes()["name"]; 
          if (name) 
            scopes->addName(name); 
          break; 
        default: 
          ; // do nothing (?) 
      } 
    } 
  foreach (node->get_children(), Node child) { 
    if (child->get_node_type() == XML_ELEMENT) { 
      string tag = child->get_any_name(); 
      switch (tag) { 
        case "class": 
        case "module": 
          scopes->enter(tag, child->get_attributes()["name"]); 
          { 
            resolveFun(scopes, child); 
          } 
          scopes->leave(); 
          break; 
        case "docgroup": 
          scopes->enter("params"); 
          { 
            Node doc = 0; 
            foreach (child->get_children(), Node n) 
              if (n->get_any_name() == "doc") 
                doc = n; 
              else if (n->get_any_name() == "method") { 
                foreach (filter(n->get_children(), 
                                lambda (Node n) { 
                                  return n->get_any_name() == "arguments"; 
                                }), Node m) 
                  foreach (m->get_children(), Node argnode) 
                    scopes->addName(argnode->get_attributes()["name"]); 
              } 
            fixupRefs(scopes, doc); 
          } 
          scopes->leave(); 
          break; 
        case "doc":  // doc for the <class>/<module> itself 
          fixupRefs(scopes, child); 
          break; 
        default: 
          ; // do nothing 
      } 
    } 
  } 
} 
 
// Resolves references, regarding the Node 'tree' as the root of the whole 
// hierarchy. 'tree' might be the node <root> or <module name="Pike"> 
void resolveRefs(Node tree) { 
  ScopeStack scopes = ScopeStack(); 
  scopes->enter("module", "");    // The top level scope 
  resolveFun(scopes, tree); 
  scopes->leave(); 
} 
 
// Call this method after the extraction and merge of the tree. 
void postProcess(Node tree) { 
  handleAppears(tree); 
  resolveRefs(tree); 
} 
 
 
//======================================================================== 
// SPLITTING THE BIG TREE INTO DIRECTORIES AND FILES 
//======================================================================== 
 
// Structure description file format: 
// 
// <!-- Top container has no name attribute, maps to the dir 
//      supplied as a parameter to the splitIntoDirHier function --> 
// <dir> 
//   <dir name="Process"> 
//     <file name="create_process.xml"> 
//       <!-- Only create and limit_value på den här sidan, 
//       in that order --> 
//       <target module="Process" class="create_process" entity="create"/> 
//       <target module="Process" class="create_process" entity="limit_value"/> 
//     </file> 
//   </dir> 
//   <dir name="Protocols"> 
//     <file name="HTTP.xml"> 
//       <!-- All in Protocols.HTTP, document order. --> 
//       <target module="Protocols.HTTP"/> 
//     </file> 
//   </dir> 
//   <dir name="Standards"> 
//     <file name="URI.xml"> 
//       <!-- All in Standards.URI, document order. --> 
//       <target module="Standards" class="URI"/> 
//     </file> 
//   </dir> 
// </dir> 
 
static class Target { 
  string type;         // "class", "module", or "entity" 
  int moduleNest = 0;  // How many idents (except last) are modules 
  int classNest = 0;   // How many idents (except last) are classes 
  string file = 0;     // relative to the virtual root 
  Node data = 0;       // the XML to insert in the file 
} 
 
static int splitError(string s, mixed ... args) { 
  s = sprintf(s, @args); 
  throw(AutoDocError(0, "ProcessXML/splitting", s)); 
} 
 
//! Takes an autodoc-XML string, an autodoc structure file and a 
//! directory name and splits the first according to the second into 
//! the third. 
//! @param docXMLFile 
//!   The contents of the autodoc XML blob to be split up 
//! @param structureXMLFile 
//!   The structure description for the file tree to be created in 
//!   @[rootDir] 
//! @param rootDir 
//!   The name of the directory that maps to the <dir> element on the 
//!   top level of @[structureXMLFile] 
void splitIntoDirHier(string docXMLFile, string structureXMLFile, 
                      string rootDir) 
{ 
  Node doc = parse_input(docXMLFile)[0]; 
  Node struc = parse_input(structureXMLFile)[0]; 
 
  // First we find and all targets 
 
  // If the value is a string, it's just the href to the file, because the 
  // entity was just moved together with something in its docgroup 
  mapping(string : Target | string) targets = ([]); 
 
  array(string) dirs = ({}); 
  string fileName = 0; 
  struc->walk_preorder_2( 
    lambda(Node n) { 
      if (n->get_node_type() == XML_ELEMENT) { 
        mapping attr = n->get_attributes(); 
        switch(n->get_any_name()) { 
          case "target": 
            { 
            Target t = Target(); 
            array(string) ids = ({}); 
            if (attr["module"]) 
              t->moduleNest = sizeof(ids = attr["module"]/"."); 
            if (attr["class"]) 
              t->classNest = sizeof(ids += attr["class"]/".") - t->moduleNest; 
            if (attr["entity"]) { 
              ids += ({ attr["entity"] }); 
              t->type = "entity"; 
            } 
            else if (attr["class"]) { 
              t->type = "class"; 
              --t->classNest; 
            } 
            else if (attr["module"]) { 
              t->type = "module"; 
              --t->moduleNest; 
            } 
            else 
              splitError("Bad <target> tag"); 
            t->file = dirs * "/" + "/" + 
              (fileName || splitError("<target> must be inside a <file>")); 
            targets[ids * "."] = t; 
            attr["full_ref"] = ids * "."; 
            break; 
            } 
          case "file": 
            attr["full_dir"] = rootDir + "/" + dirs * "/"; 
            fileName = attr["name"] 
              || splitError("<file> must have a name attribute"); 
            break; 
          case "dir": 
            if (attr["name"]) 
              dirs += ({ attr["name"] }); 
            break; 
          default: 
            splitError("Unknown structure tag <" + n->get_any_name() + ">"); 
        } 
      } 
    }, 
    lambda(Node n) { 
      if (n->get_node_type() == XML_ELEMENT) 
        switch (n->get_any_name()) { 
          case "file": 
            fileName = 0; 
            break; 
          case "dir": 
            if (n->get_attributes()["name"]) 
              dirs = dirs[0..sizeof(dirs) - 2]; 
            break; 
        } 
    } 
  ); 
 
  // 2. For all targets, beginning with the most specific (most '.'s) 
  //   2.1 Remove the corresponding class/docgroup from the doc tree 
  //   2.2 Wrap it in placeholder class/modules 
  //   2.3 Put it in the Target.data member 
  array(string) sortedTargets = 
    Array.sort_array(indices(targets), 
                     lambda(string s1, string s2) { 
                       return sizeof(s1 / ".") < sizeof(s2 / "."); 
                     } 
                    ); 
  foreach(sortedTargets, string target) { 
    object(Target)|string t = targets[target]; 
    if (!objectp(t)) 
      continue; // It was just a "moved along" entry in a docgroup! 
 
    array(string) ref = target / "."; 
    Node n = findStuff(doc, ref); 
    if (!n) 
      splitError("unable to find %O in the doc tree", target); 
 
    // Remove it from the <class> or <module> that it belongs to, 
    // replacing it with a placeholder. 
    Node parent = n->get_parent(); 
    if (parent) { 
      mapping nattr = n->get_attributes(); 
      mapping(string : string) attr = ([ 
        "type" : "inner-placeholder", 
        "href" : t->file, 
      ]); 
      if (nattr["name"]) 
        attr["name"] = nattr["name"]; 
      Node placeHolder = Node(XML_ELEMENT, n->get_any_name(), attr, 0); 
      if (n->get_any_name() == "docgroup") { 
        // put some placeholder stuff inside it too... 
        foreach (n->get_children(), Node child) 
          if (child->get_node_type() == XML_ELEMENT && child->get_any_name() != "doc") { 
            // Reparse it to get a COPY of it ... 
            Node new = parse_input(child->html_of_node())[0]; 
            mapping attr = new->get_attributes() || ([]); 
            // Should we really add it? 
            attr["type"] = "inner-placeholder"; 
            placeHolder->add_child(new); 
          } 
      } 
      parent->replace_child(n, placeHolder); 
    } 
 
    if (n->get_any_name() == "docgroup") { 
      string container = sizeof(ref) > 1 ? ref[0 .. sizeof(ref) - 2] * "." + "." : ""; 
      foreach (n->get_children(), Node child) 
        if (child->get_node_type() == XML_ELEMENT && child->get_any_name() != "doc") { 
          string name = (child->get_attributes() || ([])) ["name"]; 
          if (name && name != ref[-1]) 
            if (targets[container + name]) 
              splitError("attempt to split docgroup of %s", container + name); 
            else 
              // Setting it to a string: 
              targets[container + name] = t->file; 
        } 
    } 
 
    // Traverse it, resolving all <ref> hrefs 
    n->walk_preorder( 
      lambda(Node n) { 
        if (n->get_any_name() == "ref" && n->get_node_type() == XML_ELEMENT) { 
          mapping(string : string) attr = n->get_attributes() || ([]); 
          if (attr["resolved"]) { 
            string href = findHref(targets, attr["resolved"] / "."); 
            if (!href) 
              werror("unable to find href to %O", attr["resolved"]); 
            else 
              attr["href"] = href; 
          } 
        } 
      } 
    ); 
 
    // Wrap the stuff in placeholder classes and modules 
    for (int i = sizeof(ref) - 2; i >= 0; --i) { 
      mapping attr = ([]); 
      attr["href"] = findHref(targets, ref[0 .. i]) 
        || (werror("unable to find href to %O", ref[0 .. i] * "."), 0); 
      attr["name"] = ref[i]; 
      attr["type"] = "outer-placeholder"; 
      Node newNode = 
        Node(XML_ELEMENT, i < t->moduleNest ? "module" : "class", attr, 0); 
      newNode->add_child(n); 
      n = newNode; 
    } 
    t->data = n; 
  } 
 
  // Traverse the tree again, creating the dirs, and writing the 
  // targets to the files. 
  string path; 
  Stdio.File file = Stdio.File(); 
  struc->walk_preorder_2( 
    lambda(Node n) { 
      if (n->get_node_type() == XML_ELEMENT) { 
        mapping(string : string) attr = n->get_attributes(); 
        switch (n->get_any_name()) { 
          case "file": 
            { 
            string dir = attr["full_dir"]; 
            if (!Stdio.mkdirhier(dir)) 
              splitError("unable to create directory %O", dir); 
            path = dir + "/" + attr["name"]; 
            if (!file->open(path, "cwt")) 
              splitError("unable to open file %O for write access", path); 
            } 
            break; 
          case "target": 
            { 
            string target = attr["full_ref"]; 
            object(Target)|string t = targets[target]; 
            if (!objectp(t)) 
              splitError("THIS SHOULDN'T HAPPEN!"); 
            string xml = t->data->html_of_node(); 
            if (file->write(xml) != strlen(xml)) 
              splitError("unable to write to file %O", path); 
            } 
            break; 
        } 
      } 
    }, 
    lambda(Node n) { 
      if (n->get_node_type() == XML_ELEMENT && n->get_any_name() == "file") 
        if (!file->close()) 
          splitError("unable to close file %O", path); 
    } 
  ); 
} 
 
// Find the file where the entity will have its documentation. 
static string findHref(mapping(string : Target|string) targets, array(string) ids) { 
  for (;;) { 
    object(Target)|string t = targets[mergeRef(ids)]; 
    if (t) 
      return stringp(t) ? t : t->file; 
    if (sizeof(ids) <= 1) 
      break; 
    ids = ids[0 .. sizeof(ids) - 2]; 
  } 
  return 0; 
} 
 
// If ref points to a class or module, return that <class> / <module>, 
// else the <docgroup> that contains the thing... 
static Node findStuff(Node root, array(string) ref) { 
  Node n = root; 
  while (sizeof(ref)) { 
    array(Node) children = n->get_children(); 
    Node found = 0; 
    foreach (children, Node child) { 
      string tag = child->get_any_name(); 
      if (tag == "module" || tag == "class") { 
        string name = child->get_attributes()["name"]; 
        if (name && name == ref[0]) { 
          found = child; 
          break; 
        } 
      } 
    } 
    if (!found && sizeof(ref) == 1) 
      outerloop: foreach (children, Node child) 
        if (child->get_any_name() == "docgroup") 
          foreach (child->get_children(), Node thing) 
            if ((thing->get_attributes() || ([]))["name"] == ref[0]) 
              if (child->get_attributes()["type"] == "inner-placeholder") 
                splitError("%s is in the same docgroup as another target, cannot", ref * "."); 
              else { 
                found = child; 
                break outerloop; 
              } 
    if (!found) 
      return 0; 
    n = found; 
    ref = ref[1..]; 
  } 
  return n; 
}