Add Tag handler. Also convert all tabs to spaces.
[blerg.git] / www / jssrc / blerg / Util.js
index 1bfab4c..d47770e 100644 (file)
@@ -2,112 +2,121 @@ blerg = window.blerg || {};
 
 blerg.Util = {};
 
+// Dirty, terrible shim for rewritten links below
+blerg.Util.qlink = function() {
+    try {
+        location.href = event.target.href;
+    } catch(e) { }
+    enyo.$.blerg.bubble('onNavigate');
+    return false;
+}
+
 blerg.Util.blergFormat = function(text) {
-       var lines = text.split(/\r?\n/);
-       if (lines[lines.length - 1] == '')
-               lines.pop();
-
-       var out = ['<p>'];
-       var endpush = null;
-       var listMode = false;
-       lines.forEach(function(l) {
-               if (l == '') {
-                       if (out[out.length - 1] == '<br>') {
-                               out[out.length - 1] = '<p>';
-                       }
-                       if (out[out.length - 1] == '</li>') {
-                               out.push('</ul>');
-                               out.push('<p>');
-                               listMode = false;
-                       }
-                       return;
-               }
-
-               // Put quoted material into a special paragraph
-               if (l[0] == '>') {
-                       var pi = out.lastIndexOf('<p>');
-                       if (pi != -1) {
-                               out[pi] = '<p class="quote">';
-                               l = l.replace(/^>\s*/, '');
-                       }
-               }
-
-               // Sanitize HTML input
-               l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
-
-               // Turn HTTP URLs into links
-               l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
-
-               // Turn markdown links into links
-               var re;
-
-               /*
-               // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.audioExtensions.
-               re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.audioExtensions.join('|') + '))\\)', 'g');
-               l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
-
-               // Ditto, but use the extended markdown link syntax to specify the format
-               re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+audio:(' + BlergMedia.audioExtensions.join('|') + ')\\)', 'g');
-               l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
-
-               // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.videoExtensions.
-               re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.videoExtensions.join('|') + '))\\)', 'g');
-               l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
-
-               // Ditto, but use the extended markdown link syntax to specify the format
-               re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+video:(' + BlergMedia.videoExtensions.join('|') + ')\\)', 'g');
-               l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
-               */
-
-               // Regular markdown links
-               l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
-
-               // Turn *foo* into italics and **foo** into bold
-               l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
-               l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
-
-               // Turn refs and tags into links
-               l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return qlink()">#$2</a>');
-               l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return qlink()">@$2</a>');
-
-               // Create lists when lines begin with *
-               if (l[0] == '*') {
-                       if (!listMode) {
-                               var pi = out.lastIndexOf('<p>');
-                               out[pi] = '<ul>';
-                               listMode = true;
-                       }
-                       l = l.replace(/^\*\s*/, '');
-                       out.push('<li>');
-                       endpush = '</li>';
-               }
-
-               // Create headers when lines begin with = or #
-               if (l[0] == '=' || l[0] == '#') {
-                       var m = l.match(/^([=#]+)/);
-                       var depth = m[1].length;
-                       if (depth <= 5) {
-                               l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
-                               out.push('<h' + depth + '>');
-                               endpush = '</h' + depth + '>';
-                       }
-               }
-
-               // Remove backslashes from escaped metachars
-               l = l.replace(/\\([*\[\]@#])/g, '$1');
-
-               out.push(l);
-               if (endpush) {
-                       out.push(endpush);
-                       endpush = null;
-               } else {
-                       out.push('<br>');
-               }
-       });
-       while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')
-               out.pop();
-       if (listMode)
-               out.push('</ul>');
-
-       return out.join('');
+    var lines = text.split(/\r?\n/);
+    if (lines[lines.length - 1] == '')
+        lines.pop();
+
+    var out = ['<p>'];
+    var endpush = null;
+    var listMode = false;
+    lines.forEach(function(l) {
+        if (l == '') {
+            if (out[out.length - 1] == '<br>') {
+                out[out.length - 1] = '<p>';
+            }
+            if (out[out.length - 1] == '</li>') {
+                out.push('</ul>');
+                out.push('<p>');
+                listMode = false;
+            }
+            return;
+        }
+
+        // Put quoted material into a special paragraph
+        if (l[0] == '>') {
+            var pi = out.lastIndexOf('<p>');
+            if (pi != -1) {
+                out[pi] = '<p class="quote">';
+                l = l.replace(/^>\s*/, '');
+            }
+        }
+
+        // Sanitize HTML input
+        l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+
+        // Turn HTTP URLs into links
+        l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
+
+        // Turn markdown links into links
+        var re;
+
+        /*
+        // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.audioExtensions.
+        re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.audioExtensions.join('|') + '))\\)', 'g');
+        l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
+
+        // Ditto, but use the extended markdown link syntax to specify the format
+        re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+audio:(' + BlergMedia.audioExtensions.join('|') + ')\\)', 'g');
+        l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
+
+        // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.videoExtensions.
+        re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.videoExtensions.join('|') + '))\\)', 'g');
+        l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
+
+        // Ditto, but use the extended markdown link syntax to specify the format
+        re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+video:(' + BlergMedia.videoExtensions.join('|') + ')\\)', 'g');
+        l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
+        */
+
+        // Regular markdown links
+        l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
+
+        // Turn *foo* into italics and **foo** into bold
+        l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
+        l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
+
+        // Turn refs and tags into links
+        l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return blerg.Util.qlink()">#$2</a>');
+        l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return blerg.Util.qlink()">@$2</a>');
+
+        // Create lists when lines begin with *
+        if (l[0] == '*') {
+            if (!listMode) {
+                var pi = out.lastIndexOf('<p>');
+                out[pi] = '<ul>';
+                listMode = true;
+            }
+            l = l.replace(/^\*\s*/, '');
+            out.push('<li>');
+            endpush = '</li>';
+        }
+
+        // Create headers when lines begin with = or #
+        if (l[0] == '=' || l[0] == '#') {
+            var m = l.match(/^([=#]+)/);
+            var depth = m[1].length;
+            if (depth <= 5) {
+                l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
+                out.push('<h' + depth + '>');
+                endpush = '</h' + depth + '>';
+            }
+        }
+
+        // Remove backslashes from escaped metachars
+        l = l.replace(/\\([*\[\]@#])/g, '$1');
+
+        out.push(l);
+        if (endpush) {
+            out.push(endpush);
+            endpush = null;
+        } else {
+            out.push('<br>');
+        }
+    });
+    while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')
+        out.pop();
+    if (listMode)
+        out.push('</ul>');
+
+    return out.join('');
 }