Fixes to make it work again on FxOS, and to keep the new structure compatible with unit tests

+ a few cosmetic modifications
This commit is contained in:
mossroy 2014-02-28 11:44:49 +01:00
parent 3f7abc7a24
commit 84f5ab62a3
5 changed files with 1048 additions and 1038 deletions

View File

@ -2,12 +2,12 @@
"version": "1.1.0-SNAPSHOT", "version": "1.1.0-SNAPSHOT",
"name": "Evopedia", "name": "Evopedia",
"description": "Offline Wikipedia Viewer", "description": "Offline Wikipedia Viewer",
"launch_path": "/index.html", "launch_path": "/www/index.html",
"icons": { "icons": {
"16": "/img/icons/evopedia-16.png", "16": "/www/img/icons/evopedia-16.png",
"48": "/img/icons/evopedia-48.png", "48": "/www/img/icons/evopedia-48.png",
"60": "/img/icons/evopedia-60.png", "60": "/www/img/icons/evopedia-60.png",
"128": "/img/icons/evopedia-128.png" "128": "/www/img/icons/evopedia-128.png"
}, },
"developer": { "developer": {
"name": "Mossroy <mossroy@free.fr>", "name": "Mossroy <mossroy@free.fr>",

View File

@ -33,7 +33,7 @@
http://requirejs.org/docs/api.html#jsfiles --> http://requirejs.org/docs/api.html#jsfiles -->
<script type="text/javascript" <script type="text/javascript"
data-main="tests/init.js" data-main="tests/init.js"
src="js/lib/require.js"></script> src="www/js/lib/require.js"></script>
</head> </head>

View File

@ -21,7 +21,7 @@
* along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/> * along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/ */
require.config({ require.config({
baseUrl: 'js/lib', baseUrl: 'www/js/lib',
paths: { paths: {
'zepto': 'zepto', 'zepto': 'zepto',
'jquery': 'jquery-2.0.3', 'jquery': 'jquery-2.0.3',
@ -30,4 +30,4 @@ require.config({
} }
}); });
requirejs(['../../tests/tests']); requirejs(['../../../tests/tests']);

File diff suppressed because it is too large Load Diff

View File

@ -1,121 +1,126 @@
/** /**
* titleIterators.js : Various classes to iterate over titles, for example as a * titleIterators.js : Various classes to iterate over titles, for example as a
* result of searching. * result of searching.
* *
* Copyright 2014 Evopedia developers * Copyright 2014 Evopedia developers
* License GPL v3: * License GPL v3:
* *
* This file is part of Evopedia. * This file is part of Evopedia.
* *
* Evopedia is free software: you can redistribute it and/or modify * Evopedia is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Evopedia is distributed in the hope that it will be useful, * Evopedia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/> * along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/ */
define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util, jQuery) { define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util, jQuery) {
// Maximum length of a title // Maximum length of a title
// 300 bytes is arbitrary : we actually do not really know how long the titles will be // 300 bytes is arbitrary : we actually do not really know how long the titles will be
// But mediawiki titles seem to be limited to ~200 bytes, so 300 should be more than enough // But mediawiki titles seem to be limited to ~200 bytes, so 300 should be more than enough
var MAX_TITLE_LENGTH = 300; var MAX_TITLE_LENGTH = 300;
/** /**
* Iterates over all titles starting at the given offset. * Iterates over all titles starting at the given offset.
* The asynchronous method advance has to be called before this.title is * The asynchronous method advance has to be called before this.title is
* valid. * valid.
*/ * @param archive
function SequentialTitleIterator(archive, offset) { * @param offset
this._titleFile = archive.titleFile; */
this._archive = archive; function SequentialTitleIterator(archive, offset) {
this._offset = offset; this._titleFile = archive.titleFile;
this.title = null; this._archive = archive;
} this._offset = offset;
/** this.title = null;
* Advances to the next title (or the first), if possible. };
* @returns jQuery promise containing the next title or null if there is no /**
* next title * Advances to the next title (or the first), if possible.
*/ * @returns jQuery promise containing the next title or null if there is no
SequentialTitleIterator.prototype.advance = function() { * next title
if (this._offset >= this._titleFile.size) { */
this.title = null; SequentialTitleIterator.prototype.advance = function() {
return jQuery.when(this.title); if (this._offset >= this._titleFile.size) {
} this.title = null;
var that = this; return jQuery.when(this.title);
return util.readFileSlice(this._titleFile, this._offset, }
this._offset + MAX_TITLE_LENGTH).then(function(byteArray) { var that = this;
var newLineIndex = 15; return util.readFileSlice(this._titleFile, this._offset,
while (newLineIndex < byteArray.length && byteArray[newLineIndex] != 10) { this._offset + MAX_TITLE_LENGTH).then(function(byteArray) {
newLineIndex++; var newLineIndex = 15;
} while (newLineIndex < byteArray.length && byteArray[newLineIndex] != 10) {
var encodedTitle = byteArray.subarray(0, newLineIndex); newLineIndex++;
that._title = evopediaTitle.Title.parseTitle(encodedTitle, that._archive, that._offset); }
that._offset += newLineIndex + 1; var encodedTitle = byteArray.subarray(0, newLineIndex);
return that._title; that._title = evopediaTitle.Title.parseTitle(encodedTitle, that._archive, that._offset);
}); that._offset += newLineIndex + 1;
} return that._title;
});
/** };
* Searches for the offset into the given title file where the first title
* with the given prefix (or lexicographically larger) is located. /**
* The given function normalize is applied to every title before comparison. * Searches for the offset into the given title file where the first title
* @returns jQuery promise giving the offset * with the given prefix (or lexicographically larger) is located.
*/ * The given function normalize is applied to every title before comparison.
function FindPrefixOffset(titleFile, prefix, normalize) { * @param titleFile
prefix = normalize(prefix); * @param prefix
var lo = 0; * @param normalize function to be applied to every title before comparison
var hi = titleFile.size; * @returns jQuery promise giving the offset
var iterate = function() { */
if (lo >= hi) { function FindPrefixOffset(titleFile, prefix, normalize) {
if (lo > 0) prefix = normalize(prefix);
lo += 2; // Let lo point to the start of an entry var lo = 0;
return jQuery.when(lo); var hi = titleFile.size;
} else { var iterate = function() {
var mid = Math.floor((lo + hi) / 2); if (lo >= hi) {
return util.readFileSlice(titleFile, mid, mid + MAX_TITLE_LENGTH).then(function(byteArray) { if (lo > 0)
// Look for the index of the next NewLine lo += 2; // Let lo point to the start of an entry
var newLineIndex = 0; return jQuery.when(lo);
while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) { } else {
newLineIndex++; var mid = Math.floor((lo + hi) / 2);
} return util.readFileSlice(titleFile, mid, mid + MAX_TITLE_LENGTH).then(function(byteArray) {
var startIndex = 0; // Look for the index of the next NewLine
if (mid > 0) { var newLineIndex = 0;
startIndex = newLineIndex + 16; while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) {
newLineIndex = startIndex; newLineIndex++;
// Look for the index of the next NewLine }
while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) { var startIndex = 0;
newLineIndex++; if (mid > 0) {
} startIndex = newLineIndex + 16;
} newLineIndex = startIndex;
if (newLineIndex === startIndex) { // Look for the index of the next NewLine
// End of file reached while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) {
hi = mid; newLineIndex++;
} else { }
var normalizedTitle = normalize(utf8.parse(byteArray.subarray(startIndex, newLineIndex))); }
if (normalizedTitle < prefix) { if (newLineIndex === startIndex) {
lo = mid + newLineIndex - 1; // End of file reached
} else { hi = mid;
hi = mid; } else {
} var normalizedTitle = normalize(utf8.parse(byteArray.subarray(startIndex, newLineIndex)));
} if (normalizedTitle < prefix) {
return iterate(); lo = mid + newLineIndex - 1;
}); } else {
} hi = mid;
} }
return iterate(); }
} return iterate();
});
/** }
* Functions and classes exposed by this module };
*/ return iterate();
return { }
SequentialTitleIterator : SequentialTitleIterator,
FindPrefixOffset : FindPrefixOffset /**
}; * Functions and classes exposed by this module
}); */
return {
SequentialTitleIterator : SequentialTitleIterator,
FindPrefixOffset : FindPrefixOffset
};
});