]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: tex2any.cpp | |
3 | // Purpose: Utilities for Latex conversion. | |
4 | // Author: Julian Smart | |
5 | // Modified by: Wlodzimierz ABX Skiba 2003/2004 Unicode support | |
6 | // Ron Lee | |
7 | // Created: 01/01/99 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) Julian Smart | |
10 | // Licence: wxWindows licence | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
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 | |
21 | #endif | |
22 | ||
23 | #include <ctype.h> | |
24 | #include "tex2any.h" | |
25 | #include <stdlib.h> | |
26 | #include <time.h> | |
27 | ||
28 | static inline wxChar* copystring(const wxChar* s) | |
29 | { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); } | |
30 | ||
31 | /* | |
32 | * Variables accessible from clients | |
33 | * | |
34 | */ | |
35 | ||
36 | TexChunk * DocumentTitle = NULL; | |
37 | TexChunk * DocumentAuthor = NULL; | |
38 | TexChunk * DocumentDate = NULL; | |
39 | ||
40 | // Header/footers/pagestyle | |
41 | TexChunk * LeftHeaderEven = NULL; | |
42 | TexChunk * LeftFooterEven = NULL; | |
43 | TexChunk * CentreHeaderEven = NULL; | |
44 | TexChunk * CentreFooterEven = NULL; | |
45 | TexChunk * RightHeaderEven = NULL; | |
46 | TexChunk * RightFooterEven = NULL; | |
47 | TexChunk * LeftHeaderOdd = NULL; | |
48 | TexChunk * LeftFooterOdd = NULL; | |
49 | TexChunk * CentreHeaderOdd = NULL; | |
50 | TexChunk * CentreFooterOdd = NULL; | |
51 | TexChunk * RightHeaderOdd = NULL; | |
52 | TexChunk * RightFooterOdd = NULL; | |
53 | wxChar * PageStyle = copystring(_T("plain")); | |
54 | ||
55 | int DocumentStyle = LATEX_REPORT; | |
56 | int MinorDocumentStyle = 0; | |
57 | wxPathList TexPathList; | |
58 | wxChar * BibliographyStyleString = copystring(_T("plain")); | |
59 | wxChar * DocumentStyleString = copystring(_T("report")); | |
60 | wxChar * MinorDocumentStyleString = NULL; | |
61 | int ParSkip = 0; | |
62 | int ParIndent = 0; | |
63 | ||
64 | int normalFont = 10; | |
65 | int smallFont = 8; | |
66 | int tinyFont = 6; | |
67 | int largeFont1 = 12; | |
68 | int LargeFont2 = 14; | |
69 | int LARGEFont3 = 18; | |
70 | int hugeFont1 = 20; | |
71 | int HugeFont2 = 24; | |
72 | int HUGEFont3 = 28; | |
73 | ||
74 | // All of these tokens MUST be found on a line by themselves (no other | |
75 | // text) and must start at the first character of the line, or tex2rtf | |
76 | // will fail to process them correctly (a limitation of tex2rtf, not TeX) | |
77 | static const wxString syntaxTokens[] = | |
78 | { _T("\\begin{verbatim}"), | |
79 | _T("\\begin{toocomplex}"), | |
80 | _T("\\end{verbatim}"), | |
81 | _T("\\end{toocomplex}"), | |
82 | _T("\\verb"), | |
83 | _T("\\begin{comment}"), | |
84 | _T("\\end{comment}"), | |
85 | _T("\\verbatiminput"), | |
86 | // _T("\\par"), | |
87 | _T("\\input"), | |
88 | _T("\\helpinput"), | |
89 | _T("\\include"), | |
90 | wxEmptyString | |
91 | }; | |
92 | ||
93 | ||
94 | /* | |
95 | * USER-ADJUSTABLE SETTINGS | |
96 | * | |
97 | */ | |
98 | ||
99 | // Section font sizes | |
100 | int chapterFont = 12; // LARGEFont3; | |
101 | int sectionFont = 12; // LargeFont2; | |
102 | int subsectionFont = 12; // largeFont1; | |
103 | int titleFont = LARGEFont3; | |
104 | int authorFont = LargeFont2; | |
105 | int mirrorMargins = true; | |
106 | bool winHelp = false; // Output in Windows Help format if true, linear otherwise | |
107 | bool isInteractive = false; | |
108 | bool runTwice = false; | |
109 | int convertMode = TEX_RTF; | |
110 | bool checkCurlyBraces = false; | |
111 | bool checkSyntax = false; | |
112 | bool headerRule = false; | |
113 | bool footerRule = false; | |
114 | bool compatibilityMode = false; // If true, maximum Latex compatibility | |
115 | // (Quality of RTF generation deteriorate) | |
116 | bool generateHPJ; // Generate WinHelp Help Project file | |
117 | wxChar *winHelpTitle = NULL; // Windows Help title | |
118 | int defaultTableColumnWidth = 2000; | |
119 | ||
120 | int labelIndentTab = 18; // From left indent to item label (points) | |
121 | int itemIndentTab = 40; // From left indent to item (points) | |
122 | ||
123 | bool useUpButton = true; | |
124 | int htmlBrowseButtons = HTML_BUTTONS_TEXT; | |
125 | ||
126 | bool truncateFilenames = false; // Truncate for DOS | |
127 | int winHelpVersion = 3; // WinHelp Version (3 for Windows 3.1, 4 for Win95) | |
128 | bool winHelpContents = false; // Generate .cnt file for WinHelp 4 | |
129 | bool htmlIndex = false; // Generate .htx file for HTML | |
130 | bool htmlFrameContents = false; // Use frames for HTML contents page | |
131 | wxChar *htmlStylesheet = NULL; // Use this CSS stylesheet for HTML pages | |
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 | wxChar *bitmapMethod = copystring(_T("includepicture")); | |
138 | bool upperCaseNames = false; | |
139 | // HTML background and text colours | |
140 | wxChar *backgroundImageString = NULL; | |
141 | wxChar *backgroundColourString = copystring(_T("255;255;255")); | |
142 | wxChar *textColourString = NULL; | |
143 | wxChar *linkColourString = NULL; | |
144 | wxChar *followedLinkColourString = NULL; | |
145 | bool combineSubSections = false; | |
146 | bool htmlWorkshopFiles = false; | |
147 | bool ignoreBadRefs = false; | |
148 | wxChar *htmlFaceName = NULL; | |
149 | ||
150 | extern int passNumber; | |
151 | ||
152 | extern wxHashTable TexReferences; | |
153 | ||
154 | /* | |
155 | * International support | |
156 | */ | |
157 | ||
158 | // Names to help with internationalisation | |
159 | wxChar *ContentsNameString = copystring(_T("Contents")); | |
160 | wxChar *AbstractNameString = copystring(_T("Abstract")); | |
161 | wxChar *GlossaryNameString = copystring(_T("Glossary")); | |
162 | wxChar *ReferencesNameString = copystring(_T("References")); | |
163 | wxChar *FiguresNameString = copystring(_T("List of Figures")); | |
164 | wxChar *TablesNameString = copystring(_T("List of Tables")); | |
165 | wxChar *FigureNameString = copystring(_T("Figure")); | |
166 | wxChar *TableNameString = copystring(_T("Table")); | |
167 | wxChar *IndexNameString = copystring(_T("Index")); | |
168 | wxChar *ChapterNameString = copystring(_T("chapter")); | |
169 | wxChar *SectionNameString = copystring(_T("section")); | |
170 | wxChar *SubsectionNameString = copystring(_T("subsection")); | |
171 | wxChar *SubsubsectionNameString = copystring(_T("subsubsection")); | |
172 | wxChar *UpNameString = copystring(_T("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 | unsigned long LineNumbers[15]; | |
195 | wxChar *FileNames[15]; | |
196 | int CurrentInputIndex = 0; | |
197 | ||
198 | wxChar *TexFileRoot = NULL; | |
199 | wxChar *TexBibName = NULL; // Bibliography output file name | |
200 | wxChar *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 | wxChar *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; | |
210 | int noArgs = 0; | |
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 | wxChar *BigBuffer = NULL; // For reading in large chunks of text | |
217 | TexMacroDef *SoloBlockDef = NULL; | |
218 | TexMacroDef *VerbatimMacroDef = NULL; | |
219 | ||
220 | #define IncrementLineNumber() LineNumbers[CurrentInputIndex] ++ | |
221 | ||
222 | ||
223 | TexRef::TexRef(const wxChar *label, const wxChar *file, | |
224 | const wxChar *section, const wxChar *sectionN) | |
225 | { | |
226 | refLabel = copystring(label); | |
227 | refFile = file ? copystring(file) : (wxChar*) NULL; | |
228 | sectionNumber = section ? copystring(section) : copystring(_T("??")); | |
229 | sectionName = sectionN ? copystring(sectionN) : copystring(_T("??")); | |
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 | ||
249 | void TexOutput(const wxChar *s, bool ordinaryText) | |
250 | { | |
251 | int len = wxStrlen(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 | wxFprintf(CurrentOutput1, _T("%s"), s); | |
267 | if (CurrentOutput2) | |
268 | wxFprintf(CurrentOutput2, _T("%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 | { | |
280 | wxString informBuf; | |
281 | switch (def->forbidden) | |
282 | { | |
283 | case FORBID_WARN: | |
284 | { | |
285 | informBuf.Printf(_T("Warning: it is recommended that command %s is not used."), def->name); | |
286 | OnInform(informBuf); | |
287 | break; | |
288 | } | |
289 | case FORBID_ABSOLUTELY: | |
290 | { | |
291 | informBuf.Printf(_T("Error: command %s cannot be used and will lead to errors."), def->name); | |
292 | OnInform(informBuf); | |
293 | break; | |
294 | } | |
295 | default: | |
296 | break; | |
297 | } | |
298 | } | |
299 | ||
300 | TexMacroDef *MatchMacro(wxChar *buffer, int *pos, wxChar **env, bool *parseToBrace) | |
301 | { | |
302 | *parseToBrace = true; | |
303 | int i = (*pos); | |
304 | TexMacroDef *def = NULL; | |
305 | wxChar macroBuf[40]; | |
306 | ||
307 | // First, try to find begin{thing} | |
308 | if (wxStrncmp(buffer+i, _T("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 | |
329 | { | |
330 | return NULL; | |
331 | } | |
332 | } | |
333 | ||
334 | // Failed, so try to find macro from definition list | |
335 | int j = i; | |
336 | ||
337 | // First try getting a one-character macro, but ONLY | |
338 | // if these TWO characters are not both alphabetical (could | |
339 | // be a longer macro) | |
340 | if (!(isalpha(buffer[i]) && isalpha(buffer[i+1]))) | |
341 | { | |
342 | macroBuf[0] = buffer[i]; | |
343 | macroBuf[1] = 0; | |
344 | ||
345 | def = (TexMacroDef *)MacroDefs.Get(macroBuf); | |
346 | if (def) j ++; | |
347 | } | |
348 | ||
349 | if (!def) | |
350 | { | |
351 | while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39)) | |
352 | { | |
353 | macroBuf[j-i] = buffer[j]; | |
354 | j ++; | |
355 | } | |
356 | macroBuf[j-i] = 0; | |
357 | def = (TexMacroDef *)MacroDefs.Get(macroBuf); | |
358 | } | |
359 | ||
360 | if (def) | |
361 | { | |
362 | i = j; | |
363 | ||
364 | // We want to check whether this is a space-consuming macro | |
365 | // (e.g. {\bf word}) | |
366 | // No brace, e.g. \input thing.tex instead of \input{thing}; | |
367 | // or a numeric argument, such as \parindent0pt | |
368 | if ((def->no_args > 0) && ((buffer[i] == 32) || (buffer[i] == '=') || (isdigit(buffer[i])))) | |
369 | { | |
370 | if ((buffer[i] == 32) || (buffer[i] == '=')) | |
371 | i ++; | |
372 | ||
373 | *parseToBrace = false; | |
374 | } | |
375 | *pos = i; | |
376 | ForbidWarning(def); | |
377 | return def; | |
378 | } | |
379 | return NULL; | |
380 | } | |
381 | ||
382 | void EatWhiteSpace(wxChar *buffer, int *pos) | |
383 | { | |
384 | int len = wxStrlen(buffer); | |
385 | int j = *pos; | |
386 | bool keepGoing = true; | |
387 | bool moreLines = true; | |
388 | while ((j < len) && keepGoing && | |
389 | (buffer[j] == 10 || buffer[j] == 13 || buffer[j] == ' ' || buffer[j] == 9)) | |
390 | { | |
391 | j ++; | |
392 | if (j >= len) | |
393 | { | |
394 | if (moreLines) | |
395 | { | |
396 | moreLines = read_a_line(buffer); | |
397 | len = wxStrlen(buffer); | |
398 | j = 0; | |
399 | } | |
400 | else | |
401 | keepGoing = false; | |
402 | } | |
403 | } | |
404 | *pos = j; | |
405 | } | |
406 | ||
407 | bool FindEndEnvironment(wxChar *buffer, int *pos, wxChar *env) | |
408 | { | |
409 | int i = (*pos); | |
410 | ||
411 | // Try to find end{thing} | |
412 | if ((wxStrncmp(buffer+i, _T("end{"), 4) == 0) && | |
413 | (wxStrncmp(buffer+i+4, env, wxStrlen(env)) == 0)) | |
414 | { | |
415 | *pos = i + 5 + wxStrlen(env); | |
416 | return true; | |
417 | } | |
418 | else return false; | |
419 | } | |
420 | ||
421 | bool readingVerbatim = false; | |
422 | bool readInVerbatim = false; // Within a verbatim, but not nec. verbatiminput | |
423 | ||
424 | // Switched this off because e.g. \verb${$ causes it to fail. There is no | |
425 | // detection of \verb yet. | |
426 | // #define CHECK_BRACES 1 | |
427 | ||
428 | unsigned long leftCurly = 0; | |
429 | unsigned long rightCurly = 0; | |
430 | static wxString currentFileName = wxEmptyString; | |
431 | ||
432 | bool read_a_line(wxChar *buf) | |
433 | { | |
434 | if (CurrentInputIndex < 0) | |
435 | { | |
436 | buf[0] = 0; | |
437 | return false; | |
438 | } | |
439 | ||
440 | int ch = -2; | |
441 | unsigned long bufIndex = 0; | |
442 | buf[0] = 0; | |
443 | int lastChar; | |
444 | ||
445 | while (ch != EOF && ch != 10) | |
446 | { | |
447 | if (bufIndex >= MAX_LINE_BUFFER_SIZE) | |
448 | { | |
449 | wxString errBuf; | |
450 | errBuf.Printf(_T("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated."), | |
451 | LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str(), MAX_LINE_BUFFER_SIZE); | |
452 | OnError(errBuf); | |
453 | return false; | |
454 | } | |
455 | ||
456 | if (((bufIndex == 14) && (wxStrncmp(buf, _T("\\end{verbatim}"), 14) == 0)) || | |
457 | ((bufIndex == 16) && (wxStrncmp(buf, _T("\\end{toocomplex}"), 16) == 0))) | |
458 | readInVerbatim = false; | |
459 | ||
460 | lastChar = ch; | |
461 | ch = getc(Inputs[CurrentInputIndex]); | |
462 | ||
463 | if (checkCurlyBraces) | |
464 | { | |
465 | if (ch == '{' && !readInVerbatim && lastChar != _T('\\')) | |
466 | leftCurly++; | |
467 | if (ch == '}' && !readInVerbatim && lastChar != _T('\\')) | |
468 | { | |
469 | rightCurly++; | |
470 | if (rightCurly > leftCurly) | |
471 | { | |
472 | wxString errBuf; | |
473 | errBuf.Printf(_T("An extra right Curly brace ('}') was detected at line %lu inside file %s"), LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str()); | |
474 | OnError(errBuf); | |
475 | ||
476 | // Reduce the count of right Curly braces, so the mismatched count | |
477 | // isn't reported on every line that has a '}' after the first mismatch | |
478 | rightCurly--; | |
479 | } | |
480 | } | |
481 | } | |
482 | ||
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 | |
492 | if (ch1 == 13) | |
493 | getc(Inputs[CurrentInputIndex]); | |
494 | buf[bufIndex] = 0; | |
495 | IncrementLineNumber(); | |
496 | // wxStrcat(buf, "\\par\n"); | |
497 | // i += 6; | |
498 | if (bufIndex+5 >= MAX_LINE_BUFFER_SIZE) | |
499 | { | |
500 | wxString errBuf; | |
501 | errBuf.Printf(_T("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated."), | |
502 | LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE); | |
503 | OnError(errBuf); | |
504 | return false; | |
505 | } | |
506 | wxStrcat(buf, _T("\\par")); | |
507 | bufIndex += 5; | |
508 | ||
509 | } | |
510 | else | |
511 | { | |
512 | ungetc(ch1, Inputs[CurrentInputIndex]); | |
513 | if (bufIndex >= MAX_LINE_BUFFER_SIZE) | |
514 | { | |
515 | wxString errBuf; | |
516 | errBuf.Printf(_T("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated."), | |
517 | LineNumbers[CurrentInputIndex], (const wxChar*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE); | |
518 | OnError(errBuf); | |
519 | return false; | |
520 | } | |
521 | ||
522 | buf[bufIndex] = (wxChar)ch; | |
523 | bufIndex ++; | |
524 | } | |
525 | } | |
526 | else | |
527 | { | |
528 | ||
529 | // Convert embedded characters to RTF equivalents | |
530 | switch(ch) | |
531 | { | |
532 |