]>
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 | ||
75 | /* | |
76 | * USER-ADJUSTABLE SETTINGS | |
77 | * | |
78 | */ | |
79 | ||
80 | // Section font sizes | |
81 | int chapterFont = 12; // LARGEFont3; | |
82 | int sectionFont = 12; // LargeFont2; | |
83 | int subsectionFont = 12; // largeFont1; | |
84 | int titleFont = LARGEFont3; | |
85 | int authorFont = LargeFont2; | |
86 | int mirrorMargins = TRUE; | |
87 | bool winHelp = FALSE; // Output in Windows Help format if TRUE, linear otherwise | |
88 | bool isInteractive = FALSE; | |
89 | bool runTwice = FALSE; | |
90 | int convertMode = TEX_RTF; | |
91 | bool headerRule = FALSE; | |
92 | bool footerRule = FALSE; | |
93 | bool compatibilityMode = FALSE; // If TRUE, maximum Latex compatibility | |
94 | // (Quality of RTF generation deteriorate) | |
95 | bool generateHPJ; // Generate WinHelp Help Project file | |
96 | char *winHelpTitle = NULL; // Windows Help title | |
97 | int defaultTableColumnWidth = 2000; | |
98 | ||
99 | int labelIndentTab = 18; // From left indent to item label (points) | |
100 | int itemIndentTab = 40; // From left indent to item (points) | |
101 | ||
102 | bool useUpButton = TRUE; | |
103 | int htmlBrowseButtons = HTML_BUTTONS_TEXT; | |
104 | ||
105 | bool truncateFilenames = FALSE; // Truncate for DOS | |
106 | int winHelpVersion = 3; // WinHelp Version (3 for Windows 3.1, 4 for Win95) | |
107 | bool winHelpContents = FALSE; // Generate .cnt file for WinHelp 4 | |
108 | bool htmlIndex = FALSE; // Generate .htx file for HTML | |
109 | bool htmlFrameContents = FALSE; // Use frames for HTML contents page | |
110 | bool useHeadingStyles = TRUE; // Insert \s1, s2 etc. | |
111 | bool useWord = TRUE; // Insert proper Word table of contents, etc etc | |
112 | int contentsDepth = 4; // Depth of Word table of contents | |
113 | bool indexSubsections = TRUE; // Index subsections in linear RTF | |
114 | // Linear RTF method of including bitmaps. Can be "includepicture", "hex" | |
115 | char *bitmapMethod = copystring("includepicture"); | |
116 | bool upperCaseNames = FALSE; | |
117 | // HTML background and text colours | |
118 | char *backgroundImageString = NULL; | |
119 | char *backgroundColourString = copystring("255;255;255"); | |
120 | char *textColourString = NULL; | |
121 | char *linkColourString = NULL; | |
122 | char *followedLinkColourString = NULL; | |
123 | bool combineSubSections = FALSE; | |
124 | ||
125 | /* | |
126 | * International support | |
127 | */ | |
128 | ||
129 | // Names to help with internationalisation | |
130 | char *ContentsNameString = copystring("Contents"); | |
131 | char *AbstractNameString = copystring("Abstract"); | |
132 | char *GlossaryNameString = copystring("Glossary"); | |
133 | char *ReferencesNameString = copystring("References"); | |
134 | char *FiguresNameString = copystring("List of Figures"); | |
135 | char *TablesNameString = copystring("List of Tables"); | |
136 | char *FigureNameString = copystring("Figure"); | |
137 | char *TableNameString = copystring("Table"); | |
138 | char *IndexNameString = copystring("Index"); | |
139 | char *ChapterNameString = copystring("chapter"); | |
140 | char *SectionNameString = copystring("section"); | |
141 | char *SubsectionNameString = copystring("subsection"); | |
142 | char *SubsubsectionNameString = copystring("subsubsection"); | |
143 | char *UpNameString = copystring("Up"); | |
144 | ||
145 | /* | |
146 | * Section numbering | |
147 | * | |
148 | */ | |
149 | ||
150 | int chapterNo = 0; | |
151 | int sectionNo = 0; | |
152 | int subsectionNo = 0; | |
153 | int subsubsectionNo = 0; | |
154 | int figureNo = 0; | |
155 | int tableNo = 0; | |
156 | ||
157 | /* | |
158 | * Other variables | |
159 | * | |
160 | */ | |
161 | ||
162 | FILE *CurrentOutput1 = NULL; | |
163 | FILE *CurrentOutput2 = NULL; | |
164 | FILE *Inputs[15]; | |
165 | int LineNumbers[15]; | |
166 | char *FileNames[15]; | |
167 | int CurrentInputIndex = 0; | |
168 | ||
169 | char *TexFileRoot = NULL; | |
170 | char *TexBibName = NULL; // Bibliography output file name | |
171 | char *TexTmpBibName = NULL; // Temporary bibliography output file name | |
172 | bool isSync = FALSE; // If TRUE, should not yield to other processes. | |
173 | bool stopRunning = FALSE; // If TRUE, should abort. | |
174 | ||
175 | static int currentColumn = 0; | |
176 | char *currentArgData = NULL; | |
177 | bool haveArgData = FALSE; // If TRUE, we're simulating the data. | |
178 | TexChunk *currentArgument = NULL; | |
179 | TexChunk *nextChunk = NULL; | |
180 | bool isArgOptional = FALSE; | |
181 | bool noArgs = 0; | |
182 | ||
183 | TexChunk *TopLevel = NULL; | |
184 | // wxList MacroDefs(wxKEY_STRING); | |
185 | wxHashTable MacroDefs(wxKEY_STRING); | |
186 | wxStringList IgnorableInputFiles; // Ignorable \input files, e.g. psbox.tex | |
187 | char *BigBuffer = NULL; // For reading in large chunks of text | |
188 | TexMacroDef *SoloBlockDef = NULL; | |
189 | TexMacroDef *VerbatimMacroDef = NULL; | |
190 | ||
191 | #define IncrementLineNumber() LineNumbers[CurrentInputIndex] ++ | |
192 | ||
193 | void TexOutput(char *s, bool ordinaryText) | |
194 | { | |
195 | int len = strlen(s); | |
196 | ||
197 | // Update current column, but only if we're guaranteed to | |
198 | // be ordinary text (not mark-up stuff) | |
199 | int i; | |
200 | if (ordinaryText) | |
201 | for (i = 0; i < len; i++) | |
202 | { | |
203 | if (s[i] == 13 || s[i] == 10) | |
204 | currentColumn = 0; | |
205 | else | |
206 | currentColumn ++; | |
207 | } | |
208 | ||
209 | if (CurrentOutput1) | |
210 | fprintf(CurrentOutput1, "%s", s); | |
211 | if (CurrentOutput2) | |
212 | fprintf(CurrentOutput2, "%s", s); | |
213 | } | |
214 | ||
215 | /* | |
216 | * Try to find a Latex macro, in one of the following forms: | |
217 | * (1) \begin{} ... \end{} | |
218 | * (2) \macroname{arg1}...{argn} | |
219 | * (3) {\bf arg1} | |
220 | */ | |
221 | ||
222 | void ForbidWarning(TexMacroDef *def) | |
223 | { | |
224 | char buf[100]; | |
225 | switch (def->forbidden) | |
226 | { | |
227 | case FORBID_WARN: | |
228 | { | |
229 | sprintf(buf, "Warning: it is recommended that command %s is not used.", def->name); | |
230 | OnInform(buf); | |
231 | break; | |
232 | } | |
233 | case FORBID_ABSOLUTELY: | |
234 | { | |
235 | sprintf(buf, "Error: command %s cannot be used and will lead to errors.", def->name); | |
236 | OnInform(buf); | |
237 | break; | |
238 | } | |
239 | default: | |
240 | break; | |
241 | } | |
242 | } | |
243 | ||
244 | TexMacroDef *MatchMacro(char *buffer, int *pos, char **env, bool *parseToBrace) | |
245 | { | |
246 | *parseToBrace = TRUE; | |
247 | int i = (*pos); | |
248 | TexMacroDef *def = NULL; | |
249 | char macroBuf[40]; | |
250 | ||
251 | // First, try to find begin{thing} | |
252 | if (strncmp(buffer+i, "begin{", 6) == 0) | |
253 | { | |
254 | i += 6; | |
255 | ||
256 | int j = i; | |
257 | while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39)) | |
258 | { | |
259 | macroBuf[j-i] = buffer[j]; | |
260 | j ++; | |
261 | } | |
262 | macroBuf[j-i] = 0; | |
263 | def = (TexMacroDef *)MacroDefs.Get(macroBuf); | |
264 | ||
265 | if (def) | |
266 | { | |
267 | *pos = j + 1; // BUGBUG Should this be + 1??? | |
268 | *env = def->name; | |
269 | ForbidWarning(def); | |
270 | return def; | |
271 | } | |
272 | else return NULL; | |
273 | } | |
274 | ||
275 | // Failed, so try to find macro from definition list | |
276 | int j = i; | |
277 | ||
278 | // First try getting a one-character macro, but ONLY | |
279 | // if these TWO characters are not both alphabetical (could | |
280 | // be a longer macro) | |
281 | if (!(isalpha(buffer[i]) && isalpha(buffer[i+1]))) | |
282 | { | |
283 | macroBuf[0] = buffer[i]; | |
284 | macroBuf[1] = 0; | |
285 | ||
286 | def = (TexMacroDef *)MacroDefs.Get(macroBuf); | |
287 | if (def) j ++; | |
288 | } | |
289 | ||
290 | if (!def) | |
291 | { | |
292 | while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39)) | |
293 | { | |
294 | macroBuf[j-i] = buffer[j]; | |
295 | j ++; | |
296 | } | |
297 | macroBuf[j-i] = 0; | |
298 | def = (TexMacroDef *)MacroDefs.Get(macroBuf); | |
299 | } | |
300 | ||
301 | if (def) | |
302 | { | |
303 | i = j; | |
304 | ||
305 | // We want to check whether this is a space-consuming macro | |
306 | // (e.g. {\bf word}) | |
307 | // No brace, e.g. \input thing.tex instead of \input{thing}; | |
308 | // or a numeric argument, such as \parindent0pt | |
309 | if ((def->no_args > 0) && ((buffer[i] == 32) || (buffer[i] == '=') || (isdigit(buffer[i])))) | |
310 | { | |
311 | if ((buffer[i] == 32) || (buffer[i] == '=')) | |
312 | i ++; | |
313 | ||
314 | *parseToBrace = FALSE; | |
315 | } | |
316 | *pos = i; | |
317 | ForbidWarning(def); | |
318 | return def; | |
319 | } | |
320 | return NULL; | |
321 | } | |
322 | ||
323 | void EatWhiteSpace(char *buffer, int *pos) | |
324 | { | |
325 | int len = strlen(buffer); | |
326 | int j = *pos; | |
327 | bool keepGoing = TRUE; | |
328 | bool moreLines = TRUE; | |
329 | while ((j < len) && keepGoing && | |
330 | (buffer[j] == 10 || buffer[j] == 13 || buffer[j] == ' ' || buffer[j] == 9)) | |
331 | { | |
332 | j ++; | |
333 | if (j >= len) | |
334 | { | |
335 | if (moreLines) | |
336 | { | |
337 | moreLines = read_a_line(buffer); | |
338 | len = strlen(buffer); | |
339 | j = 0; | |
340 | } | |
341 | else | |
342 | keepGoing = FALSE; | |
343 | } | |
344 | } | |
345 | *pos = j; | |
346 | } | |
347 | ||
348 | bool FindEndEnvironment(char *buffer, int *pos, char *env) | |
349 | { | |
350 | int i = (*pos); | |
351 | ||
352 | // Try to find end{thing} | |
353 | if ((strncmp(buffer+i, "end{", 4) == 0) && | |
354 | (strncmp(buffer+i+4, env, strlen(env)) == 0)) | |
355 | { | |
356 | *pos = i + 5 + strlen(env); | |
357 | return TRUE; | |
358 | } | |
359 | else return FALSE; | |
360 | } | |
361 | ||
362 | bool readingVerbatim = FALSE; | |
363 | bool readInVerbatim = FALSE; // Within a verbatim, but not nec. verbatiminput | |
364 | ||
365 | bool read_a_line(char *buf) | |
366 | { | |
367 | if (CurrentInputIndex < 0) | |
368 | { | |
369 | buf[0] = 0; | |
370 | return FALSE; | |
371 | } | |
372 | ||
373 | int ch = -2; | |
374 | int i = 0; | |
375 | buf[0] = 0; | |
376 | while (ch != EOF && ch != 10) | |
377 | { | |
378 | if (((i == 14) && (strncmp(buf, "\\end{verbatim}", 14) == 0)) || | |
379 | ((i == 16) && (strncmp(buf, "\\end{toocomplex}", 16) == 0))) | |
380 | readInVerbatim = FALSE; | |
381 | ||
382 | ch = getc(Inputs[CurrentInputIndex]); | |
383 | if (ch != EOF) | |
384 | { | |
385 | // Check for 2 consecutive newlines and replace with \par | |
386 | if (ch == 10 && !readInVerbatim) | |
387 | { | |
388 | int ch1 = getc(Inputs[CurrentInputIndex]); | |
389 | if ((ch1 == 10) || (ch1 == 13)) | |
390 | { | |
391 | // Eliminate newline (10) following DOS linefeed | |
392 | if (ch1 == 13) ch1 = getc(Inputs[CurrentInputIndex]); | |
393 | buf[i] = 0; | |
394 | IncrementLineNumber(); | |
395 | // strcat(buf, "\\par\n"); | |
396 | // i += 6; | |
397 | strcat(buf, "\\par"); | |
398 | i += 5; | |
399 | } | |
400 | else | |
401 | { | |
402 | ungetc(ch1, Inputs[CurrentInputIndex]); | |
403 | buf[i] = ch; | |
404 | i ++; | |
405 | } | |
406 | } | |
407 | else | |
408 | { | |
409 | ||
410 | // Convert embedded characters to RTF equivalents | |
411 | switch(ch) | |
412 | { | |
413 |