More markdown syntax!
[blerg.git] / www / js / blerg.js
index 96f6cb2..9f92594 100644 (file)
@@ -8,10 +8,10 @@ var recordTemplate = new Template(
     '<div class="record">#{data}<div class="info">Posted #{date}</div></div>'
 );
 var tagRecordTemplate = new Template(
-    '<div class="record">#{data}<div class="info">Posted by <a class="author" href="/\##{author}">@#{author}</a> on #{date}</div></div>'
+    '<div class="record">#{data}<div class="info">Posted by <a class="author ref" href="/\##{author}">@#{author}</a> on #{date}</div></div>'
 );
 var latestRecordsTemplate = new Template(
-    '<div class="record"><a class="author" href="/\##{author}">@#{author}</a> #{data}</div>'
+    '<div class="record"><a class="author ref" href="/\##{author}">@#{author}</a> #{data}</div>'
 );
 
 // Page elements
@@ -188,6 +188,28 @@ function User(username) {
     this.initPager();
     this.username = username;
 
+    $$('[name=user.obsesslink]').each(Element.hide);
+    $$('[name=user.unobsesslink]').each(Element.hide);
+
+    if (loginStatus.loggedIn) {
+        new Ajax.Request(baseURL + '/feedinfo/' + username, {
+            method: 'post',
+            parameters: {
+                username: loginStatus.username
+            },
+            onSuccess: function(r) {
+                var json = r.responseText.evalJSON();
+                if (json.subscribed) {
+                    $$('[name=user.obsesslink]').each(Element.hide);
+                    $$('[name=user.unobsesslink]').each(Element.show);
+                } else {
+                    $$('[name=user.obsesslink]').each(Element.show);
+                    $$('[name=user.unobsesslink]').each(Element.hide);
+                }
+            }
+        });
+    }
+
     new Ajax.Request(baseURL + '/info/' + username, {
         method: 'get',
         onSuccess: function(r) {
@@ -208,6 +230,10 @@ User.prototype.show = function() {
     items.show();
     $('rss').show();
     $('rsslink').href = '/rss/' + this.username;
+    $$('[name=user.reflink]').each(function(e) {
+        e.href = '/#ref/' + this.username;
+    }.bind(this));
+    $('usercontrols').show();
 }
 
 User.prototype.loadItems = function(from, to) {
@@ -246,26 +272,82 @@ User.prototype.loadItems = function(from, to) {
 function mangleRecord(record, template) {
     record.recInt = parseInt(record.record);
 
-    // Sanitize HTML input
-    record.data = record.data.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+    var lines = record.data.split(/\r?\n/);
+    if (lines[lines.length - 1] == '')
+        lines.pop();
+
+    var out = ['<p>'];
+    var endpush = null;
+    var listMode = false;
+    lines.each(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;
+        }
 
-    // Turn HTTP URLs into links
-    record.data = record.data.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
+        if (l[0] == '>') {
+            var pi = out.lastIndexOf('<p>');
+            if (pi != -1) {
+                out[pi] = '<p class="quote">';
+                l = l.replace(/^>\s*/, '');
+            }
+        }
+        if (l[0] == '*') {
+            if (!listMode) {
+                var pi = out.lastIndexOf('<p>');
+                out[pi] = '<ul>';
+                listMode = true;
+            }
+            l = l.replace(/^\*\s*/, '');
+            out.push('<li>');
+            endpush = '</li>';
+        }
+        if (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 + '>';
+            }
+        }
+
+        // Sanitize HTML input
+        l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
 
-    // Turn markdown links into links
-    record.data = record.data.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
+        // 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 *foo* into italics and **foo** into bold
-    record.data = record.data.replace(/(\s)\*\*([^*]+)\*\*(\s)/g, '$1<b>$2</b>$3');
-    record.data = record.data.replace(/(\s)\*([^*]+)\*(\s)/g, '$1<i>$2</i>$3');
+        // 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 refs and tags into links
-    record.data = record.data.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#tag/$2">#$2</a>');
-    record.data = record.data.replace(/(\s|^)@([A-Za-z0-9_-]+)/g, '$1<a href="#$2">@$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 newlines into linebreaks and paragraphs
-    record.data = record.data.replace(/\r?\n\r?\n/g, "<p>").replace(/\r?\n/g, "<br>");
+        // Turn refs and tags into links
+        l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#tag/$2" class="ref">#$2</a>');
+        l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)/g, '$1<a href="#$2" class="ref">@$2</a>');
 
+        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();
+
+    record.data = out.join('');
     record.date = (new Date(record.timestamp * 1000)).toString();
     record.html = template.evaluate(record);
 }
@@ -332,6 +414,7 @@ Tag.prototype.show = function() {
     $('newer_link').hide();
     $('rss').hide();
     items.show();
+    $('usercontrols').hide();
 }
 
 function postPopup() {
@@ -386,6 +469,50 @@ function older_page() {
         currentPager.olderPage();
 }
 
+function obsess() {
+    new Ajax.Request(baseURL + '/subscribe/' + currentPager.username, {
+        method: 'post',
+        parameters: {
+            username: loginStatus.username
+        },
+        onSuccess: function(r) {
+            var response = r.responseText.evalJSON();
+            if (response.status == 'success') {
+                alert("You call " + currentPager.username + " and begin breathing heavily into the handset.");
+                $$('[name=user.obsesslink]').each(Element.hide);
+                $$('[name=user.unobsesslink]').each(Element.show);
+            } else {
+                alert('Failed to obsess. This is probably for the best');
+            }
+        },
+        onFailure: function(r) {
+            alert('Failed to obsess. This is probably for the best');
+        }
+    });
+}
+
+function unobsess() {
+    new Ajax.Request(baseURL + '/unsubscribe/' + currentPager.username, {
+        method: 'post',
+        parameters: {
+            username: loginStatus.username
+        },
+        onSuccess: function(r) {
+            var response = r.responseText.evalJSON();
+            if (response.status == 'success') {
+                alert("You come to your senses.");
+                $$('[name=user.obsesslink]').each(Element.show);
+                $$('[name=user.unobsesslink]').each(Element.hide);
+            } else {
+                alert('You are unable to tear yourself away (because something failed on the server)');
+            }
+        },
+        onFailure: function(r) {
+            alert('You are unable to tear yourself away (because something failed on the server)');
+        }
+    });
+}
+
 var resizePostContentTimeout = null;
 function resizePostContent() {
     if (resizePostContentTimeout)
@@ -420,9 +547,6 @@ function tickerFader(a, b, p) {
         b.hide();
 }
 
-function fadeOut(p) {
-}
-
 function ticker() {
     tickerHead.show();
     Bytex64.FX.run(tickerFader.curry(tickerHead, tickerTail), 0.5);
@@ -442,7 +566,7 @@ function startTicker() {
 
     // Show the first five
     tickerHead = $('latest-posts').firstChild;
-    for (var i = 0; i < 5 && tickerHead; i++) {
+    for (var i = 0; i < 10 && tickerHead; i++) {
         tickerHead.show();
         tickerHead = tickerHead.nextSibling;
     }
@@ -465,6 +589,7 @@ function loadLatest() {
             j.tags.each(function(v) {
                 var a = new Element('a', {href: '/#tag/' + v});
                 a.insert('#' + v);
+                a.className = 'ref';
                 $('latest-tags').insert(a);
                 $('latest-tags').appendChild(document.createTextNode(' '));
             });
@@ -499,7 +624,7 @@ function hashSwitch() {
         if (m[3]) {
             var r = parseInt(m[3]);
             currentPager.showRecord = r;
-            if (currentPager.recordCache[r]) {
+            if (currentPager.itemCache[r]) {
                 currentPager.displayItems();
             } else {
                 currentPager.loadItems((r >= 49 ? r - 49 : 0), r);
@@ -517,6 +642,7 @@ function hashSwitch() {
         $('older_link').hide();
         $('welcome').show();
         $('rss').hide();
+        $('usercontrols').hide();
         loadLatest();
     }
 }