/*
**      $VER: Funcs.c 1.0
**
**      Library functions for HTML export
*/

#define __USE_SYSBASE

#include <exec/types.h>
#include <exec/memory.h>

#include <proto/exec.h>
#include <proto/dos.h>

#include <string.h>

#include "ixex.h"

#define e(x) data->ci_entry[x]
#define ep(x) data->cie_position[x]
#define el(x) data->cie_length[x]
#define ed(x) data->ci_data+data->cie_position[x]

/*
** Every iX Export module has two functions:
**
** STRPTR EXF_GetName(APTR future);
** - it is used to get the name of this export module which will
**   be shown in the menu of iX-Guide. The return string should
**   be allocated with AllocVec()
**   'future' argument should be always NULL for now.
**
** LONG EXF_Export(struct iX_ChunkInfo *data,STRPTR name,
**                 struct iX_Node *nodeinfo,APTR control);
** - this function is used to convert the 'data' in iX_ChunkInfo form
**   into some format and save that converted data into 'name' file.
**   'nodeinfo' can be used e.g. to obtain the background filename etc.
**   'control' is the pointer to iX_Database structure. This structure
**   can be used to obtain miscellaneous information about current
**   document like its header or if wordwrapping is enabled etc. 
**   If everything was ok the function should return 0 otherwise 1.
*/

#define EXNAME "HTML"

STRPTR __saveds __asm EXF_GetName(register __a0 APTR future)
{
 STRPTR name;
 
 name = AllocVec(strlen(EXNAME)+1,MEMF_CLEAR|MEMF_PUBLIC);
 strcpy(name,EXNAME);
 
 return(name);
}

LONG __saveds __asm EXF_Export(register __a0 struct iX_ChunkInfo *data,
                               register __a1 STRPTR name,
                               register __a2 struct iX_Node *nodeinfo,
                               register __a3 APTR control)
{
 BPTR f;
 long e_c = 0L,cn;
 char *head = "<HTML>\n"
              "<HEAD>\n"
              "<META NAME=\"Translator\" "
              "CONTENT=\"HTML iX Export Module V15.30, "
              "Copyright (c) 1997, Ivan Sturlic\">\n"
              "<TITLE>";
 char *head2 = "</TITLE>\n"
               "</HEAD>\n"
               "<BODY";
 char *tail = "</BODY>\n</HTML>\n";
 //char tmp[2048];
 struct iX_Database *ixd = (struct iX_Database *)control;
 struct Alias *al;
 BOOL bytebybyte,colset=FALSE;
 char lastfont[10];
 
 lastfont[0]='\0';
 
 if (f = Open(name,MODE_NEWFILE))
  {
   // first write the header of HTML document
   Write(f,head,strlen(head));
   if (nodeinfo->title) Write(f,nodeinfo->title,strlen(nodeinfo->title));
   Write(f,head2,strlen(head2));
   if (nodeinfo->background)
    {
     if (*nodeinfo->background=='#') Write(f," BGCOLOR=\"",10);
     else Write(f," BACKGROUND=\"",13);
     Write(f,nodeinfo->background,strlen(nodeinfo->background));
     Write(f,"\"",1);
    }
   Write(f,">\n",2);
   // convert
   if (!ixd->WordWrap) Write(f,"<PRE>",5);
   while(e(e_c) != ET_END)
    {
     switch(e(e_c))
      {
        case ET_WORD:
         // check if there are any '<' or '>' chars
         bytebybyte=FALSE;
         for(cn=0;cn<el(e_c);cn++)
          if ( (*(ed(e_c)+cn)=='<') || (*(ed(e_c)+cn)=='>') ) bytebybyte=TRUE;
         if (bytebybyte)
          {
           for(cn=0;cn<el(e_c);cn++)
            {
             if (*(ed(e_c)+cn)=='<') Write(f,"&lt",3);
             else if (*(ed(e_c)+cn)=='>') Write(f,"&gt",3);
             else Write(f,(APTR)(ed(e_c)+cn),1);
            }
          }
         else Write(f,(APTR)(ed(e_c)),el(e_c));
        break;
        case ET_RTRN:
          Write(f,"\n",1);
        break;
        case ET_SPC:
         if (ep(e_c)<0) Write(f,"\t",1); // this is tab character
         else // spaces
         for (cn=0;cn<el(e_c);cn++) Write(f," ",1);
        break;
        case ET_STAG:
         e_c++;
         switch(e(e_c))
          {
           case ET_FONT:
           break;
           case ET_WORDWRAP:
           break;
          }
        break;
        case ET_TAG:
         e_c++;
         switch(e(e_c))
          {
           case ET_B:
            Write(f,"<B>",3);
           break;
           case ET_UB:
            Write(f,"</B>",4);
           break;
           case ET_I:
            Write(f,"<I>",3);
           break;
           case ET_UI:
            Write(f,"</I>",4);
           break;
           case ET_U:
            Write(f,"<U>",3);
           break;
           case ET_UU:
            Write(f,"</U>",4);
           break;
           case ET_A:
            e_c++;
            if (e(e_c)==ET_ATTR)
             {
              if (strnicmp((char *)(ed(e_c)),"c",1)==0) Write(f,"<CENTER>",8);
              if (strnicmp((char *)(ed(e_c)),"defa",4)==0) Write(f,"</CENTER>",9);
             }
           break;
           case ET_ATTR:
            if (e(e_c+1)==ET_LINK)
             {
              Write(f,"<A HREF=\"",9);
              Write(f,(APTR)(ed(e_c+2)),el(e_c+2));
              Write(f,"\">",2);
              Write(f,(APTR)(ed(e_c)),el(e_c));
              Write(f,"</A>",4);
             }
            if ((e(e_c+1)==ET_PIC) || (e(e_c+1)==ET_PICT))
             {
              Write(f,"<IMG SRC=\"",10);
              // search the alias list for the real filename
              al = ixd->AliasList;
              while(al)
               {
                if (strnicmp((char *)(ed(e_c)),al->alias,el(e_c))==0) break;
                al = al->Next;
               }
              if (al) Write(f,al->realname,strlen(al->realname));
              else Write(f,(APTR)(ed(e_c)),el(e_c));
              Write(f,"\">",2);
             }
           break;
           case ET_HR:
            Write(f,"<HR>",4);
           break;
           case ET_N:
            Write(f,"<BR>",4);
           break;
           case ET_RR:
            Write(f,"<PRE>",5);
           break;
           case ET_URR:
            Write(f,"</PRE>",6);
           break;
           case ET_UL:
            Write(f,"<UL>",4);
           break;
           case ET_LI:
            Write(f,"<LI>",4);
           break;
           case ET_UUL:
            Write(f,"</UL>",5);
           break;
           case ET_CL:
            Write(f,"<TABLE>\n<TR>",12);
           break;
           case ET_UCL:
            Write(f,"</TR>\n</TABLE>",14);
           break;
           case ET_BEGINML:
            Write(f,"<TD>",4);
           break;
           case ET_ENDML:
            Write(f,"</TD>",5);
           break;
           case ET_MFONT:
            e_c++;
            if (lastfont[0]!='\0') {Write(f,"</",2);Write(f,lastfont,strlen(lastfont));Write(f,">",1);lastfont[0]='\0';}
            if (strnicmp((char *)(ed(e_c)),"f0",2)==0)
             {Write(f,"<H6>",4);strcpy(lastfont,"H6");}
            if (strnicmp((char *)(ed(e_c)),"f1",2)==0)
             {Write(f,"<H5>",4);strcpy(lastfont,"H5");}
            if (strnicmp((char *)(ed(e_c)),"f2",2)==0)
             {Write(f,"<H4>",4);strcpy(lastfont,"H4");}
            if (strnicmp((char *)(ed(e_c)),"f3",2)==0)
             {Write(f,"<H3>",4);strcpy(lastfont,"H3");}
            if (strnicmp((char *)(ed(e_c)),"f4",2)==0)
             {Write(f,"<H2>",4);strcpy(lastfont,"H2");}
            if (strnicmp((char *)(ed(e_c)),"f5",2)==0)
             {Write(f,"<H1>",4);strcpy(lastfont,"H1");}
            if (strnicmp((char *)(ed(e_c)),"f6",2)==0)
             {Write(f,"<H1>",4);strcpy(lastfont,"H1");}
            if (strnicmp((char *)(ed(e_c)),"x0",2)==0)
             {Write(f,"<CODE>",6);strcpy(lastfont,"CODE");}
            if (strnicmp((char *)(ed(e_c)),"x1",2)==0)
             {Write(f,"<CODE>",6);strcpy(lastfont,"CODE");}
            if (strnicmp((char *)(ed(e_c)),"x2",2)==0)
             {Write(f,"<CODE>",6);strcpy(lastfont,"CODE");}
            if (strnicmp((char *)(ed(e_c)),"x3",2)==0)
             {Write(f,"<VAR>",5);strcpy(lastfont,"VAR");}
            if (strnicmp((char *)(ed(e_c)),"x4",2)==0)
             {Write(f,"<VAR>",5);strcpy(lastfont,"VAR");}
            if (strnicmp((char *)(ed(e_c)),"x5",2)==0)
             {Write(f,"<VAR>",5);strcpy(lastfont,"VAR");}
            if (strnicmp((char *)(ed(e_c)),"x6",2)==0)
             {Write(f,"<KBD>",5);strcpy(lastfont,"KBD");}
           break;
           case ET_FG:
            e_c++;
            if (colset) {Write(f,"</FONT>",7);colset=FALSE;}
            if (*(ed(e_c))=='#')
             {
              Write(f,"<FONT COLOR=",12);
              Write(f,ed(e_c),el(e_c));
              Write(f,">",1);
              colset=TRUE;
             }
           break;
          } // end switch tag
        break;
      } // end switch entry
     e_c++;
    } // end while entry != ET_END
   if (!ixd->WordWrap) Write(f,"</PRE>",6);
   // write the tail
   Write(f,tail,strlen(tail));
   Close(f);
  } else return(1L);
 return(0L);
}
