]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetText.cpp
DocBook: merge code duplicates for HTML and CALS tables.
[features.git] / src / insets / InsetText.cpp
index 2fc63cef7fcfcde209408c184fb635e376106a60..a4521848c310f280d9a0cbb6c9a42fdc9bf2ba75 100644 (file)
@@ -63,6 +63,7 @@
 
 #include "support/convert.h"
 #include "support/debug.h"
+#include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
@@ -663,14 +664,26 @@ void InsetText::docbookRenderAsImage(XMLStream & xs, OutputParams const & rp, XH
        // TODO: no real support for Unicode. This code is very similar to RenderPreview::addPreview, the same gotcha applies.
 
        graphics::PreviewLoader* loader = buffer().loader();
+       // This should be OK because we are exporting
+       LASSERT(loader != nullptr, return);
        loader->add(snippet);
        loader->startLoading(true); // Generate the image and wait until done.
        graphics::PreviewImage const * img = loader->preview(snippet);
        LASSERT(img != nullptr, return);
        support::FileName const & filename = img->filename();
 
+       // Use a file name that is only determined by the LaTeX code: the name of
+       // the snippet is more or less random (i.e., if the user generates the file
+       // several times, they will have a clutter of preview files).
+       // Hence: use a cryptographic hash of the snippet. If the snippet changes,
+       // the file name will change a lot; two snippets are unlikely to have the
+       // same hash (by design of cryptographic hash functions). Computing a hash
+       // is typically slow, but extremely fast compared to compilation of the
+       // preview and image rendering.
+       std::string newFileName = "lyx_" + sanitizeFileName(toHexHash(snippet)) + "." + filename.extension();
+
        // Copy the image into the right folder.
-       rp.exportdata->addExternalFile("docbook5", filename, filename.onlyFileName());
+       rp.exportdata->addExternalFile("docbook5", filename, newFileName);
 
        // TODO: deal with opts. What exactly is the WriterOuterTag here, for instance?
        // Start writing the DocBook code for the image.
@@ -680,7 +693,7 @@ void InsetText::docbookRenderAsImage(XMLStream & xs, OutputParams const & rp, XH
        // Output the rendered inset.
        xs << xml::StartTag("imageobject")
           << xml::CR()
-          << xml::CompTag("imagedata", std::string("fileref='") + filename.onlyFileName() + "'")
+          << xml::CompTag("imagedata", std::string("fileref='") + newFileName + "'")
           << xml::CR()
           << xml::EndTag("imageobject")
           << xml::CR();