From 9c470ea779bcdf283ee9c7c7654e6216d90f773b Mon Sep 17 00:00:00 2001 From: mossroy Date: Mon, 15 Jul 2013 13:36:27 +0200 Subject: [PATCH] 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 --- index.html | 216 +++++++++++++++++++++++---------------------- js/app.js | 29 ++++-- js/lib/evopedia.js | 56 +++++++++--- tests/tests.html | 2 + tests/tests.js | 4 +- 5 files changed, 183 insertions(+), 124 deletions(-) diff --git a/index.html b/index.html index 3a4782b7..97022240 100644 --- a/index.html +++ b/index.html @@ -1,106 +1,110 @@ - - - - - - Evopedia - - - - - - - - -

Evopedia

- - - - - -
Find titles starting with : -   - - -

Choose a title from the filtered list : - -
-
- - -
 
-
- - - - - - + + + + + + Evopedia + + + + + + + + +

Evopedia

+ + + + + +
Find titles starting with : +   + + +

Choose a title from the filtered list : + +
+
+ + +
 
+
+ + + + + + diff --git a/js/app.js b/js/app.js index 6b201da1..836a806c 100644 --- a/js/app.js +++ b/js/app.js @@ -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 { diff --git a/js/lib/evopedia.js b/js/lib/evopedia.js index d2aa2119..b9f6dc93 100644 --- a/js/lib/evopedia.js +++ b/js/lib/evopedia.js @@ -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) { @@ -129,6 +127,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 @@ -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 }; }); diff --git a/tests/tests.html b/tests/tests.html index 38bce090..74409797 100644 --- a/tests/tests.html +++ b/tests/tests.html @@ -26,6 +26,8 @@
Please select the file math.dat from the same dump:
+
Please select the file metadata.txt from the same dump:
+

diff --git a/tests/tests.js b/tests/tests.js index 7016bf92..e2fb1622 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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() {