Skip to content

Commit

Permalink
opt: beautify the layout of dictserver output
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed Dec 7, 2024
1 parent 853700a commit 647a353
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/common/utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
/**
Expand Down
36 changes: 34 additions & 2 deletions src/dict/dictserver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,38 @@ class DictServerArticleRequest: public Dictionary::DataRequest
return;
}

//modify the articleText,remove extra lines[start with 15X etc.]
QList<QString> 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( "</div([^>]*)><div([^>]*)>",
Expand All @@ -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() );
Expand Down

0 comments on commit 647a353

Please sign in to comment.