]>
git.saurik.com Git - wxWidgets.git/blob - utils/tex2rtf/src/tex2rtf.cpp
a61db85708b7e9cfd62fc528c96d500a5247c15c
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Converts Latex to linear/WinHelp RTF, HTML, wxHelp.
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
48 #if (defined(__WXGTK__) || defined(__WXMOTIF__)) && !defined(NO_GUI)
49 #include "tex2rtf.xpm"
52 const float versionNo
= 2.0;
54 TexChunk
*currentMember
= NULL
;
55 bool startedSections
= FALSE
;
56 char *contentsString
= NULL
;
57 bool suppressNameDecoration
= FALSE
;
58 bool OkToClose
= TRUE
;
62 wxHelpController
*HelpInstance
= NULL
;
65 static char *ipc_buffer
= NULL
;
66 static char Tex2RTFLastStatus
[100];
67 Tex2RTFServer
*TheTex2RTFServer
= NULL
;
71 char *bulletFile
= NULL
;
73 FILE *Contents
= NULL
; // Contents page
74 FILE *Chapters
= NULL
; // Chapters (WinHelp RTF) or rest of file (linear RTF)
75 FILE *Sections
= NULL
;
76 FILE *Subsections
= NULL
;
77 FILE *Subsubsections
= NULL
;
79 FILE *WinHelpContentsFile
= NULL
;
81 char *InputFile
= NULL
;
82 char *OutputFile
= NULL
;
83 char *MacroFile
= copystring("tex2rtf.ini");
85 char *FileRoot
= NULL
;
86 char *ContentsName
= NULL
; // Contents page from last time around
87 char *TmpContentsName
= NULL
; // Current contents page
88 char *TmpFrameContentsName
= NULL
; // Current frame contents page
89 char *WinHelpContentsFileName
= NULL
; // WinHelp .cnt file
90 char *RefName
= NULL
; // Reference file name
92 char *RTFCharset
= copystring("ansi");
95 int BufSize
= 100; // Size of buffer in K
101 void ShowOptions(void);
104 int main(int argc
, char **argv
)
106 wxMenuBar
*menuBar
= NULL
;
107 MyFrame
*frame
= NULL
;
109 // DECLARE_APP(MyApp)
112 // `Main program' equivalent, creating windows and returning main app frame
116 // Use default list of macros defined in tex2any.cc
117 DefineDefaultMacros();
118 AddMacroDef(ltHARDY
, "hardy", 0);
120 FileRoot
= new char[300];
121 ContentsName
= new char[300];
122 TmpContentsName
= new char[300];
123 TmpFrameContentsName
= new char[300];
124 WinHelpContentsFileName
= new char[300];
125 RefName
= new char[300];
129 // Read input/output files
132 if (argv
[1][0] != '-')
139 if (argv
[2][0] != '-')
141 OutputFile
= argv
[2];
149 if (!InputFile
|| !OutputFile
)
151 cout
<< "Tex2RTF: input or output file is missing.\n";
158 TexPathList
.EnsureFileAccessible(InputFile
);
160 if (!InputFile
|| !OutputFile
)
161 isInteractive
= TRUE
;
163 for (int i
= n
; i
< argc
;)
165 if (strcmp(argv
[i
], "-winhelp") == 0)
168 convertMode
= TEX_RTF
;
172 else if (strcmp(argv
[i
], "-interactive") == 0)
175 isInteractive
= TRUE
;
178 else if (strcmp(argv
[i
], "-sync") == 0) // Don't yield
183 else if (strcmp(argv
[i
], "-rtf") == 0)
186 convertMode
= TEX_RTF
;
188 else if (strcmp(argv
[i
], "-html") == 0)
191 convertMode
= TEX_HTML
;
193 else if (strcmp(argv
[i
], "-xlp") == 0)
196 convertMode
= TEX_XLP
;
198 else if (strcmp(argv
[i
], "-twice") == 0)
203 else if (strcmp(argv
[i
], "-macros") == 0)
208 MacroFile
= copystring(argv
[i
]);
212 else if (strcmp(argv
[i
], "-bufsize") == 0)
217 BufSize
= atoi(argv
[i
]);
221 else if (strcmp(argv
[i
], "-charset") == 0)
228 if (strcmp(s
, "ansi") == 0 || strcmp(s
, "pc") == 0 || strcmp(s
, "mac") == 0 ||
229 strcmp(s
, "pca") == 0)
230 RTFCharset
= copystring(s
);
233 OnError("Incorrect argument for -charset");
240 sprintf(buf
, "Invalid switch %s.\n", argv
[i
]);
250 #if defined(__WXMSW__) && !defined(NO_GUI)
252 Tex2RTFLastStatus
[0] = 0; // DDE connection return value
253 TheTex2RTFServer
= new Tex2RTFServer
;
254 TheTex2RTFServer
->Create("TEX2RTF");
257 #if defined(__WXMSW__) && defined(__WIN16__)
258 // Limit to max Windows array size
259 if (BufSize
> 64) BufSize
= 64;
262 TexInitialize(BufSize
);
263 ResetContentsLevels(0);
271 // Create the main frame window
272 frame
= new MyFrame(NULL
, -1, "Tex2RTF", wxPoint(-1, -1), wxSize(400, 300));
273 frame
->CreateStatusBar(2);
276 // TODO: uncomment this when we have tex2rtf.xpm
277 frame
->SetIcon(wxICON(tex2rtf
));
281 sprintf(buf
, "Tex2RTF [%s]", FileNameFromPath(InputFile
));
282 frame
->SetTitle(buf
);
286 wxMenu
*file_menu
= new wxMenu
;
287 file_menu
->Append(TEX_GO
, "&Go", "Run converter");
288 file_menu
->Append(TEX_SET_INPUT
, "Set &Input File", "Set the LaTeX input file");
289 file_menu
->Append(TEX_SET_OUTPUT
, "Set &Output File", "Set the output file");
290 file_menu
->AppendSeparator();
291 file_menu
->Append(TEX_VIEW_LATEX
, "View &LaTeX File", "View the LaTeX input file");
292 file_menu
->Append(TEX_VIEW_OUTPUT
, "View Output &File", "View output file");
293 file_menu
->Append(TEX_SAVE_FILE
, "&Save log file", "Save displayed text into file");
294 file_menu
->AppendSeparator();
295 file_menu
->Append(TEX_QUIT
, "E&xit", "Exit Tex2RTF");
297 wxMenu
*macro_menu
= new wxMenu
;
299 macro_menu
->Append(TEX_LOAD_CUSTOM_MACROS
, "&Load Custom Macros", "Load custom LaTeX macro file");
300 macro_menu
->Append(TEX_VIEW_CUSTOM_MACROS
, "View &Custom Macros", "View custom LaTeX macros");
302 wxMenu
*mode_menu
= new wxMenu
;
304 mode_menu
->Append(TEX_MODE_RTF
, "Output linear &RTF", "Wordprocessor-compatible RTF");
305 mode_menu
->Append(TEX_MODE_WINHELP
, "Output &WinHelp RTF", "WinHelp-compatible RTF");
306 mode_menu
->Append(TEX_MODE_HTML
, "Output &HTML", "HTML World Wide Web hypertext file");
307 mode_menu
->Append(TEX_MODE_XLP
, "Output &XLP", "wxHelp hypertext help file");
309 wxMenu
*help_menu
= new wxMenu
;
311 help_menu
->Append(TEX_HELP
, "&Help", "Tex2RTF Contents Page");
312 help_menu
->Append(TEX_ABOUT
, "&About Tex2RTF", "About Tex2RTF");
314 menuBar
= new wxMenuBar
;
315 menuBar
->Append(file_menu
, "&File");
316 menuBar
->Append(macro_menu
, "&Macros");
317 menuBar
->Append(mode_menu
, "&Conversion Mode");
318 menuBar
->Append(help_menu
, "&Help");
320 frame
->SetMenuBar(menuBar
);
321 frame
->textWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(-1, -1), wxSize(-1, -1), wxTE_READONLY
|wxTE_MULTILINE
);
323 (*frame
->textWindow
) << "Welcome to Julian Smart's LaTeX to RTF converter.\n";
326 HelpInstance
= new wxHelpController();
327 HelpInstance
->Initialize("tex2rtf");
330 * Read macro/initialisation file
335 if ((path
= TexPathList
.FindValidPath(MacroFile
)) != "")
336 ReadCustomMacros((char*) (const char*) path
);
340 if (winHelp
&& (convertMode
== TEX_RTF
))
341 strcat(buf
, "WinHelp RTF");
342 else if (!winHelp
&& (convertMode
== TEX_RTF
))
343 strcat(buf
, "linear RTF");
344 else if (convertMode
== TEX_HTML
) strcat(buf
, "HTML");
345 else if (convertMode
== TEX_XLP
) strcat(buf
, "XLP");
346 strcat(buf
, " mode.");
347 frame
->SetStatusText(buf
, 1);
356 * Read macro/initialisation file
361 if ((path
= TexPathList
.FindValidPath(MacroFile
)) != "")
362 ReadCustomMacros((char*) (const char*) path
);
374 // Return the main frame window
383 wxNode
*node
= CustomMacroList
.First();
386 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
389 node
= CustomMacroList
.First();
391 MacroDefs
.BeginFind();
392 node
= MacroDefs
.Next();
395 TexMacroDef
* def
= (TexMacroDef
*) node
->Data();
397 node
= MacroDefs
.Next();
401 delete TheTex2RTFServer
;
406 // TODO: this simulates zero-memory leaks!
407 // Otherwise there are just too many...
409 wxDebugContext::SetCheckpoint();
415 void ShowOptions(void)
418 sprintf(buf
, "Tex2RTF version %.2f", versionNo
);
420 OnInform("Usage: tex2rtf [input] [output] [switches]\n");
421 OnInform("where valid switches are");
422 OnInform(" -interactive");
423 OnInform(" -bufsize <size in K>");
424 OnInform(" -charset <pc | pca | ansi | mac> (default ansi)");
427 OnInform(" -macros <filename>");
428 OnInform(" -winhelp");
436 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
437 EVT_CLOSE(MyFrame::OnCloseWindow
)
438 EVT_MENU(TEX_QUIT
, MyFrame::OnExit
)
439 EVT_MENU(TEX_GO
, MyFrame::OnGo
)
440 EVT_MENU(TEX_SET_INPUT
, MyFrame::OnSetInput
)
441 EVT_MENU(TEX_SET_OUTPUT
, MyFrame::OnSetOutput
)
442 EVT_MENU(TEX_SAVE_FILE
, MyFrame::OnSaveFile
)
443 EVT_MENU(TEX_VIEW_LATEX
, MyFrame::OnViewLatex
)
444 EVT_MENU(TEX_VIEW_OUTPUT
, MyFrame::OnViewOutput
)
445 EVT_MENU(TEX_VIEW_CUSTOM_MACROS
, MyFrame::OnShowMacros
)
446 EVT_MENU(TEX_LOAD_CUSTOM_MACROS
, MyFrame::OnLoadMacros
)
447 EVT_MENU(TEX_MODE_RTF
, MyFrame::OnModeRTF
)
448 EVT_MENU(TEX_MODE_WINHELP
, MyFrame::OnModeWinHelp
)
449 EVT_MENU(TEX_MODE_HTML
, MyFrame::OnModeHTML
)
450 EVT_MENU(TEX_MODE_XLP
, MyFrame::OnModeXLP
)
451 EVT_MENU(TEX_HELP
, MyFrame::OnHelp
)
452 EVT_MENU(TEX_ABOUT
, MyFrame::OnAbout
)
455 // My frame constructor
456 MyFrame::MyFrame(wxFrame
*frame
, wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
457 wxFrame(frame
, id
, title
, pos
, size
)
460 void MyFrame::OnCloseWindow(wxCloseEvent
& event
)
462 if (!stopRunning
&& !OkToClose
)
474 void MyFrame::OnExit(wxCommandEvent
& event
)
479 void MyFrame::OnGo(wxCommandEvent
& event
)
481 menuBar
->EnableTop(0, FALSE
);
482 menuBar
->EnableTop(1, FALSE
);
483 menuBar
->EnableTop(2, FALSE
);
484 menuBar
->EnableTop(3, FALSE
);
494 menuBar
->EnableTop(0, TRUE
);
495 menuBar
->EnableTop(1, TRUE
);
496 menuBar
->EnableTop(2, TRUE
);
497 menuBar
->EnableTop(3, TRUE
);
500 void MyFrame::OnSetInput(wxCommandEvent
& event
)
502 ChooseInputFile(TRUE
);
505 void MyFrame::OnSetOutput(wxCommandEvent
& event
)
507 ChooseOutputFile(TRUE
);
510 void MyFrame::OnSaveFile(wxCommandEvent
& event
)
512 wxString s
= wxFileSelector("Save text to file", "", "", "txt", "*.txt");
515 textWindow
->SaveFile(s
);
517 sprintf(buf
, "Saved text to %s", (const char*) s
);
518 frame
->SetStatusText(buf
, 0);
522 void MyFrame::OnViewOutput(wxCommandEvent
& event
)
525 if (OutputFile
&& wxFileExists(OutputFile
))
527 textWindow
->LoadFile(OutputFile
);
529 wxString
str(wxFileNameFromPath(OutputFile
));
530 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
531 frame
->SetTitle(buf
);
535 void MyFrame::OnViewLatex(wxCommandEvent
& event
)
538 if (InputFile
&& wxFileExists(InputFile
))
540 textWindow
->LoadFile(InputFile
);
542 wxString
str(wxFileNameFromPath(OutputFile
));
543 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
544 frame
->SetTitle(buf
);
548 void MyFrame::OnLoadMacros(wxCommandEvent
& event
)
551 wxString s
= wxFileSelector("Choose custom macro file", wxPathOnly(MacroFile
), wxFileNameFromPath(MacroFile
), "ini", "*.ini");
552 if (s
!= "" && wxFileExists(s
))
554 MacroFile
= copystring(s
);
555 ReadCustomMacros((char*) (const char*) s
);
560 void MyFrame::OnShowMacros(wxCommandEvent
& event
)
567 void MyFrame::OnModeRTF(wxCommandEvent
& event
)
569 convertMode
= TEX_RTF
;
573 SetStatusText("In linear RTF mode.", 1);
576 void MyFrame::OnModeWinHelp(wxCommandEvent
& event
)
578 convertMode
= TEX_RTF
;
582 SetStatusText("In WinHelp RTF mode.", 1);
585 void MyFrame::OnModeHTML(wxCommandEvent
& event
)
587 convertMode
= TEX_HTML
;
591 SetStatusText("In HTML mode.", 1);
594 void MyFrame::OnModeXLP(wxCommandEvent
& event
)
596 convertMode
= TEX_XLP
;
599 SetStatusText("In XLP mode.", 1);
602 void MyFrame::OnHelp(wxCommandEvent
& event
)
604 HelpInstance
->LoadFile();
605 HelpInstance
->DisplayContents();
608 void MyFrame::OnAbout(wxCommandEvent
& event
)
612 char *platform
= " (32-bit)";
615 char *platform
= " (16-bit)";
620 sprintf(buf
, "Tex2RTF Version %.2f%s\nLaTeX to RTF, WinHelp, HTML and wxHelp Conversion\n\n(c) Julian Smart 1999", versionNo
, platform
);
621 wxMessageBox(buf
, "About Tex2RTF");
624 void ChooseInputFile(bool force
)
626 if (force
|| !InputFile
)
628 wxString s
= wxFileSelector("Choose LaTeX input file", wxPathOnly(InputFile
), wxFileNameFromPath(InputFile
), "tex", "*.tex");
631 // Different file, so clear index entries.
633 ResetContentsLevels(0);
636 InputFile
= copystring(s
);
637 wxString str
= wxFileNameFromPath(InputFile
);
638 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
639 frame
->SetTitle(buf
);
645 void ChooseOutputFile(bool force
)
647 char extensionBuf
[10];
649 strcpy(wildBuf
, "*.");
652 path
= wxPathOnly(OutputFile
);
654 path
= wxPathOnly(InputFile
);
660 strcpy(extensionBuf
, "rtf");
661 strcat(wildBuf
, "rtf");
666 strcpy(extensionBuf
, "xlp");
667 strcat(wildBuf
, "xlp");
672 #if defined(__WXMSW__) && defined(__WIN16__)
673 strcpy(extensionBuf
, "htm");
674 strcat(wildBuf
, "htm");
676 strcpy(extensionBuf
, "html");
677 strcat(wildBuf
, "html");
682 if (force
|| !OutputFile
)
684 wxString s
= wxFileSelector("Choose output file", path
, wxFileNameFromPath(OutputFile
),
685 extensionBuf
, wildBuf
);
687 OutputFile
= copystring(s
);
699 if (!InputFile
|| !OutputFile
)
706 wxString str
= wxFileNameFromPath(InputFile
);
708 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
709 frame
->SetTitle(buf
);
715 // Find extension-less filename
716 strcpy(FileRoot
, OutputFile
);
717 StripExtension(FileRoot
);
719 if (truncateFilenames
&& convertMode
== TEX_HTML
)
721 // Truncate to five characters. This ensures that
722 // we can generate DOS filenames such as thing999. But 1000 files
723 // may not be enough, of course...
724 char* sName
= wxFileNameFromPath( FileRoot
); // this Julian's method is non-destructive reference
727 if(strlen( sName
) > 5)
728 sName
[5] = '\0'; // that should do!
731 sprintf(ContentsName
, "%s.con", FileRoot
);
732 sprintf(TmpContentsName
, "%s.cn1", FileRoot
);
733 sprintf(TmpFrameContentsName
, "%s.frc", FileRoot
);
734 sprintf(WinHelpContentsFileName
, "%s.cnt", FileRoot
);
735 sprintf(RefName
, "%s.ref", FileRoot
);
737 TexPathList
.EnsureFileAccessible(InputFile
);
740 wxString s
= TexPathList
.FindValidPath("bullet.bmp");
743 wxString str
= wxFileNameFromPath(s
);
744 bulletFile
= copystring(str
);
748 if (wxFileExists(RefName
))
749 ReadTexReferences(RefName
);
751 bool success
= FALSE
;
753 if (InputFile
&& OutputFile
)
755 if (!FileExists(InputFile
))
757 OnError("Cannot open input file!");
765 sprintf(buf
, "Working, pass %d...", passNumber
);
766 frame
->SetStatusText(buf
);
770 OnInform("Reading LaTeX file...");
771 TexLoadFile(InputFile
);
794 OnInform("*** Aborted by user.");
801 WriteTexReferences(RefName
);
803 startedSections
= FALSE
;
807 long tim
= wxGetElapsedTime();
808 sprintf(buf
, "Finished in %ld seconds.", (long)(tim
/1000.0));
812 sprintf(buf
, "Done, %d %s.", passNumber
, (passNumber
> 1) ? "passes" : "pass");
813 frame
->SetStatusText(buf
);
816 sprintf(buf
, "Done, %d %s.", passNumber
, (passNumber
> 1) ? "passes" : "pass");
825 startedSections
= FALSE
;
827 OnInform("Sorry, unsuccessful.");
832 void OnError(char *msg
)
835 cerr
<< "Error: " << msg
<< "\n";
839 (*frame
->textWindow
) << "Error: " << msg
<< "\n";
843 cerr
<< "Error: " << msg
<< "\n";
854 void OnInform(char *msg
)
861 (*frame
->textWindow
) << msg
<< "\n";
879 void OnMacro(int macroId
, int no_args
, bool start
)
885 RTFOnMacro(macroId
, no_args
, start
);
890 XLPOnMacro(macroId
, no_args
, start
);
895 HTMLOnMacro(macroId
, no_args
, start
);
901 bool OnArgument(int macroId
, int arg_no
, bool start
)
907 return RTFOnArgument(macroId
, arg_no
, start
);
912 return XLPOnArgument(macroId
, arg_no
, start
);
917 return HTMLOnArgument(macroId
, arg_no
, start
);
927 #if defined(__WXMSW__) && !defined(NO_GUI)
933 wxConnectionBase
*Tex2RTFServer::OnAcceptConnection(const wxString
& topic
)
935 if (topic
== "TEX2RTF")
938 ipc_buffer
= new char[1000];
940 return new Tex2RTFConnection(ipc_buffer
, 4000);
950 Tex2RTFConnection::Tex2RTFConnection(char *buf
, int size
):wxDDEConnection(buf
, size
)
954 Tex2RTFConnection::~Tex2RTFConnection(void)
958 bool SplitCommand(char *data
, char *firstArg
, char *secondArg
)
963 int len
= strlen(data
);
965 // Find first argument (command name)
968 if (data
[i
] == ' ' || data
[i
] == 0)
972 firstArg
[i
] = data
[i
];
979 // Find second argument
984 secondArg
[j
] = data
[i
];
993 bool Tex2RTFConnection::OnExecute(const wxString
& topic
, char *data
, int size
, int format
)
995 strcpy(Tex2RTFLastStatus
, "OK");
999 if (SplitCommand(data
, firstArg
, secondArg
))
1001 bool hasArg
= (strlen(secondArg
) > 0);
1002 if (strcmp(firstArg
, "INPUT") == 0 && hasArg
)
1004 if (InputFile
) delete[] InputFile
;
1005 InputFile
= copystring(secondArg
);
1009 wxString str
= wxFileNameFromPath(InputFile
);
1010 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
1011 frame
->SetTitle(buf
);
1014 else if (strcmp(firstArg
, "OUTPUT") == 0 && hasArg
)
1016 if (OutputFile
) delete[] OutputFile
;
1017 OutputFile
= copystring(secondArg
);
1019 else if (strcmp(firstArg
, "GO") == 0)
1021 strcpy(Tex2RTFLastStatus
, "WORKING");
1023 strcpy(Tex2RTFLastStatus
, "CONVERSION ERROR");
1025 strcpy(Tex2RTFLastStatus
, "OK");
1027 else if (strcmp(firstArg
, "EXIT") == 0)
1029 if (frame
) frame
->Close();
1031 else if (strcmp(firstArg
, "MINIMIZE") == 0 || strcmp(firstArg
, "ICONIZE") == 0)
1034 frame
->Iconize(TRUE
);
1036 else if (strcmp(firstArg
, "SHOW") == 0 || strcmp(firstArg
, "RESTORE") == 0)
1040 frame
->Iconize(FALSE
);
1046 // Try for a setting
1047 strcpy(Tex2RTFLastStatus
, RegisterSetting(firstArg
, secondArg
, FALSE
));
1049 if (frame
&& strcmp(firstArg
, "conversionMode") == 0)
1054 if (winHelp
&& (convertMode
== TEX_RTF
))
1055 strcat(buf
, "WinHelp RTF");
1056 else if (!winHelp
&& (convertMode
== TEX_RTF
))
1057 strcat(buf
, "linear RTF");
1058 else if (convertMode
== TEX_HTML
) strcat(buf
, "HTML");
1059 else if (convertMode
== TEX_XLP
) strcat(buf
, "XLP");
1060 strcat(buf
, " mode.");
1061 frame
->SetStatusText(buf
, 1);
1069 char *Tex2RTFConnection::OnRequest(const wxString
& topic
, const wxString
& item
, int *size
, int format
)
1071 return Tex2RTFLastStatus
;