📄 htmlout.c
字号:
#include <stdio.h>#include "rtf.h"#include "rtftohtml.h"#include "string.h"static void AppendText();static void PutHex ();static char *MapChar();#define MAXNEST 8/* Destinations can be files or strings. */struct SDestHName = { NULL, 0, 0},HHref = { NULL, 0, 0},Title = { NULL, 0, 0},SkipCol = { NULL, 0, 0},BkName = { NULL, 0, 0},Item = { NULL, 0, 0};/* SSC codes are used in saving the state */#define SSCSPECIAL 0#define SSCSTYLECHG 1#define SSCNCODES 2 /* Set this to number of SSCodes */struct SSData { int Special; int cpos; InStateStack *TheState;};struct SSStruct { struct SDest SD; int alloc; int used; struct SSData *data;} PNSave,FNSave;TMatchRec FNMatch;char * (badstyles[256]); int anyfoot;int anyTOC;int InBody = 0;int PNText = 0;int FNText = 0;int HToCLev = 0;int oToCLev = 0;int ToCidx=0;int ToCText=0;struct FDest { char filename[256]; /* Name of output file */ char wbuff[82]; /* Output buffer */ FILE *outfd; /* Output file - (file destinations only) */ int wrap; /* should we wrap text or not */ int HPTags; /* Highest Paragraph Tag */ int HTTags; /* Highest Text Tag */ int NeedPar; /* Output Paragraph before next text */ int InCol2; /* We are past Column 1 */ int PTidx; /* Paragraph Tag Index */ int TSpecial; /* Special Tag */ int cpos; /* character position */ int TStack[MAXNEST]; /* Stack of tags */} EmptyFD = { "", "",NULL,1, -1, -1, 0, 0, 0,0, 0}, File1 = { "","", NULL,1, -1, -1, 0, 0, 0,0, 0},ToCFile = { "","", NULL,1, -1, -1, 0, 0, 0,0, 0},Footnote = { "","", NULL,1, -1, -1, 0, 0, 0,0, 0};#define ConvertCaps ((TStyle&(styleAllCaps|styleSmallCaps))!=0)int outfuniq = 0;char *LineBuffer = (char *) NULL;FILE *PictFile;int uniqnum = 0;#define MAXOBUF 8096intnextfield(cp, w, ob, adj, EntIsOne)char *cp;int w; char *ob; int adj; int EntIsOne;{ int i,j,br = -1,lp,rp,nb= -1,ow=w; for(i=0;i<w&&cp[i]!='\0';i++){ if(cp[i]==' '||cp[i]=='\t'){ if(nb==i-1)br=i; } else { if(EntIsOne){ if(cp[i]=='&'){ while(cp[i]!=';'&&cp[i]!='\0'){ w++; i++; } } else if(cp[i]=='<'){ while(cp[i]!='>'&&cp[i]!='\0'){ w++; i++; } } } nb=i; } } if(i==0){ ob[0]='\0'; return(0); } if(cp[i]=='\0'&&nb==i-1)br=i; if(br==-1)br=w; if(w!=ow){ /* now subtract off any entities not used */ for(i=br,j=0;i<w&&cp[i]!='\0';i++){ if(cp[i]=='&'){ while(cp[i]!=';'){ j++; i++; } } } w-=j; } switch(adj){ case AdjL: /* left justify - no pad */ lp=0; rp=br; break; case AdjLPad: /* left justify */ lp=0; rp=w; break; case AdjRPad: /* right justify */ lp=w-br; rp=w; break; case AdjCpad: /* center */ lp=(w-br)/2; rp=w; break; } if(rp>=MAXOBUF){ RTFPanic ("Overflowed MAXOBUF too many Entities in a table cell"); } for(i=0;i<lp;i++)ob[i]=' '; for(j=0;j<br;i++,j++)ob[i]=cp[j]; for(;i<rp;i++)ob[i]=' '; ob[i]='\0'; return(br);}voidputwrap( s, OD, wrap)char *s;struct FDest *OD;int wrap;{ int i; int n; char obuff[133]; /* If writing to a table, just save up the text */ if(!wrap){ if(OD->cpos>0){ OD->wbuff[OD->cpos]='\0'; fputs(OD->wbuff,OD->outfd); fputc('\n',OD->outfd); } fputs(s,OD->outfd); OD->cpos= -1; return; } i=OD->cpos; while( *s != '\0'){ if(i==81){ n=nextfield(OD->wbuff, 80, obuff, AdjL,0); fputs(obuff,OD->outfd); if(n<80){ /* only if we have a break */ fputc('\n',OD->outfd); for(;n<81;n++) if(OD->wbuff[n]!=' '&&OD->wbuff[n]!='\t') break; for(i=0;n<81;i++,n++) OD->wbuff[i]=OD->wbuff[n]; } else { /* force break at next opportunity */ i= -1; } } if(i<0){ if(strchr(" \t\n",*s)!=NULL){ /*found break */ fputc('\n',OD->outfd); i=0; s++; } else{ fputc(*(s++),OD->outfd); } } else if(*s=='\n'){ OD->wbuff[i]='\0'; fputs(OD->wbuff,OD->outfd); fputc(*(s++),OD->outfd); i=0; continue; } else OD->wbuff[i++] = *(s++); } OD->cpos=i;}HTMLInit(){ int i; if (LineBuffer == (char *) NULL && (LineBuffer = RTFAlloc (512)) == (char *) NULL) RTFPanic ("cannot allocate line buffer."); for(i=0;i<256;i++) badstyles[i]=NULL; HName.used=0; HHref.used=0; Title.used=0; Item.used=0; SkipCol.used=0; File1=EmptyFD; Footnote=EmptyFD; ToCFile=EmptyFD; outfuniq=0; uniqnum=0; ToCidx=1; ToCText=0; anyfoot=0; anyTOC=0; InBody = 0; HToCLev=0; oToCLev = 0; for(i=0;i<PNSave.used;i++){ if(PNSave.data[i].TheState){ RTFFree(PNSave.data[i].TheState); } } PNSave.SD.used=0; PNSave.used=0; for(i=0;i<FNSave.used;i++){ if(FNSave.data[i].TheState){ RTFFree(FNSave.data[i].TheState); } } FNSave.SD.used=0; FNSave.used=0; FNMatch.Font= "NotAFont"; strcpy(File1.filename, OutfileName); if (OutfileName[0] != '\0') { File1.outfd = OpenOutputFile(OutfileName, "w",FTTEXT); if (File1.outfd == NULL) { RTFPanic ("Open of %s Failed", OutfileName); } } else { File1.outfd = stdout; } /* open a file for footnotes */ sprintf(Footnote.filename, "%s_fn.html", FPrefix); Footnote.outfd = OpenOutputFile(Footnote.filename, "w",FTTEXT); if (Footnote.outfd == NULL) { RTFPanic ("Open of %s Failed", Footnote.filename); } if(ToC){ /* open a file for Table of Contents */ sprintf(ToCFile.filename, "%s_ToC.html", FPrefix); ToCFile.outfd = OpenOutputFile(ToCFile.filename, "w",FTTEXT); if (ToCFile.outfd == NULL) { RTFPanic ("Open of %s Failed", ToCFile.filename); } putwrap("<html><head>", &ToCFile,ToCFile.wrap); putwrap("<!-- This document was created from RTF source by rtftohtml version ", &ToCFile,ToCFile.wrap); putwrap(PVers, &ToCFile,ToCFile.wrap); putwrap(" -->", &ToCFile,ToCFile.wrap); putwrap("</head><body>", &ToCFile,ToCFile.wrap); } putwrap("<html><head>", &File1,File1.wrap); putwrap("<!-- This document was created from RTF source by rtftohtml version ", &File1,File1.wrap); putwrap(PVers, &File1,File1.wrap); putwrap(" -->", &File1,File1.wrap); putwrap("<html><head>", &Footnote,Footnote.wrap); putwrap("<!-- This document was created from RTF source by rtftohtml version ", &Footnote,Footnote.wrap); putwrap(PVers, &Footnote,Footnote.wrap); putwrap(" -->", &Footnote,Footnote.wrap); putwrap("</head><body>", &Footnote,Footnote.wrap);}static voidTestStyle();HTMLCleanup(){ char buff[256]; /* Fake some plain text so that test-style will undo all current markup. */ TFont="--Not a Font--"; TStyle=0; TSize=9999; ParStyle=PMatchArr[0].PStyle; TestStyle(&File1); if(anyfoot){ /* We have a footnote */ putwrap("</body></html>", &Footnote,0); fclose(Footnote.outfd); } else { fclose(Footnote.outfd); remove (Footnote.filename); } if(ToC){ if(anyTOC){ /* We have a Table of Contents */ while(oToCLev>0){ putwrap("</ol>",&ToCFile,0); oToCLev--; } putwrap("</body></html>", &ToCFile,0); fclose(ToCFile.outfd); } else { fclose(ToCFile.outfd); remove (ToCFile.filename); } } putwrap("</body></html>", &File1,0); if (File1.outfd != stdout) fclose(File1.outfd);}HTMLStartDivert(){ struct FDest *OD; OD = &File1; switch (destination) { case rtfObjResult: if(linkself&&Item.used>0&&!inTable){ sprintf(LineBuffer,"<a href=\"#%s\">",Item.ptr); putwrap(LineBuffer,OD,0); Item.used=0; } break; }}int BkNum=0;HTMLEndDivert(){ struct FDest *OD; OD = &File1; if (ISS->destination != destination) { switch (destination) { case rtfPict: if(PictFile)fclose(PictFile); RTFSetClassCallback(rtfText, PutHTML); break; case rtfFootnote: Footnote.NeedPar = 1; Footnote.InCol2 = 0; break; case rtfObjResult: if(linkself){putwrap("</a>",OD,OD->wrap);} break; case rtfBookmarkStart: if(BkName.used>0){ sprintf(LineBuffer,"<a name=\"%s\">",BkName.ptr); putwrap(LineBuffer,OD,0); BkName.used=0; }else { sprintf(LineBuffer,"<a name=\"RTFtohtmlxx%d\">",BkNum++); putwrap(LineBuffer,OD,OD->wrap); } break; case rtfBookmarkEnd: putwrap("</a>",OD,OD->wrap); break; } } if(ISS->ToCLev != ToCLev&&ToC&&ToCLev>0){ putwrap("</a>",&ToCFile,0); putwrap("</a>",OD,0); ToCText=0; }}int xval=0;int charnum = 0;extern int csTop;voidReadPict(){ int mval; int level=0; int savecsTop; if(csTop>0)savecsTop=csTop-1; else savecsTop=0; while (1) { if (rtfClass == rtfEOF) break; if (rtfClass == rtfGroup) { if (rtfMajor == rtfBeginGroup) ++level; else if (rtfMajor == rtfEndGroup) { if (--level < 1) break; /* end of initial group */ } } else { if(rtfMajor != '\n' && rtfMajor != '\r' && rtfMajor != '\0'){ if(rtfMajor <= '9'){ mval=rtfMajor-'0'; } else if(rtfMajor <= 'F'){ mval=rtfMajor-'A'+10; } else { mval=rtfMajor-'a'+10; } if (++charnum == 2) { xval=xval*16+mval; fputc((char) xval, PictFile); charnum = 0; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -