\n/g;
  return $_;
}
sub layout 
{ my ($Name, $Head, $Code, $Doku, $Dflt, $Attr) = @_;
  if ($html) 
  {
    print "
| $Name\n";
    if ($Attr eq '')
    {
      print "     | $Head\n";
    }
    else
    {
      print "     | $Head\n";
      print "     | $Attr\n";
    }
    if ($Code ne '')
    {
      my @Part = split('\|',$Code);
      my $Type = $Part[0];
      my $Indi = $#Part > 0 ? $Part[1] : "";
      my $Parm = $#Part > 1 ? $Part[2] : "";
      $Code = $Parm                 if $Type eq 'PRN';
      $Code = $Indi                 if $Type eq 'CTL';
      $Code = "ESC $Indi"           if $Type eq 'ESC';
      $Code = "0x7f"                if $Type eq 'DEL';
      $Code = "ESC # $Indi"         if $Type eq 'HSH';
      $Code = "ESC $Parm"           if $Type eq 'SCS';
      $Code = "ESC Y $Parm"         if $Type eq 'VT5';
      $Code = "ESC [ $Parm $Indi"   if $Type eq 'CSI';
      $Code = "ESC [ ? $Parm $Indi" if $Type eq 'PRI';
      print " | 
$Head\n";
foreach $p (sort keys %$t)
{
  my @Fld = split('\.', $p);
  if ($#Fld == 1 && $Fld[1] eq 'head')
  {
    my $name = $Fld[0];
    my $head = $t->{$p};
    my $attr = exists $t->{"$name.sect"}?$t->{"$name.sect"}:"";
    if ($attr =~ /$Include/)
    {
    layout2( $name, $head, exists $t->{"$name.code"}?$t->{"$name.code"}:"");
    }
  }
}
}
sub sortTest
{
  my ($t) = @_;
  my $p;
  my $s = {};
  my $n = {};
  my $curr = "";
  foreach $p (keys %$t)
  {
    my @Fld = split('\.', $p);
    if ($#Fld == 1 && $Fld[1] eq 'head')
    {
      my $name = $Fld[0];
      if (exists $t->{"$name.code"})
      {
        $s->{$t->{"$name.code"}} = $name;
      }
    }
  }
  print "\n";
  foreach $p (sort keys %$s)
  {
    my $name = $s->{$p};
    my @Fld = split('\|', $p);
    if ($Fld[0] ne $curr)
    {
      print "$Fld[0] codes\n";
    }
    $curr = $Fld[0];
    layout2($name,$t->{"$name.head"},$p);
  }
  print " |   \n";
}
sub htmlsect
{
  my ($h) = @_;
  $_ = $all->{"$h.html"};
  s/\n  \.\n/\n  \n/g;
  print "$_\n";
}
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# MAIN ------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
my $t = parse();
my $p;
my $table = 0;
# -------------------------------
htmlsect("Introduction");
# -------------------------------
secthead("Control Sequences");
htmlsect("Sequences");
# -------------------------------
secthead("Host to Terminal (Instructions by Code)");
sortTest($t);
# -------------------------------
secthead("Host to Terminal (Instructions by Group)");
htmlsect("Operations");
print " \n";
layoutTable("Commands (Character Display Operation)",$t,"Command\.Display");
layoutTable("Commands (Rendition related status)",$t,"Command\.RenderMode");
layoutTable("Commands (Cursor)",$t,"Command\.Cursor");
layoutTable("Commands (Cursor related status)",$t,"Command\.CursMode");
layoutTable("Commands (Edit)",$t,"Command\.Erase|Command\.Insert|Command.\Delete");
layoutTable("Commands (Miscellaneous)",$t,"Command[^.]|Command\$");
layoutTable("Commands (General mode setting)",$t,"Command\.SetMode");
layoutTable("Commands (Miscellaneous status)",$t,"Command\.Mode");
layoutTable("Commands (VT52)",$t,"Command\.VT52");
layoutTable("Commands (Not implemented)",$t,"Command\.NoImp");
layoutTable("Commands (Ignored)",$t,"Command\.Ignored");
layoutTable("Commands (Requests)",$t,"Command\.Request");
print " \n";
# -------------------------------
secthead("Terminal to Host");
print "\n";
layoutTable("Replies",$t,"Reply");
layoutTable("Events",$t,"Event");
# -------------------------------
print " \n";
secthead("Modes");
print "\n";
layoutTable("Modes",$t,"Mode");
#print "Other Codes\n";
print " \n";
# -------------------------------
secthead("Appendix A - Notion Details");
htmlsect("ConceptDB");
# -------------------------------
head();
foreach $p (sort keys %$t)
{
  my @Fld = split('\.', $p);
  if ($#Fld == 1 && $Fld[1] eq 'head')
  {
#   print "\n" if ($table);
    my $name = $Fld[0];
    my $head = $t->{$p};
    layout( $name, $head,
            exists $t->{"$name.code"}?$t->{"$name.code"}:"", 
            exists $t->{"$name.text"}?$t->{"$name.text"}:"",
            exists $t->{"$name.dflt"}?$t->{"$name.dflt"}:"",
            exists $t->{"$name.emus"}?$t->{"$name.emus"}:"" );
    $table = 0;
  }
  if ($html && $#Fld == 2 && $Fld[1] eq 'table')
  {
    my $lines = $t->{$p};
    my $line;
    my $field;
    my @fldspan = ();
    my $ln = 0;
    print " |