[polari] chatView: Move findUrls() to utils



commit c45096cafeb0e2b8a104a859fd9f1ad0a14cf560
Author: Florian Müllner <fmuellner gnome org>
Date:   Sat Oct 12 02:19:10 2013 +0200

    chatView: Move findUrls() to utils
    
    We will use it to identify links in topics.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=709889

 src/chatView.js |   45 ++-------------------------------------------
 src/utils.js    |   43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 43 deletions(-)
---
diff --git a/src/chatView.js b/src/chatView.js
index 9a2af36..b710952 100644
--- a/src/chatView.js
+++ b/src/chatView.js
@@ -6,51 +6,10 @@ const Tp = imports.gi.TelepathyGLib;
 
 const Lang = imports.lang;
 const Notify = imports.notify;
+const Utils = imports.utils;
 
 const MAX_NICK_CHARS = 8;
 
-// http://daringfireball.net/2010/07/improved_regex_for_matching_urls
-const _balancedParens = '\\((?:[^\\s()<>]+|(?:\\(?:[^\\s()<>]+\\)))*\\)';
-const _leadingJunk = '[\\s`(\\[{\'\\"<\u00AB\u201C\u2018]';
-const _notTrailingJunk = '[^\\s`!()\\[\\]{};:\'\\".,<>?\u00AB\u00BB\u201C\u201D\u2018\u2019]';
-
-const _urlRegexp = new RegExp(
-    '(^|' + _leadingJunk + ')' +
-    '(' +
-        '(?:' +
-            '(?:http|https|ftp)://' +             // scheme://
-            '|' +
-            'www\\d{0,3}[.]' +                    // www.
-            '|' +
-            '[a-z0-9.\\-]+[.][a-z]{2,4}/' +       // foo.xx/
-        ')' +
-        '(?:' +                                   // one or more:
-            '[^\\s()<>]+' +                       // run of non-space non-()
-            '|' +                                 // or
-            _balancedParens +                     // balanced parens
-        ')+' +
-        '(?:' +                                   // end with:
-            _balancedParens +                     // balanced parens
-            '|' +                                 // or
-            _notTrailingJunk +                    // last non-junk char
-        ')' +
-    ')', 'gi');
-
-// findUrls:
-// @str: string to find URLs in
-//
-// Searches @str for URLs and returns an array of objects with %url
-// properties showing the matched URL string, and %pos properties indicating
-// the position within @str where the URL was found.
-//
-// Return value: the list of match objects, as described above
-function findUrls(str) {
-    let res = [], match;
-    while ((match = _urlRegexp.exec(str)))
-        res.push({ url: match[2], pos: match.index + match[1].length });
-    return res;
-}
-
 // Workaround for GtkTextView growing horizontally over time when
 // added to a GtkScrolledWindow with horizontal scrolling disabled
 const TextView = new Lang.Class({
@@ -438,7 +397,7 @@ const ChatView = new Lang.Class({
             }
         }
 
-        let urls = findUrls(text);
+        let urls = Utils.findUrls(text);
         let pos = 0;
         for (let i = 0; i < urls.length; i++) {
             let url = urls[i];
diff --git a/src/utils.js b/src/utils.js
index 465c723..75d5d56 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -28,6 +28,34 @@ const Signals = imports.signals;
 
 const FPASTE_BASEURL = 'http://paste.fedoraproject.org/'
 
+// http://daringfireball.net/2010/07/improved_regex_for_matching_urls
+const _balancedParens = '\\((?:[^\\s()<>]+|(?:\\(?:[^\\s()<>]+\\)))*\\)';
+const _leadingJunk = '[\\s`(\\[{\'\\"<\u00AB\u201C\u2018]';
+const _notTrailingJunk = '[^\\s`!()\\[\\]{};:\'\\".,<>?\u00AB\u00BB\u201C\u201D\u2018\u2019]';
+
+const _urlRegexp = new RegExp(
+    '(^|' + _leadingJunk + ')' +
+    '(' +
+        '(?:' +
+            '(?:http|https|ftp)://' +             // scheme://
+            '|' +
+            'www\\d{0,3}[.]' +                    // www.
+            '|' +
+            '[a-z0-9.\\-]+[.][a-z]{2,4}/' +       // foo.xx/
+        ')' +
+        '(?:' +                                   // one or more:
+            '[^\\s()<>]+' +                       // run of non-space non-()
+            '|' +                                 // or
+            _balancedParens +                     // balanced parens
+        ')+' +
+        '(?:' +                                   // end with:
+            _balancedParens +                     // balanced parens
+            '|' +                                 // or
+            _notTrailingJunk +                    // last non-junk char
+        ')' +
+    ')', 'gi');
+
+
 let debugInit = false;
 let debugEnabled = false;
 
@@ -51,6 +79,21 @@ function addJSSignalMethods(proto) {
     proto.disconnectAllJS = Signals._disconnectAll;
 }
 
+// findUrls:
+// @str: string to find URLs in
+//
+// Searches @str for URLs and returns an array of objects with %url
+// properties showing the matched URL string, and %pos properties indicating
+// the position within @str where the URL was found.
+//
+// Return value: the list of match objects, as described above
+function findUrls(str) {
+    let res = [], match;
+    while ((match = _urlRegexp.exec(str)))
+        res.push({ url: match[2], pos: match.index + match[1].length });
+    return res;
+}
+
 function fpaste(text, user, callback) {
     let getUrl = function(session, id) {
         let longUrl = FPASTE_BASEURL + id;


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