]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetText.cpp
Change label "Show Zoom" to "Show Zoom Value"
[features.git] / src / insets / InsetText.cpp
index 819c87d6a6bef5511f1ed8693e22719b85abd532..a4521848c310f280d9a0cbb6c9a42fdc9bf2ba75 100644 (file)
 
 #include <config.h>
 
-#include "InsetLayout.h"
 #include "InsetText.h"
 
+#include "mathed/MacroTable.h"
+
 #include "insets/InsetArgument.h"
 #include "insets/InsetLayout.h"
+#include "insets/InsetPreview.h"
+
+#include "graphics/PreviewImage.h"
+#include "graphics/PreviewLoader.h"
 
 #include "buffer_funcs.h"
 #include "Buffer.h"
@@ -26,6 +31,7 @@
 #include "CutAndPaste.h"
 #include "DispatchResult.h"
 #include "ErrorList.h"
+#include "Exporter.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "InsetList.h"
 
 #include "support/convert.h"
 #include "support/debug.h"
+#include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
 #include "support/Changer.h"
+#include "support/FileName.h"
 
 #include <algorithm>
 #include <stack>
@@ -72,9 +80,6 @@ using namespace lyx::support;
 
 namespace lyx {
 
-using graphics::PreviewLoader;
-
-
 /////////////////////////////////////////////////////////////////////
 
 InsetText::InsetText(Buffer * buf, UsePlain type)
@@ -606,7 +611,6 @@ int InsetText::plaintext(odocstringstream & os,
 }
 
 
-
 void InsetText::docbook(XMLStream & xs, OutputParams const & rp) const
 {
        docbook(xs, rp, WriteEverything);
@@ -615,8 +619,7 @@ void InsetText::docbook(XMLStream & xs, OutputParams const & rp) const
 
 void InsetText::docbook(XMLStream & xs, OutputParams const & rp, XHTMLOptions opts) const
 {
-       // we will always want to output all our paragraphs when we are
-       // called this way.
+       // Always output all the paragraphs.
        OutputParams runparams = rp;
        runparams.par_begin = 0;
        runparams.par_end = text().paragraphs().size();
@@ -628,13 +631,92 @@ void InsetText::docbook(XMLStream & xs, OutputParams const & rp, XHTMLOptions op
                return;
        }
 
-       InsetLayout const & il = getLayout();
+       InsetLayout const &il = getLayout();
 
-       // Maybe this is an <info> paragraph that should not be generated at all (i.e. right now, its place is somewhere
-       // else, typically outside the current paragraph).
+       // Maybe this is an <info> paragraph that should not be generated
+       // at all (i.e. right now, its place is somewhere else, typically outside
+       // the current paragraph).
        if (!rp.docbook_generate_info && il.docbookininfo() != "never")
                return;
 
+       // Maybe this inset must be rendered before being output.
+       if (il.docbookrenderasimage()) {
+               docbookRenderAsImage(xs, runparams, opts);
+               return;
+       }
+
+       // If none of the special cases before apply, output the inset.
+       docbookText(xs, runparams, opts);
+}
+
+
+void InsetText::docbookRenderAsImage(XMLStream & xs, OutputParams const & rp, XHTMLOptions opts) const
+{
+       LASSERT(getLayout().docbookrenderasimage(), return);
+
+       // Generate the LaTeX code to compile in order to get the image.
+       // This code actually does the same as an InsetPreview, but without
+       // an InsetPreview.
+       // Also, the image must be generated before the DocBook output is finished,
+       // unlike a preview that is not immediately required for display.
+       docstring const latex_snippet = insetToLaTeXSnippet(&buffer(), this);
+       std::string const snippet = support::trim(to_utf8(latex_snippet));
+       // 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, newFileName);
+
+       // TODO: deal with opts. What exactly is the WriterOuterTag here, for instance?
+       // Start writing the DocBook code for the image.
+       xs << xml::StartTag("mediaobject")
+          << xml::CR();
+
+       // Output the rendered inset.
+       xs << xml::StartTag("imageobject")
+          << xml::CR()
+          << xml::CompTag("imagedata", std::string("fileref='") + newFileName + "'")
+          << xml::CR()
+          << xml::EndTag("imageobject")
+          << xml::CR();
+
+       // Output the raw content.
+       xs << xml::StartTag("textobject")
+          << xml::CR()
+          << xml::StartTag("programlisting", "language='latex' role='" + getLayout().latexname() + "'");
+       docbookText(xs, rp, opts);
+       xs << xml::EndTag("programlisting")
+          << xml::CR()
+          << xml::EndTag("textobject")
+          << xml::CR();
+
+       xs << xml::EndTag("mediaobject");
+}
+
+
+void InsetText::docbookText(XMLStream & xs, OutputParams const & rp, XHTMLOptions opts) const
+{
+       InsetLayout const &il = getLayout();
+       OutputParams runparams = rp;
+
        // In some cases, the input parameters must be overridden for outer tags.
        bool writeOuterTag = opts & WriteOuterTag;
        if (writeOuterTag) {
@@ -667,7 +749,7 @@ void InsetText::docbook(XMLStream & xs, OutputParams const & rp, XHTMLOptions op
                        if (par.getInset(i) && par.getInset(i)->lyxCode() == ARG_CODE) {
                                InsetArgument const *arg = par.getInset(i)->asInsetArgument();
                                if (arg->docbookargumentaftermaintag())
-                    appendedArguments.insert(par.getInset(i)->asInsetArgument());
+                                       appendedArguments.insert(par.getInset(i)->asInsetArgument());
                        }
                }
        }
@@ -686,6 +768,9 @@ void InsetText::docbook(XMLStream & xs, OutputParams const & rp, XHTMLOptions op
                                attrs += from_ascii(" xlink:href=\"") + text_.asString() + from_ascii("\"");
                        xml::openTag(xs, il.docbooktag(), attrs, il.docbooktagtype());
                }
+
+               if (!il.docbookinnertag().empty() && il.docbookinnertag() != "NONE" && il.docbookinnertag() != "IGNORE")
+                       xml::openTag(xs, il.docbookinnertag(), il.docbookinnerattr(), il.docbookinnertagtype());
        }
 
        // - Think about the arguments before the paragraph.
@@ -711,7 +796,7 @@ void InsetText::docbook(XMLStream & xs, OutputParams const & rp, XHTMLOptions op
        // No need for labels that are generated from counters. They should be handled by the external DocBook processor.
 
        // With respect to XHTML, paragraphs are still allowed here.
-       if (!allowMultiPar())
+       if (runparams.docbook_consider_allow_multi_par && !allowMultiPar())
                runparams.docbook_make_pars = false;
        if (il.isPassThru())
                runparams.pass_thru = true;
@@ -733,6 +818,9 @@ void InsetText::docbook(XMLStream & xs, OutputParams const & rp, XHTMLOptions op
                if (!il.docbookitemwrappertag().empty() && il.docbookitemwrappertag() != "NONE" && il.docbookitemwrappertag() != "IGNORE")
                        xml::closeTag(xs, il.docbookitemwrappertag(), il.docbookitemwrappertagtype());
 
+               if (!il.docbookinnertag().empty() && il.docbookinnertag() != "NONE" && il.docbookinnertag() != "IGNORE")
+                       xml::closeTag(xs, il.docbookinnertag(), il.docbookinnertagtype());
+
                if (!il.docbooktag().empty() && il.docbooktag() != "NONE" && il.docbooktag() != "IGNORE")
                        xml::closeTag(xs, il.docbooktag(), il.docbooktagtype());
 
@@ -895,7 +983,7 @@ void InsetText::appendParagraphs(ParagraphList & plist)
 
 
 void InsetText::addPreview(DocIterator const & text_inset_pos,
-       PreviewLoader & loader) const
+       graphics::PreviewLoader & loader) const
 {
        ParagraphList::const_iterator pit = paragraphs().begin();
        ParagraphList::const_iterator pend = paragraphs().end();