]>
git.saurik.com Git - wxWidgets.git/blob - utils/tex2rtf/src/tex2rtf.cpp
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"
32 #if defined(NO_GUI) || defined(__UNIX__)
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
;
64 wxHelpController
*HelpInstance
= NULL
;
68 static char *ipc_buffer
= NULL
;
69 static char Tex2RTFLastStatus
[100];
70 Tex2RTFServer
*TheTex2RTFServer
= NULL
;
74 char *bulletFile
= NULL
;
76 FILE *Contents
= NULL
; // Contents page
77 FILE *Chapters
= NULL
; // Chapters (WinHelp RTF) or rest of file (linear RTF)
78 FILE *Sections
= NULL
;
79 FILE *Subsections
= NULL
;
80 FILE *Subsubsections
= NULL
;
82 FILE *WinHelpContentsFile
= NULL
;
84 char *InputFile
= NULL
;
85 char *OutputFile
= NULL
;
86 char *MacroFile
= copystring("tex2rtf.ini");
88 char *FileRoot
= NULL
;
89 char *ContentsName
= NULL
; // Contents page from last time around
90 char *TmpContentsName
= NULL
; // Current contents page
91 char *TmpFrameContentsName
= NULL
; // Current frame contents page
92 char *WinHelpContentsFileName
= NULL
; // WinHelp .cnt file
93 char *RefName
= NULL
; // Reference file name
95 char *RTFCharset
= copystring("ansi");
98 int BufSize
= 100; // Size of buffer in K
104 void ShowOptions(void);
108 extern char *wxBuffer
; // we must init it, otherwise tex2rtf will crash
110 int main(int argc
, char **argv
)
112 wxMenuBar
*menuBar
= NULL
;
113 MyFrame
*frame
= NULL
;
115 // DECLARE_APP(MyApp)
118 // `Main program' equivalent, creating windows and returning main app frame
122 // Use default list of macros defined in tex2any.cc
123 DefineDefaultMacros();
124 AddMacroDef(ltHARDY
, "hardy", 0);
126 FileRoot
= new char[300];
127 ContentsName
= new char[300];
128 TmpContentsName
= new char[300];
129 TmpFrameContentsName
= new char[300];
130 WinHelpContentsFileName
= new char[300];
131 RefName
= new char[300];
135 // Read input/output files
138 if (argv
[1][0] != '-')
145 if (argv
[2][0] != '-')
147 OutputFile
= argv
[2];
155 wxBuffer
= new char[1500];
156 // this is done in wxApp, but NO_GUI version doesn't call it :-(
158 if (!InputFile
|| !OutputFile
)
160 cout
<< "Tex2RTF: input or output file is missing.\n";
168 TexPathList
.EnsureFileAccessible(InputFile
);
170 if (!InputFile
|| !OutputFile
)
171 isInteractive
= TRUE
;
173 for (int i
= n
; i
< argc
;)
175 if (strcmp(argv
[i
], "-winhelp") == 0)
178 convertMode
= TEX_RTF
;
182 else if (strcmp(argv
[i
], "-interactive") == 0)
185 isInteractive
= TRUE
;
188 else if (strcmp(argv
[i
], "-sync") == 0) // Don't yield
193 else if (strcmp(argv
[i
], "-rtf") == 0)
196 convertMode
= TEX_RTF
;
198 else if (strcmp(argv
[i
], "-html") == 0)
201 convertMode
= TEX_HTML
;
203 else if (strcmp(argv
[i
], "-xlp") == 0)
206 convertMode
= TEX_XLP
;
208 else if (strcmp(argv
[i
], "-twice") == 0)
213 else if (strcmp(argv
[i
], "-macros") == 0)
218 MacroFile
= copystring(argv
[i
]);
222 else if (strcmp(argv
[i
], "-bufsize") == 0)
227 BufSize
= atoi(argv
[i
]);
231 else if (strcmp(argv
[i
], "-charset") == 0)
238 if (strcmp(s
, "ansi") == 0 || strcmp(s
, "pc") == 0 || strcmp(s
, "mac") == 0 ||
239 strcmp(s
, "pca") == 0)
240 RTFCharset
= copystring(s
);
243 OnError("Incorrect argument for -charset");
250 sprintf(buf
, "Invalid switch %s.\n", argv
[i
]);
260 #if defined(__WXMSW__) && !defined(NO_GUI)
262 Tex2RTFLastStatus
[0] = 0; // DDE connection return value
263 TheTex2RTFServer
= new Tex2RTFServer
;
264 TheTex2RTFServer
->Create("TEX2RTF");
267 #if defined(__WXMSW__) && defined(__WIN16__)
268 // Limit to max Windows array size
269 if (BufSize
> 64) BufSize
= 64;
272 TexInitialize(BufSize
);
273 ResetContentsLevels(0);
281 // Create the main frame window
282 frame
= new MyFrame(NULL
, -1, "Tex2RTF", wxPoint(-1, -1), wxSize(400, 300));
283 frame
->CreateStatusBar(2);
286 // TODO: uncomment this when we have tex2rtf.xpm
287 frame
->SetIcon(wxICON(tex2rtf
));
291 sprintf(buf
, "Tex2RTF [%s]", FileNameFromPath(InputFile
));
292 frame
->SetTitle(buf
);
296 wxMenu
*file_menu
= new wxMenu
;
297 file_menu
->Append(TEX_GO
, "&Go", "Run converter");
298 file_menu
->Append(TEX_SET_INPUT
, "Set &Input File", "Set the LaTeX input file");
299 file_menu
->Append(TEX_SET_OUTPUT
, "Set &Output File", "Set the output file");
300 file_menu
->AppendSeparator();
301 file_menu
->Append(TEX_VIEW_LATEX
, "View &LaTeX File", "View the LaTeX input file");
302 file_menu
->Append(TEX_VIEW_OUTPUT
, "View Output &File", "View output file");
303 file_menu
->Append(TEX_SAVE_FILE
, "&Save log file", "Save displayed text into file");
304 file_menu
->AppendSeparator();
305 file_menu
->Append(TEX_QUIT
, "E&xit", "Exit Tex2RTF");
307 wxMenu
*macro_menu
= new wxMenu
;
309 macro_menu
->Append(TEX_LOAD_CUSTOM_MACROS
, "&Load Custom Macros", "Load custom LaTeX macro file");
310 macro_menu
->Append(TEX_VIEW_CUSTOM_MACROS
, "View &Custom Macros", "View custom LaTeX macros");
312 wxMenu
*mode_menu
= new wxMenu
;
314 mode_menu
->Append(TEX_MODE_RTF
, "Output linear &RTF", "Wordprocessor-compatible RTF");
315 mode_menu
->Append(TEX_MODE_WINHELP
, "Output &WinHelp RTF", "WinHelp-compatible RTF");
316 mode_menu
->Append(TEX_MODE_HTML
, "Output &HTML", "HTML World Wide Web hypertext file");
317 mode_menu
->Append(TEX_MODE_XLP
, "Output &XLP", "wxHelp hypertext help file");
319 wxMenu
*help_menu
= new wxMenu
;
321 help_menu
->Append(TEX_HELP
, "&Help", "Tex2RTF Contents Page");
322 help_menu
->Append(TEX_ABOUT
, "&About Tex2RTF", "About Tex2RTF");
324 menuBar
= new wxMenuBar
;
325 menuBar
->Append(file_menu
, "&File");
326 menuBar
->Append(macro_menu
, "&Macros");
327 menuBar
->Append(mode_menu
, "&Conversion Mode");
328 menuBar
->Append(help_menu
, "&Help");
330 frame
->SetMenuBar(menuBar
);
331 frame
->textWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(-1, -1), wxSize(-1, -1), wxTE_READONLY
|wxTE_MULTILINE
);
333 (*frame
->textWindow
) << "Welcome to Julian Smart's LaTeX to RTF converter.\n";
337 HelpInstance
= new wxHelpController();
338 HelpInstance
->Initialize("tex2rtf");
342 * Read macro/initialisation file
347 if ((path
= TexPathList
.FindValidPath(MacroFile
)) != "")
348 ReadCustomMacros((char*) (const char*) path
);
352 if (winHelp
&& (convertMode
== TEX_RTF
))
353 strcat(buf
, "WinHelp RTF");
354 else if (!winHelp
&& (convertMode
== TEX_RTF
))
355 strcat(buf
, "linear RTF");
356 else if (convertMode
== TEX_HTML
) strcat(buf
, "HTML");
357 else if (convertMode
== TEX_XLP
) strcat(buf
, "XLP");
358 strcat(buf
, " mode.");
359 frame
->SetStatusText(buf
, 1);
368 * Read macro/initialisation file
373 if ((path
= TexPathList
.FindValidPath(MacroFile
)) != "")
374 ReadCustomMacros((char*) (const char*) path
);
386 // Return the main frame window
397 wxNode
*node
= CustomMacroList
.First();
400 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
403 node
= CustomMacroList
.First();
405 MacroDefs
.BeginFind();
406 node
= MacroDefs
.Next();
409 TexMacroDef
* def
= (TexMacroDef
*) node
->Data();
411 node
= MacroDefs
.Next();
415 delete TheTex2RTFServer
;
423 // TODO: this simulates zero-memory leaks!
424 // Otherwise there are just too many...
426 wxDebugContext::SetCheckpoint();
432 void ShowOptions(void)
435 sprintf(buf
, "Tex2RTF version %.2f", versionNo
);
437 OnInform("Usage: tex2rtf [input] [output] [switches]\n");
438 OnInform("where valid switches are");
439 OnInform(" -interactive");
440 OnInform(" -bufsize <size in K>");
441 OnInform(" -charset <pc | pca | ansi | mac> (default ansi)");
444 OnInform(" -macros <filename>");
445 OnInform(" -winhelp");
453 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
454 EVT_CLOSE(MyFrame::OnCloseWindow
)
455 EVT_MENU(TEX_QUIT
, MyFrame::OnExit
)
456 EVT_MENU(TEX_GO
, MyFrame::OnGo
)
457 EVT_MENU(TEX_SET_INPUT
, MyFrame::OnSetInput
)
458 EVT_MENU(TEX_SET_OUTPUT
, MyFrame::OnSetOutput
)
459 EVT_MENU(TEX_SAVE_FILE
, MyFrame::OnSaveFile
)
460 EVT_MENU(TEX_VIEW_LATEX
, MyFrame::OnViewLatex
)
461 EVT_MENU(TEX_VIEW_OUTPUT
, MyFrame::OnViewOutput
)
462 EVT_MENU(TEX_VIEW_CUSTOM_MACROS
, MyFrame::OnShowMacros
)
463 EVT_MENU(TEX_LOAD_CUSTOM_MACROS
, MyFrame::OnLoadMacros
)
464 EVT_MENU(TEX_MODE_RTF
, MyFrame::OnModeRTF
)
465 EVT_MENU(TEX_MODE_WINHELP
, MyFrame::OnModeWinHelp
)
466 EVT_MENU(TEX_MODE_HTML
, MyFrame::OnModeHTML
)
467 EVT_MENU(TEX_MODE_XLP
, MyFrame::OnModeXLP
)
468 EVT_MENU(TEX_HELP
, MyFrame::OnHelp
)
469 EVT_MENU(TEX_ABOUT
, MyFrame::OnAbout
)
472 // My frame constructor
473 MyFrame::MyFrame(wxFrame
*frame
, wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
474 wxFrame(frame
, id
, title
, pos
, size
)
477 void MyFrame::OnCloseWindow(wxCloseEvent
& event
)
479 if (!stopRunning
&& !OkToClose
)
491 void MyFrame::OnExit(wxCommandEvent
& event
)
496 void MyFrame::OnGo(wxCommandEvent
& event
)
498 menuBar
->EnableTop(0, FALSE
);
499 menuBar
->EnableTop(1, FALSE
);
500 menuBar
->EnableTop(2, FALSE
);
501 menuBar
->EnableTop(3, FALSE
);
511 menuBar
->EnableTop(0, TRUE
);
512 menuBar
->EnableTop(1, TRUE
);
513 menuBar
->EnableTop(2, TRUE
);
514 menuBar
->EnableTop(3, TRUE
);
517 void MyFrame::OnSetInput(wxCommandEvent
& event
)
519 ChooseInputFile(TRUE
);
522 void MyFrame::OnSetOutput(wxCommandEvent
& event
)
524 ChooseOutputFile(TRUE
);
527 void MyFrame::OnSaveFile(wxCommandEvent
& event
)
529 wxString s
= wxFileSelector("Save text to file", "", "", "txt", "*.txt");
532 textWindow
->SaveFile(s
);
534 sprintf(buf
, "Saved text to %s", (const char*) s
);
535 frame
->SetStatusText(buf
, 0);
539 void MyFrame::OnViewOutput(wxCommandEvent
& event
)
542 if (OutputFile
&& wxFileExists(OutputFile
))
544 textWindow
->LoadFile(OutputFile
);
546 wxString
str(wxFileNameFromPath(OutputFile
));
547 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
548 frame
->SetTitle(buf
);
552 void MyFrame::OnViewLatex(wxCommandEvent
& event
)
555 if (InputFile
&& wxFileExists(InputFile
))
557 textWindow
->LoadFile(InputFile
);
559 wxString
str(wxFileNameFromPath(OutputFile
));
560 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
561 frame
->SetTitle(buf
);
565 void MyFrame::OnLoadMacros(wxCommandEvent
& event
)
568 wxString s
= wxFileSelector("Choose custom macro file", wxPathOnly(MacroFile
), wxFileNameFromPath(MacroFile
), "ini", "*.ini");
569 if (s
!= "" && wxFileExists(s
))
571 MacroFile
= copystring(s
);
572 ReadCustomMacros((char*) (const char*) s
);
577 void MyFrame::OnShowMacros(wxCommandEvent
& event
)
584 void MyFrame::OnModeRTF(wxCommandEvent
& event
)
586 convertMode
= TEX_RTF
;
590 SetStatusText("In linear RTF mode.", 1);
593 void MyFrame::OnModeWinHelp(wxCommandEvent
& event
)
595 convertMode
= TEX_RTF
;
599 SetStatusText("In WinHelp RTF mode.", 1);
602 void MyFrame::OnModeHTML(wxCommandEvent
& event
)
604 convertMode
= TEX_HTML
;
608 SetStatusText("In HTML mode.", 1);
611 void MyFrame::OnModeXLP(wxCommandEvent
& event
)
613 convertMode
= TEX_XLP
;
616 SetStatusText("In XLP mode.", 1);
619 void MyFrame::OnHelp(wxCommandEvent
& event
)
622 HelpInstance
->LoadFile();
623 HelpInstance
->DisplayContents();
627 void MyFrame::OnAbout(wxCommandEvent
& event
)
631 char *platform
= " (32-bit)";
634 char *platform
= " (16-bit)";
639 sprintf(buf
, "Tex2RTF Version %.2f%s\nLaTeX to RTF, WinHelp, HTML and wxHelp Conversion\n\n(c) Julian Smart 1999", versionNo
, platform
);
640 wxMessageBox(buf
, "About Tex2RTF");
643 void ChooseInputFile(bool force
)
645 if (force
|| !InputFile
)
647 wxString s
= wxFileSelector("Choose LaTeX input file", wxPathOnly(InputFile
), wxFileNameFromPath(InputFile
), "tex", "*.tex");
650 // Different file, so clear index entries.
652 ResetContentsLevels(0);
655 InputFile
= copystring(s
);
656 wxString str
= wxFileNameFromPath(InputFile
);
657 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
658 frame
->SetTitle(buf
);
664 void ChooseOutputFile(bool force
)
666 char extensionBuf
[10];
668 strcpy(wildBuf
, "*.");
671 path
= wxPathOnly(OutputFile
);
673 path
= wxPathOnly(InputFile
);
679 strcpy(extensionBuf
, "rtf");
680 strcat(wildBuf
, "rtf");
685 strcpy(extensionBuf
, "xlp");
686 strcat(wildBuf
, "xlp");
691 #if defined(__WXMSW__) && defined(__WIN16__)
692 strcpy(extensionBuf
, "htm");
693 strcat(wildBuf
, "htm");
695 strcpy(extensionBuf
, "html");
696 strcat(wildBuf
, "html");
701 if (force
|| !OutputFile
)
703 wxString s
= wxFileSelector("Choose output file", path
, wxFileNameFromPath(OutputFile
),
704 extensionBuf
, wildBuf
);
706 OutputFile
= copystring(s
);
718 if (!InputFile
|| !OutputFile
)
725 wxString str
= wxFileNameFromPath(InputFile
);
727 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
728 frame
->SetTitle(buf
);
734 // Find extension-less filename
735 strcpy(FileRoot
, OutputFile
);
736 StripExtension(FileRoot
);
738 if (truncateFilenames
&& convertMode
== TEX_HTML
)
740 // Truncate to five characters. This ensures that
741 // we can generate DOS filenames such as thing999. But 1000 files
742 // may not be enough, of course...
743 char* sName
= wxFileNameFromPath( FileRoot
); // this Julian's method is non-destructive reference
746 if(strlen( sName
) > 5)
747 sName
[5] = '\0'; // that should do!
750 sprintf(ContentsName
, "%s.con", FileRoot
);
751 sprintf(TmpContentsName
, "%s.cn1", FileRoot
);
752 sprintf(TmpFrameContentsName
, "%s.frc", FileRoot
);
753 sprintf(WinHelpContentsFileName
, "%s.cnt", FileRoot
);
754 sprintf(RefName
, "%s.ref", FileRoot
);
756 TexPathList
.EnsureFileAccessible(InputFile
);
759 wxString s
= TexPathList
.FindValidPath("bullet.bmp");
762 wxString str
= wxFileNameFromPath(s
);
763 bulletFile
= copystring(str
);
767 if (wxFileExists(RefName
))
768 ReadTexReferences(RefName
);
770 bool success
= FALSE
;
772 if (InputFile
&& OutputFile
)
774 if (!FileExists(InputFile
))
776 OnError("Cannot open input file!");
784 sprintf(buf
, "Working, pass %d...", passNumber
);
785 frame
->SetStatusText(buf
);
789 OnInform("Reading LaTeX file...");
790 TexLoadFile(InputFile
);
813 OnInform("*** Aborted by user.");
820 WriteTexReferences(RefName
);
822 startedSections
= FALSE
;
826 long tim
= wxGetElapsedTime();
827 sprintf(buf
, "Finished in %ld seconds.", (long)(tim
/1000.0));
831 sprintf(buf
, "Done, %d %s.", passNumber
, (passNumber
> 1) ? "passes" : "pass");
832 frame
->SetStatusText(buf
);
835 sprintf(buf
, "Done, %d %s.", passNumber
, (passNumber
> 1) ? "passes" : "pass");
844 startedSections
= FALSE
;
846 OnInform("Sorry, unsuccessful.");
851 void OnError(char *msg
)
854 cerr
<< "Error: " << msg
<< "\n";
858 (*frame
->textWindow
) << "Error: " << msg
<< "\n";
862 cerr
<< "Error: " << msg
<< "\n";
873 void OnInform(char *msg
)
880 (*frame
->textWindow
) << msg
<< "\n";
898 void OnMacro(int macroId
, int no_args
, bool start
)
904 RTFOnMacro(macroId
, no_args
, start
);
909 XLPOnMacro(macroId
, no_args
, start
);
914 HTMLOnMacro(macroId
, no_args
, start
);
920 bool OnArgument(int macroId
, int arg_no
, bool start
)
926 return RTFOnArgument(macroId
, arg_no
, start
);
931 return XLPOnArgument(macroId
, arg_no
, start
);
936 return HTMLOnArgument(macroId
, arg_no
, start
);
946 #if defined(__WXMSW__) && !defined(NO_GUI)
952 wxConnectionBase
*Tex2RTFServer::OnAcceptConnection(const wxString
& topic
)
954 if (topic
== "TEX2RTF")
957 ipc_buffer
= new char[1000];
959 return new Tex2RTFConnection(ipc_buffer
, 4000);
969 Tex2RTFConnection::Tex2RTFConnection(char *buf
, int size
):wxDDEConnection(buf
, size
)
973 Tex2RTFConnection::~Tex2RTFConnection(void)
977 bool SplitCommand(char *data
, char *firstArg
, char *secondArg
)
982 int len
= strlen(data
);
984 // Find first argument (command name)
987 if (data
[i
] == ' ' || data
[i
] == 0)
991 firstArg
[i
] = data
[i
];
998 // Find second argument
1001 while (data
[i
] != 0)
1003 secondArg
[j
] = data
[i
];
1012 bool Tex2RTFConnection::OnExecute(const wxString
& topic
, char *data
, int size
, int format
)
1014 strcpy(Tex2RTFLastStatus
, "OK");
1017 char secondArg
[300];
1018 if (SplitCommand(data
, firstArg
, secondArg
))
1020 bool hasArg
= (strlen(secondArg
) > 0);
1021 if (strcmp(firstArg
, "INPUT") == 0 && hasArg
)
1023 if (InputFile
) delete[] InputFile
;
1024 InputFile
= copystring(secondArg
);
1028 wxString str
= wxFileNameFromPath(InputFile
);
1029 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
1030 frame
->SetTitle(buf
);
1033 else if (strcmp(firstArg
, "OUTPUT") == 0 && hasArg
)
1035 if (OutputFile
) delete[] OutputFile
;
1036 OutputFile
= copystring(secondArg
);
1038 else if (strcmp(firstArg
, "GO") == 0)
1040 strcpy(Tex2RTFLastStatus
, "WORKING");
1042 strcpy(Tex2RTFLastStatus
, "CONVERSION ERROR");
1044 strcpy(Tex2RTFLastStatus
, "OK");
1046 else if (strcmp(firstArg
, "EXIT") == 0)
1048 if (frame
) frame
->Close();
1050 else if (strcmp(firstArg
, "MINIMIZE") == 0 || strcmp(firstArg
, "ICONIZE") == 0)
1053 frame
->Iconize(TRUE
);
1055 else if (strcmp(firstArg
, "SHOW") == 0 || strcmp(firstArg
, "RESTORE") == 0)
1059 frame
->Iconize(FALSE
);
1065 // Try for a setting
1066 strcpy(Tex2RTFLastStatus
, RegisterSetting(firstArg
, secondArg
, FALSE
));
1068 if (frame
&& strcmp(firstArg
, "conversionMode") == 0)
1073 if (winHelp
&& (convertMode
== TEX_RTF
))
1074 strcat(buf
, "WinHelp RTF");
1075 else if (!winHelp
&& (convertMode
== TEX_RTF
))
1076 strcat(buf
, "linear RTF");
1077 else if (convertMode
== TEX_HTML
) strcat(buf
, "HTML");
1078 else if (convertMode
== TEX_XLP
) strcat(buf
, "XLP");
1079 strcat(buf
, " mode.");
1080 frame
->SetStatusText(buf
, 1);
1088 char *Tex2RTFConnection::OnRequest(const wxString
& topic
, const wxString
& item
, int *size
, int format
)
1090 return Tex2RTFLastStatus
;
1097 //void wxObject::Dump(ostream& str)
1099 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1100 // str << GetClassInfo()->GetClassName();
1102 // str << "unknown object class";