[Libreoffice-commits] dev-tools.git: prettyprint/formatxml.cpp

Miklos Vajna vmiklos at collabora.co.uk
Wed Feb 11 06:07:08 PST 2015


 prettyprint/formatxml.cpp |   36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)

New commits:
commit df83cccc7fc15cfc4b669dc805e1a786a38282ac
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date:   Wed Feb 11 11:34:35 2015 +0100

    formatxml: avoid corrupting ODF paragraphs / headings
    
    The problem was that
    
    <text:p>a<text:span>b</text:span>c</text:p>
    
    was formatted as
    
          <text:p text:style-name="P1">He
            <text:span text:style-name="T1">ll</text:span>
            o</text:p>
    
    So the resulting imported paragraph string changed from "Hello" to "He ll o".
    
    If there is situation where formatting children of text:p / text:h is wanted,
    it would be possible to add a cmdline option to disable this behavior.
    
    Also, for now this assumes that all children of these elements should
    not be formatted to be on the safe side.
    
    Change-Id: Ifcb846eef594d53206fa870b5eb31afabba7b5ae

diff --git a/prettyprint/formatxml.cpp b/prettyprint/formatxml.cpp
index 938cc32..4bf2546 100644
--- a/prettyprint/formatxml.cpp
+++ b/prettyprint/formatxml.cpp
@@ -153,16 +153,29 @@ static TokenType analyzeToken( const QString& token )
     return Text;
     }
 
+static int noFormat = 0;
+
+static bool shouldNoFormat(const QString& rString)
+{
+    // Formatting children of a few elements would change the meaning of the
+    // document, don't do that.
+    return rString == "text:p" || rString == "text:h";
+}
+
 static QString indent( int size )
     {
-    return QString().fill( ' ', size );
+    if (noFormat)
+        return QString();
+    else
+        return QString().fill( ' ', size );
     }
 
 static void ensureNewLine( QTextStream& out, bool* needNewLine )
     {
     if( *needNewLine )
         {
-        out << endl;
+        if (!noFormat)
+            out << endl;
         *needNewLine = false;
         }
     }
@@ -183,11 +196,16 @@ static bool format( QTextStream& in, QTextStream& out )
         switch( analyzeToken( token ))
             {
             case OpeningTag:
+                {
                 ensureNewLine( out, &needNewLine );
                 out << INDENT << token;
+                QString tag = tagName(token);
+                if (shouldNoFormat(tag))
+                    ++noFormat;
                 needNewLine = true;
-                stack.push( tagName( token ));
+                stack.push(tag);
                 break;
+                }
             case ClosingTag:
                 {
                 QString tag = tagName( token );
@@ -207,13 +225,19 @@ static bool format( QTextStream& in, QTextStream& out )
                     }
                 if( !needNewLine ) // not line continuation
                     out << INDENT;
-                out << token << endl;
+                if (shouldNoFormat(tag))
+                    --noFormat;
+                out << token;
+                if (!noFormat)
+                    out << endl;
                 needNewLine = false;
                 break;
                 }
             case StandaloneTag:
                 ensureNewLine( out, &needNewLine );
-                out << INDENT << token << endl;
+                out << INDENT << token;
+                if (!noFormat)
+                    out << endl;
                 break;
             case OtherTag:
                 ensureNewLine( out, &needNewLine );
@@ -261,3 +285,5 @@ int main( int argc, char* argv[] )
     out.setCodec( "UTF-8" );
     return format( in, out ) ? 0 : 1;
     }
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list