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