clean: replace the string with rawstring

replace some html make section with rawstring to achieve a more clear readability
This commit is contained in:
Xiao YiFang 2023-04-15 15:39:49 +08:00 committed by xiaoyifang
parent 9fe2e136da
commit a840a99e5c
11 changed files with 84 additions and 89 deletions

View file

@ -522,11 +522,7 @@ void AardDictionary::loadArticle( quint32 address,
articleText = QObject::tr( "Article decoding error" ).toStdString(); articleText = QObject::tr( "Article decoding error" ).toStdString();
// See Issue #271: A mechanism to clean-up invalid HTML cards. // See Issue #271: A mechanism to clean-up invalid HTML cards.
string cleaner = "</font>""</font>""</font>""</font>""</font>""</font>" const string cleaner = Utils::Html::getHtmlCleaner();
"</font>""</font>""</font>""</font>""</font>""</font>"
"</b></b></b></b></b></b></b></b>"
"</i></i></i></i></i></i></i></i>"
"</a></a></a></a></a></a></a></a>";
string prefix( "<div class=\"aard\"" ); string prefix( "<div class=\"aard\"" );
if( isToLanguageRTL() ) if( isToLanguageRTL() )

View file

@ -64,14 +64,16 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word,
// document ready ,init webchannel // document ready ,init webchannel
{ {
result += "<script>" result += R"(
" $_$(document).ready( function ($){ " <script>
" console.log(\"webchannel ready...\"); " $_$(document).ready( function ($){
" new QWebChannel(qt.webChannelTransport, function(channel) { " console.log("webchannel ready...");
" window.articleview = channel.objects.articleview; " new QWebChannel(qt.webChannelTransport, function(channel) {
" }); " window.articleview = channel.objects.articleview;
" }); " });
"</script>"; });
</script>
)";
} }
// Add a css stylesheet // Add a css stylesheet
@ -99,10 +101,13 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word,
// Turn on/off expanding of article optional parts // Turn on/off expanding of article optional parts
if( expandOptionalParts ) if( expandOptionalParts )
{ {
result += "<!-- Expand optional parts css -->\n"; result += R"(<!-- Expand optional parts css -->
result += "<style type=\"text/css\" media=\"all\">\n"; <style type="text/css" media="all">
result += "\n.dsl_opt\n{\n display: inline;\n}\n\n.hidden_expand_opt\n{\n display: none;\n}\n"; .dsl_opt{
result += "</style>\n"; display: inline;
}
.hidden_expand_opt{ display: none;}
</style>)";
} }
} }
@ -128,17 +133,18 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word,
if ( icon.size() ) if ( icon.size() )
result += R"(<link rel="icon" type="image/png" href="qrc:///flags/)" + Html::escape( icon.toUtf8().data() ) + "\" />\n"; result += R"(<link rel="icon" type="image/png" href="qrc:///flags/)" + Html::escape( icon.toUtf8().data() ) + "\" />\n";
result += "<script type=\"text/javascript\">" result += QString::fromUtf8( R"(
"function tr(key) {" <script type="text/javascript">
" var tr_map = {" function tr(key) {
"\"Expand article\":\""; var tr_map = {
result += tr("Expand article").toUtf8().data(); "Expand article": "%1", "Collapse article": "%2"
result += R"(","Collapse article":")"; };
result += tr("Collapse article").toUtf8().data(); return tr_map[key] || '';
result += "\" };" }
"return tr_map[key] || '';" </script>
"}" )" ).arg( tr( "Expand article" ), tr( "Collapse article" ) )
"</script>"; .toStdString();
result+= R"(<script type="text/javascript" src="qrc:///scripts/gd-builtin.js"></script>)"; result+= R"(<script type="text/javascript" src="qrc:///scripts/gd-builtin.js"></script>)";
if( GlobalBroadcaster::instance()->getPreference()->darkReaderMode ) if( GlobalBroadcaster::instance()->getPreference()->darkReaderMode )
@ -643,31 +649,37 @@ void ArticleRequest::bodyFinished()
string jsVal = Html::escapeForJavaScript( dictId ); string jsVal = Html::escapeForJavaScript( dictId );
head += string( "<div class=\"gdarticle" ) + head += QString::fromUtf8(
( closePrevSpan ? "" : " gdactivearticle" ) + R"( <div class="gdarticle %1 %2" id="%3"
( collapse ? " gdcollapsedarticle" : "" ) + onClick="gdMakeArticleActive( '%4' );"
"\" id=\"" + gdFrom + onContextMenu="gdMakeArticleActive( '%4' );">)" )
"\" onClick=\"gdMakeArticleActive( '" + jsVal + "' );\" " + .arg( closePrevSpan ? "" : " gdactivearticle" ,
" onContextMenu=\"gdMakeArticleActive( '" + jsVal + "' );\"" collapse ? " gdcollapsedarticle" : "" ,
+ ">"; gdFrom.c_str() ,
jsVal.c_str() )
.toStdString();
closePrevSpan = true; closePrevSpan = true;
head += string( R"(<div class="gddictname" onclick="gdExpandArticle(')" ) + dictId + "\');" head += QString::fromUtf8(
+ ( collapse ? "\" style=\"cursor:pointer;" : "" ) R"(<div class="gddictname" onclick="gdExpandArticle('%1');" %2 id="gddictname-%1" title="%3">
+ "\" id=\"gddictname-" + Html::escape( dictId ) + "\"" <span class="gddicticon"><img src="gico://%1/dicticon.png"></span>
+ ( collapse ? string( " title=\"" ) + tr( "Expand article" ).toUtf8().data() + "\"" : "" ) <span class="gdfromprefix">%4</span>
+ R"(><span class="gddicticon"><img src="gico://)" + Html::escape( dictId ) <span class="gddicttitle">%5</span>
+ R"(/dicticon.png"></span><span class="gdfromprefix">)" + <span class="collapse_expand_area"><img src="qrc:///icons/blank.png" class="%6" id="expandicon-%1" title="%7" ></span>
Html::escape( tr( "From " ).toUtf8().data() ) + "</span><span class=\"gddicttitle\">" + </div>)" )
Html::escape( activeDict->getName().c_str() ) + "</span>" .arg( dictId.c_str(),
+ R"(<span class="collapse_expand_area"><img src="qrc:///icons/blank.png" class=")" collapse ? R"(style="cursor:pointer;")" : "",
+ ( collapse ? "gdexpandicon" : "gdcollapseicon" ) collapse ? tr( "Expand article" ) : QString(),
+ "\" id=\"expandicon-" + Html::escape( dictId ) + "\"" Html::escape( tr( "From " ).toStdString() ).c_str(),
+ ( collapse ? "" : string( " title=\"" ) + tr( "Collapse article" ).toUtf8().data() + "\"" ) Html::escape( activeDict->getName() ).c_str(),
+ "></span>" + "</div>"; collapse ? "gdexpandicon" : "gdcollapseicon",
collapse ? "" : tr( "Collapse article" )
head += "<div class=\"gddictnamebodyseparator\"></div>"; )
.toStdString();
head += R"(<div class="gddictnamebodyseparator"></div>)";
// If the user has enabled Anki integration in settings, // If the user has enabled Anki integration in settings,
// Show a (+) button that lets the user add a new Anki card. // Show a (+) button that lets the user add a new Anki card.
@ -680,14 +692,13 @@ void ArticleRequest::bodyFinished()
head += link.arg( Html::escape( dictId ).c_str(), tr( "Make a new Anki note" ) ).toStdString(); head += link.arg( Html::escape( dictId ).c_str(), tr( "Make a new Anki note" ) ).toStdString();
} }
head += "<div class=\"gdarticlebody gdlangfrom-"; head += QString::fromUtf8(
head += LangCoder::intToCode2( activeDict->getLangFrom() ).toLatin1().data(); R"(<div class="gdarticlebody gdlangfrom-%1" lang="%2" style="display:%3" id="gdarticlefrom-%4">)" )
head += "\" lang=\""; .arg( LangCoder::intToCode2( activeDict->getLangFrom() ),
head += LangCoder::intToCode2( activeDict->getLangTo() ).toLatin1().data(); LangCoder::intToCode2( activeDict->getLangTo() ),
head += "\""; collapse ? "none" : "inline",
head += " style=\"display:"; dictId.c_str() )
head += collapse ? "none" : "inline"; .toStdString();
head += string( "\" id=\"gdarticlefrom-" ) + Html::escape( dictId ) + "\">";
if( errorString.size() ) { if( errorString.size() ) {
head += "<div class=\"gderrordesc\">" head += "<div class=\"gderrordesc\">"

5
bgl.cc
View file

@ -848,10 +848,7 @@ void BglArticleRequest::run()
multimap< wstring, pair< string, string > >::const_iterator i; multimap< wstring, pair< string, string > >::const_iterator i;
string cleaner = "</font>""</font>""</font>""</font>""</font>""</font>" string cleaner = Utils::Html::getHtmlCleaner();
"</font>""</font>""</font>""</font>""</font>""</font>"
"</b></b></b></b></b></b></b></b>"
"</i></i></i></i></i></i></i></i>";
for( i = mainArticles.begin(); i != mainArticles.end(); ++i ) for( i = mainArticles.begin(); i != mainArticles.end(); ++i )
{ {
if (dict.isFromLanguageRTL() ) // RTL support if (dict.isFromLanguageRTL() ) // RTL support

6
mdx.cc
View file

@ -664,11 +664,7 @@ void MdxArticleRequest::run()
} }
// See Issue #271: A mechanism to clean-up invalid HTML cards. // See Issue #271: A mechanism to clean-up invalid HTML cards.
string cleaner = "</font>""</font>""</font>""</font>""</font>""</font>" string cleaner = Utils::Html::getHtmlCleaner();
"</font>""</font>""</font>""</font>""</font>""</font>"
"</b></b></b></b></b></b></b></b>"
"</i></i></i></i></i></i></i></i>"
"</a></a></a></a></a></a></a></a>";
articleText += "<div class=\"mdict\">" + articleBody + cleaner + "</div>\n"; articleText += "<div class=\"mdict\">" + articleBody + cleaner + "</div>\n";
} }

View file

@ -801,11 +801,7 @@ void SlobDictionary::loadArticle( quint32 address,
articleText = QObject::tr( "Article decoding error" ).toStdString(); articleText = QObject::tr( "Article decoding error" ).toStdString();
// See Issue #271: A mechanism to clean-up invalid HTML cards. // See Issue #271: A mechanism to clean-up invalid HTML cards.
string cleaner = "</font>""</font>""</font>""</font>""</font>""</font>" string cleaner = Utils::Html::getHtmlCleaner();
"</font>""</font>""</font>""</font>""</font>""</font>"
"</b></b></b></b></b></b></b></b>"
"</i></i></i></i></i></i></i></i>"
"</a></a></a></a></a></a></a></a>";
string prefix( "<div class=\"slobdict\"" ); string prefix( "<div class=\"slobdict\"" );
if( isToLanguageRTL() ) if( isToLanguageRTL() )

View file

@ -1483,10 +1483,7 @@ void StardictArticleRequest::run()
multimap< wstring, pair< string, string > >::const_iterator i; multimap< wstring, pair< string, string > >::const_iterator i;
string cleaner = "</font>""</font>""</font>""</font>""</font>""</font>" string cleaner = Utils::Html::getHtmlCleaner();
"</font>""</font>""</font>""</font>""</font>""</font>"
"</b></b></b></b></b></b></b></b>"
"</i></i></i></i></i></i></i></i>";
for( i = mainArticles.begin(); i != mainArticles.end(); ++i ) for( i = mainArticles.begin(); i != mainArticles.end(); ++i )
{ {

View file

@ -32,3 +32,11 @@ void Utils::Widget::setNoResultColor(QWidget * widget, bool noResult)
widget->setPalette( pal ); widget->setPalette( pal );
} }
} }
std::string Utils::Html::getHtmlCleaner()
{
return R"(</font></font></font></font></font></font></font></font></font></font></font></font>
</b></b></b></b></b></b></b></b>
</i></i></i></i></i></i></i></i>
</a></a></a></a></a></a></a></a>)";
}

View file

@ -319,6 +319,11 @@ namespace Widget{
void setNoResultColor(QWidget * widget, bool noResult); void setNoResultColor(QWidget * widget, bool noResult);
} }
namespace Html {
// See Issue #271: A mechanism to clean-up invalid HTML cards.
std::string getHtmlCleaner();
}
} }
#endif // UTILS_HH #endif // UTILS_HH

View file

@ -275,11 +275,7 @@ void WebSiteArticleRequest::requestFinished( QNetworkReply * r )
} }
// See Issue #271: A mechanism to clean-up invalid HTML cards. // See Issue #271: A mechanism to clean-up invalid HTML cards.
articleString += "</font>""</font>""</font>""</font>""</font>""</font>" articleString += QString::fromStdString(Utils::Html::getHtmlCleaner());
"</font>""</font>""</font>""</font>""</font>""</font>"
"</b></b></b></b></b></b></b></b>"
"</i></i></i></i></i></i></i></i>"
"</a></a></a></a></a></a></a></a>";
QByteArray articleBody = articleString.toUtf8(); QByteArray articleBody = articleString.toUtf8();

View file

@ -596,10 +596,7 @@ void XdxfArticleRequest::run()
multimap< wstring, pair< string, string > >::const_iterator i; multimap< wstring, pair< string, string > >::const_iterator i;
string cleaner = "</font>""</font>""</font>""</font>""</font>""</font>" string cleaner = Utils::Html::getHtmlCleaner();
"</font>""</font>""</font>""</font>""</font>""</font>"
"</b></b></b></b></b></b></b></b>"
"</i></i></i></i></i></i></i></i>";
for( i = mainArticles.begin(); i != mainArticles.end(); ++i ) for( i = mainArticles.begin(); i != mainArticles.end(); ++i )
{ {

6
zim.cc
View file

@ -1382,11 +1382,7 @@ void ZimArticleRequest::run()
string result; string result;
// See Issue #271: A mechanism to clean-up invalid HTML cards. // See Issue #271: A mechanism to clean-up invalid HTML cards.
string cleaner = "</font>""</font>""</font>""</font>""</font>""</font>" string cleaner = Utils::Html::getHtmlCleaner();
"</font>""</font>""</font>""</font>""</font>""</font>"
"</b></b></b></b></b></b></b></b>"
"</i></i></i></i></i></i></i></i>"
"</a></a></a></a></a></a></a></a>";
multimap< wstring, pair< string, string > >::const_iterator i; multimap< wstring, pair< string, string > >::const_iterator i;