package xm::grabitemxdoc;
use strict;
use xm::o;

sub DESC
{"
  will look for all global CDOC-comments that do not belong to any ITEMCDEF.
  Mark these as ITEMXDOC, and leave the rest to other scan-modules.
"}

sub DO
{
    my $in = shift;
    print STDERR "[";

    # use-CDEFS style    
    $in =~ s{(^|</ITEMCDEF(?:\s[^<>]*)?>(?!<ITEMCDEF[\s>]))
		 ( (?:[^<]|<(?!/?(?:CDOC|ITEMCDEF)[\s>]))*.? )
		     ((?:\ *<CDOC(?:\s[^<>]*)?> 
		       (?:.(?!</?CDOC[\s>]))*.
		       </CDOC(?:\s[^<>]*)?> \ *\s?)+)
		     } 
         { 
	      print STDERR "."; $1.$2."<ITEMXDOC>".$3."</ITEMXDOC>" 
	 }gsex;

    while(
	  $in =~ s{(^|</ITEMXDOC(?:\s[^<>]*)?(?=>))
		       ( (?:[^<]|<(?!/?(?:CDOC|ITEMCDEF|ITEMXDOC)[\s>]))* )
			   ((?:\ *<CDOC(?:\s[^<>]*)?> 
			     (?:.(?!</?CDOC[\s>]))*. 
			     </CDOC(?:\s[^<>]*)?> \ *\s?)+)
			   } 
	  { 
	      print STDERR "."; $1.$2."<ITEMXDOC>".$3."</ITEMXDOC>" 
	  }gsex
    ) { };
    
    # delete any ITEMXDOC that does not start on the line. this is "mgex" instead of "sgex"
    while(
    $in =~ s{(^\s*\S+.*) 
		 (<ITEMXDOC>(?!</ITEMXDOC[\s>])) 
		     ((?:.(?!</?ITEMXDOC[\s>]))*.?) 
			 (</ITEMXDOC>)}
	  { print STDERR ","; $1.$3 }gmex
    ) { };

    print STDERR "]";
    return $in;
} 

sub ARGS { return    xm::o::args_stdin(@_, DESC); }
sub main { return DO(xm::o::args_stdin(@_, DESC)); }

1;