From 647a353e0854e97f07a3282bbd69315d27947140 Mon Sep 17 00:00:00 2001 From: YiFang Xiao Date: Sat, 7 Dec 2024 17:41:37 +0800 Subject: [PATCH] opt: beautify the layout of dictserver output --- src/common/utils.hh | 13 +++++++++++++ src/dict/dictserver.cc | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/common/utils.hh b/src/common/utils.hh index dfa7301e54..6cff5ac530 100644 --- a/src/common/utils.hh +++ b/src/common/utils.hh @@ -39,6 +39,19 @@ inline QString rstrip( const QString & str ) 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 ); bool endsWithIgnoreCase( QByteArrayView str, QByteArrayView extension ); /** diff --git a/src/dict/dictserver.cc b/src/dict/dictserver.cc index 2dff23b283..9794ee980c 100644 --- a/src/dict/dictserver.cc +++ b/src/dict/dictserver.cc @@ -598,6 +598,38 @@ class DictServerArticleRequest: public Dictionary::DataRequest return; } + //modify the articleText,remove extra lines[start with 15X etc.] + QList lines = articleText.split("\n", Qt::SkipEmptyParts); + + QStringList resultStr; + + // proccess the line + QRegularExpression re("^\\d{3} "); + uint32_t leadingSpaceCount = 0; + for ( const QString & line : lines ) { + //ignore 15X lines + if (re.match(line).hasMatch()) { + continue; + } + // ignore . endline + if(line.trimmed()=="."){ + break; + } + + auto lsc = Utils::leadingSpaceCount( line ); + + + if ( lsc >= leadingSpaceCount && lsc > 4 ) { + resultStr.append( line.trimmed() ); + } + else{ + resultStr.append( "\n" ); + resultStr.append( line ); + } + leadingSpaceCount = lsc; + + } + static QRegularExpression phonetic( R"(\\([^\\]+)\\)", QRegularExpression::CaseInsensitiveOption ); // phonetics: \stuff\ ... static QRegularExpression divs_inside_phonetic( "]*)>]*)>", @@ -610,10 +642,10 @@ class DictServerArticleRequest: public Dictionary::DataRequest string articleStr; if ( contentInHtml ) { - articleStr = articleText.toUtf8().data(); + articleStr = resultStr.toUtf8().data(); } else { - articleStr = Html::preformat( articleText.toUtf8().data() ); + articleStr = Html::preformat( resultStr.toUtf8().data() ); } articleText = QString::fromUtf8( articleStr.c_str(), articleStr.size() );