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"
30 #if !WXWIN_COMPATIBILITY_2_4
31 static inline wxChar
* copystring(const wxChar
* s
)
32 { return wxStrcpy(new wxChar
[wxStrlen(s
) + 1], s
); }
35 long currentBlockId
= -1;
36 static TexChunk
*descriptionItemArg
= NULL
;
37 static int indentLevel
= 0;
38 static int noColumns
= 0;
39 static int currentTab
= 0;
40 static bool tableVerticalLineLeft
= FALSE
;
41 static bool tableVerticalLineRight
= FALSE
;
42 static bool inTable
= FALSE
;
43 static int citeCount
= 1;
44 wxList
hyperLinks(wxKEY_INTEGER
);
45 wxList
hyperLabels(wxKEY_STRING
);
49 extern wxHashTable TexReferences
;
52 void PadToTab(int tabPos
)
54 int currentCol
= GetCurrentColumn();
55 for (int i
= currentCol
; i
< tabPos
; i
++)
59 static long xlpBlockId
= 0;
65 // Called on start/end of macro examination
66 void XLPOnMacro(int macroId
, int no_args
, bool start
)
73 case ltCHAPTERHEADING
:
81 if (macroId
!= ltCHAPTERSTAR
)
84 SetCurrentOutputs(Contents
, Chapters
);
85 long id1
= NewBlockId();
86 currentBlockId
= NewBlockId();
88 startedSections
= TRUE
;
89 fprintf(Contents
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_HEADING
, id1
);
90 fprintf(Chapters
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
91 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
93 OutputCurrentSection(); // Repeat section header
95 fprintf(Contents
, "}\n\n");
96 fprintf(Chapters
, "}\n\n");
97 SetCurrentOutput(Chapters
);
98 char *topicName
= FindTopicName(GetNextChunk());
99 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
105 case ltSECTIONHEADING
:
113 if (macroId
!= ltSECTIONSTAR
)
116 SetCurrentOutputs(Chapters
, Sections
);
117 long id1
= NewBlockId();
118 currentBlockId
= NewBlockId();
120 startedSections
= TRUE
;
122 if (DocumentStyle
== LATEX_ARTICLE
)
123 fprintf(Contents
, "\\hy-%d{%ld}{", hyBLOCK_LARGE_HEADING
, id1
);
125 fprintf(Chapters
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id1
);
126 fprintf(Sections
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
127 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
129 OutputCurrentSection(); // Repeat section header
131 if (DocumentStyle
== LATEX_ARTICLE
)
132 fprintf(Contents
, "}\n\n");
134 fprintf(Chapters
, "}\n\n");
135 fprintf(Sections
, "}\n\n");
136 SetCurrentOutput(Sections
);
137 char *topicName
= FindTopicName(GetNextChunk());
138 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
143 case ltSUBSECTIONSTAR
:
144 case ltMEMBERSECTION
:
145 case ltFUNCTIONSECTION
:
151 if (macroId
!= ltSUBSECTIONSTAR
)
154 SetCurrentOutputs(Sections
, Subsections
);
155 long id1
= NewBlockId();
156 currentBlockId
= NewBlockId();
157 fprintf(Sections
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id1
);
158 fprintf(Subsections
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
159 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
161 OutputCurrentSection(); // Repeat section header
163 fprintf(Sections
, "}\n\n");
164 fprintf(Subsections
, "}\n\n");
165 SetCurrentOutput(Subsections
);
166 char *topicName
= FindTopicName(GetNextChunk());
167 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
171 case ltSUBSUBSECTION
:
172 case ltSUBSUBSECTIONSTAR
:
176 if (macroId
!= ltSUBSUBSECTIONSTAR
)
179 SetCurrentOutputs(Subsections
, Subsubsections
);
180 long id1
= NewBlockId();
181 currentBlockId
= NewBlockId();
182 fprintf(Subsections
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id1
);
183 fprintf(Subsubsections
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
184 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
186 OutputCurrentSection(); // Repeat section header
188 fprintf(Subsections
, "}\n\n");
189 fprintf(Subsubsections
, "}\n\n");
190 SetCurrentOutput(Subsubsections
);
191 char *topicName
= FindTopicName(GetNextChunk());
192 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
200 SetCurrentOutput(Subsections
);
203 long id
= NewBlockId();
204 fprintf(Subsections
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
207 fprintf(Subsections
, "}");
212 // TexOutput("void", TRUE);
214 case ltBACKSLASHCHAR
:
216 TexOutput("\n", TRUE
);
223 TexOutput("\n", TRUE
);
224 TexOutput("\n", TRUE
);
241 long id
= NewBlockId();
242 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
255 long id
= NewBlockId();
256 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_ITALIC
, id
);
268 long id
= NewBlockId();
269 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_TELETYPE
, id
);
279 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_TEXT
, NewBlockId());
289 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_TEXT
, NewBlockId());
299 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_NORMAL
, NewBlockId());
309 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_HEADING
, NewBlockId());
312 else TexOutput("}\n");
319 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_LARGE_HEADING
, NewBlockId());
322 else TexOutput("}\n");
334 // if (indentLevel > 0)
335 // TexOutput("\\par\\par\n");
338 if (macroId
== ltENUMERATE
)
339 listType
= LATEX_ENUMERATE
;
340 else if (macroId
== ltITEMIZE
)
341 listType
= LATEX_ITEMIZE
;
343 listType
= LATEX_DESCRIPTION
;
344 itemizeStack
.Insert(new ItemizeStruc(listType
));
351 if (itemizeStack
.First())
353 ItemizeStruc
*struc
= (ItemizeStruc
*)itemizeStack
.First()->Data();
355 delete itemizeStack
.First();
362 wxNode
*node
= itemizeStack
.First();
365 ItemizeStruc
*struc
= (ItemizeStruc
*)node
->Data();
368 struc
->currentItem
+= 1;
371 switch (struc
->listType
)
373 case LATEX_ENUMERATE
:
375 sprintf(indentBuf
, "\\hy-%d{%ld}{%d.} ",
376 hyBLOCK_BOLD
, NewBlockId(), struc
->currentItem
);
377 TexOutput(indentBuf
);
382 sprintf(indentBuf
, "\\hy-%d{%ld}{o} ",
383 hyBLOCK_BOLD
, NewBlockId());
384 TexOutput(indentBuf
);
388 case LATEX_DESCRIPTION
:
390 if (descriptionItemArg
)
392 sprintf(indentBuf
, "\\hy-%d{%ld}{",
393 hyBLOCK_BOLD
, NewBlockId());
394 TexOutput(indentBuf
);
395 TraverseChildrenFromChunk(descriptionItemArg
);
397 descriptionItemArg
= NULL
;
408 if (start
&& DocumentTitle
&& DocumentAuthor
)
410 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_LARGE_HEADING
, NewBlockId());
412 TraverseChildrenFromChunk(DocumentTitle
);
414 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_HEADING
, NewBlockId());
416 TraverseChildrenFromChunk(DocumentAuthor
);
420 TraverseChildrenFromChunk(DocumentDate
);
426 case ltTABLEOFCONTENTS
:
430 FILE *fd
= fopen(ContentsName
, "r");
443 TexOutput("RUN TEX2RTF AGAIN FOR CONTENTS PAGE\n");
444 OnInform("Run Tex2RTF again to include contents page.");
452 TexOutput("HARDY", TRUE
);
458 TexOutput("wxCLIPS", TRUE
);
466 long id
= NewBlockId();
467 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_TELETYPE
, id
);
477 TexOutput("\n------------------------------------------------------------------", TRUE
);
485 TexOutput("--------------------------------------------------------------------------------", TRUE
);
489 case ltSPECIALAMPERSAND
:
494 int tabPos
= (80/noColumns
)*currentTab
;
504 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_TELETYPE
, NewBlockId());
511 case ltNUMBEREDBIBITEM
:
514 TexOutput("\n\n", TRUE
);
525 if (DocumentStyle
!= LATEX_ARTICLE
)
526 sprintf(figBuf
, "Figure %d.%d: ", chapterNo
, figureNo
);
528 sprintf(figBuf
, "Figure %d: ", figureNo
);
534 char *topicName
= FindTopicName(GetNextChunk());
536 AddTexRef(topicName
, NULL
, NULL
,
537 ((DocumentStyle
!= LATEX_ARTICLE
) ? chapterNo
: figureNo
),
538 ((DocumentStyle
!= LATEX_ARTICLE
) ? figureNo
: 0));
544 DefaultOnMacro(macroId
, no_args
, start
);
550 bool XLPOnArgument(int macroId
, int arg_no
, bool start
)
557 case ltCHAPTERHEADING
:
560 case ltSECTIONHEADING
:
562 case ltSUBSECTIONSTAR
:
563 case ltSUBSUBSECTION
:
564 case ltSUBSUBSECTIONSTAR
:
566 case ltMEMBERSECTION
:
567 case ltFUNCTIONSECTION
:
569 if (!start
&& (arg_no
== 1))
570 currentSection
= GetArgChunk();
576 if (!start
&& (arg_no
== 1))
577 TexOutput(" ", TRUE
);
578 if (start
&& (arg_no
== 3))
579 TexOutput("(", TRUE
);
580 if (!start
&& (arg_no
== 3))
581 TexOutput(")", TRUE
);
586 if (!start
&& (arg_no
== 1))
587 TexOutput(" ", TRUE
);
589 if (start
&& (arg_no
== 2))
590 TexOutput("(*", TRUE
);
591 if (!start
&& (arg_no
== 2))
592 TexOutput(")", TRUE
);
594 if (start
&& (arg_no
== 3))
595 TexOutput("(", TRUE
);
596 if (!start
&& (arg_no
== 3))
597 TexOutput(")", TRUE
);
602 if (!start
&& (arg_no
== 1))
603 TexOutput(" ", TRUE
);
604 if (start
&& (arg_no
== 2))
606 TexOutput("(", TRUE
);
607 long id
= NewBlockId();
608 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
611 if (!start
&& (arg_no
== 2))
615 if (!start
&& (arg_no
== 3))
616 TexOutput(")", TRUE
);
621 if (start
&& (arg_no
== 2))
623 long id
= NewBlockId();
624 sprintf(buf
, " \\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
627 if (!start
&& (arg_no
== 2))
635 if (start
&& (arg_no
== 2))
637 long id
= NewBlockId();
638 sprintf(buf
, " \\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
641 if (!start
&& (arg_no
== 2))
649 if (!start
&& (arg_no
== 1))
650 TexOutput(" ", TRUE
);
664 char *refName
= GetArgData();
667 TexRef
*texRef
= FindReference(refName
);
670 sec
= texRef
->sectionNumber
;
689 currentBlockId
= NewBlockId();
690 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_RED_ITALIC
, currentBlockId
);
699 char *label
= GetArgData();
700 hyperLinks
.Append(currentBlockId
, (wxObject
*)copystring(label
));
713 else if (arg_no
== 2)
725 if (start
&& IsArgOptional())
727 descriptionItemArg
= GetArgChunk();
740 tableVerticalLineLeft
= FALSE
;
741 tableVerticalLineRight
= FALSE
;
743 char *alignString
= copystring(GetArgData());
745 // Count the number of columns
747 int len
= strlen(alignString
);
750 if (alignString
[0] == '|')
751 tableVerticalLineLeft
= TRUE
;
752 if (alignString
[len
-1] == '|')
753 tableVerticalLineRight
= TRUE
;
756 for (int i
= 0; i
< len
; i
++)
757 if (isalpha(alignString
[i
]))
762 TexOutput("\\brdrt\\brdrs");
763 if (tableVerticalLineLeft)
764 TexOutput("\\brdrl\\brdrs");
765 if (tableVerticalLineRight)
766 TexOutput("\\brdrr\\brdrs");
769 // Calculate a rough size for each column
770 // int tabPos = 80/noColumns;
776 else if (arg_no
== 2 && !start
)
780 else if (arg_no
== 2 && start
)
785 case ltMARGINPAREVEN
:
792 TexOutput("----------------------------------------------------------------------\n", TRUE
);
796 TexOutput("\n----------------------------------------------------------------------\n", TRUE
);
802 if (arg_no
== 1 && start
)
804 char *citeKey
= GetArgData();
805 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
808 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
809 sprintf(buf
, "[%d]", citeCount
);
810 ref
->sectionNumber
= copystring(buf
);
813 sprintf(buf
, "\\hy-%d{%ld}{[%d]} ", hyBLOCK_BOLD
, NewBlockId(), citeCount
);
821 case ltTHEBIBLIOGRAPHY
:
823 if (start
&& (arg_no
== 1))
827 SetCurrentOutput(Chapters
);
829 SetCurrentOutputs(Contents
, Chapters
);
830 long id1
= NewBlockId();
831 long id2
= NewBlockId();
832 fprintf(Contents
, "\\hy-%d{%ld}{%s}\n", hyBLOCK_SMALL_HEADING
, id1
, ReferencesNameString
);
833 fprintf(Chapters
, "\\hy-%d{%ld}{%s}\n\n\n", hyBLOCK_LARGE_VISIBLE_SECTION
, id2
, ReferencesNameString
);
834 fprintf(Index
, "%ld %ld\n", id1
, id2
);
836 SetCurrentOutput(Chapters
);
839 if (!start
&& (arg_no
== 2))
846 case ltTWOCOLITEMRULED
:
848 if (start
&& (arg_no
== 2))
851 if (!start
&& (arg_no
== 2))
863 char *val
= GetArgData();
910 char *val
= GetArgData();
963 char *val
= GetArgData();
1006 case ltACCENT_TILDE
:
1010 char *val
= GetArgData();
1044 case ltACCENT_UMLAUT
:
1048 char *val
= GetArgData();
1101 char *val
= GetArgData();
1120 case ltACCENT_CADILLA
:
1124 char *val
= GetArgData();
1145 return DefaultOnArgument(macroId
, arg_no
, start
);
1156 if (InputFile
&& OutputFile
)
1158 Contents
= fopen(TmpContentsName
, "w");
1159 Chapters
= fopen("chapters.xlp", "w");
1160 Sections
= fopen("sections.xlp", "w");
1161 Subsections
= fopen("subsections.xlp", "w");
1162 Subsubsections
= fopen("subsubsections.xlp", "w");
1163 Index
= fopen("index.xlp", "w");
1165 // Insert invisible section marker at beginning
1166 fprintf(Chapters
, "\\hy-%d{%ld}{%s}\n",
1167 hyBLOCK_INVISIBLE_SECTION
, NewBlockId(), "\n");
1169 fprintf(Contents
, "\\hy-%d{%ld}{%s}\n\n",
1170 // hyBLOCK_LARGE_HEADING, NewBlockId(), "\n\n%s\n\n", ContentsNameString);
1171 hyBLOCK_LARGE_HEADING
, NewBlockId(), ContentsNameString
);
1173 SetCurrentOutput(Chapters
);
1175 fprintf(Index
, "\n\\hyindex{\n\"%s\"\n",
1176 contentsString
? contentsString
: "WXHELPCONTENTS");
1179 wxNode
*node
= hyperLinks
.First();
1182 long from
= node
->GetKeyInteger();
1183 char *label
= (char *)node
->Data();
1184 wxNode
*otherNode
= hyperLabels
.Find(label
);
1187 long to
= (long)otherNode
->Data();
1188 fprintf(Index
, "%ld %ld\n", from
, to
);
1190 node
= node
->Next();
1193 fprintf(Index
, "}\n");
1195 fclose(Contents
); Contents
= NULL
;
1196 fclose(Chapters
); Chapters
= NULL
;
1197 fclose(Sections
); Sections
= NULL
;
1198 fclose(Subsections
); Subsections
= NULL
;
1199 fclose(Subsubsections
); Subsubsections
= NULL
;
1200 fclose(Index
); Index
= NULL
;
1202 if (wxFileExists(ContentsName
)) wxRemoveFile(ContentsName
);
1204 if (!wxRenameFile(TmpContentsName
, ContentsName
))
1206 wxCopyFile(TmpContentsName
, ContentsName
);
1207 wxRemoveFile(TmpContentsName
);
1210 wxConcatFiles("chapters.xlp", "sections.xlp", "tmp2.xlp");
1211 wxConcatFiles("tmp2.xlp", "subsections.xlp", "tmp1.xlp");
1212 wxConcatFiles("tmp1.xlp", "subsubsections.xlp", "tmp2.xlp");
1213 wxConcatFiles("tmp2.xlp", "index.xlp", OutputFile
);
1215 wxRemoveFile("tmp1.xlp");
1216 wxRemoveFile("tmp2.xlp");
1218 wxRemoveFile("chapters.xlp");
1219 wxRemoveFile("sections.xlp");
1220 wxRemoveFile("subsections.xlp");
1221 wxRemoveFile("subsubsections.xlp");
1222 wxRemoveFile("index.xlp");