Fix qlink(), rearrange mangleRecord(), fix some style
[blerg.git] / www / js / blerg.js
index 9b1db74..7203f09 100644 (file)
@@ -307,6 +307,7 @@ function mangleRecord(record, template) {
             return;
         }
 
+        // Put quoted material into a special paragraph
         if (l[0] == '>') {
             var pi = out.lastIndexOf('<p>');
             if (pi != -1) {
@@ -314,6 +315,25 @@ function mangleRecord(record, template) {
                 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
+        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|$)/g, '$1<b>$2</b>$3');
+        l = l.replace(/(\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_-]+)/g, '$1<a href="#$2" class="ref" onclick="return qlink()">@$2</a>');
+
+        // Create lists when lines begin with *
         if (l[0] == '*') {
             if (!listMode) {
                 var pi = out.lastIndexOf('<p>');
@@ -324,6 +344,8 @@ function mangleRecord(record, template) {
             out.push('<li>');
             endpush = '</li>';
         }
+
+        // Create headers when lines begin with =
         if (l[0] == '=') {
             var m = l.match(/^(=+)/);
             var depth = m[1].length;
@@ -334,23 +356,6 @@ function mangleRecord(record, template) {
             }
         }
 
-        // 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
-        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(/(\s)\*\*([^*]+)\*\*(\s)/g, '$1<b>$2</b>$3');
-        l = l.replace(/(\s)\*([^*]+)\*(\s)/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_-]+)/g, '$1<a href="#$2" class="ref" onclick="return qlink()">@$2</a>');
-
         out.push(l);
         if (endpush) {
             out.push(endpush);
@@ -361,6 +366,8 @@ function mangleRecord(record, template) {
     });
     while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')
         out.pop();
+    if (listMode)
+        out.push('</ul>');
 
     record.data = out.join('');
     record.date = (new Date(record.timestamp * 1000)).toString();
@@ -662,8 +669,8 @@ function loadLatest() {
 function qlink(loc) {
     if (loc) {
         location.hash = loc;
-    } else if (this.href) {
-        location.hash = this.href;
+    } else if (event && event.target) {
+        location.href = event.target.href;
     } else {
         // Bogus qlink
         return;
@@ -737,7 +744,7 @@ function init() {
     setInterval(hashCheck, 250);
 
     document.body.observe('keyup', function(event) {
-        if (event.shiftKey && event.keyCode == '32') {
+        if (event.shiftKey && event.keyCode == 32) {
             postPopup();
             event.stop();
         }