Rewrite docs
[blerg.git] / www / js / blerg.js
index 7bb5fe2..dcb5679 100644 (file)
@@ -1,10 +1,14 @@
+/* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
+ * BSD-style license.  Please see the COPYING file for details.
+ */
+
 // Config
 var baseURL = '';
 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 href="\##{author}">@#{author}</a> on #{date}</div></div>'
+    '<div class="record">#{data}<div class="info">Posted by <a href="/\##{author}">@#{author}</a> on #{date}</div></div>'
 );
 
 // Page elements
@@ -35,7 +39,7 @@ LoginStatus.prototype.login = function(username, password) {
     new Ajax.Request(baseURL + '/login', {
         parameters: {
             username: username,
-            password: password,
+            password: password
         },
         onSuccess: function(r) {
             var j = r.responseText.evalJSON();
@@ -60,20 +64,20 @@ LoginStatus.prototype.login = function(username, password) {
 LoginStatus.prototype.logout = function() {
     new Ajax.Request(baseURL + '/logout', {
         parameters: {
-            username: this.username,
+            username: this.username
         },
         onSuccess: function(r) {
             this.loggedIn = false;
             document.cookie = "auth=; expires=1-Jan-1970 00:00:00 GMT";
             this.update();
-        }.bind(this),
+        }.bind(this)
     });
     document.cookie = "username=; expires=1-Jan-1970 00:00:00 GMT";
 }
 
 LoginStatus.prototype.update = function() {
     if (this.loggedIn) {
-        $('userlink').href = '#' + this.username;
+        $('userlink').href = '/#' + this.username;
         $('userlink').update('@' + this.username);
         $('login').hide();
         $('logout').show();
@@ -100,7 +104,7 @@ LoginStatus.prototype.post = function(msg) {
             if (j && j.status == 'success') {
                 $('post.content').value = '';
                 if (location.hash != '#' + this.username) {
-                    location.hash = this.username;
+                    location.href = '/#' + this.username;
                     hashSwitch();
                 } else {
                     currentPager.itemCount++;
@@ -188,7 +192,7 @@ function User(username) {
                 this.itemCount = parseInt(j.record_count);
                 this.displayItems();
             }
-        }.bind(this),
+        }.bind(this)
     });
 }
 User.prototype = new Pager();
@@ -198,7 +202,7 @@ User.prototype.show = function() {
     $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this));
     $('welcome').hide();
     items.show();
-    $('reflink').href = '#ref/' + this.username;
+    $('reflink').href = '/#ref/' + this.username;
     $('rss').show();
     $('rsslink').href = '/rss/' + this.username;
 }
@@ -243,10 +247,14 @@ function mangleRecord(record, template) {
     record.data = record.data.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt');
 
     // 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>');
+    record.data = record.data.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
-    record.data = record.data.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)]*?)?)\)/, '$1<a href="$3">$2</a>');
+    record.data = record.data.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/, '$1<a href="$3">$2</a>');
+
+    // Turn *foo* into italics and **foo** into bold
+    record.data = record.data.replace(/(\s)\*\*([^*]+)\*\*(\s)/, '$1<b>$2</b>$3');
+    record.data = record.data.replace(/(\s)\*([^*]+)\*(\s)/, '$1<i>$2</i>$3');
 
     // Turn refs and tags into links
     record.data = record.data.replace(/(\s|^)#(\w+)/g, '$1<a href="#tag/$2">#$2</a>');
@@ -345,11 +353,11 @@ function signup() {
     new Ajax.Request(baseURL + '/create', {
         parameters: {
             username: username,
-            password: password,
+            password: password
         },
         onSuccess: function(r) {
             $('signup').hide();
-            location.hash = username;
+            location.href = '/#' + username;
             hashSwitch();
 
             loginStatus.login(username, password);
@@ -395,29 +403,30 @@ function resizePostContent() {
 }
 
 function hashSwitch() {
-    var m = location.hash.match(/^#((ref|tag)\/)?(\w+)(:(\d+))?/);
-    if (m) {
-        if (m[1]) {
-            currentPager = new Tag(m[2], m[3]);
-            currentPager.show();
-        } else {
-            if (!currentPager || currentPager.username != m[3])
-                currentPager = new User(m[3]);
-            currentPager.show();
-            loginStatus.update();
-
-            if (m[5]) {
-                var r = parseInt(m[5]);
-                currentPager.showRecord = r;
-                if (currentPager.recordCache[r]) {
-                    currentPager.displayItems();
-                } else {
-                    currentPager.loadItems((r >= 49 ? r - 49 : 0), r);
-                }
+    var m;
+    if (m = location.search.match(/^\?post\/([^/]+)\/(.+)/)) {
+        $('post').show();
+        $('post.content').value = '[' + decodeURIComponent(m[1]).replace(']','').replace('[','') + '](' + decodeURIComponent(m[2]) + ')';
+    } else if (m = location.hash.match(/^#(ref|tag)\/(\w+)$/)) {
+        currentPager = new Tag(m[1], m[2]);
+        currentPager.show();
+    } else if (m = location.hash.match(/^#(\w+)(:(\d+))?$/)) {
+        if (!currentPager || currentPager.username != m[1])
+            currentPager = new User(m[1]);
+        currentPager.show();
+        loginStatus.update();
+
+        if (m[3]) {
+            var r = parseInt(m[3]);
+            currentPager.showRecord = r;
+            if (currentPager.recordCache[r]) {
+                currentPager.displayItems();
             } else {
-                currentPager.pageStart = currentPager.itemCount - 1;
-                currentPager.loadItems();
+                currentPager.loadItems((r >= 49 ? r - 49 : 0), r);
             }
+        } else {
+            currentPager.pageStart = currentPager.itemCount - 1;
+            currentPager.loadItems();
         }
     } else {
         $$('[name=section]').each(function(v) { v.update('Welcome') });
@@ -454,4 +463,7 @@ function init() {
             event.stop();
         }
     });
+    $('post.content').addEventListener('keyup', function(event) {
+        event.stopPropagation();
+    }, true);
 }