]>
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");
251 sprintf(buf
, "Invalid switch %s.\n", argv
[i
]);
262 #if defined(__WXMSW__) && !defined(NO_GUI)
264 Tex2RTFLastStatus
[0] = 0; // DDE connection return value
265 TheTex2RTFServer
= new Tex2RTFServer
;
266 TheTex2RTFServer
->Create("TEX2RTF");
269 #if defined(__WXMSW__) && defined(__WIN16__)
270 // Limit to max Windows array size
271 if (BufSize
> 64) BufSize
= 64;
274 TexInitialize(BufSize
);
275 ResetContentsLevels(0);
283 // Create the main frame window
284 frame
= new MyFrame(NULL
, -1, "Tex2RTF", wxPoint(-1, -1), wxSize(400, 300));
285 frame
->CreateStatusBar(2);
288 // TODO: uncomment this when we have tex2rtf.xpm
289 frame
->SetIcon(wxICON(tex2rtf
));
293 sprintf(buf
, "Tex2RTF [%s]", FileNameFromPath(InputFile
));
294 frame
->SetTitle(buf
);
298 wxMenu
*file_menu
= new wxMenu
;
299 file_menu
->Append(TEX_GO
, "&Go", "Run converter");
300 file_menu
->Append(TEX_SET_INPUT
, "Set &Input File", "Set the LaTeX input file");
301 file_menu
->Append(TEX_SET_OUTPUT
, "Set &Output File", "Set the output file");
302 file_menu
->AppendSeparator();
303 file_menu
->Append(TEX_VIEW_LATEX
, "View &LaTeX File", "View the LaTeX input file");
304 file_menu
->Append(TEX_VIEW_OUTPUT
, "View Output &File", "View output file");
305 file_menu
->Append(TEX_SAVE_FILE
, "&Save log file", "Save displayed text into file");
306 file_menu
->AppendSeparator();
307 file_menu
->Append(TEX_QUIT
, "E&xit", "Exit Tex2RTF");
309 wxMenu
*macro_menu
= new wxMenu
;
311 macro_menu
->Append(TEX_LOAD_CUSTOM_MACROS
, "&Load Custom Macros", "Load custom LaTeX macro file");
312 macro_menu
->Append(TEX_VIEW_CUSTOM_MACROS
, "View &Custom Macros", "View custom LaTeX macros");
314 wxMenu
*mode_menu
= new wxMenu
;
316 mode_menu
->Append(TEX_MODE_RTF
, "Output linear &RTF", "Wordprocessor-compatible RTF");
317 mode_menu
->Append(TEX_MODE_WINHELP
, "Output &WinHelp RTF", "WinHelp-compatible RTF");
318 mode_menu
->Append(TEX_MODE_HTML
, "Output &HTML", "HTML World Wide Web hypertext file");
319 mode_menu
->Append(TEX_MODE_XLP
, "Output &XLP", "wxHelp hypertext help file");
321 wxMenu
*help_menu
= new wxMenu
;
323 help_menu
->Append(TEX_HELP
, "&Help", "Tex2RTF Contents Page");
324 help_menu
->Append(TEX_ABOUT
, "&About Tex2RTF", "About Tex2RTF");
326 menuBar
= new wxMenuBar
;
327 menuBar
->Append(file_menu
, "&File");
328 menuBar
->Append(macro_menu
, "&Macros");
329 menuBar
->Append(mode_menu
, "&Conversion Mode");
330 menuBar
->Append(help_menu
, "&Help");
332 frame
->SetMenuBar(menuBar
);
333 frame
->textWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(-1, -1), wxSize(-1, -1), wxTE_READONLY
|wxTE_MULTILINE
);
335 (*frame
->textWindow
) << "Welcome to Julian Smart's LaTeX to RTF converter.\n";
339 HelpInstance
= new wxHelpController();
340 HelpInstance
->Initialize("tex2rtf");
344 * Read macro/initialisation file
349 if ((path
= TexPathList
.FindValidPath(MacroFile
)) != "")
350 ReadCustomMacros((char*) (const char*) path
);
354 if (winHelp
&& (convertMode
== TEX_RTF
))
355 strcat(buf
, "WinHelp RTF");
356 else if (!winHelp
&& (convertMode
== TEX_RTF
))
357 strcat(buf
, "linear RTF");
358 else if (convertMode
== TEX_HTML
) strcat(buf
, "HTML");
359 else if (convertMode
== TEX_XLP
) strcat(buf
, "XLP");
360 strcat(buf
, " mode.");
361 frame
->SetStatusText(buf
, 1);
370 * Read macro/initialisation file
375 if ((path
= TexPathList
.FindValidPath(MacroFile
)) != "")
376 ReadCustomMacros((char*) (const char*) path
);
388 // Return the main frame window
399 wxNode
*node
= CustomMacroList
.First();
402 CustomMacro
*macro
= (CustomMacro
*)node
->Data();
405 node
= CustomMacroList
.First();
407 MacroDefs
.BeginFind();
408 node
= MacroDefs
.Next();
411 TexMacroDef
* def
= (TexMacroDef
*) node
->Data();
413 node
= MacroDefs
.Next();
417 delete TheTex2RTFServer
;
425 // TODO: this simulates zero-memory leaks!
426 // Otherwise there are just too many...
428 wxDebugContext::SetCheckpoint();
434 void ShowOptions(void)
437 sprintf(buf
, "Tex2RTF version %.2f", versionNo
);
439 OnInform("Usage: tex2rtf [input] [output] [switches]\n");
440 OnInform("where valid switches are");
441 OnInform(" -interactive");
442 OnInform(" -bufsize <size in K>");
443 OnInform(" -charset <pc | pca | ansi | mac> (default ansi)");
446 OnInform(" -macros <filename>");
447 OnInform(" -winhelp");
455 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
456 EVT_CLOSE(MyFrame::OnCloseWindow
)
457 EVT_MENU(TEX_QUIT
, MyFrame::OnExit
)
458 EVT_MENU(TEX_GO
, MyFrame::OnGo
)
459 EVT_MENU(TEX_SET_INPUT
, MyFrame::OnSetInput
)
460 EVT_MENU(TEX_SET_OUTPUT
, MyFrame::OnSetOutput
)
461 EVT_MENU(TEX_SAVE_FILE
, MyFrame::OnSaveFile
)
462 EVT_MENU(TEX_VIEW_LATEX
, MyFrame::OnViewLatex
)
463 EVT_MENU(TEX_VIEW_OUTPUT
, MyFrame::OnViewOutput
)
464 EVT_MENU(TEX_VIEW_CUSTOM_MACROS
, MyFrame::OnShowMacros
)
465 EVT_MENU(TEX_LOAD_CUSTOM_MACROS
, MyFrame::OnLoadMacros
)
466 EVT_MENU(TEX_MODE_RTF
, MyFrame::OnModeRTF
)
467 EVT_MENU(TEX_MODE_WINHELP
, MyFrame::OnModeWinHelp
)
468 EVT_MENU(TEX_MODE_HTML
, MyFrame::OnModeHTML
)
469 EVT_MENU(TEX_MODE_XLP
, MyFrame::OnModeXLP
)
470 EVT_MENU(TEX_HELP
, MyFrame::OnHelp
)
471 EVT_MENU(TEX_ABOUT
, MyFrame::OnAbout
)
474 // My frame constructor
475 MyFrame::MyFrame(wxFrame
*frame
, wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
476 wxFrame(frame
, id
, title
, pos
, size
)
479 void MyFrame::OnCloseWindow(wxCloseEvent
& event
)
481 if (!stopRunning
&& !OkToClose
)
493 void MyFrame::OnExit(wxCommandEvent
& event
)
498 void MyFrame::OnGo(wxCommandEvent
& event
)
500 menuBar
->EnableTop(0, FALSE
);
501 menuBar
->EnableTop(1, FALSE
);
502 menuBar
->EnableTop(2, FALSE
);
503 menuBar
->EnableTop(3, FALSE
);
513 menuBar
->EnableTop(0, TRUE
);
514 menuBar
->EnableTop(1, TRUE
);
515 menuBar
->EnableTop(2, TRUE
);
516 menuBar
->EnableTop(3, TRUE
);
519 void MyFrame::OnSetInput(wxCommandEvent
& event
)
521 ChooseInputFile(TRUE
);
524 void MyFrame::OnSetOutput(wxCommandEvent
& event
)
526 ChooseOutputFile(TRUE
);
529 void MyFrame::OnSaveFile(wxCommandEvent
& event
)
531 wxString s
= wxFileSelector("Save text to file", "", "", "txt", "*.txt");
534 textWindow
->SaveFile(s
);
536 sprintf(buf
, "Saved text to %s", (const char*) s
);
537 frame
->SetStatusText(buf
, 0);
541 void MyFrame::OnViewOutput(wxCommandEvent
& event
)
544 if (OutputFile
&& wxFileExists(OutputFile
))
546 textWindow
->LoadFile(OutputFile
);
548 wxString
str(wxFileNameFromPath(OutputFile
));
549 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
550 frame
->SetTitle(buf
);
554 void MyFrame::OnViewLatex(wxCommandEvent
& event
)
557 if (InputFile
&& wxFileExists(InputFile
))
559 textWindow
->LoadFile(InputFile
);
561 wxString
str(wxFileNameFromPath(OutputFile
));
562 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
563 frame
->SetTitle(buf
);
567 void MyFrame::OnLoadMacros(wxCommandEvent
& event
)
570 wxString s
= wxFileSelector("Choose custom macro file", wxPathOnly(MacroFile
), wxFileNameFromPath(MacroFile
), "ini", "*.ini");
571 if (s
!= "" && wxFileExists(s
))
573 MacroFile
= copystring(s
);
574 ReadCustomMacros((char*) (const char*) s
);
579 void MyFrame::OnShowMacros(wxCommandEvent
& event
)
586 void MyFrame::OnModeRTF(wxCommandEvent
& event
)
588 convertMode
= TEX_RTF
;
592 SetStatusText("In linear RTF mode.", 1);
595 void MyFrame::OnModeWinHelp(wxCommandEvent
& event
)
597 convertMode
= TEX_RTF
;
601 SetStatusText("In WinHelp RTF mode.", 1);
604 void MyFrame::OnModeHTML(wxCommandEvent
& event
)
606 convertMode
= TEX_HTML
;
610 SetStatusText("In HTML mode.", 1);
613 void MyFrame::OnModeXLP(wxCommandEvent
& event
)
615 convertMode
= TEX_XLP
;
618 SetStatusText("In XLP mode.", 1);
621 void MyFrame::OnHelp(wxCommandEvent
& event
)
624 HelpInstance
->LoadFile();
625 HelpInstance
->DisplayContents();
629 void MyFrame::OnAbout(wxCommandEvent
& event
)
633 char *platform
= " (32-bit)";
636 char *platform
= " (16-bit)";
641 sprintf(buf
, "Tex2RTF Version %.2f%s\nLaTeX to RTF, WinHelp, HTML and wxHelp Conversion\n\n(c) Julian Smart 1999", versionNo
, platform
);
642 wxMessageBox(buf
, "About Tex2RTF");
645 void ChooseInputFile(bool force
)
647 if (force
|| !InputFile
)
649 wxString s
= wxFileSelector("Choose LaTeX input file", wxPathOnly(InputFile
), wxFileNameFromPath(InputFile
), "tex", "*.tex");
652 // Different file, so clear index entries.
654 ResetContentsLevels(0);
657 InputFile
= copystring(s
);
658 wxString str
= wxFileNameFromPath(InputFile
);
659 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
660 frame
->SetTitle(buf
);
666 void ChooseOutputFile(bool force
)
668 char extensionBuf
[10];
670 strcpy(wildBuf
, "*.");
673 path
= wxPathOnly(OutputFile
);
675 path
= wxPathOnly(InputFile
);
681 strcpy(extensionBuf
, "rtf");
682 strcat(wildBuf
, "rtf");
687 strcpy(extensionBuf
, "xlp");
688 strcat(wildBuf
, "xlp");
693 #if defined(__WXMSW__) && defined(__WIN16__)
694 strcpy(extensionBuf
, "htm");
695 strcat(wildBuf
, "htm");
697 strcpy(extensionBuf
, "html");
698 strcat(wildBuf
, "html");
703 if (force
|| !OutputFile
)
705 wxString s
= wxFileSelector("Choose output file", path
, wxFileNameFromPath(OutputFile
),
706 extensionBuf
, wildBuf
);
708 OutputFile
= copystring(s
);
720 if (!InputFile
|| !OutputFile
)
727 wxString str
= wxFileNameFromPath(InputFile
);
729 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
730 frame
->SetTitle(buf
);
736 // Find extension-less filename
737 strcpy(FileRoot
, OutputFile
);
738 StripExtension(FileRoot
);
740 if (truncateFilenames
&& convertMode
== TEX_HTML
)
742 // Truncate to five characters. This ensures that
743 // we can generate DOS filenames such as thing999. But 1000 files
744 // may not be enough, of course...
745 char* sName
= wxFileNameFromPath( FileRoot
); // this Julian's method is non-destructive reference
748 if(strlen( sName
) > 5)
749 sName
[5] = '\0'; // that should do!
752 sprintf(ContentsName
, "%s.con", FileRoot
);
753 sprintf(TmpContentsName
, "%s.cn1", FileRoot
);
754 sprintf(TmpFrameContentsName
, "%s.frc", FileRoot
);
755 sprintf(WinHelpContentsFileName
, "%s.cnt", FileRoot
);
756 sprintf(RefName
, "%s.ref", FileRoot
);
758 TexPathList
.EnsureFileAccessible(InputFile
);
761 wxString s
= TexPathList
.FindValidPath("bullet.bmp");
764 wxString str
= wxFileNameFromPath(s
);
765 bulletFile
= copystring(str
);
769 if (wxFileExists(RefName
))
770 ReadTexReferences(RefName
);
772 bool success
= FALSE
;
774 if (InputFile
&& OutputFile
)
776 if (!FileExists(InputFile
))
778 OnError("Cannot open input file!");
786 sprintf(buf
, "Working, pass %d...", passNumber
);
787 frame
->SetStatusText(buf
);
791 OnInform("Reading LaTeX file...");
792 TexLoadFile(InputFile
);
815 OnInform("*** Aborted by user.");
822 WriteTexReferences(RefName
);
824 startedSections
= FALSE
;
828 long tim
= wxGetElapsedTime();
829 sprintf(buf
, "Finished in %ld seconds.", (long)(tim
/1000.0));
833 sprintf(buf
, "Done, %d %s.", passNumber
, (passNumber
> 1) ? "passes" : "pass");
834 frame
->SetStatusText(buf
);
837 sprintf(buf
, "Done, %d %s.", passNumber
, (passNumber
> 1) ? "passes" : "pass");
846 startedSections
= FALSE
;
848 OnInform("Sorry, unsuccessful.");
853 void OnError(char *msg
)
856 cerr
<< "Error: " << msg
<< "\n";
859 if (isInteractive
&& frame
)
860 (*frame
->textWindow
) << "Error: " << msg
<< "\n";
864 cerr
<< "Error: " << msg
<< "\n";
875 void OnInform(char *msg
)
881 if (isInteractive
&& frame
)
882 (*frame
->textWindow
) << msg
<< "\n";
900 void OnMacro(int macroId
, int no_args
, bool start
)
906 RTFOnMacro(macroId
, no_args
, start
);
911 XLPOnMacro(macroId
, no_args
, start
);
916 HTMLOnMacro(macroId
, no_args
, start
);
922 bool OnArgument(int macroId
, int arg_no
, bool start
)
928 return RTFOnArgument(macroId
, arg_no
, start
);
933 return XLPOnArgument(macroId
, arg_no
, start
);
938 return HTMLOnArgument(macroId
, arg_no
, start
);
948 #if defined(__WXMSW__) && !defined(NO_GUI)
954 wxConnectionBase
*Tex2RTFServer::OnAcceptConnection(const wxString
& topic
)
956 if (topic
== "TEX2RTF")
959 ipc_buffer
= new char[1000];
961 return new Tex2RTFConnection(ipc_buffer
, 4000);
971 Tex2RTFConnection::Tex2RTFConnection(char *buf
, int size
):wxDDEConnection(buf
, size
)
975 Tex2RTFConnection::~Tex2RTFConnection(void)
979 bool SplitCommand(char *data
, char *firstArg
, char *secondArg
)
984 int len
= strlen(data
);
986 // Find first argument (command name)
989 if (data
[i
] == ' ' || data
[i
] == 0)
993 firstArg
[i
] = data
[i
];
1000 // Find second argument
1003 while (data
[i
] != 0)
1005 secondArg
[j
] = data
[i
];
1014 bool Tex2RTFConnection::OnExecute(const wxString
& topic
, char *data
, int size
, int format
)
1016 strcpy(Tex2RTFLastStatus
, "OK");
1019 char secondArg
[300];
1020 if (SplitCommand(data
, firstArg
, secondArg
))
1022 bool hasArg
= (strlen(secondArg
) > 0);
1023 if (strcmp(firstArg
, "INPUT") == 0 && hasArg
)
1025 if (InputFile
) delete[] InputFile
;
1026 InputFile
= copystring(secondArg
);
1030 wxString str
= wxFileNameFromPath(InputFile
);
1031 sprintf(buf
, "Tex2RTF [%s]", (const char*) str
);
1032 frame
->SetTitle(buf
);
1035 else if (strcmp(firstArg
, "OUTPUT") == 0 && hasArg
)
1037 if (OutputFile
) delete[] OutputFile
;
1038 OutputFile
= copystring(secondArg
);
1040 else if (strcmp(firstArg
, "GO") == 0)
1042 strcpy(Tex2RTFLastStatus
, "WORKING");
1044 strcpy(Tex2RTFLastStatus
, "CONVERSION ERROR");
1046 strcpy(Tex2RTFLastStatus
, "OK");
1048 else if (strcmp(firstArg
, "EXIT") == 0)
1050 if (frame
) frame
->Close();
1052 else if (strcmp(firstArg
, "MINIMIZE") == 0 || strcmp(firstArg
, "ICONIZE") == 0)
1055 frame
->Iconize(TRUE
);
1057 else if (strcmp(firstArg
, "SHOW") == 0 || strcmp(firstArg
, "RESTORE") == 0)
1061 frame
->Iconize(FALSE
);
1067 // Try for a setting
1068 strcpy(Tex2RTFLastStatus
, RegisterSetting(firstArg
, secondArg
, FALSE
));
1070 if (frame
&& strcmp(firstArg
, "conversionMode") == 0)
1075 if (winHelp
&& (convertMode
== TEX_RTF
))
1076 strcat(buf
, "WinHelp RTF");
1077 else if (!winHelp
&& (convertMode
== TEX_RTF
))
1078 strcat(buf
, "linear RTF");
1079 else if (convertMode
== TEX_HTML
) strcat(buf
, "HTML");
1080 else if (convertMode
== TEX_XLP
) strcat(buf
, "XLP");
1081 strcat(buf
, " mode.");
1082 frame
->SetStatusText(buf
, 1);
1090 char *Tex2RTFConnection::OnRequest(const wxString
& topic
, const wxString
& item
, int *size
, int format
)
1092 return Tex2RTFLastStatus
;
1099 //void wxObject::Dump(ostream& str)
1101 // if (GetClassInfo() && GetClassInfo()->GetClassName())
1102 // str << GetClassInfo()->GetClassName();
1104 // str << "unknown object class";