This patch is a HTML exporter cleanup. The changes are:

- do not export an image more then once if it is used multiple times in
the document, but reuse it instead.
- cleanup the filenames of the images written [they are now exactly the
same as the original filename, but with extension "png"]
- replace the "_d" from the created data directory with "_data"

Bye
  Marc

Index: abi/src/wp/impexp/xp/ie_exp_HTML.cpp
===================================================================
RCS file: /cvsroot/abi/src/wp/impexp/xp/ie_exp_HTML.cpp,v
retrieving revision 1.114
diff -u -r1.114 ie_exp_HTML.cpp
--- abi/src/wp/impexp/xp/ie_exp_HTML.cpp        2002/01/07 08:53:13     1.114
+++ abi/src/wp/impexp/xp/ie_exp_HTML.cpp        2002/01/21 15:38:56
@@ -252,6 +252,7 @@
        void                            _convertFontSize(char* szDest, const char* 
pszFontSize);
        void                            _convertColor(char* szDest, const char* 
pszColor);
        void                            _storeStyles(void);
+       char *                          _stripSuffix(const char* from, char delimiter);
        
        PD_Document *           m_pDocument;
        IE_Exp_HTML *           m_pie;
@@ -1906,12 +1907,17 @@
 
                                        m_utvDataIDs.push_back(dataid);
 
-                                       sprintf(buf, "%s.png", UT_basename(szValue));
-                                       m_pie->write("<img alt=\"AbiWord Image");
+                                       char * temp = 
+_stripSuffix(UT_basename(szValue), '_');
+                                       char * fstripped = _stripSuffix(temp, '.');
+                                       FREEP(temp);
+                                       sprintf(buf, "%s.png", fstripped);
+                                       FREEP(fstripped);
+                                       
+                                       m_pie->write("<img alt=\"AbiWord Image ");
                                        m_pie->write(buf);
                                        m_pie->write("\" src=\"");
                                        
m_pie->write(UT_basename(m_pie->getFileName()));
-                                       m_pie->write("_d/");
+                                       m_pie->write("_data/");
                                        m_pie->write(buf);
                                        m_pie->write("\" ");
                                        
@@ -2145,6 +2151,30 @@
 /*****************************************************************/
 /*****************************************************************/
 
+/*!
+   removes the suffix from a string by searching backwards for the specified 
+   character delimiter. If the delimiter is not found, a copy of the original 
+   string is returned
+   
+   eg. _stripSuffix("/home/user/file.png, '.') returns "/home/user/file" 
+       _stripSuffix("/home/user/foo_bar, '_') returns /home/user/foo 
+       _stripSuffix("/home/user/file.png, '_') returns /home/user/file.png"
+*/
+char *s_HTML_Listener::_stripSuffix(const char* from, char delimiter)
+{
+    char * fremove_s = (char *)malloc(strlen(from)+1);
+    strcpy(fremove_s, from);   
+
+    char * p = fremove_s + strlen(fremove_s);
+    while ((p >= fremove_s) && (*p != delimiter))
+        p--;
+       
+    if (p >= fremove_s)
+       *p = '\0';
+    
+    return fremove_s;
+}
+
 void s_HTML_Listener::_handleDataItems(void)
 {
        const char * szName;
@@ -2168,7 +2198,7 @@
                        FILE *fp;
                        char fname [1024]; // EVIL EVIL bad hardcoded buffer size
                        
-                       sprintf(fname, "%s_d", m_pie->getFileName());
+                       sprintf(fname, "%s_data", m_pie->getFileName());
                        int result = m_pDocument->getApp()->makeDirectory(fname, 0750);
                        
                        if (!UT_strcmp(szMimeType, "image/svg-xml"))
@@ -2176,22 +2206,31 @@
                        if (!UT_strcmp(szMimeType, "text/mathml"))
                                sprintf(fname, "%s/%s_%d.mathml", fname, szName, loc);
                        else // PNG Image
-                               sprintf(fname, "%s/%s.png", fname, 
UT_basename(szName));
+                       {  
+                               char * temp = _stripSuffix(UT_basename(szName), '_');
+                               char * fstripped = _stripSuffix(temp, '.');
+                               FREEP(temp);
+                               sprintf(fname, "%s/%s.png", fname, fstripped);
+                               FREEP(fstripped);
+                       }
+                       
+                       if (!UT_isRegularFile(fname))
+                       {
+                           fp = fopen (fname, "wb+");
                        
-                       fp = fopen (fname, "wb+");
+                           if(!fp)
+                                   continue;
                        
-                       if(!fp)
-                               continue;
+                           int cnt = 0, len = pByteBuf->getLength();
                        
-                       int cnt = 0, len = pByteBuf->getLength();
+                           while (cnt < len)
+                           {
+                                   cnt += fwrite (pByteBuf->getPointer(cnt), 
+                                                            sizeof(UT_Byte), len-cnt, 
+fp);
+                           }
                        
-                       while (cnt < len)
-                       {
-                               cnt += fwrite (pByteBuf->getPointer(cnt), 
-                                                          sizeof(UT_Byte), len-cnt, 
fp);
+                           fclose(fp);
                        }
-                       
-                       fclose(fp);
                }
        }
        

Reply via email to