/*   -*- coding: utf-8 -*-  */
/******************************************************************************
*
*               Module   : Atilf
*               Fichier  : TlfiToWicri.c
*               Auteur   : J. DUCLOY
******************************************************************************/
#include "SxmlNode.h"
#include "Buffer.h"
#include "StrDict.h"

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */

StrDict *TlfAbrToAuthor;

Buffer *indent;

void analRoot(SxmlNode *root);

void notYet(SxmlNode *item)
{
  printf ("**<<**");
  SxmlPrint(item);
  printf ("**>>**");
}

void classCtitre (SxmlNode *span)
{
  SxmlNode *s2;
  SxmlReset(span);
  while ((s2=SxmlNextNode(span)))
    {
      if (SxmlIsElement(s2)) 
	{ 
	  if (SxmlNodeHasName(s2, "i"))
	    {
	      printf ("\'\'%s\'\'", SxmlLeafText(s2));
	      continue;
	    }
	  else
	    { 
	      notYet(s2);
	      continue;
	    }
	}
      else if (SxmlIsText(s2))
	{
	  printf("%s", SxmlNodeValue(s2));
	  continue;
	}
      notYet(s2);
    }
}

void classCauteurPrint(char *t1)
{
  char *nameDict;
  nameDict=StrDictSearch(TlfAbrToAuthor, t1);
  printf("{{Petites capitales|");
  if (nameDict) printf("[[A pour auteur cité::%s|%s]]", nameDict, t1);
  else printf("[[%s]]", t1);
  printf("}}");
}

void classCauteurSmallCaps (SxmlNode *s2)
{
  char *t1;
  int flagComma;

  flagComma=0;
  t1=SxmlLeafText(s2);
  if (t1[strlen(t1)-1]==',')
    {
      static Buffer *bufTerm=NULL;
      if (!bufTerm)bufTerm=NewBuffer();
      BufferStrcpy(bufTerm, t1);
      BufferTailCut(bufTerm,1);
      t1=BufferString(bufTerm);
      flagComma=1;
    }
  classCauteurPrint(t1);
  if (flagComma) printf(", ");
 }

void classCauteur (SxmlNode *span)
{
  SxmlNode *s2;
  s2=SxmlFirstChild(span);
  if (!s2) {notYet(span); return;}
  if (SxmlIsElement(s2))
    {
      if (SxmlNodeHasName(s2, "span") && (SxmlHasAttribute(s2, "class" ,"tlf_smallcaps")))
	{
	  classCauteurSmallCaps (s2);
	}
      else  {notYet(s2); return;}
    }
  else        /*  exemple CL. Bernard */
    {
      SxmlNode *s3;
      if (SxmlIsText(s2) && ((s3=SxmlNextSibling(s2))) && SxmlNodeHasName(s3, "span") && (SxmlHasAttribute(s3, "class" ,"tlf_smallcaps")))
	{
	  char *t1;
	  t1=SxmlLeafText(s3);
	  static Buffer *bufTerm=NULL;
	  if (!bufTerm)bufTerm=NewBuffer();
	  BufferStrcpy(bufTerm, SxmlNodeValue(s2));
	  BufferStrcat(bufTerm, t1);
	  classCauteurPrint(BufferString (bufTerm));
	}
      else  {notYet(span); return;}
    
    }
}

void classExemple (SxmlNode *span)
{
  SxmlNode *s1;
  SxmlPrint(span);
  printf ("\n");
  printf ("*");
  SxmlReset(span);
  while ((s1=SxmlNextNode(span)))
    {
      if (SxmlIsElement(s1)) 
	{ 
	  if (SxmlNodeHasName(s1, "i"))
	    {
	      printf ("\'\'%s\'\'", SxmlLeafText(s1));
	      continue;
	    }
	  else if (SxmlNodeHasName(s1, "span"))
	    {
	      if (SxmlHasAttribute(s1, "class", "tlf_cauteur"))
		{
		  classCauteur (s1);
		  continue;
		}
	      else if (SxmlHasAttribute(s1, "class", "tlf_ctitre"))
		{
		  classCtitre (s1);
		  continue;
		}
	      else if (SxmlHasAttribute(s1, "class", "tlf_cdate"))
		{
		  if (SxmlIsLeaf(s1))
		    {
		      printf("%s", SxmlLeafText(s1));
		      continue;
		    }
		}
	    }
	}
      else if (SxmlIsText(s1)) 
	{
	  printf("%s", SxmlNodeValue(s1));
	  continue;
	}
      notYet(s1);
    }
  printf ("\n");
}

void analSpan(SxmlNode *span)
{
  char *class;
  class=SxmlGetAttribute(span, "class");
  if (strcmp(class, "tlf_cdefinition")==0)
    {
      printf(":%s\n", SxmlLeafText(span));
    }
  else if  (strcmp(class, "tlf_smallcaps")==0)
    {
      printf("{{Petites capitales|%s}}",  SxmlLeafText(span));
    }
  else if  (strcmp(class, "tlf_cexemple")==0)
    {
      classExemple(span);
    }
}

void tlfParah(SxmlNode *div)
{

  BufferStrcat(indent, ":");
  BufferTailCut(indent, 1);
}

void analDiv(SxmlNode *div)
{
  char *id;
  id=SxmlGetAttribute(div, "id");
  if (id)
    {
      if (strcmp(id, "wrap")==0)
	{
	  SxmlNode *elem;
	  SxmlReset (div);
	  while ((elem=SxmlNextNode(div))) analRoot(elem);
	}
      else if (strcmp(id, "header")==0) return;
      else if (strcmp(id, "nav")==0) return;
      else if (strcmp(id, "main_content")==0)  /* {SxmlPrint (div); putchar('\n'); return;} */
	{
	  SxmlNode *content;
	  content= SxmlGetFirstChildTagAtt  (div, "div", "id" ,"content");
	  if (content)  analDiv(content);
	  return;
	}
      else if (strcmp(id, "content")==0) /* {SxmlPrint (div); putchar('\n'); return;} */
	{
	  SxmlNode *table;
	  SxmlNode *tr1;
	  SxmlNode *td1;
	  SxmlNode *td2;
	  SxmlNode *div2;
	  if ((table=SxmlGetFirstChildByTagName(div, "table")))
	    {
	      if ((tr1=SxmlGetFirstChildByTagName(table, "tr")))
		{
		  if ((td1=SxmlGetFirstChildByTagName(tr1, "td")))
		    {
		      if ((td2=SxmlGetNextSiblingByTagName(td1, "td"))) 
			{
			  if ((div2=SxmlGetFirstChildTagAtt(td2, "div", "id", "contentbox")))
			    /*	{SxmlPrint (div2); putchar('\n'); return;} */
			    {
			      SxmlNode *item;
			      SxmlReset (div2);
			      while ((item=SxmlNextNode(div2))) analRoot(item);
			    }
			}
		    }
		}
	    }
	  
	}
      else if (strcmp(id, "lexicontent")==0)
	{
	  SxmlNode *item;
	  SxmlReset (div);
	  while ((item=SxmlNextNode(div))) analRoot(item);
	}
      else if (strncmp(id, "art", 3)==0)
	{
	  SxmlNode *item;
	  printf ("<!-- article %s -->\n", id+3);
	  SxmlReset (div);
	  while ((item=SxmlNextNode(div))) analRoot(item);
	}
      else { printf("*********** id : %s not yet implemented \n", id);
	exit(2);
      }
    }
  else
    {
      char *class;
      class=SxmlGetAttribute(div, "class");
      if (class)
	{
	  if (strcmp(class, "tlf_parah")==0) tlfParah(div);
	  return;
	}
      else
	{
	  printf ("**** div without id\n");
	  {SxmlPrint (div); putchar('\n'); return;}
	}
    }
}

void analRoot(SxmlNode *root)
{
  if(SxmlNodeHasName(root, "html")) 
    {
      SxmlNode *body;
      if ((body=SxmlGetFirstChildByTagName (root, "body"))) analRoot(body);
      printf("*********** pb with html \n");
      exit(2);
    }
  else if (SxmlNodeHasName(root, "body"))
    {
      SxmlNode *elem;
      SxmlReset (root);
      while ((elem=SxmlNextNode(root))) analRoot(elem);
    }
  else if (SxmlNodeHasName(root, "div"))
    {
      analDiv(root);
    }
  else if (SxmlNodeHasName(root, "span"))
    {
      analSpan(root);
    }
  else if (SxmlIsText(root)) printf ("%s\n", SxmlNodeValue(root));
  else { printf("*********** %s not yet implemented \n", SxmlNodeName(root));
    exit(2);
    }
}

int main(int argc, char **argv)
{
  SxmlNode *docInput;
  
  indent=NewBuffer();

  TlfAbrToAuthor=StrDictFromVarPath ("DILIB_ROOT", "Data/Atilf/Tlf/TlfAuteursAbrevToWicri.dict");

  while ((docInput=SxmlInputNextDocumentElement()))
    {
      if (SxmlNodeType(docInput)!=SXML_NODE_ELEMENT) 
	{
	  SxmlNode *sibling;
	  if ((sibling=SxmlNextSibling(docInput))) docInput=sibling;  
	  else docInput=SxmlInputNextDocumentElement();
	}
      if (!docInput) exit (2);
      
      analRoot(docInput);
      
      putchar('\n');
    }
  exit(EXIT_SUCCESS);
}
