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
++)
56 TexOutput(_T(" "), TRUE
);
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 wxFprintf(Contents
, _T("\\hy-%d{%ld}{"), hyBLOCK_SMALL_HEADING
, id1
);
90 wxFprintf(Chapters
, _T("\n\\hy-%d{%ld}{"), hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
91 wxFprintf(Index
, _T("%ld %ld\n"), id1
, currentBlockId
);
93 OutputCurrentSection(); // Repeat section header
95 wxFprintf(Contents
, _T("}\n\n"));
96 wxFprintf(Chapters
, _T("}\n\n"));
97 SetCurrentOutput(Chapters
);
98 wxChar
*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 wxFprintf(Contents
, _T("\\hy-%d{%ld}{"), hyBLOCK_LARGE_HEADING
, id1
);
125 wxFprintf(Chapters
, _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD
, id1
);
126 wxFprintf(Sections
, _T("\n\\hy-%d{%ld}{"), hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
127 wxFprintf(Index
, _T("%ld %ld\n"), id1
, currentBlockId
);
129 OutputCurrentSection(); // Repeat section header
131 if (DocumentStyle
== LATEX_ARTICLE
)
132 wxFprintf(Contents
, _T("}\n\n"));
134 wxFprintf(Chapters
, _T("}\n\n"));
135 wxFprintf(Sections
, _T("}\n\n"));
136 SetCurrentOutput(Sections
);
137 wxChar
*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 wxFprintf(Sections
, _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD
, id1
);
158 wxFprintf(Subsections
, _T("\n\\hy-%d{%ld}{"), hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
159 wxFprintf(Index
, _T("%ld %ld\n"), id1
, currentBlockId
);
161 OutputCurrentSection(); // Repeat section header
163 wxFprintf(Sections
, _T("}\n\n"));
164 wxFprintf(Subsections
, _T("}\n\n"));
165 SetCurrentOutput(Subsections
);
166 wxChar
*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 wxFprintf(Subsections
, _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD
, id1
);
183 wxFprintf(Subsubsections
, _T("\n\\hy-%d{%ld}{"), hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
184 wxFprintf(Index
, _T("%ld %ld\n"), id1
, currentBlockId
);
186 OutputCurrentSection(); // Repeat section header
188 wxFprintf(Subsections
, _T("}\n\n"));
189 wxFprintf(Subsubsections
, _T("}\n\n"));
190 SetCurrentOutput(Subsubsections
);
191 wxChar
*topicName
= FindTopicName(GetNextChunk());
192 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
200 SetCurrentOutput(Subsections
);
203 long id
= NewBlockId();
204 wxFprintf(Subsections
, _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD
, id
);
207 wxFprintf(Subsections
, _T("}"));
212 // TexOutput(_T("void"), TRUE);
214 case ltBACKSLASHCHAR
:
216 TexOutput(_T("\n"), TRUE
);
223 TexOutput(_T("\n"), TRUE
);
224 TexOutput(_T("\n"), TRUE
);
241 long id
= NewBlockId();
242 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD
, id
);
245 else TexOutput(_T("}"));
255 long id
= NewBlockId();
256 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_ITALIC
, id
);
259 else TexOutput(_T("}"));
268 long id
= NewBlockId();
269 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_TELETYPE
, id
);
272 else TexOutput(_T("}"));
279 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_SMALL_TEXT
, NewBlockId());
282 else TexOutput(_T("}"));
289 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_SMALL_TEXT
, NewBlockId());
292 else TexOutput(_T("}"));
299 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_NORMAL
, NewBlockId());
302 else TexOutput(_T("}"));
309 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_SMALL_HEADING
, NewBlockId());
312 else TexOutput(_T("}\n"));
319 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_LARGE_HEADING
, NewBlockId());
322 else TexOutput(_T("}\n"));
334 // if (indentLevel > 0)
335 // TexOutput(_T("\\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
.GetFirst())
353 ItemizeStruc
*struc
= (ItemizeStruc
*)itemizeStack
.GetFirst()->GetData();
355 delete itemizeStack
.GetFirst();
362 wxNode
*node
= itemizeStack
.GetFirst();
365 ItemizeStruc
*struc
= (ItemizeStruc
*)node
->GetData();
368 struc
->currentItem
+= 1;
369 wxChar indentBuf
[30];
371 switch (struc
->listType
)
373 case LATEX_ENUMERATE
:
375 wxSprintf(indentBuf
, _T("\\hy-%d{%ld}{%d.} "),
376 hyBLOCK_BOLD
, NewBlockId(), struc
->currentItem
);
377 TexOutput(indentBuf
);
382 wxSprintf(indentBuf
, _T("\\hy-%d{%ld}{o} "),
383 hyBLOCK_BOLD
, NewBlockId());
384 TexOutput(indentBuf
);
388 case LATEX_DESCRIPTION
:
390 if (descriptionItemArg
)
392 wxSprintf(indentBuf
, _T("\\hy-%d{%ld}{"),
393 hyBLOCK_BOLD
, NewBlockId());
394 TexOutput(indentBuf
);
395 TraverseChildrenFromChunk(descriptionItemArg
);
397 descriptionItemArg
= NULL
;
408 if (start
&& DocumentTitle
&& DocumentAuthor
)
410 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_LARGE_HEADING
, NewBlockId());
412 TraverseChildrenFromChunk(DocumentTitle
);
413 TexOutput(_T("}\n\n"));
414 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_SMALL_HEADING
, NewBlockId());
416 TraverseChildrenFromChunk(DocumentAuthor
);
417 TexOutput(_T("}\n\n"));
420 TraverseChildrenFromChunk(DocumentDate
);
426 case ltTABLEOFCONTENTS
:
430 FILE *fd
= wxFopen(ContentsName
, _T("r"));
443 TexOutput(_T("RUN TEX2RTF AGAIN FOR CONTENTS PAGE\n"));
444 OnInform(_T("Run Tex2RTF again to include contents page."));
452 TexOutput(_T("HARDY"), TRUE
);
458 TexOutput(_T("wxCLIPS"), TRUE
);
466 long id
= NewBlockId();
467 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_TELETYPE
, id
);
470 else TexOutput(_T("}"));
477 TexOutput(_T("\n------------------------------------------------------------------"), TRUE
);
485 TexOutput(_T("--------------------------------------------------------------------------------"), TRUE
);
489 case ltSPECIALAMPERSAND
:
494 int tabPos
= (80/noColumns
)*currentTab
;
504 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_TELETYPE
, NewBlockId());
511 case ltNUMBEREDBIBITEM
:
514 TexOutput(_T("\n\n"), TRUE
);
525 if (DocumentStyle
!= LATEX_ARTICLE
)
526 wxSprintf(figBuf
, _T("Figure %d.%d: "), chapterNo
, figureNo
);
528 wxSprintf(figBuf
, _T("Figure %d: "), figureNo
);
534 wxChar
*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();
575 if (!start
&& (arg_no
== 1))
576 TexOutput(_T(" "), TRUE
);
577 if (start
&& (arg_no
== 3))
578 TexOutput(_T("("), TRUE
);
579 if (!start
&& (arg_no
== 3))
580 TexOutput(_T(")"), TRUE
);
585 if (!start
&& (arg_no
== 1))
586 TexOutput(_T(" "), TRUE
);
588 if (start
&& (arg_no
== 2))
589 TexOutput(_T("(*"), TRUE
);
590 if (!start
&& (arg_no
== 2))
591 TexOutput(_T(")"), TRUE
);
593 if (start
&& (arg_no
== 3))
594 TexOutput(_T("("), TRUE
);
595 if (!start
&& (arg_no
== 3))
596 TexOutput(_T(")"), TRUE
);
601 if (!start
&& (arg_no
== 1))
602 TexOutput(_T(" "), TRUE
);
603 if (start
&& (arg_no
== 2))
605 TexOutput(_T("("), TRUE
);
606 long id
= NewBlockId();
607 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD
, id
);
610 if (!start
&& (arg_no
== 2))
614 if (!start
&& (arg_no
== 3))
615 TexOutput(_T(")"), TRUE
);
620 if (start
&& (arg_no
== 2))
622 long id
= NewBlockId();
623 wxSprintf(buf
, _T(" \\hy-%d{%ld}{"), hyBLOCK_BOLD
, id
);
626 if (!start
&& (arg_no
== 2))
634 if (start
&& (arg_no
== 2))
636 long id
= NewBlockId();
637 wxSprintf(buf
, _T(" \\hy-%d{%ld}{"), hyBLOCK_BOLD
, id
);
640 if (!start
&& (arg_no
== 2))
648 if (!start
&& (arg_no
== 1))
649 TexOutput(_T(" "), TRUE
);
662 wxChar
*refName
= GetArgData();
665 TexRef
*texRef
= FindReference(refName
);
668 sec
= texRef
->sectionNumber
;
687 currentBlockId
= NewBlockId();
688 wxSprintf(buf
, _T("\\hy-%d{%ld}{"), hyBLOCK_RED_ITALIC
, currentBlockId
);
691 else TexOutput(_T("}"));
697 wxChar
*label
= GetArgData();
698 hyperLinks
.Append(currentBlockId
, (wxObject
*)copystring(label
));
711 else if (arg_no
== 2)
723 if (start
&& IsArgOptional())
725 descriptionItemArg
= GetArgChunk();
738 tableVerticalLineLeft
= FALSE
;
739 tableVerticalLineRight
= FALSE
;
741 wxChar
*alignString
= copystring(GetArgData());
743 // Count the number of columns
745 int len
= wxStrlen(alignString
);
748 if (alignString
[0] == '|')
749 tableVerticalLineLeft
= TRUE
;
750 if (alignString
[len
-1] == '|')
751 tableVerticalLineRight
= TRUE
;
754 for (int i
= 0; i
< len
; i
++)
755 if (isalpha(alignString
[i
]))
760 TexOutput(_T("\\brdrt\\brdrs"));
761 if (tableVerticalLineLeft)
762 TexOutput(_T("\\brdrl\\brdrs"));
763 if (tableVerticalLineRight)
764 TexOutput(_T("\\brdrr\\brdrs"));
767 // Calculate a rough size for each column
768 // int tabPos = 80/noColumns;
774 else if (arg_no
== 2 && !start
)
778 else if (arg_no
== 2 && start
)
783 case ltMARGINPAREVEN
:
790 TexOutput(_T("----------------------------------------------------------------------\n"), TRUE
);
794 TexOutput(_T("\n----------------------------------------------------------------------\n"), TRUE
);
800 if (arg_no
== 1 && start
)
802 wxChar
*citeKey
= GetArgData();
803 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
806 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
807 wxSprintf(buf
, _T("[%d]"), citeCount
);
808 ref
->sectionNumber
= copystring(buf
);
811 wxSprintf(buf
, _T("\\hy-%d{%ld}{[%d]} "), hyBLOCK_BOLD
, NewBlockId(), citeCount
);
818 case ltTHEBIBLIOGRAPHY
:
820 if (start
&& (arg_no
== 1))
824 SetCurrentOutput(Chapters
);
826 SetCurrentOutputs(Contents
, Chapters
);
827 long id1
= NewBlockId();
828 long id2
= NewBlockId();
829 wxFprintf(Contents
, _T("\\hy-%d{%ld}{%s}\n"), hyBLOCK_SMALL_HEADING
, id1
, ReferencesNameString
);
830 wxFprintf(Chapters
, _T("\\hy-%d{%ld}{%s}\n\n\n"), hyBLOCK_LARGE_VISIBLE_SECTION
, id2
, ReferencesNameString
);
831 wxFprintf(Index
, _T("%ld %ld\n"), id1
, id2
);
833 SetCurrentOutput(Chapters
);
836 if (!start
&& (arg_no
== 2))
842 case ltTWOCOLITEMRULED
:
844 if (start
&& (arg_no
== 2))
845 TexOutput(_T("\n "));
847 if (!start
&& (arg_no
== 2))
859 wxChar
*val
= GetArgData();
905 wxChar
*val
= GetArgData();
957 wxChar
*val
= GetArgData();
1003 wxChar
*val
= GetArgData();
1036 case ltACCENT_UMLAUT
:
1040 wxChar
*val
= GetArgData();
1092 wxChar
*val
= GetArgData();
1110 case ltACCENT_CADILLA
:
1114 wxChar
*val
= GetArgData();
1134 return DefaultOnArgument(macroId
, arg_no
, start
);
1144 if (InputFile
&& OutputFile
)
1146 Contents
= wxFopen(TmpContentsName
, _T("w"));
1147 Chapters
= wxFopen(_T("chapters.xlp"), _T("w"));
1148 Sections
= wxFopen(_T("sections.xlp"), _T("w"));
1149 Subsections
= wxFopen(_T("subsections.xlp"), _T("w"));
1150 Subsubsections
= wxFopen(_T("subsubsections.xlp"), _T("w"));
1151 Index
= wxFopen(_T("index.xlp"), _T("w"));
1153 // Insert invisible section marker at beginning
1154 wxFprintf(Chapters
, _T("\\hy-%d{%ld}{%s}\n"),
1155 hyBLOCK_INVISIBLE_SECTION
, NewBlockId(), _T("\n"));
1157 wxFprintf(Contents
, _T("\\hy-%d{%ld}{%s}\n\n"),
1158 // hyBLOCK_LARGE_HEADING, NewBlockId(), "\n\n%s\n\n", ContentsNameString);
1159 hyBLOCK_LARGE_HEADING
, NewBlockId(), ContentsNameString
);
1161 SetCurrentOutput(Chapters
);
1163 wxFprintf(Index
, _T("\n\\hyindex{\n\"%s\"\n"),
1164 contentsString
? contentsString
: _T("WXHELPCONTENTS"));
1167 wxNode
*node
= hyperLinks
.GetFirst();
1170 long from
= node
->GetKeyInteger();
1171 wxChar
*label
= (wxChar
*)node
->GetData();
1172 wxNode
*otherNode
= hyperLabels
.Find(label
);
1175 long to
= (long)otherNode
->GetData();
1176 wxFprintf(Index
, _T("%ld %ld\n"), from
, to
);
1178 node
= node
->GetNext();
1181 wxFprintf(Index
, _T("}\n"));
1183 fclose(Contents
); Contents
= NULL
;
1184 fclose(Chapters
); Chapters
= NULL
;
1185 fclose(Sections
); Sections
= NULL
;
1186 fclose(Subsections
); Subsections
= NULL
;
1187 fclose(Subsubsections
); Subsubsections
= NULL
;
1188 fclose(Index
); Index
= NULL
;
1190 if (wxFileExists(ContentsName
)) wxRemoveFile(ContentsName
);
1192 if (!wxRenameFile(TmpContentsName
, ContentsName
))
1194 wxCopyFile(TmpContentsName
, ContentsName
);
1195 wxRemoveFile(TmpContentsName
);
1198 wxConcatFiles(_T("chapters.xlp"), _T("sections.xlp"), _T("tmp2.xlp"));
1199 wxConcatFiles(_T("tmp2.xlp"), _T("subsections.xlp"), _T("tmp1.xlp"));
1200 wxConcatFiles(_T("tmp1.xlp"), _T("subsubsections.xlp"), _T("tmp2.xlp"));
1201 wxConcatFiles(_T("tmp2.xlp"), _T("index.xlp"), OutputFile
);
1203 wxRemoveFile(_T("tmp1.xlp"));
1204 wxRemoveFile(_T("tmp2.xlp"));
1206 wxRemoveFile(_T("chapters.xlp"));
1207 wxRemoveFile(_T("sections.xlp"));
1208 wxRemoveFile(_T("subsections.xlp"));
1209 wxRemoveFile(_T("subsubsections.xlp"));
1210 wxRemoveFile(_T("index.xlp"));