X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7e9968f70242bde7240927593f8e9af0dd2a8d53..057bf0be5413b04a3ed638fbd61712d7b5f7cb3b:/utils/tex2rtf/src/htmlutil.cpp diff --git a/utils/tex2rtf/src/htmlutil.cpp b/utils/tex2rtf/src/htmlutil.cpp index 380c837ea2..553a563c87 100644 --- a/utils/tex2rtf/src/htmlutil.cpp +++ b/utils/tex2rtf/src/htmlutil.cpp @@ -10,10 +10,6 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -32,10 +28,8 @@ #include #define HTML_FILENAME_PATTERN _T("%s_%s.html") -#if !WXWIN_COMPATIBILITY_2_4 static inline wxChar* copystring(const wxChar* s) { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); } -#endif extern wxHashTable TexReferences; @@ -118,7 +112,7 @@ class TexNextPage: public wxObject label = copystring(theLabel); filename = copystring(theFile); } - ~TexNextPage(void) + virtual ~TexNextPage(void) { delete[] label; delete[] filename; @@ -252,6 +246,30 @@ void ReopenSectionContentsFile(void) } + + +struct textreplace +{ + wxChar *text; + size_t text_length; + wxChar *replace; + size_t replace_length; +}; + +#define tr(x,y) {x, sizeof(x)-1, y, sizeof(y)-1} + +static textreplace notverb_array[] = +{ + tr(_T("``"), _T("“")), + tr(_T("''"), _T("”")), + tr(_T("`"), _T("‘")), + tr(_T("'"), _T("’")), + tr(_T("---"), _T("—")), + tr(_T("--"), _T("–")), +}; + +#undef tr + /* * Given a TexChunk with a string value, scans through the string * converting Latex-isms into HTML-isms, such as 2 newlines ->

. @@ -264,38 +282,18 @@ void ProcessText2HTML(TexChunk *chunk) int ptr = 0; int i = 0; wxChar ch = 1; - int len = wxStrlen(chunk->value); + size_t len = wxStrlen(chunk->value); while (ch != 0) { ch = chunk->value[i]; - // 2 newlines means \par - if (!inVerbatim && chunk->value[i] == 10 && ((len > i+1 && chunk->value[i+1] == 10) || - ((len > i+1 && chunk->value[i+1] == 13) && - (len > i+2 && chunk->value[i+2] == 10)))) - { - BigBuffer[ptr] = 0; wxStrcat(BigBuffer, _T("

\n\n")); ptr += 5; - i += 2; - changed = true; - } - else if (!inVerbatim && ch == _T('`') && (len >= i+1 && chunk->value[i+1] == '`')) - { - BigBuffer[ptr] = '"'; ptr ++; - i += 2; - changed = true; - } - else if (!inVerbatim && ch == _T('`')) // Change ` to ' - { - BigBuffer[ptr] = 39; ptr ++; - i += 1; - changed = true; - } - else if (ch == _T('<')) // Change < to < + if (ch == _T('<')) // Change < to < { BigBuffer[ptr] = 0; wxStrcat(BigBuffer, _T("<")); ptr += 4; i += 1; + len--; changed = true; } else if (ch == _T('>')) // Change > to > @@ -304,13 +302,53 @@ void ProcessText2HTML(TexChunk *chunk) wxStrcat(BigBuffer, _T(">")); ptr += 4; i += 1; + len--; changed = true; } else { - BigBuffer[ptr] = ch; - i ++; - ptr ++; + bool replaced = false; + if (!inVerbatim) + { + for (size_t x = 0; x < WXSIZEOF(notverb_array); x++) + { + textreplace& tr = notverb_array[x]; + if (ch != tr.text[0]) + continue; + if (len < tr.text_length) + continue; + + size_t y; + for (y = 1; y < tr.text_length; y++) + { + if (chunk->value[y] != tr.text[y]) + break; + } + + if (y != tr.text_length) + continue; + + // can now copy it over. + for (y = 0; y < tr.replace_length; y++) + { + BigBuffer[ptr++] = tr.replace[y]; + } + + len -= tr.text_length; + i += tr.text_length; + replaced = true; + changed = true; + break; + } + } + + if (!replaced) + { + BigBuffer[ptr] = ch; + i ++; + ptr ++; + len--; + } } } BigBuffer[ptr] = 0; @@ -1826,7 +1864,7 @@ void HTMLOnMacro(int macroId, int no_args, bool start) static bool CheckTypeRef() { wxString typeDecl = GetArgData(); - if( !typeDecl.IsEmpty() ) { + if( !typeDecl.empty() ) { typeDecl.Replace(wxT("\\"),wxT("")); wxString label = typeDecl; label.Replace(wxT("const"),wxT("")); @@ -1839,7 +1877,7 @@ static bool CheckTypeRef() label.Trim(true); label.Trim(false); wxString typeName = label; label.MakeLower(); - TexRef *texRef = FindReference((wxChar*)label.c_str()); + TexRef *texRef = FindReference(label); if (texRef && texRef->refFile && wxStrcmp(texRef->refFile, _T("??")) != 0) { int a = typeDecl.Find(typeName); @@ -2115,7 +2153,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start) { wxString errBuf; errBuf.Printf(_T("Warning: unresolved reference '%s'"), refName); - OnInform((wxChar *)errBuf.c_str()); + OnInform(errBuf); } } } @@ -2247,7 +2285,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start) if (imageFile) delete[] imageFile; imageFile = NULL; - if (!f.IsEmpty()) + if (!f.empty()) { imageFile = copystring(f); } @@ -2267,7 +2305,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start) if (f != _T("")) { // The default HTML file to go to is THIS file (so a no-op) - SHGToMap((wxChar *)f.c_str(), currentFileName); + SHGToMap(f, currentFileName); } wxChar *mapName = GetArgData(); @@ -3031,223 +3069,222 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start) bool HTMLGo(void) { - fileId = 0; - inVerbatim = false; - indentLevel = 0; - inTabular = false; - startRows = false; - tableVerticalLineLeft = false; - tableVerticalLineRight = false; - noColumns = 0; - - if (InputFile && OutputFile) - { - // Do some HTML-specific transformations on all the strings, - // recursively - Text2HTML(GetTopLevelChunk()); - - wxChar buf[300]; - if (truncateFilenames) - wxSnprintf(buf, sizeof(buf), _T("%s.htm"), FileRoot); - else - wxSnprintf(buf, sizeof(buf), _T("%s_contents.html"), FileRoot); - if (TitlepageName) delete[] TitlepageName; - TitlepageName = copystring(buf); - Titlepage = wxFopen(buf, _T("w")); + fileId = 0; + inVerbatim = false; + indentLevel = 0; + inTabular = false; + startRows = false; + tableVerticalLineLeft = false; + tableVerticalLineRight = false; + noColumns = 0; - if (truncateFilenames) - wxSnprintf(buf, sizeof(buf), _T("%s_fc.htm"), FileRoot); - else - wxSnprintf(buf, sizeof(buf), _T("%s_fcontents.html"), FileRoot); + if (!InputFile.empty() && !OutputFile.empty()) + { + // Do some HTML-specific transformations on all the strings, + // recursively + Text2HTML(GetTopLevelChunk()); - contentsFrameName = copystring(buf); + wxChar buf[300]; + if (truncateFilenames) + wxSnprintf(buf, sizeof(buf), _T("%s.htm"), FileRoot); + else + wxSnprintf(buf, sizeof(buf), _T("%s_contents.html"), FileRoot); + if (TitlepageName) delete[] TitlepageName; + TitlepageName = copystring(buf); + Titlepage = wxFopen(buf, _T("w")); - Contents = wxFopen(TmpContentsName, _T("w")); + if (truncateFilenames) + wxSnprintf(buf, sizeof(buf), _T("%s_fc.htm"), FileRoot); + else + wxSnprintf(buf, sizeof(buf), _T("%s_fcontents.html"), FileRoot); - if (htmlFrameContents) - { -// FrameContents = wxFopen(TmpFrameContentsName, _T("w")); - FrameContents = wxFopen(contentsFrameName, _T("w")); - wxFprintf(FrameContents, _T("\n

\n")); + wxFprintf(FrameContents, _T("\n")); + fclose(FrameContents); + FrameContents = NULL; + } - // Output frame information - if (htmlFrameContents) - { - wxChar firstFileName[300]; - if (truncateFilenames) - wxSnprintf(firstFileName, sizeof(firstFileName), _T("%s1.htm"), FileRoot); - else - wxStrcpy(firstFileName, gs_filenames[1].c_str()); + if (Chapters) + { + wxFprintf(Chapters, _T("\n\n")); + fclose(Chapters); + Chapters = NULL; + } + if (Sections) + { + wxFprintf(Sections, _T("\n\n")); + fclose(Sections); + Sections = NULL; + } + if (Subsections && !combineSubSections) + { + wxFprintf(Subsections, _T("\n\n")); + fclose(Subsections); + Subsections = NULL; + } + if (Subsubsections && !combineSubSections) + { + wxFprintf(Subsubsections, _T("\n\n")); + fclose(Subsubsections); + Subsubsections = NULL; + } + if ( SectionContentsFD ) + { + fclose(SectionContentsFD); + SectionContentsFD = NULL; + } - wxFprintf(tmpTitle, _T("\n")); + // Create a temporary file for the title page header, add some info, + // and concat the titlepage just generated. + // This is necessary in order to put the title of the document + // at the TOP of the file within , even though we only find out + // what it is later on. + FILE *tmpTitle = wxFopen(_T("title.tmp"), _T("w")); + if (tmpTitle) + { + if (DocumentTitle) + { + SetCurrentOutput(tmpTitle); + HTMLHead(); + TexOutput(_T("\n")); + TraverseChildrenFromChunk(DocumentTitle); + TexOutput(_T("\n")); + } + else + { + SetCurrentOutput(tmpTitle); + HTMLHeadTo(tmpTitle); + if (contentsString) + wxFprintf(tmpTitle, _T("%s\n\n"), contentsString); + else + wxFprintf(tmpTitle, _T("%s\n\n"), wxFileNameFromPath(FileRoot)); + } - wxFprintf(tmpTitle, _T("\n"), ConvertCase(wxFileNameFromPath(contentsFrameName))); - wxFprintf(tmpTitle, _T("\n"), ConvertCase(wxFileNameFromPath(firstFileName))); - wxFprintf(tmpTitle, _T("\n")); + // Output frame information + if (htmlFrameContents) + { + wxChar firstFileName[300]; + if (truncateFilenames) + wxSnprintf(firstFileName, sizeof(firstFileName), _T("%s1.htm"), FileRoot); + else + wxStrcpy(firstFileName, gs_filenames[1].c_str()); - wxFprintf(tmpTitle, _T("\n")); - } + wxFprintf(tmpTitle, _T("<FRAMESET COLS=\"30%%,70%%\">\n")); - // Output <BODY...> to temporary title page - OutputBodyStart(); - fflush(tmpTitle); + wxFprintf(tmpTitle, _T("<FRAME SRC=\"%s\">\n"), ConvertCase(wxFileNameFromPath(contentsFrameName))); + wxFprintf(tmpTitle, _T("<FRAME SRC=\"%s\" NAME=\"mainwindow\">\n"), ConvertCase(wxFileNameFromPath(firstFileName))); + wxFprintf(tmpTitle, _T("</FRAMESET>\n")); - // Concat titlepage - FILE *fd = wxFopen(TitlepageName, _T("r")); - if (fd) - { - int ch = getc(fd); - while (ch != EOF) - { - wxPutc(ch, tmpTitle); - ch = getc(fd); - } - fclose(fd); - } + wxFprintf(tmpTitle, _T("<NOFRAMES>\n")); + } - wxFprintf(tmpTitle, _T("\n</FONT></BODY>\n")); + // Output <BODY...> to temporary title page + OutputBodyStart(); + fflush(tmpTitle); - if (htmlFrameContents) - { - wxFprintf(tmpTitle, _T("\n\n")); - } - wxFprintf(tmpTitle, _T("\n\n")); + // Concat titlepage + FILE *fd = wxFopen(TitlepageName, _T("r")); + if (fd) + { + int ch = getc(fd); + while (ch != EOF) + { + wxPutc(ch, tmpTitle); + ch = getc(fd); + } + fclose(fd); + } - fclose(tmpTitle); - if (wxFileExists(TitlepageName)) wxRemoveFile(TitlepageName); - if (!wxRenameFile(_T("title.tmp"), TitlepageName)) - { - wxCopyFile(_T("title.tmp"), TitlepageName); - wxRemoveFile(_T("title.tmp")); - } - } + wxFprintf(tmpTitle, _T("\n\n")); - if (lastFileName) delete[] lastFileName; - lastFileName = NULL; - if (lastTopic) delete[] lastTopic; - lastTopic = NULL; + if (htmlFrameContents) + { + wxFprintf(tmpTitle, _T("\n\n")); + } + wxFprintf(tmpTitle, _T("\n\n")); - if (wxFileExists(ContentsName)) wxRemoveFile(ContentsName); + fclose(tmpTitle); + if (wxFileExists(TitlepageName)) wxRemoveFile(TitlepageName); + if (!wxRenameFile(_T("title.tmp"), TitlepageName)) + { + wxCopyFile(_T("title.tmp"), TitlepageName); + wxRemoveFile(_T("title.tmp")); + } + } - if (!wxRenameFile(TmpContentsName, ContentsName)) - { - wxCopyFile(TmpContentsName, ContentsName); - wxRemoveFile(TmpContentsName); - } + if (lastFileName) delete[] lastFileName; + lastFileName = NULL; + if (lastTopic) delete[] lastTopic; + lastTopic = NULL; - // Generate .htx file if requested - if (htmlIndex) - { - wxChar htmlIndexName[300]; - wxSnprintf(htmlIndexName, sizeof(htmlIndexName), _T("%s.htx"), FileRoot); - GenerateHTMLIndexFile(htmlIndexName); - } + if (wxFileExists(ContentsName)) wxRemoveFile(ContentsName); - // Generate HTML Help Workshop files if requested - if (htmlWorkshopFiles) - { - HTMLWorkshopEndContents(); - GenerateHTMLWorkshopFiles(FileRoot); - } + if (!wxRenameFile(TmpContentsName, ContentsName)) + { + wxCopyFile(TmpContentsName, ContentsName); + wxRemoveFile(TmpContentsName); + } + // Generate .htx file if requested + if (htmlIndex) + { + wxChar htmlIndexName[300]; + wxSnprintf(htmlIndexName, sizeof(htmlIndexName), _T("%s.htx"), FileRoot); + GenerateHTMLIndexFile(htmlIndexName); + } - return true; - } + // Generate HTML Help Workshop files if requested + if (htmlWorkshopFiles) + { + HTMLWorkshopEndContents(); + GenerateHTMLWorkshopFiles(FileRoot); + } - return false; + return true; + } + + return false; } // Output .htx index file