commit 2811f76039c20b886ddab4979be924bf1267c977
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Wed Sep 9 11:47:47 2015 +0200

    tex2lyx: Add a newline at the end of comment if not at the end of a 
paragraph.
    
    When importing TeX code like
      %comment
      TEXT
    the LyX document currently becomes
      [ERT %comment] TEXT
    so that TEXT is now part of the comment.
    
    Now output_comment adds a trailing newline if the token after the
    comment is not a newline. Note that the newline that marks the end of
    the comment has already been parsed at this point.
    
    tex2lyx tests have been checked manually and updated.
    
    Fixes ticket #9551.

diff --git a/src/tex2lyx/test/CJK.lyx.lyx b/src/tex2lyx/test/CJK.lyx.lyx
index 802fd0b..49c8dd5 100644
--- a/src/tex2lyx/test/CJK.lyx.lyx
+++ b/src/tex2lyx/test/CJK.lyx.lyx
@@ -167,6 +167,10 @@ status collapsed
 % The following Bg5 encoded text cannot be processed as is by latex.
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
@@ -177,6 +181,10 @@ status collapsed
 % It needs to be preprocessed by bg5conv or bg5latex needs to be used.
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
@@ -213,6 +221,10 @@ status collapsed
 % The following SJIS encoded text cannot be processed as is by latex.
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
@@ -223,6 +235,10 @@ status collapsed
 % It needs to be preprocessed by sjisconv or sjislatex needs to be used.
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
diff --git a/src/tex2lyx/test/XeTeX-polyglossia.lyx.lyx 
b/src/tex2lyx/test/XeTeX-polyglossia.lyx.lyx
index 966cfc1..f5fe7ea 100644
--- a/src/tex2lyx/test/XeTeX-polyglossia.lyx.lyx
+++ b/src/tex2lyx/test/XeTeX-polyglossia.lyx.lyx
@@ -175,6 +175,10 @@ status collapsed
 %empty language paragraph
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
diff --git a/src/tex2lyx/test/box-color-size-space-align.lyx.lyx 
b/src/tex2lyx/test/box-color-size-space-align.lyx.lyx
index fe269d5..6492204 100644
--- a/src/tex2lyx/test/box-color-size-space-align.lyx.lyx
+++ b/src/tex2lyx/test/box-color-size-space-align.lyx.lyx
@@ -2400,6 +2400,10 @@ status collapsed
 %set back to justified
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
diff --git a/src/tex2lyx/test/test-insets-basic.lyx.lyx 
b/src/tex2lyx/test/test-insets-basic.lyx.lyx
index d1bc2a1..984660a 100644
--- a/src/tex2lyx/test/test-insets-basic.lyx.lyx
+++ b/src/tex2lyx/test/test-insets-basic.lyx.lyx
@@ -663,6 +663,10 @@ status collapsed
 bibliography since LaTeX throws an error.
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
@@ -675,6 +679,10 @@ status collapsed
 bibliographystyle{unsrt}
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
@@ -4348,6 +4356,10 @@ status collapsed
 % some comment
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
@@ -6753,6 +6765,10 @@ status collapsed
 % and another
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
diff --git a/src/tex2lyx/test/test-insets.lyx.lyx 
b/src/tex2lyx/test/test-insets.lyx.lyx
index 89ebe8a..d8bac4e 100644
--- a/src/tex2lyx/test/test-insets.lyx.lyx
+++ b/src/tex2lyx/test/test-insets.lyx.lyx
@@ -112,6 +112,10 @@ status collapsed
 %stupid stuff
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
@@ -639,6 +643,10 @@ status collapsed
 bibliography since LaTeX throws an error.
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
@@ -651,6 +659,10 @@ status collapsed
 bibliographystyle{unsrt}
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
@@ -4401,6 +4413,10 @@ status collapsed
 % some comment
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
@@ -7192,6 +7208,10 @@ status collapsed
 % and another
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
diff --git a/src/tex2lyx/test/test-structure.lyx.lyx 
b/src/tex2lyx/test/test-structure.lyx.lyx
index 9618213..2f9c095 100644
--- a/src/tex2lyx/test/test-structure.lyx.lyx
+++ b/src/tex2lyx/test/test-structure.lyx.lyx
@@ -124,6 +124,10 @@ status collapsed
 % this should be recognized as empty date:
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
diff --git a/src/tex2lyx/test/test.lyx.lyx b/src/tex2lyx/test/test.lyx.lyx
index 53e3ec4..68b2dc6 100644
--- a/src/tex2lyx/test/test.lyx.lyx
+++ b/src/tex2lyx/test/test.lyx.lyx
@@ -120,6 +120,10 @@ status collapsed
 %Midline comment
 \end_layout
 
+\begin_layout Plain Layout
+
+\end_layout
+
 \end_inset
 
 
diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp
index 0e593ce..ab4c75f 100644
--- a/src/tex2lyx/text.cpp
+++ b/src/tex2lyx/text.cpp
@@ -580,6 +580,16 @@ void output_ert_inset(ostream & os, string const & s, 
Context & context)
 }
 
 
+void output_comment(Parser & p, ostream & os, string const & s,
+                    Context & context)
+{
+       if (p.next_token().cat() == catNewline)
+               output_ert_inset(os, '%' + s, context);
+       else
+               output_ert_inset(os, '%' + s + '\n', context);
+}
+
+
 Layout const * findLayout(TextClass const & textclass, string const & name, 
bool command)
 {
        Layout const * layout = findLayoutWithoutModule(textclass, name, 
command);
@@ -1189,13 +1199,13 @@ void parse_box(Parser & p, ostream & os, unsigned 
outer_flags,
                // LyX puts a % after the end of the minipage
                if (p.next_token().cat() == catNewline && 
p.next_token().cs().size() > 1) {
                        // new paragraph
-                       //output_ert_inset(os, "%dummy", parent_context);
+                       //output_comment(p, os, "dummy", parent_context);
                        p.get_token();
                        p.skip_spaces();
                        parent_context.new_paragraph(os);
                }
                else if (p.next_token().cat() == catSpace || 
p.next_token().cat() == catNewline) {
-                       //output_ert_inset(os, "%dummy", parent_context);
+                       //output_comment(p, os, "dummy", parent_context);
                        p.get_token();
                        p.skip_spaces();
                        // We add a protected space if something real follows
@@ -1880,7 +1890,7 @@ void parse_comment(Parser & p, ostream & os, Token const 
& t, Context & context)
        LASSERT(t.cat() == catComment, return);
        if (!t.cs().empty()) {
                context.check_layout(os);
-               output_ert_inset(os, '%' + t.cs(), context);
+               output_comment(p, os, t.cs(), context);
                if (p.next_token().cat() == catNewline) {
                        // A newline after a comment line starts a new
                        // paragraph
@@ -2826,8 +2836,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, 
bool outer,
                        context.check_layout(os);
                        // FIXME: This is a hack to prevent paragraph
                        // deletion if it is empty. Handle this better!
-                       output_ert_inset(os,
-                               "%dummy comment inserted by tex2lyx to "
+                       output_comment(p, os,
+                               "dummy comment inserted by tex2lyx to "
                                "ensure that this paragraph is not empty",
                                context);
                        // Both measures above may generate an additional

Reply via email to