]> git.saurik.com Git - wxWidgets.git/blame - utils/tex2rtf/src/xlputils.cpp
regenerated after last changes
[wxWidgets.git] / utils / tex2rtf / src / xlputils.cpp
CommitLineData
9a29912f
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: xlputils.cpp
3// Purpose: Converts Latex to obsolete XLP format
4// Author: Julian Smart
b63b07a8
RL
5// Modified by: Wlodzimiez ABX Skiba 2003/2004 Unicode support
6// Ron Lee
9a29912f
JS
7// Created: 7.9.93
8// RCS-ID: $Id$
9// Copyright: (c) Julian Smart
10// Licence: wxWindows licence
11/////////////////////////////////////////////////////////////////////////////
12
9a29912f
JS
13// For compilers that support precompilation, includes "wx.h".
14#include "wx/wxprec.h"
15
16#ifdef __BORLANDC__
17#pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
9a29912f
JS
21#endif
22
23#include "tex2any.h"
24#include "tex2rtf.h"
25#include <ctype.h>
26
ea172e69
MB
27#if !WXWIN_COMPATIBILITY_2_4
28static inline wxChar* copystring(const wxChar* s)
29 { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); }
30#endif
31
9a29912f
JS
32long currentBlockId = -1;
33static TexChunk *descriptionItemArg = NULL;
34static int indentLevel = 0;
35static int noColumns = 0;
36static int currentTab = 0;
b63b07a8
RL
37static bool tableVerticalLineLeft = false;
38static bool tableVerticalLineRight = false;
39static bool inTable = false;
9a29912f
JS
40static int citeCount = 1;
41wxList hyperLinks(wxKEY_INTEGER);
42wxList hyperLabels(wxKEY_STRING);
43FILE *Index = NULL;
44
3924dd22
GT
45
46extern wxHashTable TexReferences;
47
48
9a29912f
JS
49void PadToTab(int tabPos)
50{
51 int currentCol = GetCurrentColumn();
52 for (int i = currentCol; i < tabPos; i++)
b63b07a8 53 TexOutput(_T(" "), true);
9a29912f
JS
54}
55
56static long xlpBlockId = 0;
57long NewBlockId(void)
58{
59 return xlpBlockId ++;
60}
61
62// Called on start/end of macro examination
63void XLPOnMacro(int macroId, int no_args, bool start)
64{
6c155d33 65 wxChar buf[100];
9a29912f
JS
66 switch (macroId)
67 {
68 case ltCHAPTER:
69 case ltCHAPTERSTAR:
70 case ltCHAPTERHEADING:
71 {
72 if (!start)
73 {
74 sectionNo = 0;
75 figureNo = 0;
76 subsectionNo = 0;
77 subsubsectionNo = 0;
78 if (macroId != ltCHAPTERSTAR)
79 chapterNo ++;
80
81 SetCurrentOutputs(Contents, Chapters);
82 long id1 = NewBlockId();
83 currentBlockId = NewBlockId();
84
b63b07a8 85 startedSections = true;
6c155d33
JS
86 wxFprintf(Contents, _T("\\hy-%d{%ld}{"), hyBLOCK_SMALL_HEADING, id1);
87 wxFprintf(Chapters, _T("\n\\hy-%d{%ld}{"), hyBLOCK_LARGE_VISIBLE_SECTION, currentBlockId);
88 wxFprintf(Index, _T("%ld %ld\n"), id1, currentBlockId);
9a29912f
JS
89
90 OutputCurrentSection(); // Repeat section header
91
6c155d33
JS
92 wxFprintf(Contents, _T("}\n\n"));
93 wxFprintf(Chapters, _T("}\n\n"));
9a29912f 94 SetCurrentOutput(Chapters);
6c155d33 95 wxChar *topicName = FindTopicName(GetNextChunk());
9a29912f
JS
96 hyperLabels.Append(topicName, (wxObject *)currentBlockId);
97 }
98 break;
99 }
100 case ltSECTION:
101 case ltSECTIONSTAR:
102 case ltSECTIONHEADING:
103 case ltGLOSS:
104 {
105 if (!start)
106 {
107 subsectionNo = 0;
108 subsubsectionNo = 0;
109
110 if (macroId != ltSECTIONSTAR)
111 sectionNo ++;
5c66e5b2 112
9a29912f
JS
113 SetCurrentOutputs(Chapters, Sections);
114 long id1 = NewBlockId();
115 currentBlockId = NewBlockId();
116
b63b07a8 117 startedSections = true;
9a29912f
JS
118
119 if (DocumentStyle == LATEX_ARTICLE)
6c155d33 120 wxFprintf(Contents, _T("\\hy-%d{%ld}{"), hyBLOCK_LARGE_HEADING, id1);
9a29912f 121 else
6c155d33
JS
122 wxFprintf(Chapters, _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD, id1);
123 wxFprintf(Sections, _T("\n\\hy-%d{%ld}{"), hyBLOCK_LARGE_VISIBLE_SECTION, currentBlockId);
124 wxFprintf(Index, _T("%ld %ld\n"), id1, currentBlockId);
9a29912f
JS
125
126 OutputCurrentSection(); // Repeat section header
127
128 if (DocumentStyle == LATEX_ARTICLE)
6c155d33 129 wxFprintf(Contents, _T("}\n\n"));
9a29912f 130 else
6c155d33
JS
131 wxFprintf(Chapters, _T("}\n\n"));
132 wxFprintf(Sections, _T("}\n\n"));
9a29912f 133 SetCurrentOutput(Sections);
6c155d33 134 wxChar *topicName = FindTopicName(GetNextChunk());
9a29912f
JS
135 hyperLabels.Append(topicName, (wxObject *)currentBlockId);
136 }
137 break;
138 }
139 case ltSUBSECTION:
140 case ltSUBSECTIONSTAR:
141 case ltMEMBERSECTION:
142 case ltFUNCTIONSECTION:
143 {
144 if (!start)
145 {
146 subsubsectionNo = 0;
147
148 if (macroId != ltSUBSECTIONSTAR)
149 subsectionNo ++;
150
151 SetCurrentOutputs(Sections, Subsections);
152 long id1 = NewBlockId();
153 currentBlockId = NewBlockId();
6c155d33
JS
154 wxFprintf(Sections, _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD, id1);
155 wxFprintf(Subsections, _T("\n\\hy-%d{%ld}{"), hyBLOCK_LARGE_VISIBLE_SECTION, currentBlockId);
156 wxFprintf(Index, _T("%ld %ld\n"), id1, currentBlockId);
9a29912f
JS
157
158 OutputCurrentSection(); // Repeat section header
159
6c155d33
JS
160 wxFprintf(Sections, _T("}\n\n"));
161 wxFprintf(Subsections, _T("}\n\n"));
9a29912f 162 SetCurrentOutput(Subsections);
6c155d33 163 wxChar *topicName = FindTopicName(GetNextChunk());
9a29912f
JS
164 hyperLabels.Append(topicName, (wxObject *)currentBlockId);
165 }
166 break;
167 }
168 case ltSUBSUBSECTION:
169 case ltSUBSUBSECTIONSTAR:
170 {
171 if (!start)
172 {
173 if (macroId != ltSUBSUBSECTIONSTAR)
174 subsubsectionNo ++;
175
176 SetCurrentOutputs(Subsections, Subsubsections);
177 long id1 = NewBlockId();
178 currentBlockId = NewBlockId();
6c155d33
JS
179 wxFprintf(Subsections, _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD, id1);
180 wxFprintf(Subsubsections, _T("\n\\hy-%d{%ld}{"), hyBLOCK_LARGE_VISIBLE_SECTION, currentBlockId);
181 wxFprintf(Index, _T("%ld %ld\n"), id1, currentBlockId);
9a29912f
JS
182
183 OutputCurrentSection(); // Repeat section header
184
6c155d33
JS
185 wxFprintf(Subsections, _T("}\n\n"));
186 wxFprintf(Subsubsections, _T("}\n\n"));
9a29912f 187 SetCurrentOutput(Subsubsections);
6c155d33 188 wxChar *topicName = FindTopicName(GetNextChunk());
9a29912f
JS
189 hyperLabels.Append(topicName, (wxObject *)currentBlockId);
190 }
191 break;
192 }
193 case ltFUNC:
194 case ltPFUNC:
195 case ltMEMBER:
196 {
197 SetCurrentOutput(Subsections);
198 if (start)
199 {
200 long id = NewBlockId();
6c155d33 201 wxFprintf(Subsections, _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD, id);
9a29912f
JS
202 }
203 else
6c155d33 204 wxFprintf(Subsections, _T("}"));
9a29912f
JS
205 break;
206 }
207 case ltVOID:
208// if (start)
b63b07a8 209// TexOutput(_T("void"), true);
9a29912f
JS
210 break;
211 case ltBACKSLASHCHAR:
212 if (start)
b63b07a8 213 TexOutput(_T("\n"), true);
9a29912f
JS
214 break;
215 case ltPAR:
216 {
217 if (start)
218 {
219 if (ParSkip > 0)
b63b07a8
RL
220 TexOutput(_T("\n"), true);
221 TexOutput(_T("\n"), true);
9a29912f
JS
222 }
223 break;
224 }
225 case ltRMFAMILY:
226 case ltTEXTRM:
227 case ltRM:
228 {
229 break;
230 }
231 case ltTEXTBF:
232 case ltBFSERIES:
233 case ltBF:
234 {
235 if (start)
236 {
6c155d33 237 wxChar buf[100];
9a29912f 238 long id = NewBlockId();
b63b07a8 239 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD, id);
9a29912f
JS
240 TexOutput(buf);
241 }
6c155d33 242 else TexOutput(_T("}"));
9a29912f
JS
243 break;
244 }
245 case ltTEXTIT:
246 case ltITSHAPE:
247 case ltIT:
248 {
249 if (start)
250 {
6c155d33 251 wxChar buf[100];
9a29912f 252 long id = NewBlockId();
b63b07a8 253 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_ITALIC, id);
9a29912f
JS
254 TexOutput(buf);
255 }
6c155d33 256 else TexOutput(_T("}"));
9a29912f
JS
257 break;
258 }
259 case ltTTFAMILY:
260 case ltTEXTTT:
261 case ltTT:
262 {
263 if (start)
264 {
265 long id = NewBlockId();
b63b07a8 266 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_TELETYPE, id);
9a29912f
JS
267 TexOutput(buf);
268 }
6c155d33 269 else TexOutput(_T("}"));
9a29912f
JS
270 break;
271 }
272 case ltSMALL:
273 {
274 if (start)
275 {
b63b07a8 276 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_SMALL_TEXT, NewBlockId());
9a29912f
JS
277 TexOutput(buf);
278 }
6c155d33 279 else TexOutput(_T("}"));
9a29912f
JS
280 break;
281 }
282 case ltTINY:
283 {
284 if (start)
285 {
b63b07a8 286 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_SMALL_TEXT, NewBlockId());
9a29912f
JS
287 TexOutput(buf);
288 }
6c155d33 289 else TexOutput(_T("}"));
9a29912f
JS
290 break;
291 }
292 case ltNORMALSIZE:
293 {
294 if (start)
295 {
b63b07a8 296 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_NORMAL, NewBlockId());
9a29912f
JS
297 TexOutput(buf);
298 }
6c155d33 299 else TexOutput(_T("}"));
9a29912f
JS
300 break;
301 }
302 case ltlarge:
303 {
304 if (start)
305 {
b63b07a8 306 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_SMALL_HEADING, NewBlockId());
9a29912f
JS
307 TexOutput(buf);
308 }
6c155d33 309 else TexOutput(_T("}\n"));
9a29912f
JS
310 break;
311 }
312 case ltLARGE:
313 {
314 if (start)
315 {
b63b07a8 316 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_LARGE_HEADING, NewBlockId());
9a29912f
JS
317 TexOutput(buf);
318 }
6c155d33 319 else TexOutput(_T("}\n"));
9a29912f
JS
320 break;
321 }
322 case ltITEMIZE:
323 case ltENUMERATE:
324 case ltDESCRIPTION:
325 case ltTWOCOLLIST:
326 {
327 if (start)
328 {
329// tabCount ++;
330
331// if (indentLevel > 0)
6c155d33 332// TexOutput(_T("\\par\\par\n"));
9a29912f
JS
333 indentLevel ++;
334 int listType;
335 if (macroId == ltENUMERATE)
336 listType = LATEX_ENUMERATE;
337 else if (macroId == ltITEMIZE)
338 listType = LATEX_ITEMIZE;
339 else
340 listType = LATEX_DESCRIPTION;
341 itemizeStack.Insert(new ItemizeStruc(listType));
342
343 }
344 else
345 {
346 indentLevel --;
347
ddc4f3b5 348 if (itemizeStack.GetFirst())
9a29912f 349 {
ddc4f3b5 350 ItemizeStruc *struc = (ItemizeStruc *)itemizeStack.GetFirst()->GetData();
9a29912f 351 delete struc;
ddc4f3b5 352 delete itemizeStack.GetFirst();
9a29912f
JS
353 }
354 }
355 break;
356 }
357 case ltITEM:
358 {
ddc4f3b5 359 wxNode *node = itemizeStack.GetFirst();
9a29912f
JS
360 if (node)
361 {
ddc4f3b5 362 ItemizeStruc *struc = (ItemizeStruc *)node->GetData();
9a29912f
JS
363 if (!start)
364 {
365 struc->currentItem += 1;
6c155d33 366 wxChar indentBuf[30];
9a29912f
JS
367
368 switch (struc->listType)
369 {
370 case LATEX_ENUMERATE:
371 {
b63b07a8 372 wxSnprintf(indentBuf, sizeof(indentBuf), _T("\\hy-%d{%ld}{%d.} "),
9a29912f
JS
373 hyBLOCK_BOLD, NewBlockId(), struc->currentItem);
374 TexOutput(indentBuf);
375 break;
376 }
377 case LATEX_ITEMIZE:
378 {
b63b07a8 379 wxSnprintf(indentBuf, sizeof(indentBuf), _T("\\hy-%d{%ld}{o} "),
9a29912f
JS
380 hyBLOCK_BOLD, NewBlockId());
381 TexOutput(indentBuf);
382 break;
383 }
384 default:
385 case LATEX_DESCRIPTION:
386 {
387 if (descriptionItemArg)
388 {
b63b07a8 389 wxSnprintf(indentBuf, sizeof(indentBuf), _T("\\hy-%d{%ld}{"),
9a29912f
JS
390 hyBLOCK_BOLD, NewBlockId());
391 TexOutput(indentBuf);
392 TraverseChildrenFromChunk(descriptionItemArg);
6c155d33 393 TexOutput(_T("} "));
9a29912f
JS
394 descriptionItemArg = NULL;
395 }
396 break;
397 }
4fe30bce 398 }
9a29912f
JS
399 }
400 }
401 break;
402 }
403 case ltMAKETITLE:
404 {
405 if (start && DocumentTitle && DocumentAuthor)
406 {
b63b07a8 407 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_LARGE_HEADING, NewBlockId());
9a29912f
JS
408 TexOutput(buf);
409 TraverseChildrenFromChunk(DocumentTitle);
6c155d33 410 TexOutput(_T("}\n\n"));
b63b07a8 411 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_SMALL_HEADING, NewBlockId());
9a29912f
JS
412 TexOutput(buf);
413 TraverseChildrenFromChunk(DocumentAuthor);
6c155d33 414 TexOutput(_T("}\n\n"));
9a29912f
JS
415 if (DocumentDate)
416 {
417 TraverseChildrenFromChunk(DocumentDate);
6c155d33 418 TexOutput(_T("\n"));
9a29912f
JS
419 }
420 }
421 break;
422 }
423 case ltTABLEOFCONTENTS:
424 {
425 if (start)
426 {
6c155d33 427 FILE *fd = wxFopen(ContentsName, _T("r"));
9a29912f
JS
428 if (fd)
429 {
430 int ch = getc(fd);
431 while (ch != EOF)
432 {
7f997541 433 wxPutc(ch, Chapters);
9a29912f
JS
434 ch = getc(fd);
435 }
436 fclose(fd);
437 }
438 else
439 {
6c155d33
JS
440 TexOutput(_T("RUN TEX2RTF AGAIN FOR CONTENTS PAGE\n"));
441 OnInform(_T("Run Tex2RTF again to include contents page."));
9a29912f
JS
442 }
443 }
444 break;
445 }
446 case ltHARDY:
447 {
448 if (start)
b63b07a8 449 TexOutput(_T("HARDY"), true);
9a29912f
JS
450 break;
451 }
452 case ltWXCLIPS:
453 {
454 if (start)
b63b07a8 455 TexOutput(_T("wxCLIPS"), true);
9a29912f
JS
456 break;
457 }
458 case ltVERBATIM:
459 {
460 if (start)
461 {
6c155d33 462 wxChar buf[100];
9a29912f 463 long id = NewBlockId();
b63b07a8 464 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_TELETYPE, id);
9a29912f
JS
465 TexOutput(buf);
466 }
6c155d33 467 else TexOutput(_T("}"));
9a29912f
JS
468 break;
469 }
470 case ltHRULE:
471 {
472 if (start)
473 {
b63b07a8 474 TexOutput(_T("\n------------------------------------------------------------------"), true);
9a29912f
JS
475 }
476 break;
477 }
478 case ltHLINE:
479 {
480 if (start)
481 {
b63b07a8 482 TexOutput(_T("--------------------------------------------------------------------------------"), true);
9a29912f
JS
483 }
484 break;
485 }
486 case ltSPECIALAMPERSAND:
487 {
488 if (start)
489 {
490 currentTab ++;
491 int tabPos = (80/noColumns)*currentTab;
492 PadToTab(tabPos);
493 }
494 break;
495 }
496 case ltTABULAR:
497 case ltSUPERTABULAR:
498 {
499 if (start)
500 {
b63b07a8 501 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_TELETYPE, NewBlockId());
9a29912f
JS
502 TexOutput(buf);
503 }
504 else
6c155d33 505 TexOutput(_T("}"));
9a29912f
JS
506 break;
507 }
508 case ltNUMBEREDBIBITEM:
509 {
510 if (!start)
b63b07a8 511 TexOutput(_T("\n\n"), true);
9a29912f
JS
512 break;
513 }
514 case ltCAPTION:
515 case ltCAPTIONSTAR:
516 {
517 if (start)
518 {
519 figureNo ++;
520
6c155d33 521 wxChar figBuf[40];
9a29912f 522 if (DocumentStyle != LATEX_ARTICLE)
b63b07a8 523 wxSnprintf(figBuf, sizeof(figBuf), _T("Figure %d.%d: "), chapterNo, figureNo);
9a29912f 524 else
b63b07a8 525 wxSnprintf(figBuf, sizeof(figBuf), _T("Figure %d: "), figureNo);
9a29912f
JS
526
527 TexOutput(figBuf);
528 }
529 else
530 {
6c155d33 531 wxChar *topicName = FindTopicName(GetNextChunk());
9a29912f
JS
532
533 AddTexRef(topicName, NULL, NULL,
534 ((DocumentStyle != LATEX_ARTICLE) ? chapterNo : figureNo),
535 ((DocumentStyle != LATEX_ARTICLE) ? figureNo : 0));
536 }
537 break;
538 }
539 default:
540 {
541 DefaultOnMacro(macroId, no_args, start);
542 break;
543 }
544 }
545}
546
547bool XLPOnArgument(int macroId, int arg_no, bool start)
548{
6c155d33 549 wxChar buf[300];
9a29912f
JS
550 switch (macroId)
551 {
552 case ltCHAPTER:
553 case ltCHAPTERSTAR:
554 case ltCHAPTERHEADING:
555 case ltSECTION:
556 case ltSECTIONSTAR:
557 case ltSECTIONHEADING:
558 case ltSUBSECTION:
559 case ltSUBSECTIONSTAR:
560 case ltSUBSUBSECTION:
561 case ltSUBSUBSECTIONSTAR:
562 case ltGLOSS:
563 case ltMEMBERSECTION:
564 case ltFUNCTIONSECTION:
565 {
566 if (!start && (arg_no == 1))
567 currentSection = GetArgChunk();
b63b07a8 568 return false;
9a29912f
JS
569 }
570 case ltFUNC:
571 {
572 if (!start && (arg_no == 1))
b63b07a8 573 TexOutput(_T(" "), true);
9a29912f 574 if (start && (arg_no == 3))
b63b07a8 575 TexOutput(_T("("), true);
9a29912f 576 if (!start && (arg_no == 3))
b63b07a8 577 TexOutput(_T(")"), true);
9a29912f
JS
578 break;
579 }
580 case ltPFUNC:
581 {
582 if (!start && (arg_no == 1))
b63b07a8 583 TexOutput(_T(" "), true);
9a29912f
JS
584
585 if (start && (arg_no == 2))
b63b07a8 586 TexOutput(_T("(*"), true);
9a29912f 587 if (!start && (arg_no == 2))
b63b07a8 588 TexOutput(_T(")"), true);
9a29912f
JS
589
590 if (start && (arg_no == 3))
b63b07a8 591 TexOutput(_T("("), true);
9a29912f 592 if (!start && (arg_no == 3))
b63b07a8 593 TexOutput(_T(")"), true);
9a29912f
JS
594 break;
595 }
596 case ltCLIPSFUNC:
597 {
598 if (!start && (arg_no == 1))
b63b07a8 599 TexOutput(_T(" "), true);
9a29912f
JS
600 if (start && (arg_no == 2))
601 {
b63b07a8 602 TexOutput(_T("("), true);
9a29912f 603 long id = NewBlockId();
b63b07a8 604 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_BOLD, id);
9a29912f
JS
605 TexOutput(buf);
606 }
607 if (!start && (arg_no == 2))
608 {
6c155d33 609 TexOutput(_T("}"));
9a29912f
JS
610 }
611 if (!start && (arg_no == 3))
b63b07a8 612 TexOutput(_T(")"), true);
9a29912f
JS
613 break;
614 }
615 case ltPARAM:
616 {
617 if (start && (arg_no == 2))
618 {
619 long id = NewBlockId();
b63b07a8 620 wxSnprintf(buf, sizeof(buf), _T(" \\hy-%d{%ld}{"), hyBLOCK_BOLD, id);
9a29912f
JS
621 TexOutput(buf);
622 }
623 if (!start && (arg_no == 2))
624 {
6c155d33 625 TexOutput(_T("}"));
9a29912f
JS
626 }
627 break;
628 }
629 case ltCPARAM:
630 {
631 if (start && (arg_no == 2))
632 {
633 long id = NewBlockId();
b63b07a8 634 wxSnprintf(buf, sizeof(buf), _T(" \\hy-%d{%ld}{"), hyBLOCK_BOLD, id);
9a29912f
JS
635 TexOutput(buf);
636 }
637 if (!start && (arg_no == 2))
638 {
6c155d33 639 TexOutput(_T("}"));
9a29912f
JS
640 }
641 break;
642 }
643 case ltMEMBER:
644 {
645 if (!start && (arg_no == 1))
b63b07a8 646 TexOutput(_T(" "), true);
9a29912f
JS
647 break;
648 }
649 case ltLABEL:
650 {
b63b07a8 651 return false;
9a29912f
JS
652 }
653 case ltREF:
654 {
655 if (start)
656 {
6c155d33 657 wxChar *sec = NULL;
5c66e5b2 658
6c155d33 659 wxChar *refName = GetArgData();
9a29912f
JS
660 if (refName)
661 {
662 TexRef *texRef = FindReference(refName);
663 if (texRef)
664 {
665 sec = texRef->sectionNumber;
666 }
667 }
668 if (sec)
669 {
670 TexOutput(sec);
671 }
b63b07a8 672 return false;
9a29912f
JS
673 }
674 break;
675 }
676 case ltHELPREF:
677 case ltHELPREFN:
678 case ltPOPREF:
679 {
680 if (arg_no == 1)
681 {
682 if (start)
683 {
684 currentBlockId = NewBlockId();
b63b07a8 685 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{"), hyBLOCK_RED_ITALIC, currentBlockId);
9a29912f
JS
686 TexOutput(buf);
687 }
6c155d33 688 else TexOutput(_T("}"));
9a29912f
JS
689 }
690 if (arg_no == 2)
691 {
692 if (start)
693 {
6c155d33 694 wxChar *label = GetArgData();
9a29912f
JS
695 hyperLinks.Append(currentBlockId, (wxObject *)copystring(label));
696 }
5c66e5b2 697
b63b07a8 698 return false;
9a29912f
JS
699 }
700 break;
701 }
702 case ltURLREF:
703 {
704 if (arg_no == 1)
705 {
b63b07a8 706 return true;
9a29912f
JS
707 }
708 else if (arg_no == 2)
709 {
710 if (start)
6c155d33 711 TexOutput(_T(" ("));
9a29912f 712 else
6c155d33 713 TexOutput(_T(")"));
b63b07a8 714 return true;
9a29912f
JS
715 }
716 break;
717 }
718 case ltITEM:
719 {
720 if (start && IsArgOptional())
721 {
722 descriptionItemArg = GetArgChunk();
b63b07a8 723 return false;
9a29912f
JS
724 }
725 break;
726 }
727 case ltTABULAR:
728 case ltSUPERTABULAR:
729 {
730 if (arg_no == 1)
731 {
732 if (start)
733 {
b63b07a8
RL
734 inTable = true;
735 tableVerticalLineLeft = false;
736 tableVerticalLineRight = false;
9a29912f 737
6c155d33 738 wxChar *alignString = copystring(GetArgData());
9a29912f
JS
739
740 // Count the number of columns
741 noColumns = 0;
6c155d33 742 int len = wxStrlen(alignString);
9a29912f
JS
743 if (len > 0)
744 {
745 if (alignString[0] == '|')
b63b07a8 746 tableVerticalLineLeft = true;
9a29912f 747 if (alignString[len-1] == '|')
b63b07a8 748 tableVerticalLineRight = true;
9a29912f
JS
749 }
750
751 for (int i = 0; i < len; i++)
752 if (isalpha(alignString[i]))
753 noColumns ++;
754
755/*
756 // Experimental
6c155d33 757 TexOutput(_T("\\brdrt\\brdrs"));
9a29912f 758 if (tableVerticalLineLeft)
6c155d33 759 TexOutput(_T("\\brdrl\\brdrs"));
9a29912f 760 if (tableVerticalLineRight)
6c155d33 761 TexOutput(_T("\\brdrr\\brdrs"));
9a29912f
JS
762*/
763
764 // Calculate a rough size for each column
765// int tabPos = 80/noColumns;
766 currentTab = 0;
767
b63b07a8 768 return false;
9a29912f
JS
769 }
770 }
771 else if (arg_no == 2 && !start)
772 {
b63b07a8 773 inTable = false;
9a29912f
JS
774 }
775 else if (arg_no == 2 && start)
b63b07a8 776 return true;
9a29912f
JS
777 break;
778 }
779 case ltMARGINPAR:
780 case ltMARGINPAREVEN:
781 case ltMARGINPARODD:
782 case ltNORMALBOX:
783 case ltNORMALBOXD:
784 {
785 if (start)
786 {
b63b07a8
RL
787 TexOutput(_T("----------------------------------------------------------------------\n"), true);
788 return true;
9a29912f
JS
789 }
790 else
b63b07a8 791 TexOutput(_T("\n----------------------------------------------------------------------\n"), true);
9a29912f
JS
792 break;
793 }
794 case ltBIBITEM:
795 {
6c155d33 796 wxChar buf[100];
9a29912f
JS
797 if (arg_no == 1 && start)
798 {
6c155d33 799 wxChar *citeKey = GetArgData();
9a29912f
JS
800 TexRef *ref = (TexRef *)TexReferences.Get(citeKey);
801 if (ref)
802 {
803 if (ref->sectionNumber) delete[] ref->sectionNumber;
b63b07a8 804 wxSnprintf(buf, sizeof(buf), _T("[%d]"), citeCount);
9a29912f
JS
805 ref->sectionNumber = copystring(buf);
806 }
807
b63b07a8 808 wxSnprintf(buf, sizeof(buf), _T("\\hy-%d{%ld}{[%d]} "), hyBLOCK_BOLD, NewBlockId(), citeCount);
9a29912f
JS
809 TexOutput(buf);
810 citeCount ++;
b63b07a8 811 return false;
9a29912f 812 }
b63b07a8 813 return true;
9a29912f
JS
814 }
815 case ltTHEBIBLIOGRAPHY:
816 {
817 if (start && (arg_no == 1))
818 {
819 citeCount = 1;
820
821 SetCurrentOutput(Chapters);
822
823 SetCurrentOutputs(Contents, Chapters);
824 long id1 = NewBlockId();
825 long id2 = NewBlockId();
6c155d33
JS
826 wxFprintf(Contents, _T("\\hy-%d{%ld}{%s}\n"), hyBLOCK_SMALL_HEADING, id1, ReferencesNameString);
827 wxFprintf(Chapters, _T("\\hy-%d{%ld}{%s}\n\n\n"), hyBLOCK_LARGE_VISIBLE_SECTION, id2, ReferencesNameString);
828 wxFprintf(Index, _T("%ld %ld\n"), id1, id2);
9a29912f
JS
829
830 SetCurrentOutput(Chapters);
b63b07a8 831 return false;
9a29912f
JS
832 }
833 if (!start && (arg_no == 2))
834 {
835 }
b63b07a8 836 return true;
9a29912f
JS
837 }
838 case ltTWOCOLITEM:
839 case ltTWOCOLITEMRULED:
840 {
841 if (start && (arg_no == 2))
6c155d33 842 TexOutput(_T("\n "));
9a29912f
JS
843
844 if (!start && (arg_no == 2))
6c155d33 845 TexOutput(_T("\n"));
b63b07a8 846 return true;
9a29912f
JS
847 }
848 /*
849 * Accents
850 *
851 */
852 case ltACCENT_GRAVE:
853 {
854 if (start)
855 {
6c155d33 856 wxChar *val = GetArgData();
9a29912f
JS
857 if (val)
858 {
859 switch (val[0])
860 {
6c155d33
JS
861 case _T('a'):
862 TexOutput(_T("a"));
9a29912f 863 break;
6c155d33
JS
864 case _T('e'):
865 TexOutput(_T("e"));
9a29912f 866 break;
6c155d33
JS
867 case _T('i'):
868 TexOutput(_T("i"));
9a29912f 869 break;
6c155d33
JS
870 case _T('o'):
871 TexOutput(_T("o"));
9a29912f 872 break;
6c155d33
JS
873 case _T('u'):
874 TexOutput(_T("u"));
9a29912f 875 break;
6c155d33
JS
876 case _T('A'):
877 TexOutput(_T("a"));
9a29912f 878 break;
6c155d33
JS
879 case _T('E'):
880 TexOutput(_T("E"));
9a29912f 881 break;
6c155d33
JS
882 case _T('I'):
883 TexOutput(_T("I"));
9a29912f 884 break;
6c155d33
JS
885 case _T('O'):
886 TexOutput(_T("O"));
9a29912f 887 break;
6c155d33
JS
888 case _T('U'):
889 TexOutput(_T("U"));
9a29912f
JS
890 break;
891 default:
892 break;
893 }
894 }
895 }
b63b07a8 896 return false;
9a29912f
JS
897 }
898 case ltACCENT_ACUTE:
899 {
900 if (start)
901 {
6c155d33 902 wxChar *val = GetArgData();
9a29912f
JS
903 if (val)
904 {
905 switch (val[0])
906 {
6c155d33
JS
907 case _T('a'):
908 TexOutput(_T("a"));
9a29912f 909 break;
6c155d33
JS
910 case _T('e'):
911 TexOutput(_T("e"));
9a29912f 912 break;
6c155d33
JS
913 case _T('i'):
914 TexOutput(_T("i"));
9a29912f 915 break;
6c155d33
JS
916 case _T('o'):
917 TexOutput(_T("o"));
9a29912f 918 break;
6c155d33
JS
919 case _T('u'):
920 TexOutput(_T("u"));
9a29912f 921 break;
6c155d33
JS
922 case _T('y'):
923 TexOutput(_T("y"));
9a29912f 924 break;
6c155d33
JS
925 case _T('A'):
926 TexOutput(_T("A"));
9a29912f 927 break;
6c155d33
JS
928 case _T('E'):
929 TexOutput(_T("E"));
9a29912f 930 break;
6c155d33
JS
931 case _T('I'):
932 TexOutput(_T("I"));
9a29912f 933 break;
6c155d33
JS
934 case _T('O'):
935 TexOutput(_T("O"));
9a29912f 936 break;
6c155d33
JS
937 case _T('U'):
938 TexOutput(_T("U"));
9a29912f 939 break;
6c155d33
JS
940 case _T('Y'):
941 TexOutput(_T("Y"));
9a29912f
JS
942 break;
943 default:
944 break;
945 }
946 }
947 }
b63b07a8 948 return false;
9a29912f
JS
949 }
950 case ltACCENT_CARET:
951 {
952 if (start)
953 {
6c155d33 954 wxChar *val = GetArgData();
9a29912f
JS
955 if (val)
956 {
957 switch (val[0])
958 {
6c155d33
JS
959 case _T('a'):
960 TexOutput(_T("a"));
9a29912f 961 break;
6c155d33
JS
962 case _T('e'):
963 TexOutput(_T("e"));
9a29912f 964 break;
6c155d33
JS
965 case _T('i'):
966 TexOutput(_T("i"));
9a29912f 967 break;
6c155d33
JS
968 case _T('o'):
969 TexOutput(_T("o"));
9a29912f 970 break;
6c155d33
JS
971 case _T('u'):
972 TexOutput(_T("u"));
9a29912f 973 break;
6c155d33
JS
974 case _T('A'):
975 TexOutput(_T("A"));
9a29912f 976 break;
6c155d33
JS
977 case _T('E'):
978 TexOutput(_T("E"));
9a29912f 979 break;
6c155d33
JS
980 case _T('I'):
981 TexOutput(_T("I"));
9a29912f 982 break;
6c155d33
JS
983 case _T('O'):
984 TexOutput(_T("O"));
9a29912f 985 break;
6c155d33
JS
986 case _T('U'):
987 TexOutput(_T("U"));
9a29912f
JS
988 break;
989 default:
990 break;
991 }
992 }
993 }
b63b07a8 994 return false;
9a29912f
JS
995 }
996 case ltACCENT_TILDE:
997 {
998 if (start)
999 {
6c155d33 1000 wxChar *val = GetArgData();
9a29912f
JS
1001 if (val)
1002 {
1003 switch (val[0])
1004 {
6c155d33
JS
1005 case _T('a'):
1006 TexOutput(_T("a"));
9a29912f 1007 break;
6c155d33
JS
1008 case _T(' '):
1009 TexOutput(_T("~"));
9a29912f 1010 break;
6c155d33
JS
1011 case _T('n'):
1012 TexOutput(_T("n"));
9a29912f 1013 break;
6c155d33
JS
1014 case _T('o'):
1015 TexOutput(_T("o"));
9a29912f 1016 break;
6c155d33
JS
1017 case _T('A'):
1018 TexOutput(_T("A"));
9a29912f 1019 break;
6c155d33
JS
1020 case _T('N'):
1021 TexOutput(_T("N"));
9a29912f 1022 break;
6c155d33
JS
1023 case _T('O'):
1024 TexOutput(_T("O"));
9a29912f
JS
1025 break;
1026 default:
1027 break;
1028 }
1029 }
1030 }
b63b07a8 1031 return false;
9a29912f
JS
1032 }
1033 case ltACCENT_UMLAUT:
1034 {
1035 if (start)
1036 {
6c155d33 1037 wxChar *val = GetArgData();
9a29912f
JS
1038 if (val)
1039 {
1040 switch (val[0])
1041 {
6c155d33
JS
1042 case _T('a'):
1043 TexOutput(_T("a"));
9a29912f 1044 break;
6c155d33
JS
1045 case _T('e'):
1046 TexOutput(_T("e"));
9a29912f 1047 break;
6c155d33
JS
1048 case _T('i'):
1049 TexOutput(_T("i"));
9a29912f 1050 break;
6c155d33
JS
1051 case _T('o'):
1052 TexOutput(_T("o"));
9a29912f 1053 break;
6c155d33
JS
1054 case _T('u'):
1055 TexOutput(_T("u"));
9a29912f 1056 break;
6c155d33
JS
1057 case _T('y'):
1058 TexOutput(_T("y"));
9a29912f 1059 break;
6c155d33
JS
1060 case _T('A'):
1061 TexOutput(_T("A"));
9a29912f 1062 break;
6c155d33
JS
1063 case _T('E'):
1064 TexOutput(_T("E"));
9a29912f 1065 break;
6c155d33
JS
1066 case _T('I'):
1067 TexOutput(_T("I"));
9a29912f 1068 break;
6c155d33
JS
1069 case _T('O'):
1070 TexOutput(_T("O"));
9a29912f 1071 break;
6c155d33
JS
1072 case _T('U'):
1073 TexOutput(_T("U"));
9a29912f 1074 break;
6c155d33
JS
1075 case _T('Y'):
1076 TexOutput(_T("Y"));
9a29912f
JS
1077 break;
1078 default:
1079 break;
1080 }
1081 }
1082 }
b63b07a8 1083 return false;
9a29912f
JS
1084 }
1085 case ltACCENT_DOT:
1086 {
1087 if (start)
1088 {
6c155d33 1089 wxChar *val = GetArgData();
9a29912f
JS
1090 if (val)
1091 {
1092 switch (val[0])
1093 {
6c155d33
JS
1094 case _T('a'):
1095 TexOutput(_T("a"));
9a29912f 1096 break;
6c155d33
JS
1097 case _T('A'):
1098 TexOutput(_T("A"));
9a29912f
JS
1099 break;
1100 default:
1101 break;
1102 }
1103 }
1104 }
b63b07a8 1105 return false;
9a29912f
JS
1106 }
1107 case ltACCENT_CADILLA:
1108 {
1109 if (start)
1110 {
6c155d33 1111 wxChar *val = GetArgData();
9a29912f
JS
1112 if (val)
1113 {
1114 switch (val[0])
1115 {
6c155d33
JS
1116 case _T('c'):
1117 TexOutput(_T("c"));
9a29912f 1118 break;
6c155d33
JS
1119 case _T('C'):
1120 TexOutput(_T("C"));
9a29912f
JS
1121 break;
1122 default:
1123 break;
1124 }
1125 }
1126 }
b63b07a8 1127 return false;
9a29912f
JS
1128 }
1129 default:
1130 {
1131 return DefaultOnArgument(macroId, arg_no, start);
9a29912f
JS
1132 }
1133 }
b63b07a8 1134 return true;
9a29912f
JS
1135}
1136
1137bool XLPGo(void)
1138{
1139 xlpBlockId = 0;
1140
5c66e5b2 1141 if (!InputFile.empty() && !OutputFile.empty())
9a29912f 1142 {
6c155d33
JS
1143 Contents = wxFopen(TmpContentsName, _T("w"));
1144 Chapters = wxFopen(_T("chapters.xlp"), _T("w"));
1145 Sections = wxFopen(_T("sections.xlp"), _T("w"));
1146 Subsections = wxFopen(_T("subsections.xlp"), _T("w"));
1147 Subsubsections = wxFopen(_T("subsubsections.xlp"), _T("w"));
1148 Index = wxFopen(_T("index.xlp"), _T("w"));
9a29912f
JS
1149
1150 // Insert invisible section marker at beginning
6c155d33
JS
1151 wxFprintf(Chapters, _T("\\hy-%d{%ld}{%s}\n"),
1152 hyBLOCK_INVISIBLE_SECTION, NewBlockId(), _T("\n"));
9a29912f 1153
6c155d33 1154 wxFprintf(Contents, _T("\\hy-%d{%ld}{%s}\n\n"),
9a29912f
JS
1155// hyBLOCK_LARGE_HEADING, NewBlockId(), "\n\n%s\n\n", ContentsNameString);
1156 hyBLOCK_LARGE_HEADING, NewBlockId(), ContentsNameString);
1157
1158 SetCurrentOutput(Chapters);
1159
6c155d33
JS
1160 wxFprintf(Index, _T("\n\\hyindex{\n\"%s\"\n"),
1161 contentsString ? contentsString : _T("WXHELPCONTENTS"));
9a29912f
JS
1162 TraverseDocument();
1163
ddc4f3b5 1164 wxNode *node = hyperLinks.GetFirst();
9a29912f
JS
1165 while (node)
1166 {
1167 long from = node->GetKeyInteger();
6c155d33 1168 wxChar *label = (wxChar *)node->GetData();
9a29912f
JS
1169 wxNode *otherNode = hyperLabels.Find(label);
1170 if (otherNode)
1171 {
ddc4f3b5 1172 long to = (long)otherNode->GetData();
6c155d33 1173 wxFprintf(Index, _T("%ld %ld\n"), from, to);
9a29912f 1174 }
ddc4f3b5 1175 node = node->GetNext();
9a29912f
JS
1176 }
1177
6c155d33 1178 wxFprintf(Index, _T("}\n"));
9a29912f
JS
1179
1180 fclose(Contents); Contents = NULL;
1181 fclose(Chapters); Chapters = NULL;
1182 fclose(Sections); Sections = NULL;
1183 fclose(Subsections); Subsections = NULL;
1184 fclose(Subsubsections); Subsubsections = NULL;
1185 fclose(Index); Index = NULL;
1186
2b5f62a0 1187 if (wxFileExists(ContentsName)) wxRemoveFile(ContentsName);
9a29912f
JS
1188
1189 if (!wxRenameFile(TmpContentsName, ContentsName))
1190 {
1191 wxCopyFile(TmpContentsName, ContentsName);
1192 wxRemoveFile(TmpContentsName);
1193 }
1194
6c155d33
JS
1195 wxConcatFiles(_T("chapters.xlp"), _T("sections.xlp"), _T("tmp2.xlp"));
1196 wxConcatFiles(_T("tmp2.xlp"), _T("subsections.xlp"), _T("tmp1.xlp"));
1197 wxConcatFiles(_T("tmp1.xlp"), _T("subsubsections.xlp"), _T("tmp2.xlp"));
1198 wxConcatFiles(_T("tmp2.xlp"), _T("index.xlp"), OutputFile);
9a29912f 1199
6c155d33
JS
1200 wxRemoveFile(_T("tmp1.xlp"));
1201 wxRemoveFile(_T("tmp2.xlp"));
9a29912f 1202
6c155d33
JS
1203 wxRemoveFile(_T("chapters.xlp"));
1204 wxRemoveFile(_T("sections.xlp"));
1205 wxRemoveFile(_T("subsections.xlp"));
1206 wxRemoveFile(_T("subsubsections.xlp"));
1207 wxRemoveFile(_T("index.xlp"));
b63b07a8 1208 return true;
9a29912f 1209 }
b63b07a8 1210 return false;
9a29912f 1211}