#include "support/convert.h"
#include "support/debug.h"
+#include "support/filetools.h"
#include "support/gettext.h"
#include "support/lassert.h"
#include "support/lstrings.h"
// 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.
// 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();