mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-27 15:24:05 +00:00
opt: extract GDWORD replace logic (#1889)
Some checks failed
SonarCloud / Build and analyze (push) Has been cancelled
Some checks failed
SonarCloud / Build and analyze (push) Has been cancelled
* opt: extract GDWORD replace logic --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
5ab89cac8c
commit
39aef4c4de
|
@ -8,6 +8,7 @@
|
||||||
#include <stub_msvc.h>
|
#include <stub_msvc.h>
|
||||||
#endif
|
#endif
|
||||||
#include <QBuffer>
|
#include <QBuffer>
|
||||||
|
#include <QTextCodec>
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
|
@ -119,3 +120,55 @@ void removeDirectory( string const & directory )
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Utils::Fs
|
} // namespace Utils::Fs
|
||||||
|
|
||||||
|
namespace Utils::WebSite {
|
||||||
|
QString urlReplaceWord( const QString url, QString inputWord )
|
||||||
|
{
|
||||||
|
//copy temp url
|
||||||
|
auto urlString = url;
|
||||||
|
|
||||||
|
urlString.replace( "%25GDWORD%25", inputWord.toUtf8().toPercentEncoding() );
|
||||||
|
|
||||||
|
QTextCodec * codec = QTextCodec::codecForName( "Windows-1251" );
|
||||||
|
if ( codec ) {
|
||||||
|
urlString.replace( "%25GD1251%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
||||||
|
}
|
||||||
|
|
||||||
|
codec = QTextCodec::codecForName( "Big-5" );
|
||||||
|
if ( codec ) {
|
||||||
|
urlString.replace( "%25GDBIG5%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
||||||
|
}
|
||||||
|
|
||||||
|
codec = QTextCodec::codecForName( "Big5-HKSCS" );
|
||||||
|
if ( codec ) {
|
||||||
|
urlString.replace( "%25GDBIG5HKSCS%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
||||||
|
}
|
||||||
|
|
||||||
|
codec = QTextCodec::codecForName( "Shift-JIS" );
|
||||||
|
if ( codec ) {
|
||||||
|
urlString.replace( "%25GDSHIFTJIS%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
||||||
|
}
|
||||||
|
|
||||||
|
codec = QTextCodec::codecForName( "GB18030" );
|
||||||
|
if ( codec ) {
|
||||||
|
urlString.replace( "%25GDGBK%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Handle all ISO-8859 encodings
|
||||||
|
for ( int x = 1; x <= 16; ++x ) {
|
||||||
|
codec = QTextCodec::codecForName( QString( "ISO 8859-%1" ).arg( x ).toLatin1() );
|
||||||
|
if ( codec ) {
|
||||||
|
urlString.replace( QString( "%25GDISO%1%25" ).arg( x ).toUtf8(),
|
||||||
|
codec->fromUnicode( inputWord ).toPercentEncoding() );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip encodings 11..12, they don't exist
|
||||||
|
if ( x == 10 ) {
|
||||||
|
x = 12;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return urlString;
|
||||||
|
}
|
||||||
|
} // namespace Utils::WebSite
|
||||||
|
|
|
@ -341,6 +341,10 @@ void removeDirectory( QString const & directory );
|
||||||
void removeDirectory( string const & directory );
|
void removeDirectory( string const & directory );
|
||||||
} // namespace Fs
|
} // namespace Fs
|
||||||
|
|
||||||
|
namespace WebSite {
|
||||||
|
QString urlReplaceWord( const QString url, QString word );
|
||||||
|
}
|
||||||
|
|
||||||
QString escapeAmps( QString const & str );
|
QString escapeAmps( QString const & str );
|
||||||
|
|
||||||
QString unescapeAmps( QString const & str );
|
QString unescapeAmps( QString const & str );
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include "gddebug.hh"
|
#include "gddebug.hh"
|
||||||
#include "globalbroadcaster.hh"
|
#include "globalbroadcaster.hh"
|
||||||
|
#include "fmt/compile.h"
|
||||||
|
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
|
@ -312,73 +313,23 @@ void WebSiteArticleRequest::requestFinished( QNetworkReply * r )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnect( netReply, 0, 0, 0 );
|
disconnect( netReply, nullptr, 0, 0 );
|
||||||
netReply->deleteLater();
|
netReply->deleteLater();
|
||||||
|
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
sptr< DataRequest >
|
sptr< DataRequest > WebSiteDictionary::getArticle( wstring const & str,
|
||||||
WebSiteDictionary::getArticle( wstring const & str, vector< wstring > const &, wstring const & context, bool )
|
vector< wstring > const & /*alts*/,
|
||||||
|
wstring const & context,
|
||||||
|
bool /*ignoreDiacritics*/ )
|
||||||
{
|
{
|
||||||
QByteArray urlString;
|
QString urlString = Utils::WebSite::urlReplaceWord( QString( urlTemplate ), QString::fromStdU32String( str ) );
|
||||||
|
|
||||||
// Context contains the right url to go to
|
|
||||||
if ( context.size() ) {
|
|
||||||
urlString = Utf8::encode( context ).c_str();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
urlString = urlTemplate;
|
|
||||||
|
|
||||||
QString inputWord = QString::fromStdU32String( str );
|
|
||||||
|
|
||||||
urlString.replace( "%25GDWORD%25", inputWord.toUtf8().toPercentEncoding() );
|
|
||||||
|
|
||||||
QTextCodec * codec = QTextCodec::codecForName( "Windows-1251" );
|
|
||||||
if ( codec ) {
|
|
||||||
urlString.replace( "%25GD1251%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
||||||
}
|
|
||||||
|
|
||||||
codec = QTextCodec::codecForName( "Big-5" );
|
|
||||||
if ( codec ) {
|
|
||||||
urlString.replace( "%25GDBIG5%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
||||||
}
|
|
||||||
|
|
||||||
codec = QTextCodec::codecForName( "Big5-HKSCS" );
|
|
||||||
if ( codec ) {
|
|
||||||
urlString.replace( "%25GDBIG5HKSCS%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
||||||
}
|
|
||||||
|
|
||||||
codec = QTextCodec::codecForName( "Shift-JIS" );
|
|
||||||
if ( codec ) {
|
|
||||||
urlString.replace( "%25GDSHIFTJIS%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
||||||
}
|
|
||||||
|
|
||||||
codec = QTextCodec::codecForName( "GB18030" );
|
|
||||||
if ( codec ) {
|
|
||||||
urlString.replace( "%25GDGBK%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Handle all ISO-8859 encodings
|
|
||||||
for ( int x = 1; x <= 16; ++x ) {
|
|
||||||
codec = QTextCodec::codecForName( QString( "ISO 8859-%1" ).arg( x ).toLatin1() );
|
|
||||||
if ( codec ) {
|
|
||||||
urlString.replace( QString( "%25GDISO%1%25" ).arg( x ).toUtf8(),
|
|
||||||
codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( x == 10 ) {
|
|
||||||
x = 12; // Skip encodings 11..12, they don't exist
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( inside_iframe ) {
|
if ( inside_iframe ) {
|
||||||
// Just insert link in <iframe> tag
|
// Just insert link in <iframe> tag
|
||||||
|
|
||||||
string result = "<div class=\"website_padding\"></div>";
|
string result = R"(<div class="website_padding"></div>)";
|
||||||
|
|
||||||
//heuristic add url to global whitelist.
|
//heuristic add url to global whitelist.
|
||||||
QUrl url( urlString );
|
QUrl url( urlString );
|
||||||
|
@ -392,14 +343,15 @@ WebSiteDictionary::getArticle( wstring const & str, vector< wstring > const &, w
|
||||||
encodeUrl = urlString;
|
encodeUrl = urlString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt::format_to( std::back_inserter( result ),
|
||||||
result += string( "<iframe id=\"gdexpandframe-" ) + getId() +
|
R"(<iframe id="gdexpandframe-{}" src="{}"
|
||||||
"\" src=\""+encodeUrl.toStdString() +
|
onmouseover="processIframeMouseOver('gdexpandframe-{}');"
|
||||||
"\" onmouseover=\"processIframeMouseOver('gdexpandframe-" + getId() + "');\" "
|
onmouseout="processIframeMouseOut();" scrolling="no"
|
||||||
"onmouseout=\"processIframeMouseOut();\" "
|
style="overflow:visible; width:100%; display:block; border:none;"
|
||||||
"scrolling=\"no\" "
|
sandbox="allow-same-origin allow-scripts allow-popups"></iframe>)",
|
||||||
"style=\"overflow:visible; width:100%; display:block; border:none;\" sandbox=\"allow-same-origin allow-scripts allow-popups\">"
|
getId(),
|
||||||
"</iframe>";
|
encodeUrl.toStdString(),
|
||||||
|
getId() );
|
||||||
|
|
||||||
auto dr = std::make_shared< DataRequestInstant >( true );
|
auto dr = std::make_shared< DataRequestInstant >( true );
|
||||||
dr->appendString( result );
|
dr->appendString( result );
|
||||||
|
|
Loading…
Reference in a new issue