]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetInclude.cpp
Restore XHTML output for InsetListings.
[lyx.git] / src / insets / InsetInclude.cpp
index cedfb5fd5474bf4e08cc2751800d86bbc08f5903..021dd4c7303f93652fb0483b2a8db4dfd89b9852 100644 (file)
@@ -33,6 +33,7 @@
 #include "LyXRC.h"
 #include "Lexer.h"
 #include "MetricsInfo.h"
+#include "output_xhtml.h"
 #include "OutputParams.h"
 #include "TextClass.h"
 #include "TocBackend.h"
@@ -147,20 +148,20 @@ FileName const includedFilename(Buffer const & buffer,
 }
 
 
-InsetLabel * createLabel(docstring const & label_str)
+InsetLabel * createLabel(Buffer * buf, docstring const & label_str)
 {
        if (label_str.empty())
                return 0;
        InsetCommandParams icp(LABEL_CODE);
        icp["name"] = label_str;
-       return new InsetLabel(icp);
+       return new InsetLabel(buf, icp);
 }
 
 } // namespace anon
 
 
-InsetInclude::InsetInclude(InsetCommandParams const & p)
-       : InsetCommand(p, "include"), include_label(uniqueID()),
+InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p)
+       : InsetCommand(buf, p, "include"), include_label(uniqueID()),
          preview_(new RenderMonitoredPreview(this)), failedtoload_(false),
          set_label_(false), label_(0), child_buffer_(0)
 {
@@ -168,7 +169,7 @@ InsetInclude::InsetInclude(InsetCommandParams const & p)
 
        if (isListings(params())) {
                InsetListingsParams listing_params(to_utf8(p["lstparams"]));
-               label_ = createLabel(from_utf8(listing_params.getParamValue("label")));
+               label_ = createLabel(buffer_, from_utf8(listing_params.getParamValue("label")));
        }
 }
 
@@ -221,7 +222,6 @@ bool InsetInclude::isCompatibleCommand(string const & s)
 
 void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
-       LASSERT(cur.buffer() == &buffer(), return);
        switch (cmd.action) {
 
        case LFUN_INSET_EDIT: {
@@ -253,7 +253,7 @@ void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
                                        if (label_) 
                                                old_label = label_->getParam("name");
                                        else {
-                                               label_ = createLabel(new_label);
+                                               label_ = createLabel(buffer_, new_label);
                                                label_->setBuffer(buffer());
                                        }                                       
 
@@ -302,10 +302,16 @@ bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
        switch (cmd.action) {
 
        case LFUN_INSET_EDIT:
-       case LFUN_INSET_MODIFY:
                flag.setEnabled(true);
                return true;
 
+       case LFUN_INSET_MODIFY:
+               if (cmd.getArg(0) == "changetype")
+                       return InsetCommand::getStatus(cur, cmd, flag);
+               else
+                       flag.setEnabled(true);
+               return true;
+
        default:
                return InsetCommand::getStatus(cur, cmd, flag);
        }
@@ -377,7 +383,7 @@ Buffer * InsetInclude::getChildBuffer() const
 Buffer * InsetInclude::loadIfNeeded() const
 {
        // Don't try to load it again if we failed before.
-       if (failedtoload_)
+       if (failedtoload_ || isVerbatim(params()) || isListings(params()))
                return 0;
 
        // Use cached Buffer if possible.
@@ -388,13 +394,9 @@ Buffer * InsetInclude::loadIfNeeded() const
                child_buffer_ = 0;
        }
 
-       InsetCommandParams const & p = params();
-       if (isVerbatim(p) || isListings(p))
-               return 0;
-
        string const parent_filename = buffer().absFileName();
        FileName const included_file = 
-               makeAbsPath(to_utf8(p["filename"]), onlyPath(parent_filename));
+               makeAbsPath(to_utf8(params()["filename"]), onlyPath(parent_filename));
 
        if (!isLyXFilename(included_file.absFilename()))
                return 0;
@@ -512,8 +514,8 @@ int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
                }
 
                // Make sure modules used in child are all included in master
-               //FIXME It might be worth loading the children's modules into the master
-               //over in BufferParams rather than doing this check.
+               // FIXME It might be worth loading the children's modules into the master
+               // over in BufferParams rather than doing this check.
                LayoutModuleList const masterModules = masterBuffer->params().getModules();
                LayoutModuleList const childModules = tmp->params().getModules();
                LayoutModuleList::const_iterator it = childModules.begin();
@@ -533,18 +535,22 @@ int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
 
                tmp->markDepClean(masterBuffer->temppath());
 
-// FIXME: handle non existing files
-// FIXME: Second argument is irrelevant!
-// since only_body is true, makeLaTeXFile will not look at second
-// argument. Should we set it to string(), or should makeLaTeXFile
-// make use of it somehow? (JMarc 20031002)
+               // FIXME: handle non existing files
+               // FIXME: Second argument is irrelevant!
+               // since only_body is true, makeLaTeXFile will not look at second
+               // argument. Should we set it to string(), or should makeLaTeXFile
+               // make use of it somehow? (JMarc 20031002)
                // The included file might be written in a different encoding
+               // and language.
                Encoding const * const oldEnc = runparams.encoding;
+               Language const * const oldLang = runparams.master_language;
                runparams.encoding = &tmp->params().encoding();
+               runparams.master_language = buffer().params().language;
                tmp->makeLaTeXFile(writefile,
                                   masterFileName(buffer()).onlyPath().absFilename(),
                                   runparams, false);
                runparams.encoding = oldEnc;
+               runparams.master_language = oldLang;
        } else {
                // In this case, it's not a LyX file, so we copy the file
                // to the temp dir, so that .aux files etc. are not created
@@ -567,12 +573,16 @@ int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
 
        string const tex_format = (runparams.flavor == OutputParams::LATEX) ?
                        "latex" : "pdflatex";
-       if (isVerbatim(params())) {
+       switch (type(params())) {
+       case VERB:
+       case VERBAST: {
                incfile = latex_path(incfile);
                // FIXME UNICODE
                os << '\\' << from_ascii(params().getCmdName()) << '{'
                   << from_utf8(incfile) << '}';
-       } else if (type(params()) == INPUT) {
+               break;
+       } 
+       case INPUT: {
                runparams.exportdata->addExternalFile(tex_format, writefile,
                                                      exportfile);
 
@@ -589,7 +599,9 @@ int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
                        os << '\\' << from_ascii(params().getCmdName())
                           << '{' << from_utf8(incfile) <<  '}';
                }
-       } else if (type(params()) == LISTINGS) {
+               break;
+       } 
+       case LISTINGS: {
                os << '\\' << from_ascii(params().getCmdName());
                string const opt = to_utf8(params()["lstparams"]);
                // opt is set in QInclude dialog and should have passed validation.
@@ -597,7 +609,9 @@ int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
                if (!params.params().empty())
                        os << "[" << from_utf8(params.params()) << "]";
                os << '{'  << from_utf8(incfile) << '}';
-       } else {
+               break;
+       } 
+       case INCLUDE: {
                runparams.exportdata->addExternalFile(tex_format, writefile,
                                                      exportfile);
 
@@ -608,12 +622,67 @@ int InsetInclude::latex(odocstream & os, OutputParams const & runparams) const
                // FIXME UNICODE
                os << '\\' << from_ascii(params().getCmdName()) << '{'
                   << from_utf8(incfile) << '}';
+               break;
+       }
+       case NONE:
+               break;
        }
 
        return 0;
 }
 
 
+docstring InsetInclude::xhtml(XHTMLStream & xs, OutputParams const &rp) const
+{
+       if (rp.inComment)
+                return docstring();
+
+       // For verbatim and listings, we just include the contents of the file as-is.
+       // In the case of listings, we wrap it in <pre>.
+       bool const listing = isListings(params());
+       if (listing || isVerbatim(params())) {
+               if (listing)
+                       xs << StartTag("pre");
+               // FIXME: We don't know the encoding of the file, default to UTF-8.
+               xs << includedFilename(buffer(), params()).fileContents("UTF-8");
+               if (listing)
+                       xs << EndTag("pre");
+               return docstring();
+       }
+
+       // We don't (yet) know how to Input or Include non-LyX files.
+       // (If we wanted to get really arcane, we could run some tex2html
+       // converter on the included file. But that's just masochistic.)
+       string const parent_filename = buffer().absFileName();
+       FileName const included_file = 
+               makeAbsPath(to_utf8(params()["filename"]), onlyPath(parent_filename));
+       if (!isLyXFilename(included_file.absFilename())) {
+               frontend::Alert::warning(_("Unsupported Inclusion"),
+                                        bformat(_("LyX does not know how to include non-LyX files when "
+                                                  "generating HTML output. Offending file:\n%1$s"),
+                                                   params()["filename"]));
+               return docstring();
+       }
+
+       // In the other cases, we will generate the HTML and include it.
+
+       // Check we're not trying to include ourselves.
+       // FIXME RECURSIVE INCLUDE
+       if (buffer().absFileName() == included_file.absFilename()) {
+               Alert::error(_("Recursive input"),
+                              bformat(_("Attempted to include file %1$s in itself! "
+                              "Ignoring inclusion."), params()["filename"]));
+               return docstring();
+       }
+
+       Buffer const * const ibuf = loadIfNeeded();
+       if (!ibuf)
+               return docstring();
+       ibuf->writeLyXHTMLSource(xs.os(), rp, true);
+       return docstring();
+}
+
+
 int InsetInclude::plaintext(odocstream & os, OutputParams const &) const
 {
        if (isVerbatim(params()) || isListings(params())) {
@@ -881,7 +950,8 @@ void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
 } // namespace anon
 
 
-void InsetInclude::addPreview(graphics::PreviewLoader & ploader) const
+void InsetInclude::addPreview(DocIterator const & /*inset_pos*/,
+       graphics::PreviewLoader & ploader) const
 {
        Buffer const & buffer = ploader.buffer();
        if (!preview_wanted(params(), buffer))
@@ -951,18 +1021,18 @@ void InsetInclude::updateCommand()
        setParams(p);   
 }
 
-void InsetInclude::updateLabels(ParIterator const & it)
+void InsetInclude::updateLabels(ParIterator const & it, bool out)
 {
        Buffer const * const childbuffer = getChildBuffer();
        if (childbuffer) {
-               childbuffer->updateLabels(Buffer::UpdateChildOnly);
+               childbuffer->updateLabels(Buffer::UpdateChildOnly, out);
                return;
        }
        if (!isListings(params()))
                return;
 
        if (label_)
-               label_->updateLabels(it);
+               label_->updateLabels(it, out);
 
        InsetListingsParams const par(to_utf8(params()["lstparams"]));
        if (par.getParamValue("caption").empty()) {