[gnome-maps/wip/mlundblad/wikipedia-validate: 1/3] wikipedia: Add function to validate a reference




commit 8ae1c418ed303ba27d8b4b2ebac94095de5ecd79
Author: Marcus Lundblad <ml update uu se>
Date:   Sun Apr 25 22:54:34 2021 +0200

    wikipedia: Add function to validate a reference

 src/wikipedia.js       | 21 +++++++++++++++++++++
 tests/meson.build      |  2 +-
 tests/wikipediaTest.js | 44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 1 deletion(-)
---
diff --git a/src/wikipedia.js b/src/wikipedia.js
index ebe3fa20..daf3dc44 100644
--- a/src/wikipedia.js
+++ b/src/wikipedia.js
@@ -27,6 +27,12 @@ const Soup = imports.gi.Soup;
 const Format = imports.format;
 const Utils = imports.utils;
 
+/**
+ * Regex matching editions of Wikipedia, e.g. "en", "arz", pt-BR", "simple".
+ * See https://en.wikipedia.org/wiki/List_of_Wikipedias  "WP code".
+ */
+const WP_REGEX = /^[a-z][a-z][a-z]?(\-[a-z]+)?$|^simple$/;
+
 let _soupSession = null;
 function _getSoupSession() {
     if (_soupSession === null) {
@@ -52,6 +58,21 @@ function getHtmlEntityEncodedArticle(wiki) {
     return GLib.markup_escape_text(wiki.split(':').splice(1).join(':'), -1);
 }
 
+/**
+ * Determine if a Wikipedia reference tag is valid
+ * (of the form "lang:Article title")
+ */
+function isValidWikipedia(wiki) {
+    let parts = wiki.split(':');
+
+    if (parts.length < 2)
+        return false;
+
+    let wpCode = parts[0];
+
+    return wpCode.match(WP_REGEX) !== null;
+}
+
 /*
  * Fetch various metadata about a Wikipedia article, given the wiki language
  * and article title.
diff --git a/tests/meson.build b/tests/meson.build
index 09728da2..3961d73f 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,6 +1,6 @@
 tests = ['addressTest', 'boundingBoxTest', 'colorTest', 'osmNamesTest',
          'placeIconsTest', 'timeTest', 'translationsTest', 'utilsTest',
-         'urlsTest']
+         'urlsTest', 'wikipediaTest']
 
 foreach test : tests
   script_conf = configuration_data()
diff --git a/tests/wikipediaTest.js b/tests/wikipediaTest.js
new file mode 100644
index 00000000..145c3cfa
--- /dev/null
+++ b/tests/wikipediaTest.js
@@ -0,0 +1,44 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2021 Marcus Lundblad
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+const JsUnit = imports.jsUnit;
+
+pkg.require({ 'Gdk':  '3.0',
+              'Gtk':  '3.0',
+              'Soup': '2.4' });
+
+const Wikipedia = imports.wikipedia;
+
+function main() {
+    isValidWikipediaTest();
+}
+
+function isValidWikipediaTest() {
+    // valid references
+    JsUnit.assertTrue(Wikipedia.isValidWikipedia('en:Test article'));
+    JsUnit.assertTrue(Wikipedia.isValidWikipedia('en:Test article:with colon'));
+    JsUnit.assertTrue(Wikipedia.isValidWikipedia('arz:ويكيبيديا مصرى'));
+    JsUnit.assertTrue(Wikipedia.isValidWikipedia('simple:Article'));
+    JsUnit.assertTrue(Wikipedia.isValidWikipedia('zh-yue:粵文維基百科'));
+
+    // invalid references
+    JsUnit.assertFalse(Wikipedia.isValidWikipedia('https://en.wikipedia.org/wiki/Article'));
+    JsUnit.assertFalse(Wikipedia.isValidWikipedia('Article with no edition'));
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]