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