From 917ed1e443c6610c202f55dd9964b050396e75a4 Mon Sep 17 00:00:00 2001 From: mossroy Date: Mon, 30 Dec 2013 22:06:43 +0100 Subject: [PATCH] Normalize prefix and titles only if the current archive supports that feature. Fixes #64 (impossible to search for articles with chinese letters) --- js/lib/archive.js | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/js/lib/archive.js b/js/lib/archive.js index b2604ef7..ff510ff4 100644 --- a/js/lib/archive.js +++ b/js/lib/archive.js @@ -56,6 +56,7 @@ define(function(require) { this.date = null; this.language = null; this.titleSearchFile = null; + this.normalizedTitles = true; }; @@ -183,6 +184,19 @@ define(function(require) { var metadata = e.target.result; currentLocalArchiveInstance.language = /\nlanguage ?\= ?([^ \n]+)/.exec(metadata)[1]; currentLocalArchiveInstance.date = /\ndate ?\= ?([^ \n]+)/.exec(metadata)[1]; + var normalizedTitlesRegex = /\normalized_titles ?\= ?([^ \n]+)/; + if (normalizedTitlesRegex.exec(metadata)) { + var normalizedTitlesInt = normalizedTitlesRegex.exec(metadata)[1]; + if (normalizedTitlesInt === 0) { + currentLocalArchiveInstance.normalizedTitles = false; + } + else { + currentLocalArchiveInstance.normalizedTitles = true; + } + } + else { + currentLocalArchiveInstance.normalizedTitles = true; + } }; reader.readAsText(file); }; @@ -340,9 +354,8 @@ define(function(require) { hi = mid; } else { - var normalizedTitle = - normalize_string.normalizeString(utf8.parse(byteArray.subarray(startIndex, - newLineIndex))); + var normalizedTitle = currentLocalArchiveInstance.normalizeStringIfCompatibleArchive( + utf8.parse(byteArray.subarray(startIndex, newLineIndex))); if (normalizedTitle < normalizedPrefix) { lo = mid + newLineIndex - 1; } @@ -427,7 +440,7 @@ define(function(require) { alert('Title file read cancelled'); }; var currentLocalArchiveInstance = this; - var normalizedTitleName = normalize_string.normalizeString(titleName); + var normalizedTitleName = currentLocalArchiveInstance.normalizeStringIfCompatibleArchive(titleName); this.recursivePrefixSearch(reader, normalizedTitleName, 0, titleFileSize, function(titleOffset) { currentLocalArchiveInstance.getTitlesStartingAtOffset(titleOffset, MAX_TITLES_WITH_SAME_NORMALIZED_NAME, function(titleList) { if (titleList !== null && titleList.length>0) { @@ -463,7 +476,7 @@ define(function(require) { LocalArchive.prototype.findTitlesWithPrefix = function(prefix, maxSize, callbackFunction) { var titleFileSize = this.titleFile.size; if (prefix) { - prefix = normalize_string.normalizeString(prefix); + prefix = this.normalizeStringIfCompatibleArchive(prefix); } var reader = new FileReader(); @@ -472,14 +485,14 @@ define(function(require) { alert('Title file read cancelled'); }; var currentLocalArchiveInstance = this; - var normalizedPrefix = normalize_string.normalizeString(prefix); + var normalizedPrefix = this.normalizeStringIfCompatibleArchive(prefix); this.recursivePrefixSearch(reader, normalizedPrefix, 0, titleFileSize, function(titleOffset) { currentLocalArchiveInstance.getTitlesStartingAtOffset(titleOffset, maxSize, function(titleList) { // Keep only the titles with names starting with the prefix var i = 0; for (i = 0; i < titleList.length; i++) { var titleName = titleList[i].name; - var normalizedTitleName = normalize_string.normalizeString(titleName); + var normalizedTitleName = currentLocalArchiveInstance.normalizeStringIfCompatibleArchive(titleName); if (normalizedTitleName.length < normalizedPrefix.length || normalizedTitleName.substring(0, normalizedPrefix.length) !== normalizedPrefix) { break; } @@ -951,6 +964,21 @@ define(function(require) { }; }; + /** + * Normalize the given String, if the current Archive is compatible. + * If it's not, return the given String, as is. + * @param string : string to normalized + * @returns normalized string, or same string if archive is not compatible + */ + LocalArchive.prototype.normalizeStringIfCompatibleArchive = function(string) { + if (this.normalizedTitles === true) { + return normalize_string.normalizeString(string); + } + else { + return string; + } + }; + /** * ErrorHandler for FileReader * @param {type} evt