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();
// See Issue #271: A mechanism to clean-up invalid HTML cards.
string cleaner = "</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>";
const string cleaner = Utils::Html::getHtmlCleaner();
string prefix( "<div class=\"aard\"" );
if( isToLanguageRTL() )

View file

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

5
bgl.cc
View file

@ -848,10 +848,7 @@ void BglArticleRequest::run()
multimap< wstring, pair< string, string > >::const_iterator i;
string cleaner = "</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>";
string cleaner = Utils::Html::getHtmlCleaner();
for( i = mainArticles.begin(); i != mainArticles.end(); ++i )
{
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.
string cleaner = "</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>";
string cleaner = Utils::Html::getHtmlCleaner();
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();
// See Issue #271: A mechanism to clean-up invalid HTML cards.
string cleaner = "</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>";
string cleaner = Utils::Html::getHtmlCleaner();
string prefix( "<div class=\"slobdict\"" );
if( isToLanguageRTL() )

View file

@ -1483,10 +1483,7 @@ void StardictArticleRequest::run()
multimap< wstring, pair< string, string > >::const_iterator i;
string cleaner = "</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>";
string cleaner = Utils::Html::getHtmlCleaner();
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 );
}
}
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);
}
namespace Html {
// See Issue #271: A mechanism to clean-up invalid HTML cards.
std::string getHtmlCleaner();
}
}
#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.
articleString += "</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>";
articleString += QString::fromStdString(Utils::Html::getHtmlCleaner());
QByteArray articleBody = articleString.toUtf8();

View file

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

6
zim.cc
View file

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