#include "latex.h" GHashTable* trata_id_latex(GHashTable* hash,GHashTable* ids,int linha) { GString *str; void* ptr; str=g_hash_table_lookup(hash,g_string_new("id")); if (str) { printf("\\label{%s}",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 _travessia_Latex(Elem* el) { GHashTable* ids; LATEXLF.TaTitulo=1; LATEXLF.Nivel=-1; ids=g_hash_table_new(Gstr2int,Compgstr); travessia_Latex(el,ids); g_hash_table_foreach(ids,ids_error_report,NULL); } void travessia_Latex(Elem* el,GHashTable* ids){ if(el) { if(el->tipo==texto) { GString* z=poe_acentos_Latex(Elem_texto_get(el)); printf("%s",z->str); g_string_free(z,TRUE); } else Tipo_to_Latex(el,ids); travessia_Latex(el->next,ids); } } GString* poe_acentos_Latex(GString* str) { /*****************/ /* ,[cC] */ /* '[aeiouAEIOU] */ /* `[aeiouAEIOU] */ /* ^[aeoAEO] */ /* ~[aeoAEO] */ /*****************/ unsigned int tamanho=0; char* ptr; GString* new=g_string_new(""); ptr=str->str; while(tamanholen) { switch(*ptr) { case 'ã': new=g_string_append(new,"\\~a"); ptr++; break; case 'õ': new=g_string_append(new,"\\~o"); ptr++; break; case 'â': new=g_string_append(new,"\\^a"); ptr++; break; case 'ê': new=g_string_append(new,"\\^e"); ptr++; break; case 'î': new=g_string_append(new,"\\^{\\i}"); ptr++; break; case 'ô': new=g_string_append(new,"\\^o"); ptr++; break; case 'û': new=g_string_append(new,"\\^u"); ptr++; break; case 'á': new=g_string_append(new,"\\'a"); ptr++; break; case 'é': new=g_string_append(new,"\\'e"); ptr++; break; case 'í': new=g_string_append(new,"\\'{\\i}"); ptr++; break; case 'ó': new=g_string_append(new,"\\'o"); ptr++; break; case 'ú': new=g_string_append(new,"\\'u"); ptr++; break; case 'à': new=g_string_append(new,"\\`a"); ptr++; break; case 'è': new=g_string_append(new,"\\`e"); ptr++; break; case 'ì': new=g_string_append(new,"\\`{\\i}"); ptr++; break; case 'ò': new=g_string_append(new,"\\`o"); ptr++; break; case 'ç': new=g_string_append(new,"\\c{c}"); ptr++; break; case 'ù': new=g_string_append(new,"\\`u"); ptr++; break; case ',': ptr++; switch(*ptr) { case 'c': case 'C': new=g_string_append(new,"\\c{"); new=g_string_append_c(new,*ptr); new=g_string_append_c(new,'}'); ptr++; break; default: new=g_string_append_c(new,','); } break; case '\'': ptr++; switch(*ptr) { case 'a': case 'e': case 'o': case 'u': case 'A': case 'E': case 'O': case 'U': new=g_string_append(new,"\\'"); new=g_string_append_c(new,*ptr); ptr++; break; case 'i': new=g_string_append(new,"\\\'\\"); new=g_string_append_c(new,*ptr); new=g_string_append(new,"{}"); ptr++; break; case 'I': new=g_string_append(new,"\\\'"); new=g_string_append_c(new,*ptr); ptr++; break; default: new=g_string_append_c(new,'\''); } break; case '`': ptr++; switch(*ptr) { case 'a': case 'e': case 'o': case 'u': case 'A': case 'E': case 'O': case 'U': new=g_string_append(new,"\\`"); new=g_string_append_c(new,*ptr); ptr++; break; case 'i': new=g_string_append(new,"\\`\\"); new=g_string_append_c(new,*ptr); new=g_string_append(new,"{}"); ptr++; break; case 'I': new=g_string_append(new,"\\`"); new=g_string_append_c(new,*ptr); 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(new,"\\^"); new=g_string_append_c(new,*ptr); ptr++; break; default: new=g_string_append(new,"\\^\\ {}"); } break; case '~': ptr++; switch(*ptr) { case 'a': case 'e': case 'o': case 'A': case 'E': case 'O': new=g_string_append(new,"\\~"); new=g_string_append_c(new,*ptr); ptr++; break; default: new=g_string_append(new,"\\~\\ {}"); } break; case '{': new=g_string_append(new,"\\{"); ptr++; break; case '&': if (*(ptr+1)=='l' && *(ptr+2)=='t' && *(ptr+3)==';') { new=g_string_append(new,"{\\tt <}"); ptr+=4; } break; case '}': new=g_string_append(new,"\\}"); ptr++; break; case '\\': new=g_string_append(new,"$\\backslash$"); 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,"{\\tt >}"); ptr++; break; default: new=g_string_append_c(new,*ptr); ptr++; } tamanho++; } return new; } void Tipo_to_Latex(Elem* el,GHashTable* ids){ char* str; int tipo; GHashTable* hash; Elem* filho; tipo=el->tipo; hash=el->hash; filho=el->content; switch(tipo) { case texto: break; case PLI_DOC: printf("\\documentclass{report} \n"); printf("\\usepackage[portuges]{babel}\n"); printf("\\usepackage[dvips]{graphics}\n"); printf("\\usepackage{amssymb}\n"); printf("\\newenvironment{exemplo}{\\parskip 0pt\\noindent\\bf Exemplo:\\hrulefill\\par}\n"); printf("{\\parskip .4cm\\par \\noindent \\hrulefill}\n"); printf("\\parskip .4cm"); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("\\end{document}\n"); break; case ABERTURA: ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); break; case TITULO: printf("\\title{"); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("}\n"); break; case DATA: printf("\\date{"); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("}\n\\author{\n"); break; case AUTOR: ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); if (el->next->tipo==AUTOR) printf("\\and "); break; case NOME: ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); break; case EMAIL: printf("\\\\ {\\it E-mail: "); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("}"); break; case URL: printf("\\\\ {\\it Home page: "); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("}"); break; case RESUMO: printf("}\n\\begin{document}\n\\maketitle\n\\abstract{\n"); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("}\n"); 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("{\\bf "); else if(Compgstr(str,g_string_new("bold"))) str2=g_string_new("{\\bf "); else if(Compgstr(str,g_string_new("italic"))) str2=g_string_new("{\\it "); else if(Compgstr(str,g_string_new("italico"))) str2=g_string_new("{\\it "); else if(Compgstr(str,g_string_new("truetype"))) str2=g_string_new("{\\tt "); else str2=g_string_new("{\\bf "); printf("%s",str2->str); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("}"); g_string_free(str2,TRUE); if (str) g_string_free(str,TRUE); } break; case REF: { GString *str; str=g_hash_table_lookup(hash,g_string_new("url")); if(str) printf("{\\it %s}",str->str); else { str=g_hash_table_lookup(hash,g_string_new("email")); if(str) printf("{\\it %s}",str->str); else { str=g_hash_table_lookup(hash,g_string_new("idref")); if(str) printf("\\ref{%s}",str->str); } } if (str) g_string_free(str,TRUE); ids=trata_id_latex(hash,ids,el->nlinha); } break; case GRAFICO: { GString *str; str=g_hash_table_lookup(hash,g_string_new("local")); printf("\\includegraphics{%s}\n",str->str); g_string_free(str,TRUE); ids=trata_id_latex(hash,ids,el->nlinha); } break; case CODPROGRAMA: ids=trata_id_latex(hash,ids,el->nlinha); printf("\\begin{verbatim}\n"); { GString* s,*n; char* ptr; unsigned int i; s=Elem_texto_get(filho); ptr=s->str; n=g_string_new(""); for(i=0;ilen;i++) { if ((*ptr=='&') && (*(ptr+1)=='l') && (*(ptr+2)=='t') && (*(ptr+3)==';')) { n=g_string_append_c(n,'<'); ptr+=3; } else n=g_string_append_c(n,*ptr); ptr++; } printf("%s",n->str); g_string_free(s,TRUE); g_string_free(n,TRUE); } printf("\\end{verbatim}\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("\\begin{enumerate}\n"); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("\\end{enumerate}\n"); } else { printf("\\begin{itemize}\n"); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("\\end{itemize}\n"); } } break; case ITEM: printf("\\item "); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); break; case EXEMPLO: { GString* z; z=poe_acentos_Latex(Elem_texto_get(el->content->content)); printf("\\begin{exemplo}{%s}\n",z->str); g_string_free(z,TRUE); } ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho->next,ids); printf("\\end{exemplo}\n"); break; case FECHO: printf("\\appendix\n"); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("\\parskip 0pt"); printf("\\tableofcontents\n"); break; case AGRADECIMENTOS: printf("\\chapter{Agradecimentos}"); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); break; case BIBLIOGRAFIA: printf("\\begin{thebibliography}{99}\n"); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("\\end{thebibliography}\n"); break; case BIBITEM: ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); break; case CHAVE: ids=trata_id_latex(hash,ids,el->nlinha); printf("\\bibitem{"); travessia_Latex(filho,ids); printf("}"); break; case CORPO: ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); break; case CAPITULO: { GString* z; z=poe_acentos_Latex(Elem_texto_get(el->content->content)); printf("\\chapter{%s}\n",z->str); g_string_free(z,TRUE); } ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(el->content->next,ids); break; case SECCAO: ++LATEXLF.Nivel; { GString* z; int zbr; z=poe_acentos_Latex(Elem_texto_get(el->content->content)); printf("\\"); for(zbr=0;zbrstr); g_string_free(z,TRUE); } ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(el->content->next,ids); --LATEXLF.Nivel; break; case PARA: printf("\n\n\\indent "); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("\n\n"); break; case COMANDO: printf("{\\tt "); ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("}"); break; case PUB: ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho,ids); printf("\n"); break; case FIGURA: printf("\\begin{figure}[ht]\n"); printf("\\begin{center}\n"); { GString* z; z=poe_acentos_Latex(Elem_texto_get(el->content->content)); printf("\\caption{%s}\n",z->str); g_string_free(z,TRUE); } ids=trata_id_latex(hash,ids,el->nlinha); travessia_Latex(filho->next,ids); printf("\\end{center}\n"); printf("\\end{figure}\n"); break; default: } }