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:
xiaoyifang 2023-07-26 23:03:03 +08:00 committed by GitHub
parent f642c9aa76
commit dbb11e293b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 16 deletions

View file

@ -6,13 +6,17 @@
std::string addAudioLink( std::string const & url, std::string const & dictionaryId ) std::string addAudioLink( std::string const & url, std::string const & dictionaryId )
{ {
if ( url.empty() || url.length() < 2 ) return addAudioLink( QString::fromStdString( url ), dictionaryId );
return {}; }
GlobalBroadcaster::instance()->pronounce_engine.sendAudio(
dictionaryId,
QString::fromStdString( url.substr( 1, url.length() - 2 ) ) );
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 ) std::string makeAudioLinkScript( std::string const & url, std::string const & dictionaryId )

View file

@ -5,6 +5,7 @@
#define __AUDIOLINK_HH_INCLUDED__ #define __AUDIOLINK_HH_INCLUDED__
#include <QString>
#include <string> #include <string>
/// Adds a piece of javascript to save the given audiolink to a special /// 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 url should be escaped and surrounded by quotes.
/// The dictionary id is used to make active dictionary feature work. /// 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( 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 ); std::string makeAudioLinkScript( std::string const & url, std::string const & dictionaryId );
#endif #endif

View file

@ -558,7 +558,12 @@ void MediaWikiArticleRequest::requestFinished( QNetworkReply * r )
QRegularExpressionMatch match2 = reg2.match( tag ); QRegularExpressionMatch match2 = reg2.match( tag );
if ( match2.hasMatch() ) { if ( match2.hasMatch() ) {
QString ref = match2.captured( 1 ); 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>)"; + R"("><img src="qrc:///icons/playsound.png" border="0" align="absmiddle" alt="Play"/></a>)";
articleNewString += audio_url; articleNewString += audio_url;
} }
@ -571,14 +576,6 @@ void MediaWikiArticleRequest::requestFinished( QNetworkReply * r )
articleNewString.clear(); 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 // Add url scheme to image source urls
articleString.replace( " src=\"//", " src=\"" + wikiUrl.scheme() + "://" ); articleString.replace( " src=\"//", " src=\"" + wikiUrl.scheme() + "://" );