fix:zim regression

image can not displayed
This commit is contained in:
xiaoyifang 2022-11-22 18:03:59 +08:00 committed by Xiao YiFang
parent 4a129005df
commit aa9ed9bc38

53
zim.cc
View file

@ -128,7 +128,7 @@ __attribute__((packed))
enum enum
{ {
Signature = 0x584D495A, // ZIMX on little-endian, XMIZ on big-endian Signature = 0x584D495A, // ZIMX on little-endian, XMIZ on big-endian
CurrentFormatVersion = 3 + BtreeIndexing::FormatVersion + Folding::Version CurrentFormatVersion = 4 + BtreeIndexing::FormatVersion + Folding::Version
}; };
struct IdxHeader struct IdxHeader
@ -839,8 +839,44 @@ string ZimDictionary::convert( const string & in )
QString( "<body \\1" ) ); QString( "<body \\1" ) );
// pattern of img and script // pattern of img and script
text.replace( QRegularExpression( "<\\s*(img|script)\\s+([^>]*)src=(\"|)(\\.\\./)*" ), // text.replace( QRegularExpression( "<\\s*(img|script)\\s+([^>]*)src=(\")([^\"]*)\\3" ),
QString( "<\\1 \\2src=\\3bres://%1/").arg( getId().c_str() ) ); // QString( "<\\1 \\2src=\\3bres://%1/").arg( getId().c_str() ) );
QRegularExpression rxImgScript( "<\\s*(img|script)\\s+([^>]*)src=(\")([^\"]*)\\3" );
QRegularExpressionMatchIterator it = rxImgScript.globalMatch( text );
int pos = 0;
QString newText;
while( it.hasNext() )
{
QRegularExpressionMatch match = it.next();
newText += text.mid( pos, match.capturedStart() - pos );
pos = match.capturedEnd();
QStringList list = match.capturedTexts();
QString url = list[ 4 ]; // a url
QString urlLink = match.captured();
QString replacedLink = urlLink;
if( !url.isEmpty() && !url.startsWith( "//" ) && !url.startsWith( "http://" ) && !url.startsWith( "https://" ) )
{
//<\\1 \\2src=\\3bres://%1/
url.replace( RX::Zim::linkSpecialChar, "" );
replacedLink =
QString( "<%1 %2 src=\"bres://%3/%4\"" ).arg( list[ 1 ], list[ 2 ], QString::fromStdString( getId() ), url );
}
newText += replacedLink;
}
if( pos )
{
newText += text.mid( pos );
text = newText;
}
newText.clear();
// Fix links without '"' // Fix links without '"'
text.replace( QRegularExpression( "href=(\\.\\.|)/([^\\s>]+)" ), text.replace( QRegularExpression( "href=(\\.\\.|)/([^\\s>]+)" ),
@ -860,9 +896,8 @@ string ZimDictionary::convert( const string & in )
// these links will be translated into local definitions // these links will be translated into local definitions
// <meta http-equiv="Refresh" content="0;url=../dsalsrv02.uchicago.edu/cgi-bin/0994.html"> // <meta http-equiv="Refresh" content="0;url=../dsalsrv02.uchicago.edu/cgi-bin/0994.html">
QRegularExpression rxLink( "<\\s*(?:a|meta)\\s+([^>]*)(?:href|url)=\"?(?!(?:\\w+://|#|mailto:|tel:))()([^\"]*)\"\\s*(title=\"[^\"]*\")?[^>]*>" ); QRegularExpression rxLink( "<\\s*(?:a|meta)\\s+([^>]*)(?:href|url)=\"?(?!(?:\\w+://|#|mailto:|tel:))()([^\"]*)\"\\s*(title=\"[^\"]*\")?[^>]*>" );
QRegularExpressionMatchIterator it = rxLink.globalMatch( text ); it = rxLink.globalMatch( text );
int pos = 0; pos = 0;
QString newText;
while( it.hasNext() ) while( it.hasNext() )
{ {
QRegularExpressionMatch match = it.next(); QRegularExpressionMatch match = it.next();
@ -1658,6 +1693,8 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
} }
else else
{ {
url.insert( url.begin(), '/' );
url.insert( url.begin(), nameSpace );
auto formatedUrl = QString::fromStdString(url).replace(RX::Zim::linkSpecialChar,""); auto formatedUrl = QString::fromStdString(url).replace(RX::Zim::linkSpecialChar,"");
indexedResources.addSingleWord( Utf8::decode( formatedUrl.toStdString() ), n ); indexedResources.addSingleWord( Utf8::decode( formatedUrl.toStdString() ), n );
} }
@ -1695,8 +1732,8 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
} }
else else
{ {
// url.insert( url.begin(), '/' ); url.insert( url.begin(), '/' );
// url.insert( url.begin(), nameSpace ); url.insert( url.begin(), nameSpace );
auto formatedUrl = QString::fromStdString(url).replace(RX::Zim::linkSpecialChar,""); auto formatedUrl = QString::fromStdString(url).replace(RX::Zim::linkSpecialChar,"");
indexedResources.addSingleWord( Utf8::decode( formatedUrl.toStdString() ), n ); indexedResources.addSingleWord( Utf8::decode( formatedUrl.toStdString() ), n );
} }