Author: baum
Date: Wed Jan 12 23:03:15 2011
New Revision: 37191
URL: http://www.lyx.org/trac/changeset/37191

Log:
Exclude more conditional commands from preamble parsing:
LyX would output the parsed stuff unconditionally, so we must not translate
it in LyX document settings.

Modified:
   lyx-devel/trunk/src/tex2lyx/TODO.txt
   lyx-devel/trunk/src/tex2lyx/preamble.cpp

Modified: lyx-devel/trunk/src/tex2lyx/TODO.txt
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/TODO.txt        Wed Jan 12 22:25:45 2011        
(r37190)
+++ lyx-devel/trunk/src/tex2lyx/TODO.txt        Wed Jan 12 23:03:15 2011        
(r37191)
@@ -22,11 +22,8 @@
 231    sidewaysfigure/sidewaystable         InsetFloat
 232    bibtopic                             InsetBibTeX
 246    framed.sty                           InsetBox
-247    lmodern.sty, charter.sty, utopia.sty font settings (header)
-       ccfonts.sty, chancery.sty,
-       beraserif.sty, berasans.sty,
-       courier.sty, luximono.sty,
-       beramono.sty, mathptmx.sty
+247    utopia.sty, ccfonts.sty,             font settings (header)
+       chancery.sty, beraserif.sty
 248    booktabs.sty                         InsetTabular
 254    esint.sty                            \use_esint
 266    armenian                             \language, \lang

Modified: lyx-devel/trunk/src/tex2lyx/preamble.cpp
==============================================================================
--- lyx-devel/trunk/src/tex2lyx/preamble.cpp    Wed Jan 12 22:25:45 2011        
(r37190)
+++ lyx-devel/trunk/src/tex2lyx/preamble.cpp    Wed Jan 12 23:03:15 2011        
(r37191)
@@ -149,6 +149,15 @@
 "rightmargin", "bottommargin", "headheight", "headsep", "footskip",
 "columnsep", 0};
 
+/// commands that can start an \if...\else...\endif sequence
+const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket",
+"ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf",
+"ifsidecap", "ifupgreek", 0};
+
+/// conditional commands with three arguments like \...@ifundefined{}{}{}
+const char * const known_if_3arg_commands[] = {"@ifundefined", "IfFileExists",
+0};
+
 // default settings
 ostringstream h_preamble;
 string h_textclass               = "article";
@@ -604,6 +613,22 @@
 }
 
 
+void handle_if(Parser & p, bool in_lyx_preamble)
+{
+       while (p.good()) {
+               Token t = p.get_token();
+               if (t.cat() == catEscape &&
+                   is_known(t.cs(), known_if_commands))
+                       handle_if(p, in_lyx_preamble);
+               else {
+                       if (!in_lyx_preamble)
+                               h_preamble << t.asInput();
+                       if (t.cat() == catEscape && t.cs() == "fi")
+                               return;
+               }
+       }
+}
+
 
 void end_preamble(ostream & os, TextClass const & /*textclass*/)
 {
@@ -1068,14 +1093,27 @@
                        }
                }
 
-               else if (t.cs() == "@ifundefined") {
+               else if (is_known(t.cs(), known_if_3arg_commands)) {
                        // prevent misparsing of \usepackage if it is used
                        // as an argument (see e.g. our own output of
                        // \...@ifundefined above)
-                       h_preamble << t.asInput();
-                       h_preamble << '{' << p.verbatim_item() << '}';
-                       h_preamble << '{' << p.verbatim_item() << '}';
-                       h_preamble << '{' << p.verbatim_item() << '}';
+                       string const arg1 = p.verbatim_item();
+                       string const arg2 = p.verbatim_item();
+                       string const arg3 = p.verbatim_item();
+                       if (!in_lyx_preamble) {
+                               h_preamble << t.asInput()
+                                          << '{' << arg1 << '}'
+                                          << '{' << arg2 << '}'
+                                          << '{' << arg3 << '}';
+                       }
+               }
+
+               else if (is_known(t.cs(), known_if_commands)) {
+                       // must not parse anything in conditional code, since
+                       // LyX would output the parsed contents unconditionally
+                       if (!in_lyx_preamble)
+                               h_preamble << t.asInput();
+                       handle_if(p, in_lyx_preamble);
                }
 
                else if (!t.cs().empty() && !in_lyx_preamble)

Reply via email to