#!/usr/bin/env perl
 #############################################################################
# Copyright (c) 2004 Pelle Johansson.                                         #
# All rights reserved.                                                        #
#                                                                             #
# This file is part of the moftpd package. Use and distribution of            #
# this software is governed by the terms in the file LICENCE, which           #
# should have come with this package.                                         #
 #############################################################################

# $moftpd: make_html.pl 1251 2005-03-06 22:24:29Z morth $

sub gen_html
{
    my @tmpl = @{$_[0]};
    my %data = %{$_[1]};
    my $name = $data{'name'};
    
    open FILE, ">html/options/$name.html" || die "Can't open html/options/$name.html";
    
    my $skipto = 0;
    for (@tmpl)
    {
	chomp;
	
	if ($skipto)
	{
	    /$skipto/ && ($skipto = 0);
	    next;
	}
	
	/^%\?(.*)$/ && do {
	    $skipto = "%[/!]$1" if !$data{$1};
	    next;
	};
	
	/^%\/(.*)$/ && do {
	    $skipto = "%!$1";
	    next;
	};
	
	/^%!.*$/ && next;
	
	while (/%([^%]*)%/)
	{
	    last if !$data{$1};
	    
	    my $pat = "%" . $1 . "%";
	    my $val = $data{$1};
	    
	    s/$pat/$val/;
	}
	
	print FILE  "$_\n";
    }
    close FILE;
}

opendir XMLDIR, "xml/" || die "No xml/ directory.";
@files = grep {/\.xml$/i} readdir(XMLDIR);
closedir (XMLDIR);

@otmpl = `cat html/optiontmpl.html`;
@ttmpl = `cat html/tagtmpl.html`;

for (@files)
{
    open FILE, "<xml/$_" || die "Can't read file xml/$_.";
    my $istag = 0;
    my %data = ();
    my $where = "top";
    while (<FILE>)
    {
      /DOCTYPE mtag/ && do 
      {
	  $istag = 1;
	  next;
      };
      
      /<m(tag|option) name=(\"[^\"]*\"|\'[^\']*\')( inherited=(\"[^\"]*\"|\'[^\']*\'))?( cumulative=(\"[^\"]*\"|\'[^\']*\'))?/ && do
      {
	  my $name = $2;
	  my $inher = $4;
	  my $cumul = $6;
	  
	  $name =~ s/[\"\']//g;
	  $inher =~ s/[\"\']//g if $inher;
	  $cumul =~ s/[\"\']//g if $cumul;
	  
	  $data{'name'} = $name;
	  $data{'inherited'} = $inher if $inher;
	  $data{'cumulative'} = $cumul if $cumul;
	  next;
      };
      
      /<versions>([^<]*)<\/versions>/ && do
      {
	  $data{'versions'} = $1;
	  next;
      };
      
      /<allowed where=(\"[^\"]*\"|\'[^\']*\')/ && do
      {
	  my $where = $1;
	  my $index = 'options';
	  my $vis = $data{'name'};
	  
	  $where =~ s/[\"\']//g;
	  
	  $cwhere = ucfirst($where);
	  $index = 'tags' if $istag;
	  $vis = "&lt;$vis&gt;" if $istag;
	  if ($tags{$cwhere}{$index}) {
	      $tags{$cwhere}{$index} .= ", ";
	  } else {
	      $tags{$cwhere}{$index} = "";
	  }
	  $tags{$cwhere}{$index} .= "<a href='$data{'name'}.html'>$vis</a>";
	  
	  if ($data{'allowed'}) {
	      $data{'allowed'} .= ", ";
	  } else {
	      $data{'allowed'} = "";
	  }
	  if ($where eq "top") {
	      $data{'allowed'} .= "<a href='Top.html'>Top-Level</a>";
	  } else {
	      $data{'allowed'} .= "<a href='$cwhere.html'>&lt;$cwhere&gt;</a>";
	  }
	  next;
      };
      
      /<argument type=(\"[^\"]*\"|\'[^\']*\')( default=(\"[^\"]*\"|\'[^\']*\'))?( example=(\"[^\"]*\"|\'[^\']*\'))?/ && do
      {
	  my $type = $1;
	  my $default = $3;
	  my $example = $5;
	  
	  $type =~ s/[\"\']//g;
	  $default =~ s/[\"\']//g if $default;
	  $example =~ s/[\"\']//g if $example && $type ne "string";
	  
	  if ($data{'arguments'}) {
	      $data{'arguments'} .= ", ";
	  } else {
	      $data{'arguments'} .= "";
	  }
	  $type = "See description" if $type =~ /list$/;
	  $data{'arguments'} .= $type;
	  if ($default)
	  {
	      if ($data{'default'}) {
		  $data{'default'} .= ", ";
	      } else {
		  $data{'default'} .= "";
	      }
	      $data{'default'} .= $default;
	  }
	  if ($example)
	  {
	      if ($data{'example'}) {
		  $data{'example'} .= ", ";
	      } else {
		  $data{'example'} .= "";
	      }
	      $data{'example'} .= $example;
	  }
	  next;
      };
      
      /<related>([^<]*)<\/related>/ && do
      {
	  if ($data{'related'}) {
	      $data{'related'} .= ", ";
	  } else {
	      $data{'related'} = "";
	  }
	  $data{'related'} .= "<a href='$1.html'>$1</a>";
	  next;
      };
      
      /<description>/ && do
      {
	  $where = "desc";
	  next;
      };
      
      /<\/description>/ && do
      {
	  $where = "top";
	  next;
      };
      
      s/<option>([^>]*)<\/option>/<a href="$1.html">$1<\/a>/g;
      s/<tag>([^>]*)<\/tag>/<a href="$1.html">&lt;$1&gt;<\/a>/g;
      /(.*)<concept>([^>]*)<\/concept>(.*)/ && do
      {
	  my $link = $2;
	  my $vislink = $2;
	  my $pre = $1;
	  my $post = $3;
	  
	  $link =~ s/ /_/g;
	  
	  $_ = "$pre<a href='$link.html'>$vislink<\/a>$post";
      };
      
      $data{'description'} .= $_ if $where eq "desc";
    }
    
    close FILE;
    
    if ($istag)
    {
	my $name = $data{'name'};
	
	while (($key, $value) = each %data)
	{
	    $tags{$name}{$key} = $value;
	}
    }
    else {
	gen_html (\@otmpl, \%data);
    }
}

while (($_, $data) = each %tags)
{
    gen_html (\@ttmpl, $data);
}


syntax highlighted by Code2HTML, v. 0.9.1