From ab6b1f62ef7852e2c86384a1a7d010a16147ba76 Mon Sep 17 00:00:00 2001 From: Abs62 Date: Sun, 30 Apr 2017 15:33:45 +0300 Subject: [PATCH] Xdxf: Fix empty tags conversion --- xdxf2html.cc | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/xdxf2html.cc b/xdxf2html.cc index 4b486a19..fcc17528 100644 --- a/xdxf2html.cc +++ b/xdxf2html.cc @@ -107,6 +107,10 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const // We build a dom representation of the given xml, then do some transforms QDomDocument dd; + // Empty element which will be removed after + // We will insert it to other empty elements to avoid output ones in form + QDomElement fakeElement = dd.createElement( "b" ); + QString errorStr; int errorLine, errorColumn; @@ -136,6 +140,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); if( isLogicalFormat ) el.setAttribute( "class", "xdxf_ex" ); @@ -149,6 +156,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); el.setAttribute( "class", "xdxf_ex_markd" ); } @@ -162,6 +172,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + if( type == STARDICT ) { el.setTagName( "span" ); @@ -279,6 +292,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); el.setAttribute( "class", "xdxf_opt" ); } @@ -289,6 +305,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "a" ); el.setAttribute( "href", QString( "bword:" ) + el.text() ); el.setAttribute( "class", "xdxf_kref" ); @@ -310,6 +329,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + QString ref = el.attribute( "href" ); if( ref.isEmpty() ) ref = el.text(); @@ -328,6 +350,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); el.setAttribute( "class", "xdxf_abbr" ); if( type == XDXF && pAbrv != NULL ) @@ -379,6 +404,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); el.setAttribute( "class", "xdxf_dtrn" ); } @@ -389,6 +417,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); if ( el.hasAttribute( "c" ) ) @@ -406,6 +437,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); if( isLogicalFormat ) el.setAttribute( "class", "xdxf_co" ); @@ -419,6 +453,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); if( isLogicalFormat ) el.setAttribute( "class", "xdxf_gr" ); @@ -430,6 +467,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); if( isLogicalFormat ) el.setAttribute( "class", "xdxf_gr" ); @@ -441,6 +481,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); if( isLogicalFormat ) el.setAttribute( "class", "xdxf_gr" ); @@ -455,6 +498,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + el.setTagName( "span" ); if( isLogicalFormat ) el.setAttribute( "class", "xdxf_tr" ); @@ -472,6 +518,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( i ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + if ( el.hasAttribute( "src" ) ) { fixLink( el, dictPtr->getId(), "src" ); @@ -494,6 +543,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const { QDomElement el = nodes.at( 0 ).toElement(); + if( el.text().isEmpty() && el.childNodes().isEmpty() ) + el.appendChild( fakeElement ); + // if( type == XDXF && dictPtr != NULL && !el.hasAttribute( "start" ) ) if( dictPtr != NULL && !el.hasAttribute( "start" ) ) { @@ -588,9 +640,9 @@ string convert( string const & in, DICT_TYPE type, map < string, string > const el.setAttribute( "class", "xdxf_rref" ); } -// DPRINTF( "Result>>>>>>>>>>: %s\n\n\n", dd.toByteArray().data() ); +// GD_DPRINTF( "Result>>>>>>>>>>: %s\n\n\n", dd.toByteArray( 0 ).data() ); - return dd.toString().remove('\n').remove( QRegExp( "<(b|i)/>" ) ).toUtf8().data(); + return dd.toString( 0 ).remove('\n').remove( QRegExp( "<(b|i)/>" ) ).toUtf8().data(); } }