1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Converts Latex to obsolete XLP format
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
31 long currentBlockId
= -1;
32 static TexChunk
*descriptionItemArg
= NULL
;
33 static int indentLevel
= 0;
34 static int noColumns
= 0;
35 static int currentTab
= 0;
36 static bool tableVerticalLineLeft
= FALSE
;
37 static bool tableVerticalLineRight
= FALSE
;
38 static bool inTable
= FALSE
;
39 static int citeCount
= 1;
40 wxList
hyperLinks(wxKEY_INTEGER
);
41 wxList
hyperLabels(wxKEY_STRING
);
45 extern wxHashTable TexReferences
;
48 void PadToTab(int tabPos
)
50 int currentCol
= GetCurrentColumn();
51 for (int i
= currentCol
; i
< tabPos
; i
++)
55 static long xlpBlockId
= 0;
61 // Called on start/end of macro examination
62 void XLPOnMacro(int macroId
, int no_args
, bool start
)
69 case ltCHAPTERHEADING
:
77 if (macroId
!= ltCHAPTERSTAR
)
80 SetCurrentOutputs(Contents
, Chapters
);
81 long id1
= NewBlockId();
82 currentBlockId
= NewBlockId();
84 startedSections
= TRUE
;
85 fprintf(Contents
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_HEADING
, id1
);
86 fprintf(Chapters
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
87 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
89 OutputCurrentSection(); // Repeat section header
91 fprintf(Contents
, "}\n\n");
92 fprintf(Chapters
, "}\n\n");
93 SetCurrentOutput(Chapters
);
94 char *topicName
= FindTopicName(GetNextChunk());
95 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
101 case ltSECTIONHEADING
:
109 if (macroId
!= ltSECTIONSTAR
)
112 SetCurrentOutputs(Chapters
, Sections
);
113 long id1
= NewBlockId();
114 currentBlockId
= NewBlockId();
116 startedSections
= TRUE
;
118 if (DocumentStyle
== LATEX_ARTICLE
)
119 fprintf(Contents
, "\\hy-%d{%ld}{", hyBLOCK_LARGE_HEADING
, id1
);
121 fprintf(Chapters
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id1
);
122 fprintf(Sections
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
123 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
125 OutputCurrentSection(); // Repeat section header
127 if (DocumentStyle
== LATEX_ARTICLE
)
128 fprintf(Contents
, "}\n\n");
130 fprintf(Chapters
, "}\n\n");
131 fprintf(Sections
, "}\n\n");
132 SetCurrentOutput(Sections
);
133 char *topicName
= FindTopicName(GetNextChunk());
134 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
139 case ltSUBSECTIONSTAR
:
140 case ltMEMBERSECTION
:
141 case ltFUNCTIONSECTION
:
147 if (macroId
!= ltSUBSECTIONSTAR
)
150 SetCurrentOutputs(Sections
, Subsections
);
151 long id1
= NewBlockId();
152 currentBlockId
= NewBlockId();
153 fprintf(Sections
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id1
);
154 fprintf(Subsections
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
155 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
157 OutputCurrentSection(); // Repeat section header
159 fprintf(Sections
, "}\n\n");
160 fprintf(Subsections
, "}\n\n");
161 SetCurrentOutput(Subsections
);
162 char *topicName
= FindTopicName(GetNextChunk());
163 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
167 case ltSUBSUBSECTION
:
168 case ltSUBSUBSECTIONSTAR
:
172 if (macroId
!= ltSUBSUBSECTIONSTAR
)
175 SetCurrentOutputs(Subsections
, Subsubsections
);
176 long id1
= NewBlockId();
177 currentBlockId
= NewBlockId();
178 fprintf(Subsections
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id1
);
179 fprintf(Subsubsections
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
180 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
182 OutputCurrentSection(); // Repeat section header
184 fprintf(Subsections
, "}\n\n");
185 fprintf(Subsubsections
, "}\n\n");
186 SetCurrentOutput(Subsubsections
);
187 char *topicName
= FindTopicName(GetNextChunk());
188 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
196 SetCurrentOutput(Subsections
);
199 long id
= NewBlockId();
200 fprintf(Subsections
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
203 fprintf(Subsections
, "}");
208 // TexOutput("void", TRUE);
210 case ltBACKSLASHCHAR
:
212 TexOutput("\n", TRUE
);
219 TexOutput("\n", TRUE
);
220 TexOutput("\n", TRUE
);
237 long id
= NewBlockId();
238 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
251 long id
= NewBlockId();
252 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_ITALIC
, id
);
264 long id
= NewBlockId();
265 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_TELETYPE
, id
);
275 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_TEXT
, NewBlockId());
285 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_TEXT
, NewBlockId());
295 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_NORMAL
, NewBlockId());
305 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_HEADING
, NewBlockId());
308 else TexOutput("}\n");
315 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_LARGE_HEADING
, NewBlockId());
318 else TexOutput("}\n");
330 // if (indentLevel > 0)
331 // TexOutput("\\par\\par\n");
334 if (macroId
== ltENUMERATE
)
335 listType
= LATEX_ENUMERATE
;
336 else if (macroId
== ltITEMIZE
)
337 listType
= LATEX_ITEMIZE
;
339 listType
= LATEX_DESCRIPTION
;
340 itemizeStack
.Insert(new ItemizeStruc(listType
));
347 if (itemizeStack
.First())
349 ItemizeStruc
*struc
= (ItemizeStruc
*)itemizeStack
.First()->Data();
351 delete itemizeStack
.First();
358 wxNode
*node
= itemizeStack
.First();
361 ItemizeStruc
*struc
= (ItemizeStruc
*)node
->Data();
364 struc
->currentItem
+= 1;
367 switch (struc
->listType
)
369 case LATEX_ENUMERATE
:
371 sprintf(indentBuf
, "\\hy-%d{%ld}{%d.} ",
372 hyBLOCK_BOLD
, NewBlockId(), struc
->currentItem
);
373 TexOutput(indentBuf
);
378 sprintf(indentBuf
, "\\hy-%d{%ld}{o} ",
379 hyBLOCK_BOLD
, NewBlockId());
380 TexOutput(indentBuf
);
384 case LATEX_DESCRIPTION
:
386 if (descriptionItemArg
)
388 sprintf(indentBuf
, "\\hy-%d{%ld}{",
389 hyBLOCK_BOLD
, NewBlockId());
390 TexOutput(indentBuf
);
391 TraverseChildrenFromChunk(descriptionItemArg
);
393 descriptionItemArg
= NULL
;
404 if (start
&& DocumentTitle
&& DocumentAuthor
)
406 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_LARGE_HEADING
, NewBlockId());
408 TraverseChildrenFromChunk(DocumentTitle
);
410 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_HEADING
, NewBlockId());
412 TraverseChildrenFromChunk(DocumentAuthor
);
416 TraverseChildrenFromChunk(DocumentDate
);
422 case ltTABLEOFCONTENTS
:
426 FILE *fd
= fopen(ContentsName
, "r");
439 TexOutput("RUN TEX2RTF AGAIN FOR CONTENTS PAGE\n");
440 OnInform("Run Tex2RTF again to include contents page.");
448 TexOutput("HARDY", TRUE
);
454 TexOutput("wxCLIPS", TRUE
);
462 long id
= NewBlockId();
463 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_TELETYPE
, id
);
473 TexOutput("\n------------------------------------------------------------------", TRUE
);
481 TexOutput("--------------------------------------------------------------------------------", TRUE
);
485 case ltSPECIALAMPERSAND
:
490 int tabPos
= (80/noColumns
)*currentTab
;
500 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_TELETYPE
, NewBlockId());
507 case ltNUMBEREDBIBITEM
:
510 TexOutput("\n\n", TRUE
);
521 if (DocumentStyle
!= LATEX_ARTICLE
)
522 sprintf(figBuf
, "Figure %d.%d: ", chapterNo
, figureNo
);
524 sprintf(figBuf
, "Figure %d: ", figureNo
);
530 char *topicName
= FindTopicName(GetNextChunk());
532 AddTexRef(topicName
, NULL
, NULL
,
533 ((DocumentStyle
!= LATEX_ARTICLE
) ? chapterNo
: figureNo
),
534 ((DocumentStyle
!= LATEX_ARTICLE
) ? figureNo
: 0));
540 DefaultOnMacro(macroId
, no_args
, start
);
546 bool XLPOnArgument(int macroId
, int arg_no
, bool start
)
553 case ltCHAPTERHEADING
:
556 case ltSECTIONHEADING
:
558 case ltSUBSECTIONSTAR
:
559 case ltSUBSUBSECTION
:
560 case ltSUBSUBSECTIONSTAR
:
562 case ltMEMBERSECTION
:
563 case ltFUNCTIONSECTION
:
565 if (!start
&& (arg_no
== 1))
566 currentSection
= GetArgChunk();
572 if (!start
&& (arg_no
== 1))
573 TexOutput(" ", TRUE
);
574 if (start
&& (arg_no
== 3))
575 TexOutput("(", TRUE
);
576 if (!start
&& (arg_no
== 3))
577 TexOutput(")", TRUE
);
582 if (!start
&& (arg_no
== 1))
583 TexOutput(" ", TRUE
);
585 if (start
&& (arg_no
== 2))
586 TexOutput("(*", TRUE
);
587 if (!start
&& (arg_no
== 2))
588 TexOutput(")", TRUE
);
590 if (start
&& (arg_no
== 3))
591 TexOutput("(", TRUE
);
592 if (!start
&& (arg_no
== 3))
593 TexOutput(")", TRUE
);
598 if (!start
&& (arg_no
== 1))
599 TexOutput(" ", TRUE
);
600 if (start
&& (arg_no
== 2))
602 TexOutput("(", TRUE
);
603 long id
= NewBlockId();
604 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
607 if (!start
&& (arg_no
== 2))
611 if (!start
&& (arg_no
== 3))
612 TexOutput(")", TRUE
);
617 if (start
&& (arg_no
== 2))
619 long id
= NewBlockId();
620 sprintf(buf
, " \\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
623 if (!start
&& (arg_no
== 2))
631 if (start
&& (arg_no
== 2))
633 long id
= NewBlockId();
634 sprintf(buf
, " \\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
637 if (!start
&& (arg_no
== 2))
645 if (!start
&& (arg_no
== 1))
646 TexOutput(" ", TRUE
);
660 char *refName
= GetArgData();
663 TexRef
*texRef
= FindReference(refName
);
666 sec
= texRef
->sectionNumber
;
685 currentBlockId
= NewBlockId();
686 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_RED_ITALIC
, currentBlockId
);
695 char *label
= GetArgData();
696 hyperLinks
.Append(currentBlockId
, (wxObject
*)copystring(label
));
709 else if (arg_no
== 2)
721 if (start
&& IsArgOptional())
723 descriptionItemArg
= GetArgChunk();
736 tableVerticalLineLeft
= FALSE
;
737 tableVerticalLineRight
= FALSE
;
739 char *alignString
= copystring(GetArgData());
741 // Count the number of columns
743 int len
= strlen(alignString
);
746 if (alignString
[0] == '|')
747 tableVerticalLineLeft
= TRUE
;
748 if (alignString
[len
-1] == '|')
749 tableVerticalLineRight
= TRUE
;
752 for (int i
= 0; i
< len
; i
++)
753 if (isalpha(alignString
[i
]))
758 TexOutput("\\brdrt\\brdrs");
759 if (tableVerticalLineLeft)
760 TexOutput("\\brdrl\\brdrs");
761 if (tableVerticalLineRight)
762 TexOutput("\\brdrr\\brdrs");
765 // Calculate a rough size for each column
766 // int tabPos = 80/noColumns;
772 else if (arg_no
== 2 && !start
)
776 else if (arg_no
== 2 && start
)
781 case ltMARGINPAREVEN
:
788 TexOutput("----------------------------------------------------------------------\n", TRUE
);
792 TexOutput("\n----------------------------------------------------------------------\n", TRUE
);
798 if (arg_no
== 1 && start
)
800 char *citeKey
= GetArgData();
801 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
804 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
805 sprintf(buf
, "[%d]", citeCount
);
806 ref
->sectionNumber
= copystring(buf
);
809 sprintf(buf
, "\\hy-%d{%ld}{[%d]} ", hyBLOCK_BOLD
, NewBlockId(), citeCount
);
817 case ltTHEBIBLIOGRAPHY
:
819 if (start
&& (arg_no
== 1))
823 SetCurrentOutput(Chapters
);
825 SetCurrentOutputs(Contents
, Chapters
);
826 long id1
= NewBlockId();
827 long id2
= NewBlockId();
828 fprintf(Contents
, "\\hy-%d{%ld}{%s}\n", hyBLOCK_SMALL_HEADING
, id1
, ReferencesNameString
);
829 fprintf(Chapters
, "\\hy-%d{%ld}{%s}\n\n\n", hyBLOCK_LARGE_VISIBLE_SECTION
, id2
, ReferencesNameString
);
830 fprintf(Index
, "%ld %ld\n", id1
, id2
);
832 SetCurrentOutput(Chapters
);
835 if (!start
&& (arg_no
== 2))
842 case ltTWOCOLITEMRULED
:
844 if (start
&& (arg_no
== 2))
847 if (!start
&& (arg_no
== 2))
859 char *val
= GetArgData();
906 char *val
= GetArgData();
959 char *val
= GetArgData();
1002 case ltACCENT_TILDE
:
1006 char *val
= GetArgData();
1040 case ltACCENT_UMLAUT
:
1044 char *val
= GetArgData();
1097 char *val
= GetArgData();
1116 case ltACCENT_CADILLA
:
1120 char *val
= GetArgData();
1141 return DefaultOnArgument(macroId
, arg_no
, start
);
1152 if (InputFile
&& OutputFile
)
1154 Contents
= fopen(TmpContentsName
, "w");
1155 Chapters
= fopen("chapters.xlp", "w");
1156 Sections
= fopen("sections.xlp", "w");
1157 Subsections
= fopen("subsections.xlp", "w");
1158 Subsubsections
= fopen("subsubsections.xlp", "w");
1159 Index
= fopen("index.xlp", "w");
1161 // Insert invisible section marker at beginning
1162 fprintf(Chapters
, "\\hy-%d{%ld}{%s}\n",
1163 hyBLOCK_INVISIBLE_SECTION
, NewBlockId(), "\n");
1165 fprintf(Contents
, "\\hy-%d{%ld}{%s}\n\n",
1166 // hyBLOCK_LARGE_HEADING, NewBlockId(), "\n\n%s\n\n", ContentsNameString);
1167 hyBLOCK_LARGE_HEADING
, NewBlockId(), ContentsNameString
);
1169 SetCurrentOutput(Chapters
);
1171 fprintf(Index
, "\n\\hyindex{\n\"%s\"\n",
1172 contentsString
? contentsString
: "WXHELPCONTENTS");
1175 wxNode
*node
= hyperLinks
.First();
1178 long from
= node
->GetKeyInteger();
1179 char *label
= (char *)node
->Data();
1180 wxNode
*otherNode
= hyperLabels
.Find(label
);
1183 long to
= (long)otherNode
->Data();
1184 fprintf(Index
, "%ld %ld\n", from
, to
);
1186 node
= node
->Next();
1189 fprintf(Index
, "}\n");
1191 fclose(Contents
); Contents
= NULL
;
1192 fclose(Chapters
); Chapters
= NULL
;
1193 fclose(Sections
); Sections
= NULL
;
1194 fclose(Subsections
); Subsections
= NULL
;
1195 fclose(Subsubsections
); Subsubsections
= NULL
;
1196 fclose(Index
); Index
= NULL
;
1198 if (wxFileExists(ContentsName
)) wxRemoveFile(ContentsName
);
1200 if (!wxRenameFile(TmpContentsName
, ContentsName
))
1202 wxCopyFile(TmpContentsName
, ContentsName
);
1203 wxRemoveFile(TmpContentsName
);
1206 wxConcatFiles("chapters.xlp", "sections.xlp", "tmp2.xlp");
1207 wxConcatFiles("tmp2.xlp", "subsections.xlp", "tmp1.xlp");
1208 wxConcatFiles("tmp1.xlp", "subsubsections.xlp", "tmp2.xlp");
1209 wxConcatFiles("tmp2.xlp", "index.xlp", OutputFile
);
1211 wxRemoveFile("tmp1.xlp");
1212 wxRemoveFile("tmp2.xlp");
1214 wxRemoveFile("chapters.xlp");
1215 wxRemoveFile("sections.xlp");
1216 wxRemoveFile("subsections.xlp");
1217 wxRemoveFile("subsubsections.xlp");
1218 wxRemoveFile("index.xlp");