#include "html.h"
void ids_error_report(gpointer key, gpointer value, gpointer data)
{
int linha;
linha=*(int*)value;
if (linha<0)
g_warning("Linha %d: Referencia a identificador nao declarado: %s",
-linha,((GString*)key)->str);
}
void _travessia_Html(Elem* el) {
GHashTable* ids;
HTMLF.inHEAD=0;
HTMLF.inFECHO=0;
HTMLF.TamTitulo=1;
HTMLF.IndiceCount=0;
ids=g_hash_table_new(Gstr2int,Compgstr);
travessia_Html(el,ids);
g_hash_table_foreach(ids,ids_error_report,NULL);
}
void travessia_Html(Elem* el,GHashTable* ids){
if(el)
{
if(el->tipo==texto)
{
GString* z=poe_acentos(Elem_texto_get(el));
printf("%s\n",z->str);
g_string_free(z,TRUE);
}
else
Tipo_to_Html(el,ids);
travessia_Html(el->next,ids);
}
}
void travessia_Indice(Elem* el){
if(el)
{
if(el->tipo!=texto)
{
if (el->tipo==CAPITULO)
{
GString* z;
z=poe_acentos(Elem_texto_get(el->content->content));
printf("
%s
\n",HTMLF.IndiceCount,z->str);
++HTMLF.IndiceCount;
g_string_free(z,TRUE);
printf("\n");
travessia_Indice(el->content);
printf("
\n");
}
else if (el->tipo==SECCAO)
{
GString* z;
z=poe_acentos(Elem_texto_get(el->content->content));
printf("%s
\n",HTMLF.IndiceCount,z->str);
++HTMLF.IndiceCount;
g_string_free(z,TRUE);
printf("\n");
travessia_Indice(el->content);
printf("
\n");
}
else
travessia_Indice(el->content);
}
travessia_Indice(el->next);
}
}
GString* poe_acentos(GString* str)
{
/*****************/
/* ,[cC] */
/* '[aeiouAEIOU] */
/* `[aeiouAEIOU] */
/* ^[aeoAEO] */
/* ~[aeoAEO] */
/*****************/
int tamanho=0;
char* ptr;
GString* new=g_string_new("");
ptr=str->str;
while(tamanholen) {
switch(*ptr) {
case 'ã':
new=g_string_append(new,"ã");
ptr++;
break;
case 'õ':
new=g_string_append(new,"õ");
ptr++;
break;
case 'â':
new=g_string_append(new,"â");
ptr++;
break;
case 'ê':
new=g_string_append(new,"ê");
ptr++;
break;
case 'î':
new=g_string_append(new,"î");
ptr++;
break;
case 'ô':
new=g_string_append(new,"ô");
ptr++;
break;
case 'û':
new=g_string_append(new,"û");
ptr++;
break;
case 'á':
new=g_string_append(new,"á");
ptr++;
break;
case 'é':
new=g_string_append(new,"é");
ptr++;
break;
case 'í':
new=g_string_append(new,"í");
ptr++;
break;
case 'ó':
new=g_string_append(new,"ó");
ptr++;
break;
case 'ú':
new=g_string_append(new,"ú");
ptr++;
break;
case 'à':
new=g_string_append(new,"à");
ptr++;
break;
case 'è':
new=g_string_append(new,"è");
ptr++;
break;
case 'ì':
new=g_string_append(new,"ì");
ptr++;
break;
case 'ò':
new=g_string_append(new,"ò");
ptr++;
break;
case 'ù':
new=g_string_append(new,"ù");
ptr++;
break;
case ',':
ptr++;
switch(*ptr) {
case 'c':
case 'C':
new=g_string_append_c(new,'&');
new=g_string_append_c(new,*ptr);
new=g_string_append(new,"cedil;");
ptr++;
break;
default:
new=g_string_append_c(new,',');
}
break;
case '\'':
ptr++;
switch(*ptr) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
new=g_string_append_c(new,'&');
new=g_string_append_c(new,*ptr);
new=g_string_append(new,"acute;");
ptr++;
break;
default:
new=g_string_append_c(new,'\'');
}
break;
case '`':
ptr++;
switch(*ptr) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
new=g_string_append_c(new,'&');
new=g_string_append_c(new,*ptr);
new=g_string_append(new,"grave;");
ptr++;
break;
default:
new=g_string_append_c(new,'`');
}
break;
case '^':
ptr++;
switch(*ptr) {
case 'a':
case 'e':
case 'o':
case 'A':
case 'E':
case 'O':
new=g_string_append_c(new,'&');
new=g_string_append_c(new,*ptr);
new=g_string_append(new,"circ;");
ptr++;
break;
default:
new=g_string_append_c(new,'^');
}
break;
case '~':
ptr++;
switch(*ptr) {
case 'a':
case 'e':
case 'o':
case 'A':
case 'E':
case 'O':
new=g_string_append_c(new,'&');
new=g_string_append_c(new,*ptr);
new=g_string_append(new,"tilde;");
ptr++;
break;
default:
new=g_string_append_c(new,'~');
}
break;
default:
new=g_string_append_c(new,*ptr);
ptr++;
}
tamanho++;
}
return new;
}
GHashTable* trata_id_html(GHashTable* hash,GHashTable* ids,int linha)
{
GString *str;
void* ptr;
str=g_hash_table_lookup(hash,g_string_new("id"));
if (str) {
printf("\n",str->str);
if (ptr=g_hash_table_lookup(ids,str))
if ((*(int*)ptr)>0)
g_warning("linha %d: Identificador %s definido anteriormente na linha %d.",
linha-1,str->str, *(int*)ptr -1 );
else
*(int*)ptr=linha;
else
{
int* i;
i=(int*)malloc(sizeof(int));
*i=linha;
g_hash_table_insert(ids,g_string_new(str->str),i);
}
}
return ids;
}
void Tipo_to_Html(Elem* el,GHashTable* ids){
char* str;
unsigned int tipo;
GHashTable* hash;
Elem* filho;
tipo=el->tipo;
hash=el->hash;
filho=el->content;
switch(tipo)
{
case texto:
break;
case PLI_DOC:
printf("\n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("\n");
break;
case ABERTURA:
printf("\n");
ids=trata_id_html(hash,ids,el->nlinha);
HTMLF.inHEAD=1;
travessia_Html(filho,ids);
printf("
Índice\n");
printf("\n");
travessia_Indice((el->next)->content);
HTMLF.IndiceCount=0;
printf("
\n");
break;
case TITULO:
if (HTMLF.inHEAD) {
printf("\n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("\n");
HTMLF.inHEAD=0;
printf("\n");
printf("\n");
printf("\n");
travessia_Html(filho,ids);
printf("
\n");
} else {
printf("\n",(HTMLF.TamTitulo>6)?6:HTMLF.TamTitulo);
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("\n",(HTMLF.TamTitulo>6)?6:HTMLF.TamTitulo);
}
break;
case DATA:
printf("\n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("
\n");
break;
case AUTOR:
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
break;
case NOME:
printf("\n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("
\n");
break;
case EMAIL:
printf("E-mail: \n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("
\n");
break;
case URL:
printf("Home page: \n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("
\n");
break;
case RESUMO:
printf("
Resumo:
\n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
break;
case REALCADO:
{
GString *str, *str2;
str=g_hash_table_lookup(hash,g_string_new("estilo"));
if (str) g_string_down(str);
if(!str) str2=g_string_new("EM");
else if(Compgstr(str,g_string_new("bold")))
str2=g_string_new("B");
else if(Compgstr(str,g_string_new("italic")))
str2=g_string_new("I");
else if(Compgstr(str,g_string_new("truetype")))
str2=g_string_new("TT");
else str2=g_string_new("EM");
printf("<%s>",str2->str);
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("%s>",str2->str);
g_string_free(str2,TRUE);
if (str) g_string_free(str,TRUE);
}
break;
case REF:
{
GString *str;
ids=trata_id_html(hash,ids,el->nlinha);
str=g_hash_table_lookup(hash,g_string_new("url"));
if(str) printf("%s",str->str,str->str);
else {
str=g_hash_table_lookup(hash,g_string_new("email"));
if(str) printf("%s",str->str,str->str);
else {
str=g_hash_table_lookup(hash,g_string_new("idref"));
if(str) {
printf("%s",str->str,str->str);
if (!g_hash_table_lookup(ids,str))
{
int *i;
i=(int*)malloc(sizeof(int));
*i=-el->nlinha;
g_hash_table_insert(ids,g_string_new(str->str),i);
}
}
}
}
if (str) g_string_free(str,TRUE);
}
break;
case GRAFICO:
{
GString *str;
str=g_hash_table_lookup(hash,g_string_new("local"));
printf("
\n",str->str);
g_string_free(str,TRUE);
}
ids=trata_id_html(hash,ids,el->nlinha);
break;
case CODPROGRAMA:
ids=trata_id_html(hash,ids,el->nlinha);
printf("\n");
{
GString* s;
s=Elem_texto_get(filho);
printf("%s",s->str);
g_string_free(s,TRUE);
}
printf("
\n");
break;
case LISTA:
{
GString *str;
str=g_hash_table_lookup(hash,g_string_new("tipo"));
if (str && Compgstr(g_string_down(str),g_string_new("numerada"))) {
printf("\n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("
\n");
} else {
printf("\n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("
\n");
}
}
break;
case ITEM:
printf("\n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("\n");
break;
case EXEMPLO:
ids=trata_id_html(hash,ids,el->nlinha);
printf("Exemplo:");
{
int t;
t=HTMLF.TamTitulo;
HTMLF.TamTitulo=6;
travessia_Html(filho,ids);
HTMLF.TamTitulo=t;
}
break;
case FECHO:
printf("
\n");
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
break;
case AGRADECIMENTOS:
ids=trata_id_html(hash,ids,el->nlinha);
printf("Agradecimentos:\n");
travessia_Html(filho,ids);
break;
case BIBLIOGRAFIA:
ids=trata_id_html(hash,ids,el->nlinha);
printf("Bibliografia:\n");
travessia_Html(filho,ids);
printf("
\n");
break;
case BIBITEM:
ids=trata_id_html(hash,ids,el->nlinha);
printf("\n");
travessia_Html(filho,ids);
printf("\n");
break;
case CHAVE:
ids=trata_id_html(hash,ids,el->nlinha);
printf("[");
travessia_Html(filho,ids);
printf("]");
break;
case CORPO:
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
printf("\n");
break;
case CAPITULO:
printf("",HTMLF.IndiceCount++);
ids=trata_id_html(hash,ids,el->nlinha);
printf("
\n");
travessia_Html(filho,ids);
break;
case SECCAO:
++HTMLF.TamTitulo;
printf("",HTMLF.IndiceCount++);
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
--HTMLF.TamTitulo;
break;
case PARA:
ids=trata_id_html(hash,ids,el->nlinha);
printf("\n");
travessia_Html(filho,ids);
printf("
\n");
break;
case COMANDO:
ids=trata_id_html(hash,ids,el->nlinha);
printf("\n");
travessia_Html(filho,ids);
printf("\n");
break;
case PUB:
ids=trata_id_html(hash,ids,el->nlinha);
travessia_Html(filho,ids);
break;
case FIGURA:
ids=trata_id_html(hash,ids,el->nlinha);
printf("Figura:");
{
int t;
t=HTMLF.TamTitulo;
HTMLF.TamTitulo=6;
travessia_Html(filho,ids);
HTMLF.TamTitulo=t;
}
break;
default:
}
}