Improvements on regexp usage, for comments on #157.

+ more information in case of error
This commit is contained in:
mossroy 2016-01-16 13:54:20 +01:00
parent f574e053fe
commit 6752539430

View File

@ -877,10 +877,10 @@ define(['jquery', 'abstractBackend', 'util', 'cookies','geometry','osabstraction
var regexpImageLink = /^.?\/?[^:]+:(.*)/; var regexpImageLink = /^.?\/?[^:]+:(.*)/;
var regexpMathImageUrl = /^\/math.*\/([0-9a-f]{32})\.png$/; var regexpMathImageUrl = /^\/math.*\/([0-9a-f]{32})\.png$/;
var regexpPath = /^(.*\/)[^\/]+$/; var regexpPath = /^(.*\/)[^\/]+$/;
var regexpImageUrl = /^\.\.\/(I\/.*)$/; // These regular expressions match both relative and absolute URLs
var regexpMetadataUrl = /^\.\.\/(-\/.*)$/; // Since late 2014, all ZIM files should use relative URLs
var regexpImageAbsoluteUrl = /^\/(I\/.*)$/; var regexpImageUrl = /^(?:\.\.\/|\/)(I\/.*)$/;
var regexpMetadataAbsoluteUrl = /^\/(-\/.*)$/; var regexpMetadataUrl = /^(?:\.\.\/|\/)(-\/.*)$/;
/** /**
* Display the the given HTML article in the web page, * Display the the given HTML article in the web page,
@ -958,11 +958,11 @@ define(['jquery', 'abstractBackend', 'util', 'cookies','geometry','osabstraction
// Add an onclick event to go to this article // Add an onclick event to go to this article
// instead of following the link // instead of following the link
if (url.length>=2 && url.substring(0, 2) === "./") { if (url.substring(0, 2) === "./") {
url = url.substring(2); url = url.substring(2);
} }
// Remove the initial slash if it's an absolute URL // Remove the initial slash if it's an absolute URL
else if (url.length>=1 && url.substring(0, 1) === "/") { else if (url.substring(0, 1) === "/") {
url = url.substring(1); url = url.substring(1);
} }
$(this).on('click', function(e) { $(this).on('click', function(e) {
@ -986,18 +986,15 @@ define(['jquery', 'abstractBackend', 'util', 'cookies','geometry','osabstraction
} else { } else {
// It's a standard image contained in the ZIM file // It's a standard image contained in the ZIM file
// We try to find its name (from an absolute or relative URL) // We try to find its name (from an absolute or relative URL)
var imageMatch = image.attr("src").match(regexpImageAbsoluteUrl); var imageMatch = image.attr("src").match(regexpImageUrl);
if (!(imageMatch)) {
imageMatch = image.attr("src").match(regexpImageUrl);
}
if (imageMatch) { if (imageMatch) {
selectedArchive.getTitleByName(imageMatch[1]).then(function(title) { selectedArchive.getTitleByName(imageMatch[1]).then(function(title) {
selectedArchive.readBinaryFile(title, function (readableTitleName, content) { selectedArchive.readBinaryFile(title, function (readableTitleName, content) {
// TODO : add the complete MIME-type of the image (as read from the ZIM file) // TODO : add the complete MIME-type of the image (as read from the ZIM file)
image.attr("src", 'data:image;base64,' + util.uint8ArrayToBase64(content)); image.attr("src", 'data:image;base64,' + util.uint8ArrayToBase64(content));
}); });
}).fail(function () { }).fail(function (error) {
console.error("could not find title for image:" + imageMatch[1]); console.error("could not find title for image " + imageMatch[1] + " : " + error);
}); });
} }
} }
@ -1007,10 +1004,7 @@ define(['jquery', 'abstractBackend', 'util', 'cookies','geometry','osabstraction
$('#articleContent').contents().find('link[rel=stylesheet]').each(function() { $('#articleContent').contents().find('link[rel=stylesheet]').each(function() {
var link = $(this); var link = $(this);
// We try to find its name (from an absolute or relative URL) // We try to find its name (from an absolute or relative URL)
var hrefMatch = link.attr("href").match(regexpMetadataAbsoluteUrl); var hrefMatch = link.attr("href").match(regexpMetadataUrl);
if (!(hrefMatch)) {
hrefMatch = link.attr("href").match(regexpMetadataUrl);
}
if (hrefMatch) { if (hrefMatch) {
// It's a CSS file contained in the ZIM file // It's a CSS file contained in the ZIM file
var titleName = util.removeUrlParameters(hrefMatch[1]); var titleName = util.removeUrlParameters(hrefMatch[1]);
@ -1019,8 +1013,8 @@ define(['jquery', 'abstractBackend', 'util', 'cookies','geometry','osabstraction
var cssContent = encodeURIComponent(util.uintToString(content)); var cssContent = encodeURIComponent(util.uintToString(content));
link.attr("href", 'data:text/css;charset=UTF-8,' + cssContent); link.attr("href", 'data:text/css;charset=UTF-8,' + cssContent);
}); });
}).fail(function () { }).fail(function (error) {
console.error("could not find title for CSS : " + hrefMatch[1]); console.error("could not find title for CSS " + titleName + " : " + error);
}); });
} }
}); });
@ -1029,10 +1023,7 @@ define(['jquery', 'abstractBackend', 'util', 'cookies','geometry','osabstraction
$('#articleContent').contents().find('script').each(function() { $('#articleContent').contents().find('script').each(function() {
var script = $(this); var script = $(this);
// We try to find its name (from an absolute or relative URL) // We try to find its name (from an absolute or relative URL)
var srcMatch = script.attr("src").match(regexpMetadataAbsoluteUrl); var srcMatch = script.attr("src").match(regexpMetadataUrl);
if (!(srcMatch)) {
srcMatch = script.attr("src").match(regexpMetadataUrl);
}
// TODO check that the type of the script is text/javascript or application/javascript // TODO check that the type of the script is text/javascript or application/javascript
if (srcMatch) { if (srcMatch) {
// It's a Javascript file contained in the ZIM file // It's a Javascript file contained in the ZIM file
@ -1043,8 +1034,8 @@ define(['jquery', 'abstractBackend', 'util', 'cookies','geometry','osabstraction
// var jsContent = encodeURIComponent(util.uintToString(content)); // var jsContent = encodeURIComponent(util.uintToString(content));
//script.attr("src", 'data:text/javascript;charset=UTF-8,' + jsContent); //script.attr("src", 'data:text/javascript;charset=UTF-8,' + jsContent);
}); });
}).fail(function () { }).fail(function (error) {
console.error("could not find title for javascript : " + srcMatch[1]); console.error("could not find title for javascript " + titleName + " : " + error);
}); });
} }
}); });