mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-12-17 23:04:06 +00:00
fix:mdx embeded @font-face support
This commit is contained in:
parent
e8764d322c
commit
98769b6dd2
|
@ -56,3 +56,6 @@ QRegularExpression Mdx::srcRe2( "([\\s\"'](?:src|srcset)\\s*=)\\s*(?![\\s\"']|\\
|
|||
|
||||
QRegularExpression Mdx::links( "url\\(\\s*(['\"]?)([^'\"]*)(['\"]?)\\s*\\)",
|
||||
QRegularExpression::CaseInsensitiveOption );
|
||||
|
||||
QRegularExpression Mdx::fontFace( "(?:url\\s*\\(\\s*\\\"(.*?)\\\"\\s*)\\)",
|
||||
QRegularExpression::CaseInsensitiveOption|QRegularExpression::DotMatchesEverythingOption );
|
||||
|
|
|
@ -37,6 +37,7 @@ public:
|
|||
static QRegularExpression srcRe2;
|
||||
|
||||
static QRegularExpression links;
|
||||
static QRegularExpression fontFace;
|
||||
};
|
||||
|
||||
} // namespace RX
|
||||
|
|
84
mdx.cc
84
mdx.cc
|
@ -301,6 +301,10 @@ private:
|
|||
/// Process resource links (images, audios, etc)
|
||||
QString & filterResource( QString const & articleId, QString & article );
|
||||
|
||||
void replaceLinks( QString & id, const QString & articleId, QString & article );
|
||||
//@font-face
|
||||
void replaceFontLinks( QString & id, QString & article );
|
||||
|
||||
void removeDirectory( QString const & directory );
|
||||
|
||||
friend class MdxHeadwordsRequest;
|
||||
|
@ -954,6 +958,13 @@ void MdxDictionary::loadArticle( uint32_t offset, string & articleText, bool noF
|
|||
QString & MdxDictionary::filterResource( QString const & articleId, QString & article )
|
||||
{
|
||||
QString id = QString::fromStdString( getId() );
|
||||
replaceLinks( id, articleId, article );
|
||||
replaceFontLinks( id, article);
|
||||
return article;
|
||||
}
|
||||
|
||||
void MdxDictionary::replaceLinks( QString & id, const QString & articleId, QString & article )
|
||||
{
|
||||
QString uniquePrefix = QString::fromLatin1( "g" ) + id + "_" + articleId + "_";
|
||||
|
||||
QString articleNewText;
|
||||
|
@ -996,19 +1007,18 @@ QString & MdxDictionary::filterResource( QString const & articleId, QString & ar
|
|||
if( match.hasMatch() )
|
||||
{
|
||||
// sounds and audio link script
|
||||
QString newTxt = match.captured( 1 ) + match.captured( 2 )
|
||||
+ "gdau://" + id + "/"
|
||||
+ match.captured( 3 ) + match.captured( 2 );
|
||||
newLink = QString::fromUtf8( addAudioLink( "\"gdau://" + getId() + "/" + match.captured( 3 ).toUtf8().data() + "\"", getId() ).c_str() )
|
||||
QString newTxt =
|
||||
match.captured( 1 ) + match.captured( 2 ) + "gdau://" + id + "/" + match.captured( 3 ) + match.captured( 2 );
|
||||
newLink =
|
||||
QString::fromUtf8(
|
||||
addAudioLink( "\"gdau://" + getId() + "/" + match.captured( 3 ).toUtf8().data() + "\"", getId() ).c_str() )
|
||||
+ newLink.replace( match.capturedStart(), match.capturedLength(), newTxt );
|
||||
}
|
||||
|
||||
match = RX::Mdx::wordCrossLink.match( newLink );
|
||||
if( match.hasMatch() )
|
||||
{
|
||||
QString newTxt = match.captured( 1 ) + match.captured( 2 )
|
||||
+ "gdlookup://localhost/"
|
||||
+ match.captured( 3 );
|
||||
QString newTxt = match.captured( 1 ) + match.captured( 2 ) + "gdlookup://localhost/" + match.captured( 3 );
|
||||
|
||||
if( match.lastCapturedIndex() >= 4 && !match.captured( 4 ).isEmpty() )
|
||||
newTxt += QString( "?gdanchor=" ) + uniquePrefix + match.captured( 4 ).mid( 1 );
|
||||
|
@ -1017,25 +1027,20 @@ QString & MdxDictionary::filterResource( QString const & articleId, QString & ar
|
|||
newLink.replace( match.capturedStart(), match.capturedLength(), newTxt );
|
||||
}
|
||||
}
|
||||
else
|
||||
if( linkType.compare( "link" ) == 0 )
|
||||
else if( linkType.compare( "link" ) == 0 )
|
||||
{
|
||||
// stylesheets
|
||||
QRegularExpressionMatch match = RX::Mdx::stylesRe.match( linkTxt );
|
||||
if( match.hasMatch() )
|
||||
{
|
||||
QString newText = match.captured( 1 ) + match.captured( 2 )
|
||||
+ "bres://" + id + "/"
|
||||
+ match.captured( 3 ) + match.captured( 2 );
|
||||
QString newText =
|
||||
match.captured( 1 ) + match.captured( 2 ) + "bres://" + id + "/" + match.captured( 3 ) + match.captured( 2 );
|
||||
newLink = linkTxt.replace( match.capturedStart(), match.capturedLength(), newText );
|
||||
}
|
||||
else
|
||||
newLink = linkTxt.replace( RX::Mdx::stylesRe2,
|
||||
"\\1\"bres://" + id + "/\\2\"" );
|
||||
newLink = linkTxt.replace( RX::Mdx::stylesRe2, "\\1\"bres://" + id + "/\\2\"" );
|
||||
}
|
||||
else
|
||||
if( linkType.compare( "script" ) == 0 || linkType.compare( "img" ) == 0
|
||||
|| linkType.compare( "source" ) == 0 )
|
||||
else if( linkType.compare( "script" ) == 0 || linkType.compare( "img" ) == 0 || linkType.compare( "source" ) == 0 )
|
||||
{
|
||||
// javascripts and images
|
||||
QRegularExpressionMatch match = RX::Mdx::inlineScriptRe.match( linkTxt );
|
||||
|
@ -1063,20 +1068,17 @@ QString & MdxDictionary::filterResource( QString const & articleId, QString & ar
|
|||
QString filename = match.captured( 3 );
|
||||
QString newName = getCachedFileName( filename );
|
||||
newName.replace( '\\', '/' );
|
||||
newText = match.captured( 1 ) + match.captured( 2 )
|
||||
+ "file:///" + newName + match.captured( 2 );
|
||||
newText = match.captured( 1 ) + match.captured( 2 ) + "file:///" + newName + match.captured( 2 );
|
||||
}
|
||||
else
|
||||
{
|
||||
newText = match.captured( 1 ) + match.captured( 2 )
|
||||
+ "bres://" + id + "/"
|
||||
+ match.captured( 3 ) + match.captured( 2 );
|
||||
newText = match.captured( 1 ) + match.captured( 2 ) + "bres://" + id + "/" + match.captured( 3 )
|
||||
+ match.captured( 2 );
|
||||
}
|
||||
newLink = linkTxt.replace( match.capturedStart(), match.capturedLength(), newText );
|
||||
}
|
||||
else
|
||||
newLink = linkTxt.replace( RX::Mdx::srcRe2,
|
||||
"\\1\"bres://" + id + "/\\2\"" );
|
||||
newLink = linkTxt.replace( RX::Mdx::srcRe2, "\\1\"bres://" + id + "/\\2\"" );
|
||||
}
|
||||
}
|
||||
if( !newLink.isEmpty() )
|
||||
|
@ -1098,8 +1100,40 @@ QString & MdxDictionary::filterResource( QString const & articleId, QString & ar
|
|||
const auto & value = idMap[ key ];
|
||||
article.replace( "\"" + key + "\"", "\"" + value + "\"" );
|
||||
}
|
||||
}
|
||||
|
||||
return article;
|
||||
void MdxDictionary::replaceFontLinks( QString & id, QString & article )
|
||||
{
|
||||
//article = article.replace( RX::Mdx::fontFace, "src:url(\"bres://" + id + "/" + "\\1\")" );
|
||||
QString articleNewText;
|
||||
int linkPos = 0;
|
||||
QRegularExpressionMatchIterator it = RX::Mdx::fontFace.globalMatch( article );
|
||||
while( it.hasNext() )
|
||||
{
|
||||
QRegularExpressionMatch allLinksMatch = it.next();
|
||||
|
||||
if( allLinksMatch.capturedEnd() < linkPos )
|
||||
continue;
|
||||
|
||||
articleNewText += article.mid( linkPos, allLinksMatch.capturedStart() - linkPos );
|
||||
linkPos = allLinksMatch.capturedEnd();
|
||||
|
||||
QString linkTxt = allLinksMatch.captured();
|
||||
QString linkType = allLinksMatch.captured( 1 );
|
||||
QString newLink = linkTxt;
|
||||
|
||||
//skip remote url
|
||||
if( !linkType.contains( ":" ) )
|
||||
{
|
||||
newLink = QString( "url(\"bres://%1/%2\")" ).arg( id, linkType );
|
||||
}
|
||||
articleNewText += newLink;
|
||||
}
|
||||
if( linkPos )
|
||||
{
|
||||
articleNewText += article.mid( linkPos );
|
||||
article = articleNewText;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue