]> git.lyx.org Git - features.git/commitdiff
tex2lyx: support for list preambles
authorJuergen Spitzmueller <spitz@lyx.org>
Sun, 31 Mar 2019 12:27:56 +0000 (14:27 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 18 Jun 2020 13:48:23 +0000 (15:48 +0200)
src/tex2lyx/Context.cpp
src/tex2lyx/Context.h
src/tex2lyx/Parser.cpp
src/tex2lyx/Parser.h
src/tex2lyx/TODO.txt
src/tex2lyx/text.cpp

index 283846e7cf4b7271ff3156643150c51be3b4e5a1..f214f770ee466a39c5803c2ad278f61caa6d8724 100644 (file)
@@ -81,7 +81,7 @@ Context::Context(bool need_layout_,
                 TeXFont const & font_)
        : need_layout(need_layout_),
          need_end_layout(false), need_end_deeper(false),
-         has_item(false), deeper_paragraph(false),
+         has_item(false), in_list_preamble(false), deeper_paragraph(false),
          new_layout_allowed(true), merging_hyphens_allowed(true),
          textclass(textclass_),
          layout(layout_), parent_layout(parent_layout_),
index 64d69e356106b64d2f9fe11cd0f1a31727375f04..2d5315b5dff7dc4a1af0540bfa101e8a0dca8211 100644 (file)
@@ -130,6 +130,8 @@ public:
        std::string par_extra_stuff;
        /// We may need to add something at the beginning of a list.
        std::string list_extra_stuff;
+       /// Stuff between list begin and first item
+       std::string list_preamble;
        /// A LaTeXParam to be ignored in parsing.
        std::string latexparam;
        /// If there has been an \\begin_deeper, we'll need a matching
@@ -139,6 +141,10 @@ public:
        /// for each paragraph, otherwise this has to be a deeper
        /// paragraph.
        bool has_item;
+       /// If we are in an itemize-like environment, this marks
+       /// the text before the first \item. Typically, list
+       /// parameters (such as lengths) are adjusted here.
+       bool in_list_preamble;
        /// we are handling a standard paragraph in an itemize-like
        /// environment
        bool deeper_paragraph;
index 5b12dddb9e4854a0683c67f4cfddee1e47f9ba93..37f7deedc191f781935017f66e2448ec431af4ce 100644 (file)
@@ -577,6 +577,26 @@ string Parser::getFullParentheseArg()
 }
 
 
+bool Parser::hasListPreamble(string const itemcmd)
+{
+       // remember current position
+       unsigned int oldpos = pos_;
+       // jump over arguments
+       if (hasOpt())
+               getOpt();
+       if (hasOpt("{"))
+               getArg('{', '}');
+       // and swallow spaces and comments
+       skip_spaces(true);
+       // we have a preamvle if the next thing that follows is not
+       // the \item command
+       bool res =  next_token().cs() != itemcmd;
+       // back to orig position
+       pos_ = oldpos;
+       return res;
+}
+
+
 string const Parser::ertEnvironment(string const & name)
 {
        if (!good())
index b15f95aa4160f9152ee4269de6e01311bbd82b6f..09125236748de7b2b283218b22c0c7cbc7371e55 100644 (file)
@@ -259,6 +259,8 @@ public:
         * empty string if there is no such argument.
         */
        std::string getFullParentheseArg();
+       /// Check if we have a list preamble
+       bool hasListPreamble(std::string const itemcmd);
        /*!
         * \returns the contents of the environment \p name.
         * <tt>\begin{name}</tt> must be parsed already, <tt>\end{name}</tt>
index d813c064fea8f7e167b88a57e3c7eecdfc1797b0..840aac91c808631863199bc5175ea5b2b765a179 100644 (file)
@@ -34,7 +34,6 @@ Format LaTeX feature                        LyX feature
 443    unicode-math.sty                     InsetMath*
 453    automatic stmaryrd loading           \use_package stmaryrd
 457    automatic stackrel loading           \use_package stackrel
-563    InsetArgument listpreamble:1         All content between \begin{env} and first \item of a list
 568    Soul package                         soul.module
        \caps{...}                           \begin_inset Flex Capitalize
        \hl{...}                             \begin_inset Flex Highlight
index 4dd5fa7589df2810f0f17f247f706daefd0db08f..51b5e931e29dafc528b83611ac51e7ba0e717132 100644 (file)
@@ -2300,6 +2300,9 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                        }
                        switch (context.layout->latextype) {
                        case  LATEX_LIST_ENVIRONMENT:
+                               context.in_list_preamble =
+                                       !context.layout->listpreamble().empty()
+                                       && p.hasListPreamble(context.layout->itemcommand());
                                context.add_par_extra_stuff("\\labelwidthstring "
                                                            + p.verbatim_item() + '\n');
                                p.skip_spaces();
@@ -2323,11 +2326,20 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                                output_arguments(os, p, outer, false, string(), context,
                                                 context.layout->latexargs());
                        else if (context.layout->latextype == LATEX_ITEM_ENVIRONMENT) {
+                               context.in_list_preamble =
+                                       !context.layout->listpreamble().empty()
+                                       && p.hasListPreamble(context.layout->itemcommand());
                                ostringstream oss;
                                output_arguments(oss, p, outer, false, string(), context,
                                                 context.layout->latexargs());
                                context.list_extra_stuff = oss.str();
                        }
+                       if (context.in_list_preamble) {
+                               // Collect the stuff between \begin and first \item
+                               context.list_preamble =
+                                       parse_text_snippet(p, FLAG_END, outer, context);
+                               context.in_list_preamble = false;
+                       }
                        parse_text(p, os, FLAG_END, outer, context);
                        if (context.layout->latextype == LATEX_ENVIRONMENT)
                                output_arguments(os, p, outer, false, "post", context,
@@ -2907,6 +2919,13 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                debugToken(cerr, t, flags);
 #endif
 
+               if (context.in_list_preamble
+                   && p.next_token().cs() == context.layout->itemcommand()) {
+                       // We are parsing a list preamble. End before first \item.
+                       flags |= FLAG_LEAVE;
+                       context.in_list_preamble = false;
+               }
+
                if (flags & FLAG_ITEM) {
                        if (t.cat() == catSpace)
                                continue;
@@ -3351,6 +3370,16 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        if (context.layout->labeltype != LABEL_MANUAL)
                                output_arguments(os, p, outer, false, "item", context,
                                                 context.layout->itemargs());
+                       if (!context.list_preamble.empty()) {
+                               // We have a list preamble. Output it here.
+                               begin_inset(os, "Argument listpreamble:1");
+                               os << "\nstatus collapsed\n\n"
+                                  << "\\begin_layout Plain Layout\n\n"
+                                  << rtrim(context.list_preamble)
+                                  << "\n\\end_layout";
+                               end_inset(os);
+                               context.list_preamble.clear();
+                       }
                        if (!context.list_extra_stuff.empty()) {
                                os << context.list_extra_stuff;
                                context.list_extra_stuff.clear();