]> git.saurik.com Git - wxWidgets.git/blame - utils/tex2rtf/src/tex2any.cpp
Added ignoreBadRefs so e.g. the PMF manual doesn't generate bad references to
[wxWidgets.git] / utils / tex2rtf / src / tex2any.cpp
CommitLineData
9a29912f
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: tex2any.cpp
3// Purpose: Utilities for Latex conversion.
4// Author: Julian Smart
5// Modified by:
6// Created: 01/01/99
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/wx.h"
25#endif
26
27#include <ctype.h>
28#include "tex2any.h"
29#include <stdlib.h>
30#include <time.h>
31
32/*
33 * Variables accessible from clients
34 *
35 */
36
37TexChunk * DocumentTitle = NULL;
38TexChunk * DocumentAuthor = NULL;
39TexChunk * DocumentDate = NULL;
40
41// Header/footers/pagestyle
42TexChunk * LeftHeaderEven = NULL;
43TexChunk * LeftFooterEven = NULL;
44TexChunk * CentreHeaderEven = NULL;
45TexChunk * CentreFooterEven = NULL;
46TexChunk * RightHeaderEven = NULL;
47TexChunk * RightFooterEven = NULL;
48TexChunk * LeftHeaderOdd = NULL;
49TexChunk * LeftFooterOdd = NULL;
50TexChunk * CentreHeaderOdd = NULL;
51TexChunk * CentreFooterOdd = NULL;
52TexChunk * RightHeaderOdd = NULL;
53TexChunk * RightFooterOdd = NULL;
54char * PageStyle = copystring("plain");
55
56int DocumentStyle = LATEX_REPORT;
57int MinorDocumentStyle = 0;
58wxPathList TexPathList;
59char * BibliographyStyleString = copystring("plain");
60char * DocumentStyleString = copystring("report");
61char * MinorDocumentStyleString = NULL;
62int ParSkip = 0;
63int ParIndent = 0;
64
65int normalFont = 10;
66int smallFont = 8;
67int tinyFont = 6;
68int largeFont1 = 12;
69int LargeFont2 = 14;
70int LARGEFont3 = 18;
71int hugeFont1 = 20;
72int HugeFont2 = 24;
73int HUGEFont3 = 28;
74
fad535ee
GT
75// All of these tokens MUST be found on a line by themselves (no other
76// text) and must start at the first character of the line, or tex2rtf
77// will fail to process them correctly (a limitation of tex2rtf, not TeX)
7cbe4e79 78static const wxString syntaxTokens[] =
fad535ee
GT
79{ "\\begin{verbatim}",
80 "\\begin{toocomplex}",
81 "\\end{verbatim}",
82 "\\end{toocomplex}",
83 "\\verb",
84 "\\begin{comment}",
85 "\\end{comment}",
86 "\\verbatiminput",
341479ff 87// "\\par",
fad535ee
GT
88 "\\input",
89 "\\helpinput",
90 "\\include",
91 wxEmptyString
92};
93
94
9a29912f
JS
95/*
96 * USER-ADJUSTABLE SETTINGS
97 *
98 */
99
100// Section font sizes
101int chapterFont = 12; // LARGEFont3;
102int sectionFont = 12; // LargeFont2;
103int subsectionFont = 12; // largeFont1;
104int titleFont = LARGEFont3;
105int authorFont = LargeFont2;
106int mirrorMargins = TRUE;
107bool winHelp = FALSE; // Output in Windows Help format if TRUE, linear otherwise
108bool isInteractive = FALSE;
109bool runTwice = FALSE;
110int convertMode = TEX_RTF;
fad535ee
GT
111bool checkCurleyBraces = FALSE;
112bool checkSyntax = FALSE;
9a29912f
JS
113bool headerRule = FALSE;
114bool footerRule = FALSE;
115bool compatibilityMode = FALSE; // If TRUE, maximum Latex compatibility
116 // (Quality of RTF generation deteriorate)
117bool generateHPJ; // Generate WinHelp Help Project file
118char *winHelpTitle = NULL; // Windows Help title
119int defaultTableColumnWidth = 2000;
120
121int labelIndentTab = 18; // From left indent to item label (points)
122int itemIndentTab = 40; // From left indent to item (points)
123
124bool useUpButton = TRUE;
125int htmlBrowseButtons = HTML_BUTTONS_TEXT;
126
127bool truncateFilenames = FALSE; // Truncate for DOS
128int winHelpVersion = 3; // WinHelp Version (3 for Windows 3.1, 4 for Win95)
129bool winHelpContents = FALSE; // Generate .cnt file for WinHelp 4
130bool htmlIndex = FALSE; // Generate .htx file for HTML
131bool htmlFrameContents = FALSE; // Use frames for HTML contents page
132bool useHeadingStyles = TRUE; // Insert \s1, s2 etc.
133bool useWord = TRUE; // Insert proper Word table of contents, etc etc
134int contentsDepth = 4; // Depth of Word table of contents
135bool indexSubsections = TRUE; // Index subsections in linear RTF
136// Linear RTF method of including bitmaps. Can be "includepicture", "hex"
137char *bitmapMethod = copystring("includepicture");
138bool upperCaseNames = FALSE;
139// HTML background and text colours
140char *backgroundImageString = NULL;
141char *backgroundColourString = copystring("255;255;255");
142char *textColourString = NULL;
143char *linkColourString = NULL;
144char *followedLinkColourString = NULL;
145bool combineSubSections = FALSE;
14204c7a 146bool htmlWorkshopFiles = FALSE;
bf16085d 147bool ignoreBadRefs = FALSE;
9a29912f 148
3924dd22
GT
149extern int passNumber;
150
151extern wxHashTable TexReferences;
152
9a29912f
JS
153/*
154 * International support
155 */
156
157// Names to help with internationalisation
158char *ContentsNameString = copystring("Contents");
159char *AbstractNameString = copystring("Abstract");
160char *GlossaryNameString = copystring("Glossary");
161char *ReferencesNameString = copystring("References");
162char *FiguresNameString = copystring("List of Figures");
163char *TablesNameString = copystring("List of Tables");
164char *FigureNameString = copystring("Figure");
165char *TableNameString = copystring("Table");
166char *IndexNameString = copystring("Index");
167char *ChapterNameString = copystring("chapter");
168char *SectionNameString = copystring("section");
169char *SubsectionNameString = copystring("subsection");
170char *SubsubsectionNameString = copystring("subsubsection");
171char *UpNameString = copystring("Up");
172
173/*
174 * Section numbering
175 *
176 */
177
178int chapterNo = 0;
179int sectionNo = 0;
180int subsectionNo = 0;
181int subsubsectionNo = 0;
182int figureNo = 0;
183int tableNo = 0;
184
185/*
186 * Other variables
187 *
188 */
189
190FILE *CurrentOutput1 = NULL;
191FILE *CurrentOutput2 = NULL;
192FILE *Inputs[15];
193int LineNumbers[15];
194char *FileNames[15];
195int CurrentInputIndex = 0;
196
197char *TexFileRoot = NULL;
198char *TexBibName = NULL; // Bibliography output file name
199char *TexTmpBibName = NULL; // Temporary bibliography output file name
200bool isSync = FALSE; // If TRUE, should not yield to other processes.
201bool stopRunning = FALSE; // If TRUE, should abort.
202
203static int currentColumn = 0;
204char *currentArgData = NULL;
205bool haveArgData = FALSE; // If TRUE, we're simulating the data.
206TexChunk *currentArgument = NULL;
207TexChunk *nextChunk = NULL;
208bool isArgOptional = FALSE;
446dd881 209int noArgs = 0;
9a29912f
JS
210
211TexChunk *TopLevel = NULL;
212// wxList MacroDefs(wxKEY_STRING);
213wxHashTable MacroDefs(wxKEY_STRING);
214wxStringList IgnorableInputFiles; // Ignorable \input files, e.g. psbox.tex
215char *BigBuffer = NULL; // For reading in large chunks of text
216TexMacroDef *SoloBlockDef = NULL;
217TexMacroDef *VerbatimMacroDef = NULL;
218
219#define IncrementLineNumber() LineNumbers[CurrentInputIndex] ++
220
3924dd22 221
bbd08c6a
GD
222TexRef::TexRef(const char *label, const char *file,
223 const char *section, const char *sectionN)
3924dd22
GT
224{
225 refLabel = copystring(label);
226 refFile = file ? copystring(file) : (char*) NULL;
227 sectionNumber = section ? copystring(section) : copystring("??");
228 sectionName = sectionN ? copystring(sectionN) : copystring("??");
229}
230
231TexRef::~TexRef(void)
232{
233 delete [] refLabel; refLabel = NULL;
234 delete [] refFile; refFile = NULL;
235 delete [] sectionNumber; sectionNumber = NULL;
236 delete [] sectionName; sectionName = NULL;
237}
238
239
240CustomMacro::~CustomMacro()
241{
242 if (macroName)
243 delete [] macroName;
244 if (macroBody)
245 delete [] macroBody;
246}
247
e4a22366 248void TexOutput(const char *s, bool ordinaryText)
9a29912f
JS
249{
250 int len = strlen(s);
251
252 // Update current column, but only if we're guaranteed to
253 // be ordinary text (not mark-up stuff)
254 int i;
255 if (ordinaryText)
256 for (i = 0; i < len; i++)
257 {
258 if (s[i] == 13 || s[i] == 10)
259 currentColumn = 0;
260 else
261 currentColumn ++;
262 }
263
264 if (CurrentOutput1)
265 fprintf(CurrentOutput1, "%s", s);
266 if (CurrentOutput2)
267 fprintf(CurrentOutput2, "%s", s);
268}
269
270/*
271 * Try to find a Latex macro, in one of the following forms:
272 * (1) \begin{} ... \end{}
273 * (2) \macroname{arg1}...{argn}
274 * (3) {\bf arg1}
275 */
276
277void ForbidWarning(TexMacroDef *def)
278{
04b9c5bb 279 wxString informBuf;
9a29912f
JS
280 switch (def->forbidden)
281 {
282 case FORBID_WARN:
283 {
04b9c5bb
GT
284 informBuf.Printf("Warning: it is recommended that command %s is not used.", def->name);
285 OnInform((char *)informBuf.c_str());
9a29912f
JS
286 break;
287 }
288 case FORBID_ABSOLUTELY:
289 {
04b9c5bb
GT
290 informBuf.Printf("Error: command %s cannot be used and will lead to errors.", def->name);
291 OnInform((char *)informBuf.c_str());
9a29912f
JS
292 break;
293 }
294 default:
295 break;
296 }
297}
298
299TexMacroDef *MatchMacro(char *buffer, int *pos, char **env, bool *parseToBrace)
300{
301 *parseToBrace = TRUE;
302 int i = (*pos);
303 TexMacroDef *def = NULL;
304 char macroBuf[40];
305
306 // First, try to find begin{thing}
307 if (strncmp(buffer+i, "begin{", 6) == 0)
308 {
309 i += 6;
310
311 int j = i;
312 while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39))
313 {
314 macroBuf[j-i] = buffer[j];
315 j ++;
316 }
317 macroBuf[j-i] = 0;
318 def = (TexMacroDef *)MacroDefs.Get(macroBuf);
319
320 if (def)
321 {
322 *pos = j + 1; // BUGBUG Should this be + 1???
323 *env = def->name;
324 ForbidWarning(def);
325 return def;
326 }
327 else return NULL;
328 }
329
330 // Failed, so try to find macro from definition list
331 int j = i;
332
333 // First try getting a one-character macro, but ONLY
334 // if these TWO characters are not both alphabetical (could
335 // be a longer macro)
336 if (!(isalpha(buffer[i]) && isalpha(buffer[i+1])))
337 {
338 macroBuf[0] = buffer[i];
339 macroBuf[1] = 0;
340
341 def = (TexMacroDef *)MacroDefs.Get(macroBuf);
342 if (def) j ++;
343 }
344
345 if (!def)
346 {
347 while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39))
348 {
349 macroBuf[j-i] = buffer[j];
350 j ++;
351 }
352 macroBuf[j-i] = 0;
353 def = (TexMacroDef *)MacroDefs.Get(macroBuf);
354 }
355
356 if (def)
357 {
358 i = j;
359
360 // We want to check whether this is a space-consuming macro
361 // (e.g. {\bf word})
362 // No brace, e.g. \input thing.tex instead of \input{thing};
363 // or a numeric argument, such as \parindent0pt
364 if ((def->no_args > 0) && ((buffer[i] == 32) || (buffer[i] == '=') || (isdigit(buffer[i]))))
365 {
366 if ((buffer[i] == 32) || (buffer[i] == '='))
367 i ++;
368
369 *parseToBrace = FALSE;
370 }
371 *pos = i;
372 ForbidWarning(def);
373 return def;
374 }
375 return NULL;
376}
377
378void EatWhiteSpace(char *buffer, int *pos)
379{
380 int len = strlen(buffer);
381 int j = *pos;
382 bool keepGoing = TRUE;
383 bool moreLines = TRUE;
384 while ((j < len) && keepGoing &&
385 (buffer[j] == 10 || buffer[j] == 13 || buffer[j] == ' ' || buffer[j] == 9))
386 {
387 j ++;
388 if (j >= len)
389 {
390 if (moreLines)
391 {
392 moreLines = read_a_line(buffer);
393 len = strlen(buffer);
394 j = 0;
395 }
396 else
397 keepGoing = FALSE;
398 }
399 }
400 *pos = j;
401}
402
403bool FindEndEnvironment(char *buffer, int *pos, char *env)
404{
405 int i = (*pos);
406
407 // Try to find end{thing}
408 if ((strncmp(buffer+i, "end{", 4) == 0) &&
409 (strncmp(buffer+i+4, env, strlen(env)) == 0))
410 {
411 *pos = i + 5 + strlen(env);
412 return TRUE;
413 }
414 else return FALSE;
415}
416
417bool readingVerbatim = FALSE;
418bool readInVerbatim = FALSE; // Within a verbatim, but not nec. verbatiminput
419
f6bcfd97
BP
420// Switched this off because e.g. \verb${$ causes it to fail. There is no
421// detection of \verb yet.
fad535ee 422// #define CHECK_BRACES 1
f6bcfd97 423
341479ff
GT
424unsigned long leftCurley = 0;
425unsigned long rightCurley = 0;
f6bcfd97
BP
426static wxString currentFileName = "";
427
9a29912f
JS
428bool read_a_line(char *buf)
429{
430 if (CurrentInputIndex < 0)
431 {
432 buf[0] = 0;
433 return FALSE;
434 }
fad535ee 435
9a29912f 436 int ch = -2;
0e6ca394 437 int bufIndex = 0;
9a29912f 438 buf[0] = 0;
f6bcfd97 439
9a29912f
JS
440 while (ch != EOF && ch != 10)
441 {
0e6ca394
GT
442 if (bufIndex >= MAX_LINE_BUFFER_SIZE)
443 {
444 wxString errBuf;
445 errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
446 LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
447 OnError((char *)errBuf.c_str());
448 return FALSE;
449 }
450
451 if (((bufIndex == 14) && (strncmp(buf, "\\end{verbatim}", 14) == 0)) ||
452 ((bufIndex == 16) && (strncmp(buf, "\\end{toocomplex}", 16) == 0)))
9a29912f
JS
453 readInVerbatim = FALSE;
454
455 ch = getc(Inputs[CurrentInputIndex]);
f6bcfd97 456
fad535ee 457 if (checkCurleyBraces)
f6bcfd97 458 {
fad535ee 459 if (ch == '{' && !readInVerbatim)
341479ff 460 leftCurley++;
fad535ee
GT
461 if (ch == '}' && !readInVerbatim)
462 {
341479ff
GT
463 rightCurley++;
464 if (rightCurley > leftCurley)
fad535ee
GT
465 {
466 wxString errBuf;
341479ff 467 errBuf.Printf("An extra right Curley brace ('}') was detected at line %lu inside file %s",LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str());
fad535ee
GT
468 OnError((char *)errBuf.c_str());
469
341479ff 470 // Reduce the count of right Curley braces, so the mismatched count
fad535ee 471 // isn't reported on every line that has a '}' after the first mismatch
341479ff 472 rightCurley--;
fad535ee
GT
473 }
474 }
f6bcfd97 475 }
f6bcfd97 476
9a29912f
JS
477 if (ch != EOF)
478 {
479 // Check for 2 consecutive newlines and replace with \par
480 if (ch == 10 && !readInVerbatim)
481 {
482 int ch1 = getc(Inputs[CurrentInputIndex]);
483 if ((ch1 == 10) || (ch1 == 13))
484 {
485 // Eliminate newline (10) following DOS linefeed
0e6ca394
GT
486 if (ch1 == 13)
487 ch1 = getc(Inputs[CurrentInputIndex]);
488 buf[bufIndex] = 0;
9a29912f
JS
489 IncrementLineNumber();
490// strcat(buf, "\\par\n");
491// i += 6;
0e6ca394
GT
492 if (bufIndex+5 >= MAX_LINE_BUFFER_SIZE)
493 {
494 wxString errBuf;
495 errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
496 LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
497 OnError((char *)errBuf.c_str());
498 return FALSE;
499 }
9a29912f 500 strcat(buf, "\\par");
0e6ca394
GT
501 bufIndex += 5;
502
9a29912f
JS
503 }
504 else
505 {
506 ungetc(ch1, Inputs[CurrentInputIndex]);
0e6ca394
GT
507 if (bufIndex >= MAX_LINE_BUFFER_SIZE)
508 {
509 wxString errBuf;
510 errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
511 LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
512 OnError((char *)errBuf.c_str());
513 return FALSE;
514 }
515
516 buf[bufIndex] = ch;
517 bufIndex ++;
9a29912f
JS
518 }
519 }
520 else
521 {
522
523 // Convert embedded characters to RTF equivalents
fad535ee
GT
524 switch(ch)
525 {
526