]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetListings.cpp
Restore XHTML output for InsetListings.
[lyx.git] / src / insets / InsetListings.cpp
index 93b1931f533c710f70220a4e758d49c8afa2da2a..e20d3c76df477b23ce7415066d4d7dfde29d152c 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Bo Peng
- * \author Jürgen Spitzmüller
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "InsetCaption.h"
-#include "InsetList.h"
 #include "Language.h"
 #include "MetricsInfo.h"
 #include "output_latex.h"
+#include "output_xhtml.h"
 #include "TextClass.h"
 
 #include "support/debug.h"
 #include "support/docstream.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
+#include "support/lassert.h"
 
 #include "frontends/alert.h"
 #include "frontends/Application.h"
@@ -51,9 +52,11 @@ using boost::regex;
 char const lstinline_delimiters[] =
        "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
 
-InsetListings::InsetListings(Buffer const & buf, InsetListingsParams const & par)
-       : InsetCollapsable(buf, par.status())
-{}
+InsetListings::InsetListings(Buffer * buf, InsetListingsParams const & par)
+       : InsetCollapsable(buf)
+{
+       status_ = par.status();
+}
 
 
 InsetListings::~InsetListings()
@@ -68,15 +71,15 @@ Inset::DisplayType InsetListings::display() const
 }
 
 
-void InsetListings::updateLabels(ParIterator const & it)
+void InsetListings::updateLabels(ParIterator const & it, bool out)
 {
-       Counters & cnts = buffer().params().documentClass().counters();
+       Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
        string const saveflt = cnts.current_float();
 
        // Tell to captions what the current float is
        cnts.current_float("listing");
 
-       InsetCollapsable::updateLabels(it);
+       InsetCollapsable::updateLabels(it, out);
 
        //reset afterwards
        cnts.current_float(saveflt);
@@ -121,19 +124,13 @@ void InsetListings::read(Lexer & lex)
 }
 
 
-docstring InsetListings::editMessage() const
-{
-       return _("Opened Listing Inset");
-}
-
-
 int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
 {
        string param_string = params().params();
        // NOTE: I use {} to quote text, which is an experimental feature
        // of the listings package (see page 25 of the manual)
        int lines = 0;
-       bool isInline = params().isInline();
+       bool const isInline = params().isInline();
        // get the paragraphs. We can not output them directly to given odocstream
        // because we can not yet determine the delimiter character of \lstinline
        docstring code;
@@ -228,14 +225,13 @@ int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
                if (param_string.empty())
                        os << "\\lstinline" << *delimiter;
                else
-                       os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
+                       os << "\\lstinline[" << from_utf8(param_string) << "]" << *delimiter;
                 os << code
                    << *delimiter;
        } else {
                OutputParams rp = runparams;
                rp.moving_arg = true;
                docstring const caption = getCaption(rp);
-               runparams.encoding = rp.encoding;
                if (param_string.empty() && caption.empty())
                        os << "\n\\begin{lstlisting}\n";
                else {
@@ -272,6 +268,42 @@ int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
 }
 
 
+docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
+{
+       odocstringstream ods;
+       XHTMLStream out(ods);
+
+       bool const isInline = params().isInline();
+       if (isInline) 
+               out << CompTag("br");
+       else {
+               out << StartTag("div", "class='float float-listings'");
+               docstring caption = getCaptionHTML(rp);
+               if (!caption.empty())
+                       out << StartTag("div", "class='float-caption'") 
+                           << caption << EndTag("div");
+       }
+
+       out << StartTag("pre");
+       OutputParams newrp = rp;
+       newrp.html_disable_captions = true;
+       docstring def = InsetText::xhtml(out, newrp);
+       out << EndTag("pre");
+
+       if (isInline) {
+               out << CompTag("br");
+               // escaping will already have been done
+               os << XHTMLStream::NextRaw() << ods.str();
+       } else {
+               out << EndTag("div");
+               // In this case, this needs to be deferred, but we'll put it
+               // before anything the text itself deferred.
+               def = ods.str() + '\n' + def;
+       }
+       return def;
+}
+
+
 docstring InsetListings::contextMenu(BufferView const &, int, int) const
 {
        return from_ascii("context-listings");
@@ -286,9 +318,11 @@ void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
                InsetListings::string2params(to_utf8(cmd.argument()), params());
                break;
        }
+
        case LFUN_INSET_DIALOG_UPDATE:
                cur.bv().updateDialog("listings", params2string(params()));
                break;
+
        default:
                InsetCollapsable::doDispatch(cur, cmd);
                break;
@@ -313,19 +347,22 @@ bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-void InsetListings::setButtonLabel()
+docstring const InsetListings::buttonLabel(BufferView const & bv) const
 {
        // FIXME UNICODE
-       if (decoration() == InsetLayout::Classic)
-               setLabel(isOpen() ?  _("Listing") : getNewLabel(_("Listing")));
+       if (decoration() == InsetLayout::CLASSIC)
+               return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
        else
-               setLabel(getNewLabel(_("Listing")));
+               return getNewLabel(_("Listing"));
 }
 
 
 void InsetListings::validate(LaTeXFeatures & features) const
 {
        features.require("listings");
+       string param_string = params().params();
+       if (param_string.find("\\color") != string::npos)
+               features.require("color");
        InsetCollapsable::validate(features);
 }
 
@@ -343,37 +380,29 @@ docstring InsetListings::getCaption(OutputParams const & runparams) const
        if (paragraphs().empty())
                return docstring();
 
-       ParagraphList::const_iterator pit = paragraphs().begin();
-       for (; pit != paragraphs().end(); ++pit) {
-               InsetList::const_iterator it = pit->insetList().begin();
-               for (; it != pit->insetList().end(); ++it) {
-                       Inset & inset = *it->inset;
-                       if (inset.lyxCode() == CAPTION_CODE) {
-                               odocstringstream ods;
-                               InsetCaption * ins =
-                                       static_cast<InsetCaption *>(it->inset);
-                               ins->getOptArg(ods, runparams);
-                               ins->getArgument(ods, runparams);
-                               // the caption may contain \label{} but the listings
-                               // package prefer caption={}, label={}
-                               docstring cap = ods.str();
-                               if (!contains(to_utf8(cap), "\\label{"))
-                                       return cap;
-                               // convert from
-                               //     blah1\label{blah2} blah3
-                               // to
-                               //     blah1 blah3},label={blah2
-                               // to form options
-                               //     caption={blah1 blah3},label={blah2}
-                               //
-                               // NOTE that } is not allowed in blah2.
-                               regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
-                               string const new_cap("\\1\\3},label={\\2");
-                               return from_utf8(regex_replace(to_utf8(cap), reg, new_cap));
-                       }
-               }
-       }
-       return docstring();
+       InsetCaption const * ins = getCaptionInset();
+       if (ins == 0)
+               return docstring();
+
+       odocstringstream ods;
+       ins->getOptArg(ods, runparams);
+       ins->getArgument(ods, runparams);
+       // the caption may contain \label{} but the listings
+       // package prefer caption={}, label={}
+       docstring cap = ods.str();
+       if (!contains(to_utf8(cap), "\\label{"))
+               return cap;
+       // convert from
+       //     blah1\label{blah2} blah3
+       // to
+       //     blah1 blah3},label={blah2
+       // to form options
+       //     caption={blah1 blah3},label={blah2}
+       //
+       // NOTE that } is not allowed in blah2.
+       regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
+       string const new_cap("\\1\\3},label={\\2");
+       return from_utf8(regex_replace(to_utf8(cap), reg, new_cap));
 }