mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 20:14:05 +00:00
opt: optimize the process of audio link which start with double // (#1008)
* opt: optimize the process of audio link which start with double // * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
f642c9aa76
commit
dbb11e293b
|
@ -6,13 +6,17 @@
|
|||
|
||||
std::string addAudioLink( std::string const & url, std::string const & dictionaryId )
|
||||
{
|
||||
if ( url.empty() || url.length() < 2 )
|
||||
return {};
|
||||
GlobalBroadcaster::instance()->pronounce_engine.sendAudio(
|
||||
dictionaryId,
|
||||
QString::fromStdString( url.substr( 1, url.length() - 2 ) ) );
|
||||
return addAudioLink( QString::fromStdString( url ), dictionaryId );
|
||||
}
|
||||
|
||||
return std::string( "<script type=\"text/javascript\">" + makeAudioLinkScript( url, dictionaryId ) + "</script>" );
|
||||
std::string addAudioLink( QString const & url, std::string const & dictionaryId )
|
||||
{
|
||||
if ( url.isEmpty() || url.length() < 2 )
|
||||
return {};
|
||||
GlobalBroadcaster::instance()->pronounce_engine.sendAudio( dictionaryId, url.mid( 1, url.length() - 2 ) );
|
||||
|
||||
return std::string( "<script type=\"text/javascript\">" + makeAudioLinkScript( url.toStdString(), dictionaryId )
|
||||
+ "</script>" );
|
||||
}
|
||||
|
||||
std::string makeAudioLinkScript( std::string const & url, std::string const & dictionaryId )
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#define __AUDIOLINK_HH_INCLUDED__
|
||||
|
||||
|
||||
#include <QString>
|
||||
#include <string>
|
||||
|
||||
/// Adds a piece of javascript to save the given audiolink to a special
|
||||
|
@ -13,7 +14,7 @@
|
|||
/// The url should be escaped and surrounded by quotes.
|
||||
/// The dictionary id is used to make active dictionary feature work.
|
||||
std::string addAudioLink( std::string const & url, std::string const & dictionaryId );
|
||||
|
||||
std::string addAudioLink( QString const & url, std::string const & dictionaryId );
|
||||
std::string makeAudioLinkScript( std::string const & url, std::string const & dictionaryId );
|
||||
|
||||
#endif
|
||||
|
|
|
@ -558,7 +558,12 @@ void MediaWikiArticleRequest::requestFinished( QNetworkReply * r )
|
|||
QRegularExpressionMatch match2 = reg2.match( tag );
|
||||
if ( match2.hasMatch() ) {
|
||||
QString ref = match2.captured( 1 );
|
||||
QString audio_url = "<a href=\"" + ref
|
||||
// audio url may like this <a href="//upload.wikimedia.org/wikipedia/a.ogg"
|
||||
if ( ref.startsWith( "//" ) ) {
|
||||
ref = wikiUrl.scheme() + ":" + ref;
|
||||
}
|
||||
auto script = addAudioLink( "\"" + ref + "\"", this->dictPtr->getId() );
|
||||
QString audio_url = QString::fromStdString( script ) + "<a href=\"" + ref
|
||||
+ R"("><img src="qrc:///icons/playsound.png" border="0" align="absmiddle" alt="Play"/></a>)";
|
||||
articleNewString += audio_url;
|
||||
}
|
||||
|
@ -571,14 +576,6 @@ void MediaWikiArticleRequest::requestFinished( QNetworkReply * r )
|
|||
articleNewString.clear();
|
||||
}
|
||||
|
||||
// audio url like this <a href="//upload.wikimedia.org/wikipedia/a.ogg"
|
||||
articleString.replace(
|
||||
QRegularExpression(
|
||||
"<a\\s+href=\"(//upload\\.wikimedia\\.org/wikipedia/[^\"'&]*\\.og[ga](?:\\.mp3|))\"" ),
|
||||
|
||||
QString::fromStdString(
|
||||
addAudioLink( string( "\"" ) + wikiUrl.scheme().toStdString() + ":\\1\"", this->dictPtr->getId() )
|
||||
+ "<a href=\"" + wikiUrl.scheme().toStdString() + ":\\1\"" ) );
|
||||
|
||||
// Add url scheme to image source urls
|
||||
articleString.replace( " src=\"//", " src=\"" + wikiUrl.scheme() + "://" );
|
||||
|
|
Loading…
Reference in a new issue