File: pass1htm.cc

package info (click to toggle)
wp2latex 3.112%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,916 kB
  • sloc: cpp: 49,708; ansic: 9,318; asm: 5,429; makefile: 542; sh: 22
file content (926 lines) | stat: -rw-r--r-- 24,108 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
/******************************************************************************
 * program:     wp2latex                                                      *
 * function:    module for conversion HTML files into LaTeX 		      *
 * modul:       pass1htm.cc                                                   *
 * description: This module contains parser for HTML documents. It could be   *
 *		optionally compiled with WP2LaTeX package.		      *
 * licency:     GPL		                                              *
 ******************************************************************************/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include<stringa.h>
#include<lists.h>
#include<dbllist.h>

#if defined(__UNIX__)||defined(__DJGPP__)
 #include <unistd.h>
#endif

#include"wp2latex.h"
#include"pass1xml.h"
#include "cp_lib/cptran.h"
#include "cp_lib/out_dir/html.trn"


list HTMLChars(Table_html,sizeof(Table_html)/sizeof(char *));

#define HTMLVersion "0.29"


/** Base class for HTML document conversion. */
class TconvertedPass1_HTML: public TconvertedPass1_XML
{    
public:
    list UnknownChars;

    virtual int Convert_first_pass(void);

    void ProcessKeyHTML(void);

    CpTranslator *ConvertUnicode;

protected:
    void ImageHTML(void);
    void IndentHTML(void);
    void ItemizeHTML(void);
    void LanguageHTML(const char *LangStr);
    void MetaHTML(void);
    void ScriptXML(void);
    void TableHTML(void);
    void TitleHTML(void);
    void StyleHTML(void);
};


/*Register translators here*/
TconvertedPass1 *Factory_HTML(void) {return new TconvertedPass1_HTML;}
FFormatTranslator FormatHTML("HTML",Factory_HTML);



typedef enum
{
  TAG_ATTR =	129,
  TAG_xATTR =	130,
  TAG_BR =	131,
  TAG_xP =	133,
  TAG_DD =	140,
  TAG_JUST =	134,
  TAG_H =	135,
  TAG_xH =	136,
  TAG_HR =	139,
  TAG_IMG =	142,
  TAG_xINDENT = 141,
  TAG_ITEMIZE =	144,
  TAG_META =	137,
  TAG_P =	132,
  TAG_SCRIPT =  138,
  TAG_TABLE =	143,
  TAG_HTML =	145,
  TAG_TITLE =	147,
  TAG_xTITLE =	148,
  TAG_STYLE =   149,
  TAG_DIRECTION=150,

  TAG_HSPACE =  200,
  TAG_EXT_CHR = 201,
  TAG_UNKNOWN = 202,
} HTML_TAGS;


typedef struct
{
  const char *String;
  unsigned char Type;
  unsigned char SubType;
} TagStruct;


TagStruct OpenTags[] =
{
  {"B",      TAG_ATTR, 12},	//ATTR on bold
  {"BIG",    TAG_ATTR, 2},	//ATTR on large
  {"BR",     TAG_BR, 0},	//HRt
  {"CENTER", TAG_JUST, 0x82},	//Justification Center
  {"DD",     TAG_DD, 0},	//Indented definition
  {"H1",     TAG_H, 1},		//section level 1
  {"H2",     TAG_H, 2},		//section level 2
  {"H3",     TAG_H, 3},		//section level 3
  {"H4",     TAG_H, 4},		//section level 4
  {"H5",     TAG_H, 5},		//section level 5
  {"H6",     TAG_H, 6},		//section level 6
  {"HR",     TAG_HR, 0},	//horizontal line
  {"HTML",   TAG_HTML, 0},	//markup start
  {"I",      TAG_ATTR, 8},	//ATTR on italic
  {"IMG",    TAG_IMG, 0},	//Image
  {"LI",     TAG_ITEMIZE, 2},	//Start of item
  {"META",   TAG_META, 0},	//meta tag
  {"P",      TAG_P, 0},		//new paragraph
  {"S",      TAG_ATTR, 13},	//ATTR on strike out
  {"SMALL",  TAG_ATTR, 3},	//ATTR on small
  {"SCRIPT", TAG_SCRIPT, 0},	//start SCRIPT
  {"STRONG", TAG_ATTR, 12},	//ATTR on bold
  {"STRIKE", TAG_ATTR, 13},	//ATTR on strike out variant
  {"STYLE",  TAG_STYLE, 0},	//STYLE
  {"SUB",    TAG_ATTR, 6},	//ATTR on subscript
  {"SUP",    TAG_ATTR, 5},	//ATTR on superscript
  {"TABLE",  TAG_TABLE, 0},	//Start of the table
  {"TD",     TAG_TABLE, 4},	//Start of cell
  {"TH",     TAG_TABLE, 6},	//Start of head cell
  {"TITLE",  TAG_TITLE, 0},	//Start of title
  {"TR",     TAG_TABLE, 2},	//Start of row
  {"TT",     TAG_ATTR, 16},	//ATTR on typewriter
  {"U",      TAG_ATTR, 14},	//ATTR on underline
  {"UL",     TAG_ITEMIZE, 0}	//Start of itemize
};


TagStruct CloseTags[] =
{
  {"B",      TAG_xATTR,  12},	//ATTR off bold
  {"BIG",    TAG_xATTR,  2},	//ATTR off large
  {"CENTER", TAG_JUST,  0x81},	//End of center justification
  {"DD",     TAG_xINDENT, 0},	//End of indented definition
  {"DL",     TAG_xINDENT, 0},	//End of definition list
  {"H1",     TAG_xH,  1},	//section off level 1
  {"H2",     TAG_xH,  2},	//section off level 2
  {"H3",     TAG_xH,  3},	//section off level 3
  {"H4",     TAG_xH,  4},	//section off level 4
  {"H5",     TAG_xH,  5},	//section off level 5
  {"H6",     TAG_xH,  6},	//section off level 6
  {"LI",     TAG_ITEMIZE,  3},	//End of item
  {"I",      TAG_xATTR,  8},	//ATTR off italic
  {"P",      TAG_xP, 0},	//end paragraph
  {"S",      TAG_xATTR,  13},	//ATTR off strike out
  {"SMALL",  TAG_xATTR,  3},    //ATTR off small
  {"SCRIPT", TAG_SCRIPT,  1},	//end SCRIPT
  {"STRONG", TAG_xATTR,  12},	//ATTR off bold
  {"STRIKE", TAG_ATTR,  13},	//ATTR on strike out variant
  {"STYLE",  TAG_STYLE, 1},	//STYLE
  {"SUB",    TAG_xATTR,  6},	//ATTR off subscript
  {"SUP",    TAG_xATTR,  5},	//ATTR off superscript
  {"TABLE",  TAG_TABLE,  1},	//End of Table
  {"TD",     TAG_TABLE,  5},	//End of cell
  {"TH",     TAG_TABLE,  7},	//End of head cell
  {"TITLE",  TAG_xTITLE, 0},	//End of title
  {"TR",     TAG_TABLE,  3},	//End of row
  {"TT",     TAG_xATTR,  16},	//ATTR off typewriter
  {"U",      TAG_xATTR,  14},	//ATTR off underline
  {"UL",     TAG_ITEMIZE,  1},	//End of itemize
};


typedef struct
{
  const char* MimeType;
  int TypeLen;
  const char *ImageExt;
} TMimeItem;

// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
const TMimeItem MimeList[] =
{
  {"data:image/bmp;base64", 21, ".bmp"},
  {"data:image/gif;base64", 21, ".gif"},
  {"data:image/jpg;base64", 21, ".jpg"},
  {"data:image/jpeg;base64",22, ".jpg"},
  {"data:image/png;base64", 21, ".png"},
  {"data:image/svg+xml;base64", 25, ".svg"},
  {"data:image/tiff;base64",22, ".tif"},
  {"data:image/tif;base64", 21, ".tif"}
};


static inline bool is_base64(BYTE c) 
{
 return (isalnum(c) || (c == '+') || (c == '/'));
}


// static const char *base64_chars = 
//             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
//             "abcdefghijklmnopqrstuvwxyz"
//             "0123456789+/";

static inline BYTE FindBase64(char ch)
{
  if(ch>='A' && ch<='Z') return ch - 'A';
  if(ch>='a' && ch<='z') return ch - 'a' + 'Z'-'A'+1;
  if(ch>='0' && ch<='9') return ch - '0' + 2*('Z'-'A')+2;
  if(ch=='+') return 2*('Z'-'A')+2 + 10;  
  if(ch=='/') return 2*('Z'-'A')+2 + 11;
return 0xFF;
}


void base64_decode(const char *encoded_string, FILE *FOut) 
{
int in_len = strlen(encoded_string);
int i = 0;
int in_ = 0;
BYTE char_array_4[4], char_array_3[3];

  while(in_len-- && (encoded_string[in_]!='=') && is_base64(encoded_string[in_])) 
    {
    char_array_4[i++] = FindBase64(encoded_string[in_++]);

    if(i==4) 
      {
      char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
      char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
      char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

      fwrite(char_array_3,1,3,FOut);      
      i = 0;
      }
    }

  if(i) 
  {
    memset(char_array_4+i, FindBase64(0), 4-i);    

    char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
    char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
    char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
    
    fwrite(&char_array_3, 1, i-1, FOut);
  }

  return;
}


#define PAPER_SIZE	210.0f
#define PagePixels 800.0f
void TconvertedPass1_HTML::ImageHTML(void)
{
#ifdef DEBUG
  fprintf(log,"\n#ImageHTML() ");fflush(log);
#endif
TBox Box;
int i;
const char *FileName=NULL;
char *str;
unsigned char terminator;
string NewFileName;

  initBox(Box);
  Box.Width = -1; 		// Undefined, use default 100mm
  Box.Image_type=0;		// Image on disk
  Box.AnchorType = 0; 		// 0-Paragraph, 1-Page, 2-Character
  Box.HorizontalPos=2;		// 0-Left, 1-Right, 2-Center, 3-Full

  if((i=TAG_Args IN "src")>=0 || (i=TAG_Args IN "SRC")>=0)
    {
    FileName = TAG_Args.Member(i,1);
    if(FileName!=NULL)
      {
      for(i=0; i<sizeof(MimeList)/sizeof(TMimeItem); i++)
        {
        if(!strncmp(FileName,MimeList[i].MimeType,MimeList[i].TypeLen))
          {
          NewFileName = MergePaths(OutputDir,RelativeFigDir) + GetSomeImgName(MimeList[i].ImageExt);
          FileName += MimeList[i].TypeLen + 1;
          break;
          }
        }      

      if(NewFileName.length()>0)
        {      
        FILE *F = fopen(NewFileName(),"wb");
        if(F)
          {
          base64_decode(FileName, F);
          fclose(F);
          }
        else
          {
          if(err != NULL)
            {
            perc.Hide();
	    fprintf(err,_("\nError: Cannot create image file: \"%s\"."),NewFileName());
            }
          }
        FileName = NewFileName();
        }
      }
    }

  if((i=TAG_Args IN "width")>=0 || (i=TAG_Args IN "WIDTH")>=0)
	{
	str=TAG_Args.Member(i,1);
	switch(sscanf(str,"%f%c",&Box.Width,&terminator))
	  {
	  case 0: terminator=255;break;
	  case 1: terminator=0;break;
	  case 2: break;
	  default:terminator=255;break;
	  }
	switch(terminator)
	  {
	  case 0:Box.Width *= PAPER_SIZE/PagePixels;
		 break;					//normal
	  case '%':Box.HorizontalPos=4;
		 if(Box.Width<=0) Box.HorizontalPos=3;
		 break;				        //percentage
	  default:break;			        //nothing
	  }
	}
  if(FileName==NULL) FileName="dummy";

  ImageWP(this, FileName, Box);

  if(NewFileName.length()>0 && SaveWPG<0)
    {
    unlink(NewFileName());
    }

return;
}


void TconvertedPass1_HTML::IndentHTML(void)
{
#ifdef DEBUG
  fprintf(log,"\n#IndentHTML() ");fflush(log);
#endif
  tabpos[0] = 470 + WP_sidemargin;
  Indent(this,0);
}


void TconvertedPass1_HTML::ItemizeHTML(void)
{
#ifdef DEBUG
  fprintf(log,"\n#ItemizeHTML() ");fflush(log);
#endif
unsigned char OldFlag;
char OldEnvir;
long FilePos;

  FilePos = ftell(wpd);
  OldFlag = flag;
  OldEnvir = envir;
  flag = Nothing;
  recursion++;

  line_term = 's';   /* Soft return */
  if(char_on_line == LEAVE_ONE_EMPTY_LINE)  // Left one enpty line for new enviroment
	{
	fputc('%', table);fputc('%', strip);
	NewLine(this);
	}
  if(char_on_line>=CHAR_PRESENT) //  if(char_on_line>=-1)
	{
	NewLine(this);
	}
  envir='!';
  fputc('%', table);fputc('%', strip);
  NewLine(this);

  envir = ' ';

  if(OldEnvir=='B') fprintf(strip, "\\vbox{"); //protect itemize inside table
  fprintf(strip, "\\begin{itemize}");

  char_on_line = false;
  nomore_valid_tabs = false;
  rownum++;
  Make_tableentry_attr(this);
  latex_tabpos = 0;

	/*Process all content of the table */
  flag=OldFlag;
  fseek(wpd,FilePos,SEEK_SET);
  ReadXMLTag();
  while(!feof(wpd))
	{
/*	if(by==0 && (subby==10 || subby==13))
		{
		subby=' ';	// remove \n from cell text
		} */
	ProcessKeyHTML();
	if(by==TAG_ITEMIZE)
		{
		if(subby==1) break; /*End of itemize*/
		if(subby==2) fprintf(strip, "\\item ");
		}

	ReadXMLTag();
	}

  fprintf(strip, "\\end{itemize}");
  if(OldEnvir=='B') fprintf(strip, "}");
  if(char_on_line <= FIRST_CHAR_MINIPAGE)  // Left one enpty line for ending enviroment.
	{
	fputc('%', table);fputc('%', strip);
	NewLine(this);
	}
  envir = '^';		//Ignore enviroments after table
  fputc('%', table);
  NewLine(this);
  char_on_line = -10;		// stronger false;

  recursion--;

  flag = OldFlag;
  envir = OldEnvir;
  TAG = "ITEMIZE";
strcpy(ObjType, "Itemize Start");
}


void TconvertedPass1_HTML::LanguageHTML(const char *LangStr)
{
 if(LangStr==NULL) return;
 if(*LangStr==0) return;

 if(!strcmp(LangStr,"en-US"))
 { 
   Language(this,'U'+256*'S');
 }
}


/** This function extracts some information from meta HTML tag */
void TconvertedPass1_HTML::MetaHTML(void)
{
#ifdef DEBUG
  fprintf(log,"\n#MetaHTML() ");fflush(log);
#endif
  int i;
  const char *content, *charset;
  string translator;

  strcpy(ObjType, "Meta");

  if((i=TAG_Args IN "content")>=0 || (i=TAG_Args IN "CONTENT")>=0)
	{
	content=TAG_Args.Member(i,1);
	if(content==NULL) return;
	charset = strstr(content,"CHARSET");
	if(charset==NULL) charset = strstr(content,"charset");
	if(charset==NULL) return;
	charset+=7;
	while(*charset==' ') charset++;
	if(*charset++!='=') return;

	SelectTranslator(charset);
	}
}


void TconvertedPass1_HTML::ScriptXML(void)
{
#ifdef DEBUG
  fprintf(log,"\n#ScriptXML() ");fflush(log);
#endif
unsigned char OldFlag;

OldFlag = flag;
flag = Nothing;

do {
   TAG.erase();
   ProcessKeyHTML();
   if(by==TAG_SCRIPT && subby==1) break;
   }while(!feof(wpd));

flag = OldFlag;
}


void TconvertedPass1_HTML::TableHTML(void)
{
#ifdef DEBUG
  fprintf(log,"\n#TableHTML() ");fflush(log);
#endif
unsigned char OldFlag;
char OldEnvir;
long FilePos;
int i,FieldCount=0;
char State=0;
char *Align;
char *FieldPos=NULL,Alignment;

FilePos = ftell(wpd);
OldFlag = flag;
OldEnvir = envir;
flag = Nothing;
recursion++;

do {
   TAG.erase();
   ProcessKeyHTML();
   if(by==TAG_TABLE)
     {
     if(subby == 0) break;			// <TABLE>
     if(subby == 1) break;			// </TABLE>
     if(subby == 2)				// <TR>
	  {
	  if(State==0) {State++;continue;}
	  if(FieldCount==0) continue;
	  break;
	  }
     if(subby == 3) break;			// </TR>
     if(subby == 4 || subby == 6)	// <TD> or <TH>
	  {
	  if(State==0) State++;
	  Alignment='l';
	  if((i=TAG_Args IN "align")>=0 || (i=TAG_Args IN "ALIGN")>=0)
	      {
	      Align=TAG_Args.Member(i,1);
	      if(!StrCmp(Align,"left"))  Alignment='l';
	      if(!StrCmp(Align,"right")) Alignment='r';
	      if(!StrCmp(Align,"center"))Alignment='c';
	      }
	  if(FieldPos==NULL) FieldPos=(char *)malloc(100);
	  if(FieldCount<100 && FieldPos!=NULL)
		{
		FieldPos[FieldCount]=Alignment;
		}
	  FieldCount++;
	  }
     }
   }while(!feof(wpd));


  if(FieldCount>0)
    {
    line_term = 's';   /* Soft return */
    if(char_on_line == LEAVE_ONE_EMPTY_LINE)  // Left one enpty line for new enviroment.
	{
	fputc('%', table);fputc('%', strip);
	NewLine(this);
	}
    if(char_on_line>=CHAR_PRESENT) //  if(char_on_line>=-1)
	{
	NewLine(this);
	}
    envir='!';
    fputc('%', table);fputc('%', strip);
    NewLine(this);

    envir = 'B';

    fprintf(strip, "{|");
    for (i = 0; i < FieldCount; i++)
	{
	Alignment='l';
	if(FieldCount<100 && FieldPos!=NULL) Alignment=FieldPos[i];
	fprintf(strip, "%c|",Alignment);
	}
    putc('}', strip);

    char_on_line = false;
    nomore_valid_tabs = false;
    rownum++;
    Make_tableentry_attr(this);
    latex_tabpos = 0;
    }

	/*Process all content of the table */
  flag=OldFlag;
  fseek(wpd,FilePos,SEEK_SET);
  ReadXMLTag();
  while(!feof(wpd))
	{
	if(by==0 && (subby==10 || subby==13))
		{
		subby=' ';	/* remove \n from cell text */
		}
	ProcessKeyHTML();
	if(by==TAG_TABLE && subby==1) break; /*End of table*/

	ReadXMLTag();
	}
  if(char_on_line <= FIRST_CHAR_MINIPAGE)  // Left one enpty line for ending enviroment.
	{
	fputc('%', table);fputc('%', strip);
	NewLine(this);
	}
  envir='^';		//Ignore enviroments after table
  fputc('%', table);
  NewLine(this);
  char_on_line = FIRST_CHAR_MINIPAGE;	// stronger false;

  recursion--;

  flag = OldFlag;
  envir = OldEnvir;
  TAG = "TABLE";
strcpy(ObjType, "Table Start");
}


void TconvertedPass1_HTML::StyleHTML(void)
{
#ifdef DEBUG
  fprintf(log,"\n#StyleHTML() ");fflush(log);
#endif
unsigned char OldFlag = flag;
 
 flag = Nothing;

 do {
    TAG.erase();
    ProcessKeyHTML();
    if(by==TAG_STYLE && subby==1) break;
    } while(!feof(wpd));

 //if(TotalSummary>0) NewLine(this);
 flag = OldFlag;
 strcpy(ObjType, "Style");
}


void TconvertedPass1_HTML::TitleHTML(void)
{
#ifdef DEBUG
  fprintf(log,"\n#TitleHTML() ");fflush(log);
#endif
unsigned char OldFlag = flag;
 
 flag = HeaderText;

 fprintf(strip, "\\title{");
 do {
    TAG.erase();
    ProcessKeyHTML();
    if(by==TAG_xTITLE) break;
    } while(!feof(wpd));
 fprintf(strip, "}");

 //if(TotalSummary>0) NewLine(this);
 flag = OldFlag;
 strcpy(ObjType, "Title Start");
}


void TconvertedPass1_HTML::ProcessKeyHTML(void)
{
#ifdef DEBUG
  fprintf(log,"\n#ProcessKeyHTML() ");fflush(log);
#endif
 string TAG;
 const char *tag = NULL;
 BYTE by, subby;
 int i;

 *ObjType = 0;
 if(this->TAG.isEmpty()) ReadXMLTag();
 by = this->by;
 subby = this->subby;

 switch(by)
	{
	case XML_char:
              switch(this->subby)
	      {
	        case 10:
	        case 13:by=128;break; //CR
	        case  9:strcpy(this->ObjType, "!Tab");
	        case 32:by=32; break;  //Space
                default: if(RequiredFont==FONT_HEBREW)
		        {
		          if(by=='.')
                            {TAG="\\textrm{.}";by=XML_extchar;break;}
                          if(by==',')
                            {TAG="\\textrm{,}";by=XML_extchar;break;}
		          if(by==':')
                            {TAG="\\textrm{:}";by=XML_extchar;break;}
                          if(by==';')
                            {TAG="\\textrm{;}";by=XML_extchar;break;}
                        }
                        if(Font==FONT_HEBREW || Font==FONT_CYRILLIC)
                        {
                          RequiredFont = FONT_NORMAL;
                        }
                        break;
              }
	      break;					//Normal character

	case XML_extchar:
	case XML_badextchar:
            TAG = copy(this->TAG,1,length(this->TAG)-2);   //Extended chatacter &xxx;
            tag = TAG();
            if(TAG.length()<=0) break;	    
	    if(TAG=="nbsp")	{by=TAG_HSPACE;break;}  	//Hard space
            if(TAG=="lrm")	{by=TAG_DIRECTION;break;}

            if(*tag=='#' && isdigit(tag[1]))
                {
                by = TAG_EXT_CHR;
                i = atoi(tag+1);
		tag = Ext_chr_str(i, this, ConvertUnicode);
                break;
                }

	    if((i=(TAG() IN HTMLChars))>0)
		{
		i--;
                by = TAG_EXT_CHR;
		tag = Ext_chr_str(i, this, ConvertHTML); // Translate HTML character set to internal charset.
		}
             else
                {
                by = TAG_UNKNOWN;
                if(!(TAG() IN this->UnknownChars))
                  {
                  this->UnknownChars += TAG();
                  if(err != NULL)
                    {
                    perc.Hide();
	            fprintf(err,_("\nError: Unknown translation for character \"&%s;\"."),TAG());
                    }
                  }
                }
//	       fprintf(strip," !!!&%s; ",TAG.ch);
	     break;

	case XML_tag:
               TAG = copy(this->TAG,1,length(this->TAG)-2);	//Normal tag <xxx>
	       if(TAG.length() <= 0) break;
               for(i=0; i<sizeof(OpenTags)/sizeof(TagStruct); i++)
                 {
                 if(TAG==OpenTags[i].String)
                   {
                     by = OpenTags[i].Type;
                     subby = OpenTags[i].SubType;
                     break;
                   }
                 }
	       break;

	case XML_closetag:
               TAG = copy(this->TAG,2,length(this->TAG)-3);	//Closing tag </xxx>
	       if(TAG.length() <= 0) break;
               for(i=0; i<sizeof(CloseTags)/sizeof(TagStruct); i++)
                 {
                 if(TAG==CloseTags[i].String)
                   {
                     by = CloseTags[i].Type;
                     subby = CloseTags[i].SubType;
                     break;
                   }
                 }
	       break;

	case XML_comment:  			//comment
	       break;
        case XML_CDATA:
               break;
	}

  this->by = by;
  this->subby = subby;
  if(this->flag<Nothing)
    switch(by)
	{
	case XML_char:  tag = Ext_chr_str(subby,this,ConvertCpg);
	                CharacterStr(this,tag);
                        break;		//Normal character
        case XML_CDATA:
	case XML_comment:CommentXML();
                        break;
	case XML_unicode:CharacterStr(this,this->TAG);
	                break;		//Already expanded unicode character

	case 32:        putc(' ', strip);   /*soft space*/
		        break;

	case 128:       if(TablePos!=1 && TablePos!=3)
		          if(char_on_line)
                            SoftReturn(this);
                        break;
	case TAG_ATTR:	AttrOn(attr,subby); break;
	case TAG_xATTR:	AttrOff(this,subby);     break;
	case TAG_BR:	HardReturn(this);        break;
	case TAG_P:	if(char_on_line) HardReturn(this);  //Paragraph on
			SoftReturn(this);
			break;
	case TAG_xP:	if(char_on_line) HardReturn(this);  //Paragraph off
			break;
	case TAG_JUST:	Justification(this,subby);break;
	case TAG_H:	StartSection(this,-subby);break;
	case TAG_xH:	EndSection(this,-subby);  break;
	case TAG_META:	MetaHTML();		  break;
	case TAG_SCRIPT:ScriptXML();		  break;
	case TAG_HR:	HLine(this,-16);	  break;
	case TAG_DD:	IndentHTML();	break;
        case TAG_DIRECTION: strcpy(ObjType, "!Direction"); break;
	case TAG_xINDENT:End_of_indent(this);	break;
	case TAG_IMG:	ImageHTML();		break;
	case TAG_TABLE: switch(subby)
		   {
		   case 0:TableHTML(); TablePos=1;break;
		   case 1:EndTable(this); TablePos=0; break;
		   case 2:RowTable(this); TablePos|=2;break;
		   case 3:TablePos&=~2;break;
		   case 4:
		   case 6:CellTable(this);TablePos|=4;break;
		   case 5:
		   case 7:TablePos&=~4;break;
		   }
		 break;
	case TAG_ITEMIZE: switch(subby)
		   {
		   case 0:ItemizeHTML();break;
		   }
		 break;
	case TAG_HTML:
		{
		if((i=TAG_Args IN "lang")>=0 || (i=TAG_Args IN "LANG")>=0)
		  {		  
		  LanguageHTML(TAG_Args.Member(i,1));
		  if(ConvertCpg==NULL) SelectTranslator("iso_8859_1");
		  }

		break;
		}

        case TAG_TITLE: TitleHTML();	break;

        case TAG_STYLE: StyleHTML();	break;

	case TAG_HSPACE:fputc('~', strip); strcpy(ObjType, " ");
		 break;
	case TAG_EXT_CHR:CharacterStr(this,tag);break;
        case TAG_UNKNOWN:fputc(' ', strip);
                 break;
	}


 this->by = by;
 this->subby = subby;
 if(log != NULL)
    {   /**/
    switch(by)
      {
      case 128: fputc('\n',log); break;

      case TAG_HSPACE:
      case ' ': fputc(' ',log); break;

      case TAG_EXT_CHR:
      case XML_char:
 	        if(tag)
	          fprintf(log,"%s",tag);
                else
	          fprintf(log,"!%c",this->subby);
                break;

      case TAG_UNKNOWN:
               fprintf(log,"!%s",this->TAG());
               break;

      default:
	        fprintf(log, _("\n%*s [%s %s]   "),
		        recursion * 2, "", this->TAG(), ObjType);
                //	if(*ObjType==0) UnknownObjects++;
                break;
      }
    }

 ActualPos = ftell(wpd);
}


int TconvertedPass1_HTML::Convert_first_pass(void)
{
#ifdef DEBUG
  fprintf(log,"\n#Convert_pass1_HTML() ");fflush(log);
#endif
DWORD fsize;

  if(Verbosing >= 1)
     printf(_("\n>>>HTML2LaTeX<<< Conversion program: From HTML to LaTeX Version %s\n"
	      "      Made by J.Fojtik  (Hosted on WP2LaTeX :))))\n\n"),
	    HTMLVersion);

  ConvertHTML = GetTranslator("htmlTOinternal");
  ConvertUnicode = GetTranslator("unicodeTOinternal");
  CharReader = &ch_fgetc;

  DocumentStart=ftell(wpd);

  TablePos=0;

  fsize = FileSize(wpd);
  perc.Init(ftell(wpd), fsize,_("First pass HTML:") );

  ActualPos = ftell(wpd);
  while (ActualPos < fsize)
      {
      if(Verbosing >= 1)		//actualise a procentage counter
	      perc.Actualise(ActualPos);

      TAG.erase();
      ProcessKeyHTML();
      }

  Finalise_Conversion(this);
  return(1);
}