]> git.lyx.org Git - features.git/commitdiff
Reduce caption hardcoding
authorGeorg Baum <baum@lyx.org>
Sat, 27 Dec 2014 17:15:07 +0000 (18:15 +0100)
committerGeorg Baum <baum@lyx.org>
Sat, 27 Dec 2014 17:15:07 +0000 (18:15 +0100)
Now we can handle all types of captions defined in layout files or modules.
This does also mean that \captionabove and \captionbelow are only recognized
in KOMA-classes and not always anymore, but this is correct, since these
captions would not work in other document classes anyway.

src/tex2lyx/test/test-insets.lyx.lyx
src/tex2lyx/test/test-structure.lyx.lyx
src/tex2lyx/text.cpp

index 0cb836fe93506f91a3b837dbc99ebecdec5cf4cd..643f0cf3611c51a5591e253ef6b61105e8aa34cd 100644 (file)
@@ -1419,6 +1419,7 @@ kill
 \begin_inset Argument 1
 status collapsed
 
+
 \begin_layout Standard
 An optional table caption (used in the list of tables)
 \end_layout
@@ -1500,6 +1501,7 @@ Second
 \begin_inset Argument 1
 status collapsed
 
+
 \end_inset
 
 (continued)
index 107bb669e153c37cf57b852a0c2571b5acb72855..6fa73fee01d555cc8cafbc383c29adff466cddb4 100644 (file)
@@ -282,6 +282,7 @@ status open
 \begin_inset Argument 1
 status collapsed
 
+
 \begin_layout Standard
 s
 \noun on
index 721db0ce6bb9212a386ed3e5a03663d1854ac7a7..8f136647c5d2dc18b1a49b76ec9dfe83be58625b 100644 (file)
@@ -45,6 +45,14 @@ using namespace lyx::support;
 namespace lyx {
 
 
+namespace {
+
+void output_arguments(ostream &, Parser &, bool, Context &,
+                      Layout::LaTeXArgMap const &);
+
+}
+
+
 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
                Context const & context, InsetLayout const * layout)
 {
@@ -55,6 +63,8 @@ void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
                newcontext.layout = &context.textclass.plainLayout();
        else
                newcontext.font = context.font;
+       if (layout)
+               output_arguments(os, p, outer, newcontext, layout->latexargs());
        parse_text(p, os, flags, outer, newcontext);
        newcontext.check_end_layout(os);
 }
@@ -613,33 +623,13 @@ void skip_spaces_braces(Parser & p, bool keepws = false)
 }
 
 
-void output_command_layout(ostream & os, Parser & p, bool outer,
-                          Context & parent_context,
-                          Layout const * newlayout)
+void output_arguments(ostream & os, Parser & p, bool outer, Context & context,
+                      Layout::LaTeXArgMap const & latexargs)
 {
-       TeXFont const oldFont = parent_context.font;
-       // save the current font size
-       string const size = oldFont.size;
-       // reset the font size to default, because the font size switches
-       // don't affect section headings and the like
-       parent_context.font.size = Context::normalfont.size;
-       // we only need to write the font change if we have an open layout
-       if (!parent_context.atParagraphStart())
-               output_font_change(os, oldFont, parent_context.font);
-       parent_context.check_end_layout(os);
-       Context context(true, parent_context.textclass, newlayout,
-                       parent_context.layout, parent_context.font);
-       if (parent_context.deeper_paragraph) {
-               // We are beginning a nested environment after a
-               // deeper paragraph inside the outer list environment.
-               // Therefore we don't need to output a "begin deeper".
-               context.need_end_deeper = true;
-       }
-       context.check_deeper(os);
        context.check_layout(os);
        int i = 0;
-       Layout::LaTeXArgMap::const_iterator lait = context.layout->latexargs().begin();
-       Layout::LaTeXArgMap::const_iterator const laend = context.layout->latexargs().end();
+       Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
+       Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
        for (; lait != laend; ++lait) {
                ++i;
                eat_whitespace(p, os, context, false);
@@ -663,6 +653,33 @@ void output_command_layout(ostream & os, Parser & p, bool outer,
                }
                eat_whitespace(p, os, context, false);
        }
+}
+
+
+void output_command_layout(ostream & os, Parser & p, bool outer,
+                          Context & parent_context,
+                          Layout const * newlayout)
+{
+       TeXFont const oldFont = parent_context.font;
+       // save the current font size
+       string const size = oldFont.size;
+       // reset the font size to default, because the font size switches
+       // don't affect section headings and the like
+       parent_context.font.size = Context::normalfont.size;
+       // we only need to write the font change if we have an open layout
+       if (!parent_context.atParagraphStart())
+               output_font_change(os, oldFont, parent_context.font);
+       parent_context.check_end_layout(os);
+       Context context(true, parent_context.textclass, newlayout,
+                       parent_context.layout, parent_context.font);
+       if (parent_context.deeper_paragraph) {
+               // We are beginning a nested environment after a
+               // deeper paragraph inside the outer list environment.
+               // Therefore we don't need to output a "begin deeper".
+               context.need_end_deeper = true;
+       }
+       context.check_deeper(os);
+       output_arguments(os, p, outer, context, context.layout->latexargs());
        parse_text(p, os, FLAG_ITEM, outer, context);
        context.check_end_layout(os);
        if (parent_context.deeper_paragraph) {
@@ -2816,44 +2833,6 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                preamble.registerAutomaticallyLoadedPackage(*it);
                }
 
-               else if (t.cs() == "caption" || t.cs() == "captionabove" ||
-                       t.cs() == "captionbelow") {
-                       if (starred)
-                               p.get_token();
-                       p.skip_spaces();
-                       context.check_layout(os);
-                       p.skip_spaces();
-                       if (starred && t.cs() == "caption")
-                               begin_inset(os, "Caption LongTableNoNumber\n");
-                       else if (t.cs() == "caption")
-                               begin_inset(os, "Caption Standard\n");
-                       else if (t.cs() == "captionabove")
-                               begin_inset(os, "Caption Above\n");
-                       else if (t.cs() == "captionbelow")
-                               begin_inset(os, "Caption Below\n");
-                       Context newcontext(true, context.textclass, 0, 0, context.font);
-                       newcontext.check_layout(os);
-                       // FIXME InsetArgument is now properly implemented in InsetLayout
-                       //       (for captions, but also for others)
-                       if (p.next_token().cat() != catEscape &&
-                           p.next_token().character() == '[') {
-                               p.get_token(); // eat '['
-                               begin_inset(os, "Argument 1\n");
-                               os << "status collapsed\n";
-                               parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
-                               end_inset(os);
-                               eat_whitespace(p, os, context, false);
-                       }
-                       parse_text(p, os, FLAG_ITEM, outer, context);
-                       context.check_end_layout(os);
-                       // We don't need really a new paragraph, but
-                       // we must make sure that the next item gets a \begin_layout.
-                       context.new_paragraph(os);
-                       end_inset(os);
-                       p.skip_spaces();
-                       newcontext.check_end_layout(os);
-               }
-
                else if (t.cs() == "subfloat") {
                        // the syntax is \subfloat[caption]{content}
                        // if it is a table of figure depends on the surrounding float
@@ -4467,9 +4446,16 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                p.get_token();
                        p.skip_spaces();
                        context.check_layout(os);
-                       begin_inset(os, "Flex ");
-                       os << to_utf8(newinsetlayout->name()) << '\n'
-                          << "status collapsed\n";
+                       docstring const name = newinsetlayout->name();
+                       bool const caption = name.find(from_ascii("Caption:")) == 0;
+                       if (caption) {
+                               begin_inset(os, "Caption ");
+                               os << to_utf8(name.substr(8)) << '\n';
+                       } else {
+                               begin_inset(os, "Flex ");
+                               os << to_utf8(name) << '\n'
+                                  << "status collapsed\n";
+                       }
                        if (newinsetlayout->isPassThru()) {
                                // set catcodes to verbatim early, just in case.
                                p.setCatcodes(VERBATIM_CATCODES);
@@ -4484,6 +4470,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                output_ert(os, arg, newcontext);
                        } else
                                parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
+                       if (caption)
+                               p.skip_spaces();
                        end_inset(os);
                }