opt: beautify the layout of dictserver output (#2012)
Some checks failed
SonarCloud / Build and analyze (push) Has been cancelled

* opt: beautify the layout of dictserver output


---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
xiaoyifang 2024-12-09 09:57:21 +08:00 committed by GitHub
parent f9d44f97c5
commit a45a3092b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 64 additions and 14 deletions

View file

@ -39,6 +39,19 @@ inline QString rstrip( const QString & str )
return {}; return {};
} }
inline uint32_t leadingSpaceCount( const QString & str )
{
for ( int i = 0; i < str.size(); i++ ) {
if ( str.at( i ).isSpace() ) {
continue;
}
else {
return i;
}
}
return 0;
}
std::string c_string( const QString & str ); std::string c_string( const QString & str );
bool endsWithIgnoreCase( QByteArrayView str, QByteArrayView extension ); bool endsWithIgnoreCase( QByteArrayView str, QByteArrayView extension );
/** /**

View file

@ -592,12 +592,49 @@ public:
cancel(); cancel();
} ); } );
connect( this, &DictServerArticleRequest::finishedArticle, this, [ this ]( QString articleText ) { connect( this, &DictServerArticleRequest::finishedArticle, this, [ this ]( QString _articleText ) {
if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) { if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) {
cancel(); cancel();
return; return;
} }
//modify the _articleText,remove extra lines[start with 15X etc.]
QList< QString > lines = _articleText.split( "\n", Qt::SkipEmptyParts );
QString resultStr;
// process the line
static QRegularExpression leadingRespCode( "^\\d{3} " );
uint32_t leadingSpaceCount = 0;
uint32_t firstLeadingSpaceCount = 0;
for ( const QString & line : lines ) {
//ignore 15X lines
if ( leadingRespCode.match( line ).hasMatch() ) {
continue;
}
// ignore dot(.),the end line character
if ( line.trimmed() == "." ) {
break;
}
auto lsc = Utils::leadingSpaceCount( line );
if ( firstLeadingSpaceCount == 0 && lsc > firstLeadingSpaceCount ) {
firstLeadingSpaceCount = lsc;
}
if ( lsc >= leadingSpaceCount && lsc > firstLeadingSpaceCount ) {
//extra space
resultStr.append( " " );
resultStr.append( line.trimmed() );
}
else {
resultStr.append( "\n" );
resultStr.append( line );
}
leadingSpaceCount = lsc;
}
static QRegularExpression phonetic( R"(\\([^\\]+)\\)", static QRegularExpression phonetic( R"(\\([^\\]+)\\)",
QRegularExpression::CaseInsensitiveOption ); // phonetics: \stuff\ ... QRegularExpression::CaseInsensitiveOption ); // phonetics: \stuff\ ...
static QRegularExpression divs_inside_phonetic( "</div([^>]*)><div([^>]*)>", static QRegularExpression divs_inside_phonetic( "</div([^>]*)><div([^>]*)>",
@ -610,26 +647,26 @@ public:
string articleStr; string articleStr;
if ( contentInHtml ) { if ( contentInHtml ) {
articleStr = articleText.toUtf8().data(); articleStr = resultStr.toUtf8().data();
} }
else { else {
articleStr = Html::preformat( articleText.toUtf8().data() ); articleStr = Html::preformat( resultStr.toUtf8().data() );
} }
articleText = QString::fromUtf8( articleStr.c_str(), articleStr.size() ); _articleText = QString::fromUtf8( articleStr.c_str(), articleStr.size() );
int pos; int pos;
if ( !contentInHtml ) { if ( !contentInHtml ) {
articleText = articleText.replace( refs, R"(<a href="gdlookup://localhost/\1">\1</a>)" ); _articleText = _articleText.replace( refs, R"(<a href="gdlookup://localhost/\1">\1</a>)" );
pos = 0; pos = 0;
QString articleNewText; QString articleNewText;
// Handle phonetics // Handle phonetics
QRegularExpressionMatchIterator it = phonetic.globalMatch( articleText ); QRegularExpressionMatchIterator it = phonetic.globalMatch( _articleText );
while ( it.hasNext() ) { while ( it.hasNext() ) {
QRegularExpressionMatch match = it.next(); QRegularExpressionMatch match = it.next();
articleNewText += articleText.mid( pos, match.capturedStart() - pos ); articleNewText += _articleText.mid( pos, match.capturedStart() - pos );
pos = match.capturedEnd(); pos = match.capturedEnd();
QString phonetic_text = match.captured( 1 ); QString phonetic_text = match.captured( 1 );
@ -638,18 +675,18 @@ public:
articleNewText += R"(<span class="dictd_phonetic">)" + phonetic_text + "</span>"; articleNewText += R"(<span class="dictd_phonetic">)" + phonetic_text + "</span>";
} }
if ( pos ) { if ( pos ) {
articleNewText += articleText.mid( pos ); articleNewText += _articleText.mid( pos );
articleText = articleNewText; _articleText = articleNewText;
articleNewText.clear(); articleNewText.clear();
} }
// Handle links // Handle links
pos = 0; pos = 0;
it = links.globalMatch( articleText ); it = links.globalMatch( _articleText );
while ( it.hasNext() ) { while ( it.hasNext() ) {
QRegularExpressionMatch match = it.next(); QRegularExpressionMatch match = it.next();
articleNewText += articleText.mid( pos, match.capturedStart() - pos ); articleNewText += _articleText.mid( pos, match.capturedStart() - pos );
pos = match.capturedEnd(); pos = match.capturedEnd();
QString link = match.captured( 1 ); QString link = match.captured( 1 );
@ -663,13 +700,13 @@ public:
articleNewText += newLink; articleNewText += newLink;
} }
if ( pos ) { if ( pos ) {
articleNewText += articleText.mid( pos ); articleNewText += _articleText.mid( pos );
articleText = articleNewText; _articleText = articleNewText;
articleNewText.clear(); articleNewText.clear();
} }
} }
articleData += string( "<div class=\"dictd_article\">" ) + articleText.toUtf8().data() + "<br></div>"; articleData += string( "<div class=\"dictd_article\">" ) + _articleText.toUtf8().data() + "<br></div>";
if ( !articleData.empty() ) { if ( !articleData.empty() ) {