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
);
44 void PadToTab(int tabPos
)
46 int currentCol
= GetCurrentColumn();
47 for (int i
= currentCol
; i
< tabPos
; i
++)
51 static long xlpBlockId
= 0;
57 // Called on start/end of macro examination
58 void XLPOnMacro(int macroId
, int no_args
, bool start
)
65 case ltCHAPTERHEADING
:
73 if (macroId
!= ltCHAPTERSTAR
)
76 SetCurrentOutputs(Contents
, Chapters
);
77 long id1
= NewBlockId();
78 currentBlockId
= NewBlockId();
80 startedSections
= TRUE
;
81 fprintf(Contents
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_HEADING
, id1
);
82 fprintf(Chapters
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
83 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
85 OutputCurrentSection(); // Repeat section header
87 fprintf(Contents
, "}\n\n");
88 fprintf(Chapters
, "}\n\n");
89 SetCurrentOutput(Chapters
);
90 char *topicName
= FindTopicName(GetNextChunk());
91 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
97 case ltSECTIONHEADING
:
105 if (macroId
!= ltSECTIONSTAR
)
108 SetCurrentOutputs(Chapters
, Sections
);
109 long id1
= NewBlockId();
110 currentBlockId
= NewBlockId();
112 startedSections
= TRUE
;
114 if (DocumentStyle
== LATEX_ARTICLE
)
115 fprintf(Contents
, "\\hy-%d{%ld}{", hyBLOCK_LARGE_HEADING
, id1
);
117 fprintf(Chapters
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id1
);
118 fprintf(Sections
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
119 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
121 OutputCurrentSection(); // Repeat section header
123 if (DocumentStyle
== LATEX_ARTICLE
)
124 fprintf(Contents
, "}\n\n");
126 fprintf(Chapters
, "}\n\n");
127 fprintf(Sections
, "}\n\n");
128 SetCurrentOutput(Sections
);
129 char *topicName
= FindTopicName(GetNextChunk());
130 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
135 case ltSUBSECTIONSTAR
:
136 case ltMEMBERSECTION
:
137 case ltFUNCTIONSECTION
:
143 if (macroId
!= ltSUBSECTIONSTAR
)
146 SetCurrentOutputs(Sections
, Subsections
);
147 long id1
= NewBlockId();
148 currentBlockId
= NewBlockId();
149 fprintf(Sections
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id1
);
150 fprintf(Subsections
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
151 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
153 OutputCurrentSection(); // Repeat section header
155 fprintf(Sections
, "}\n\n");
156 fprintf(Subsections
, "}\n\n");
157 SetCurrentOutput(Subsections
);
158 char *topicName
= FindTopicName(GetNextChunk());
159 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
163 case ltSUBSUBSECTION
:
164 case ltSUBSUBSECTIONSTAR
:
168 if (macroId
!= ltSUBSUBSECTIONSTAR
)
171 SetCurrentOutputs(Subsections
, Subsubsections
);
172 long id1
= NewBlockId();
173 currentBlockId
= NewBlockId();
174 fprintf(Subsections
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id1
);
175 fprintf(Subsubsections
, "\n\\hy-%d{%ld}{", hyBLOCK_LARGE_VISIBLE_SECTION
, currentBlockId
);
176 fprintf(Index
, "%ld %ld\n", id1
, currentBlockId
);
178 OutputCurrentSection(); // Repeat section header
180 fprintf(Subsections
, "}\n\n");
181 fprintf(Subsubsections
, "}\n\n");
182 SetCurrentOutput(Subsubsections
);
183 char *topicName
= FindTopicName(GetNextChunk());
184 hyperLabels
.Append(topicName
, (wxObject
*)currentBlockId
);
192 SetCurrentOutput(Subsections
);
195 long id
= NewBlockId();
196 fprintf(Subsections
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
199 fprintf(Subsections
, "}");
204 // TexOutput("void", TRUE);
206 case ltBACKSLASHCHAR
:
208 TexOutput("\n", TRUE
);
215 TexOutput("\n", TRUE
);
216 TexOutput("\n", TRUE
);
233 long id
= NewBlockId();
234 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
247 long id
= NewBlockId();
248 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_ITALIC
, id
);
260 long id
= NewBlockId();
261 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_TELETYPE
, id
);
271 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_TEXT
, NewBlockId());
281 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_TEXT
, NewBlockId());
291 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_NORMAL
, NewBlockId());
301 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_HEADING
, NewBlockId());
304 else TexOutput("}\n");
311 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_LARGE_HEADING
, NewBlockId());
314 else TexOutput("}\n");
326 // if (indentLevel > 0)
327 // TexOutput("\\par\\par\n");
330 if (macroId
== ltENUMERATE
)
331 listType
= LATEX_ENUMERATE
;
332 else if (macroId
== ltITEMIZE
)
333 listType
= LATEX_ITEMIZE
;
335 listType
= LATEX_DESCRIPTION
;
336 itemizeStack
.Insert(new ItemizeStruc(listType
));
343 if (itemizeStack
.First())
345 ItemizeStruc
*struc
= (ItemizeStruc
*)itemizeStack
.First()->Data();
347 delete itemizeStack
.First();
354 wxNode
*node
= itemizeStack
.First();
357 ItemizeStruc
*struc
= (ItemizeStruc
*)node
->Data();
360 struc
->currentItem
+= 1;
363 switch (struc
->listType
)
365 case LATEX_ENUMERATE
:
367 sprintf(indentBuf
, "\\hy-%d{%ld}{%d.} ",
368 hyBLOCK_BOLD
, NewBlockId(), struc
->currentItem
);
369 TexOutput(indentBuf
);
374 sprintf(indentBuf
, "\\hy-%d{%ld}{o} ",
375 hyBLOCK_BOLD
, NewBlockId());
376 TexOutput(indentBuf
);
380 case LATEX_DESCRIPTION
:
382 if (descriptionItemArg
)
384 sprintf(indentBuf
, "\\hy-%d{%ld}{",
385 hyBLOCK_BOLD
, NewBlockId());
386 TexOutput(indentBuf
);
387 TraverseChildrenFromChunk(descriptionItemArg
);
389 descriptionItemArg
= NULL
;
400 if (start
&& DocumentTitle
&& DocumentAuthor
)
402 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_LARGE_HEADING
, NewBlockId());
404 TraverseChildrenFromChunk(DocumentTitle
);
406 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_SMALL_HEADING
, NewBlockId());
408 TraverseChildrenFromChunk(DocumentAuthor
);
412 TraverseChildrenFromChunk(DocumentDate
);
418 case ltTABLEOFCONTENTS
:
422 FILE *fd
= fopen(ContentsName
, "r");
435 TexOutput("RUN TEX2RTF AGAIN FOR CONTENTS PAGE\n");
436 OnInform("Run Tex2RTF again to include contents page.");
444 TexOutput("HARDY", TRUE
);
450 TexOutput("wxCLIPS", TRUE
);
458 long id
= NewBlockId();
459 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_TELETYPE
, id
);
469 TexOutput("\n------------------------------------------------------------------", TRUE
);
477 TexOutput("--------------------------------------------------------------------------------", TRUE
);
481 case ltSPECIALAMPERSAND
:
486 int tabPos
= (80/noColumns
)*currentTab
;
496 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_TELETYPE
, NewBlockId());
503 case ltNUMBEREDBIBITEM
:
506 TexOutput("\n\n", TRUE
);
517 if (DocumentStyle
!= LATEX_ARTICLE
)
518 sprintf(figBuf
, "Figure %d.%d: ", chapterNo
, figureNo
);
520 sprintf(figBuf
, "Figure %d: ", figureNo
);
526 char *topicName
= FindTopicName(GetNextChunk());
528 AddTexRef(topicName
, NULL
, NULL
,
529 ((DocumentStyle
!= LATEX_ARTICLE
) ? chapterNo
: figureNo
),
530 ((DocumentStyle
!= LATEX_ARTICLE
) ? figureNo
: 0));
536 DefaultOnMacro(macroId
, no_args
, start
);
542 bool XLPOnArgument(int macroId
, int arg_no
, bool start
)
549 case ltCHAPTERHEADING
:
552 case ltSECTIONHEADING
:
554 case ltSUBSECTIONSTAR
:
555 case ltSUBSUBSECTION
:
556 case ltSUBSUBSECTIONSTAR
:
558 case ltMEMBERSECTION
:
559 case ltFUNCTIONSECTION
:
561 if (!start
&& (arg_no
== 1))
562 currentSection
= GetArgChunk();
568 if (!start
&& (arg_no
== 1))
569 TexOutput(" ", TRUE
);
570 if (start
&& (arg_no
== 3))
571 TexOutput("(", TRUE
);
572 if (!start
&& (arg_no
== 3))
573 TexOutput(")", TRUE
);
578 if (!start
&& (arg_no
== 1))
579 TexOutput(" ", TRUE
);
581 if (start
&& (arg_no
== 2))
582 TexOutput("(*", TRUE
);
583 if (!start
&& (arg_no
== 2))
584 TexOutput(")", TRUE
);
586 if (start
&& (arg_no
== 3))
587 TexOutput("(", TRUE
);
588 if (!start
&& (arg_no
== 3))
589 TexOutput(")", TRUE
);
594 if (!start
&& (arg_no
== 1))
595 TexOutput(" ", TRUE
);
596 if (start
&& (arg_no
== 2))
598 TexOutput("(", TRUE
);
599 long id
= NewBlockId();
600 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
603 if (!start
&& (arg_no
== 2))
607 if (!start
&& (arg_no
== 3))
608 TexOutput(")", TRUE
);
613 if (start
&& (arg_no
== 2))
615 long id
= NewBlockId();
616 sprintf(buf
, " \\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
619 if (!start
&& (arg_no
== 2))
627 if (start
&& (arg_no
== 2))
629 long id
= NewBlockId();
630 sprintf(buf
, " \\hy-%d{%ld}{", hyBLOCK_BOLD
, id
);
633 if (!start
&& (arg_no
== 2))
641 if (!start
&& (arg_no
== 1))
642 TexOutput(" ", TRUE
);
656 char *refName
= GetArgData();
659 TexRef
*texRef
= FindReference(refName
);
662 sec
= texRef
->sectionNumber
;
681 currentBlockId
= NewBlockId();
682 sprintf(buf
, "\\hy-%d{%ld}{", hyBLOCK_RED_ITALIC
, currentBlockId
);
691 char *label
= GetArgData();
692 hyperLinks
.Append(currentBlockId
, (wxObject
*)copystring(label
));
705 else if (arg_no
== 2)
717 if (start
&& IsArgOptional())
719 descriptionItemArg
= GetArgChunk();
732 tableVerticalLineLeft
= FALSE
;
733 tableVerticalLineRight
= FALSE
;
735 char *alignString
= copystring(GetArgData());
737 // Count the number of columns
739 int len
= strlen(alignString
);
742 if (alignString
[0] == '|')
743 tableVerticalLineLeft
= TRUE
;
744 if (alignString
[len
-1] == '|')
745 tableVerticalLineRight
= TRUE
;
748 for (int i
= 0; i
< len
; i
++)
749 if (isalpha(alignString
[i
]))
754 TexOutput("\\brdrt\\brdrs");
755 if (tableVerticalLineLeft)
756 TexOutput("\\brdrl\\brdrs");
757 if (tableVerticalLineRight)
758 TexOutput("\\brdrr\\brdrs");
761 // Calculate a rough size for each column
762 // int tabPos = 80/noColumns;
768 else if (arg_no
== 2 && !start
)
772 else if (arg_no
== 2 && start
)
777 case ltMARGINPAREVEN
:
784 TexOutput("----------------------------------------------------------------------\n", TRUE
);
788 TexOutput("\n----------------------------------------------------------------------\n", TRUE
);
794 if (arg_no
== 1 && start
)
796 char *citeKey
= GetArgData();
797 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
800 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
801 sprintf(buf
, "[%d]", citeCount
);
802 ref
->sectionNumber
= copystring(buf
);
805 sprintf(buf
, "\\hy-%d{%ld}{[%d]} ", hyBLOCK_BOLD
, NewBlockId(), citeCount
);
813 case ltTHEBIBLIOGRAPHY
:
815 if (start
&& (arg_no
== 1))
819 SetCurrentOutput(Chapters
);
821 SetCurrentOutputs(Contents
, Chapters
);
822 long id1
= NewBlockId();
823 long id2
= NewBlockId();
824 fprintf(Contents
, "\\hy-%d{%ld}{%s}\n", hyBLOCK_SMALL_HEADING
, id1
, ReferencesNameString
);
825 fprintf(Chapters
, "\\hy-%d{%ld}{%s}\n\n\n", hyBLOCK_LARGE_VISIBLE_SECTION
, id2
, ReferencesNameString
);
826 fprintf(Index
, "%ld %ld\n", id1
, id2
);
828 SetCurrentOutput(Chapters
);
831 if (!start
&& (arg_no
== 2))
838 case ltTWOCOLITEMRULED
:
840 if (start
&& (arg_no
== 2))
843 if (!start
&& (arg_no
== 2))
855 char *val
= GetArgData();
902 char *val
= GetArgData();
955 char *val
= GetArgData();
1002 char *val
= GetArgData();
1036 case ltACCENT_UMLAUT
:
1040 char *val
= GetArgData();
1093 char *val
= GetArgData();
1112 case ltACCENT_CADILLA
:
1116 char *val
= GetArgData();
1137 return DefaultOnArgument(macroId
, arg_no
, start
);
1148 if (InputFile
&& OutputFile
)
1150 Contents
= fopen(TmpContentsName
, "w");
1151 Chapters
= fopen("chapters.xlp", "w");
1152 Sections
= fopen("sections.xlp", "w");
1153 Subsections
= fopen("subsections.xlp", "w");
1154 Subsubsections
= fopen("subsubsections.xlp", "w");
1155 Index
= fopen("index.xlp", "w");
1157 // Insert invisible section marker at beginning
1158 fprintf(Chapters
, "\\hy-%d{%ld}{%s}\n",
1159 hyBLOCK_INVISIBLE_SECTION
, NewBlockId(), "\n");
1161 fprintf(Contents
, "\\hy-%d{%ld}{%s}\n\n",
1162 // hyBLOCK_LARGE_HEADING, NewBlockId(), "\n\n%s\n\n", ContentsNameString);
1163 hyBLOCK_LARGE_HEADING
, NewBlockId(), ContentsNameString
);
1165 SetCurrentOutput(Chapters
);
1167 fprintf(Index
, "\n\\hyindex{\n\"%s\"\n",
1168 contentsString
? contentsString
: "WXHELPCONTENTS");
1171 wxNode
*node
= hyperLinks
.First();
1174 long from
= node
->GetKeyInteger();
1175 char *label
= (char *)node
->Data();
1176 wxNode
*otherNode
= hyperLabels
.Find(label
);
1179 long to
= (long)otherNode
->Data();
1180 fprintf(Index
, "%ld %ld\n", from
, to
);
1182 node
= node
->Next();
1185 fprintf(Index
, "}\n");
1187 fclose(Contents
); Contents
= NULL
;
1188 fclose(Chapters
); Chapters
= NULL
;
1189 fclose(Sections
); Sections
= NULL
;
1190 fclose(Subsections
); Subsections
= NULL
;
1191 fclose(Subsubsections
); Subsubsections
= NULL
;
1192 fclose(Index
); Index
= NULL
;
1194 if (FileExists(ContentsName
)) wxRemoveFile(ContentsName
);
1196 if (!wxRenameFile(TmpContentsName
, ContentsName
))
1198 wxCopyFile(TmpContentsName
, ContentsName
);
1199 wxRemoveFile(TmpContentsName
);
1202 wxConcatFiles("chapters.xlp", "sections.xlp", "tmp2.xlp");
1203 wxConcatFiles("tmp2.xlp", "subsections.xlp", "tmp1.xlp");
1204 wxConcatFiles("tmp1.xlp", "subsubsections.xlp", "tmp2.xlp");
1205 wxConcatFiles("tmp2.xlp", "index.xlp", OutputFile
);
1207 wxRemoveFile("tmp1.xlp");
1208 wxRemoveFile("tmp2.xlp");
1210 wxRemoveFile("chapters.xlp");
1211 wxRemoveFile("sections.xlp");
1212 wxRemoveFile("subsections.xlp");
1213 wxRemoveFile("subsubsections.xlp");
1214 wxRemoveFile("index.xlp");