Implement reading the metadata.txt file, to find its language and date

Use this language to redirect the links to images (File:xxx.jpg for example) to the online version. Fixes #23
This commit is contained in:
mossroy 2013-07-15 13:36:27 +02:00
parent 05014e469b
commit 9c470ea779
5 changed files with 183 additions and 124 deletions

View File

@ -63,12 +63,16 @@
The <a href="https://github.com/mossroy/evopedia-html5/issues">bugtracker</a> is on github too.
</div>
<div id="openLocalFiles" style="display: none;">
<br /> Please select the file titles.idx :<br /> <input type="file"
id="titleFile" /><br /> Please select the files wikipedia_*.dat
from the same dump :<br /> <input type="file" id="dataFiles" multiple />
<br /> Please select the file math.idx from the same dump:<br /> <input type="file"
id="mathIndexFile" /><br /> Please select the file math.dat from the
same dump:<br /> <input type="file" id="mathDataFile" /><br />
<br /> Please select the file titles.idx :<br />
<input type="file" id="titleFile" /><br />
Please select the files wikipedia_*.dat from the same dump :<br />
<input type="file" id="dataFiles" multiple /><br />
Please select the file math.idx from the same dump:<br />
<input type="file" id="mathIndexFile" /><br />
Please select the file math.dat from the same dump:<br />
<input type="file" id="mathDataFile" /><br />
Please select the file metadata.txt from the same dump:<br />
<input type="file" id="metadataFile" /><br />
</div>
<div id="scanningForArchives" style="display: none;">
<br /> Scanning your sdcard for archives... Please wait <img src="img/spinner.gif" />

View File

@ -89,9 +89,10 @@ define(function(require) {
function setLocalArchiveFromArchiveList() {
var archiveDirectory = $('#archiveList').val();
localArchive = new evopedia.LocalArchive();
localArchive.readTitleFile(storage, archiveDirectory);
localArchive.readDataFiles(storage, archiveDirectory, 0);
localArchive.readMathFiles(storage, archiveDirectory);
localArchive.readTitleFileFromStorage(storage, archiveDirectory);
localArchive.readDataFilesFromStorage(storage, archiveDirectory, 0);
localArchive.readMathFilesFromStorage(storage, archiveDirectory);
localArchive.readMetadataFileFromStorage(storage, archiveDirectory);
}
/**
@ -103,6 +104,7 @@ define(function(require) {
$('#titleFile').on('change', setLocalArchiveFromFileSelect);
$('#mathIndexFile').on('change', setLocalArchiveFromFileSelect);
$('#mathDataFile').on('change', setLocalArchiveFromFileSelect);
$('#metadataFile').on('change', setLocalArchiveFromFileSelect);
}
/**
@ -113,11 +115,13 @@ define(function(require) {
var titleFile = document.getElementById('titleFile').files[0];
var mathIndexFile = document.getElementById('mathIndexFile').files[0];
var mathDataFile = document.getElementById('mathDataFile').files[0];
var metadataFile = document.getElementById('metadataFile').files[0];
localArchive = new evopedia.LocalArchive();
localArchive.dataFiles = dataFiles;
localArchive.titleFile = titleFile;
localArchive.mathIndexFile = mathIndexFile;
localArchive.mathDataFile = mathDataFile;
localArchive.readMetadataFile(metadataFile);
}
/**
@ -205,11 +209,15 @@ define(function(require) {
// Display the article inside the web page.
$('#articleContent').html(htmlArticle);
// Compile the regular expressions needed to modify links
var regexOtherLanguage = /^\.?\/?\.\.\/([^\/]+)\/(.*)/;
var regexImageLink = /^.?\/?[^:]+:(.*)/;
// Convert links into javascript calls
var regex = /^\.?\/?\.\.\/([^\/]+)\/(.*)/;
$('#articleContent').find('a').each(function() {
// Store current link's url
var url = $(this).attr("href");
var lowerCaseUrl = url.toLowerCase();
var cssClass = $(this).attr("class");
if (cssClass === "new") {
@ -225,10 +233,19 @@ define(function(require) {
else if (url.substring(0, 4) === "http") {
// It's an external link : do nothing
}
else if (url.substring(0, 2) === ".." || url.substring(0, 4) === "./..") {
else if (url.match(regexOtherLanguage)) {
// It's a link to another language : change the URL to the online version of wikipedia
// The regular expression extracts $1 as the language, and $2 as the title name
var onlineWikipediaUrl = url.replace(regex, "https://$1.wikipedia.org/wiki/$2");
var onlineWikipediaUrl = url.replace(regexOtherLanguage, "https://$1.wikipedia.org/wiki/$2");
$(this).attr("href", onlineWikipediaUrl);
}
else if (url.match(regexImageLink)
&& (evopedia.endsWith(lowerCaseUrl, ".png")
|| evopedia.endsWith(lowerCaseUrl, ".svg")
|| evopedia.endsWith(lowerCaseUrl, ".jpg")
|| evopedia.endsWith(lowerCaseUrl, ".jpeg"))) {
// It's a link to a file of wikipedia : change the URL to the online version
var onlineWikipediaUrl = url.replace(regexImageLink, "https://"+localArchive.language+".wikipedia.org/wiki/File:$1");
$(this).attr("href", onlineWikipediaUrl);
}
else {

View File

@ -72,11 +72,9 @@ define(function(require) {
this.titleFile = null;
this.mathIndexFile = null;
this.mathDataFile = null;
// TODO to be replaced by the real archive attributes
this.date = "2013-03-14";
this.language = "zz";
}
;
this.date = null;
this.language = null;
};
/**
@ -86,7 +84,7 @@ define(function(require) {
* @param storage
* @param directory
*/
LocalArchive.prototype.readTitleFile = function(storage, directory) {
LocalArchive.prototype.readTitleFileFromStorage = function(storage, directory) {
var currentLocalArchiveInstance = this;
var filerequest = storage.get(directory + '/titles.idx');
filerequest.onsuccess = function() {
@ -105,7 +103,7 @@ define(function(require) {
* @param directory
* @param index
*/
LocalArchive.prototype.readDataFiles = function(storage, directory, index) {
LocalArchive.prototype.readDataFilesFromStorage = function(storage, directory, index) {
var currentLocalArchiveInstance = this;
var prefixedFileNumber = "";
@ -118,7 +116,7 @@ define(function(require) {
+ '.dat');
filerequest.onsuccess = function() {
currentLocalArchiveInstance.dataFiles[index] = filerequest.result;
currentLocalArchiveInstance.readDataFiles(storage, directory,
currentLocalArchiveInstance.readDataFilesFromStorage(storage, directory,
index + 1);
};
filerequest.onerror = function(event) {
@ -130,6 +128,43 @@ define(function(require) {
};
};
/**
* Read the metadata.txt file in the given directory, and store its content
* in the current instance
*
* @param storage
* @param directory
*/
LocalArchive.prototype.readMetadataFileFromStorage = function(storage, directory) {
var currentLocalArchiveInstance = this;
var filerequest = storage.get(directory + '/metadata.txt');
filerequest.onsuccess = function() {
var metadataFile = filerequest.result;
currentLocalArchiveInstance.readMetadataFile(metadataFile);
};
filerequest.onerror = function(event) {
alert("error reading metadata.txt file in directory "
+ directory + " : " + event.target.error.name);
};
};
/**
* Read the metadata file, in order to populate its values in the current
* instance
* @param {File} file metadata.txt file
*/
LocalArchive.prototype.readMetadataFile = function(file) {
var currentLocalArchiveInstance = this;
var reader = new FileReader();
reader.onload = function(e) {
var metadata = e.target.result;
currentLocalArchiveInstance.language = /\nlanguage ?\= ?([^ \n]+)/.exec(metadata)[1];
currentLocalArchiveInstance.date = /\ndate ?\= ?([^ \n]+)/.exec(metadata)[1];
};
reader.readAsText(file);
};
/**
* Read the math files (math.idx and math.dat) in the given directory, and assign it to the
* current LocalArchive
@ -137,7 +172,7 @@ define(function(require) {
* @param storage
* @param directory
*/
LocalArchive.prototype.readMathFiles = function(storage, directory) {
LocalArchive.prototype.readMathFilesFromStorage = function(storage, directory) {
var currentLocalArchiveInstance = this;
var filerequest1 = storage.get(directory + '/math.idx');
filerequest1.onsuccess = function() {
@ -724,6 +759,7 @@ define(function(require) {
*/
return {
LocalArchive: LocalArchive,
Title: Title
Title: Title,
endsWith : endsWith
};
});

View File

@ -26,6 +26,8 @@
<input type="file" id="mathIndexFile" />
<br /> Please select the file math.dat from the same dump:<br />
<input type="file" id="mathDataFile" />
<br /> Please select the file metadata.txt from the same dump:<br />
<input type="file" id="metadataFile" /><br />
<br />
<input type="button" id="runTests" value="Run tests" />
<div id="qunit"></div>

View File

@ -34,8 +34,8 @@ define(function(require) {
localArchive.dataFiles = document.getElementById('dataFiles').files;
localArchive.mathIndexFile = document.getElementById('mathIndexFile').files[0];
localArchive.mathDataFile = document.getElementById('mathDataFile').files[0];
localArchive.language = "small";
localArchive.date = "2010-08-14";
var metadataFile = document.getElementById('metadataFile').files[0];
localArchive.readMetadataFile(metadataFile);
module("evopedia");
asyncTest("check getTitlesStartingAtOffset 0", function() {