Make Account Center only usable when logged in
[blerg.git] / www / jssrc / blerg / Util.js
index 51489c9..6e99f46 100644 (file)
@@ -3,9 +3,9 @@ blerg = window.blerg || {};
 blerg.Util = {};
 
 // Dirty, terrible shim for rewritten links below
-blerg.Util.qlink = function() {
+blerg.Util.qlink = function(ev) {
     try {
-        location.href = event.target.href;
+        location.href = ev.target.href;
     } catch(e) { }
     enyo.$.blerg.bubble('onNavigate');
     return false;
@@ -16,7 +16,7 @@ blerg.Util.formatLinkHooks = [
 ];
 
 blerg.Util.formatMarkdownLinks = function(l) {
-        m = l.match(/(\s[()\[\]{}]?|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)(\s([^)]*))?\)/);
+        m = l.match(/(\s[()\[\]{}]?|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?|mailto:[^)"]*?)(\s([^)]*))?\)/);
         if (!m)
             return l;
         var whitespace = m[1];
@@ -46,17 +46,65 @@ blerg.Util.blergFormat = function(text) {
     var out = ['<p>'];
     var endpush = null;
     var listMode = false;
+    var codeMode = false;
+    var githubCodeMode = false;
     lines.forEach(function(l) {
+        // Blank line
         if (l == '') {
+            // Turn single linebreaks into paragraphs
             if (out[out.length - 1] == '<br>') {
                 out[out.length - 1] = '<p>';
             }
+            // End bullet list
             if (out[out.length - 1] == '</li>') {
                 out.push('</ul>');
                 out.push('<p>');
                 listMode = false;
             }
+            // End code mode
+            if (codeMode) {
+                out.push('</pre>');
+                out.push('<p>');
+                codeMode = false;
+            }
+            return;
+        }
+
+        // Sometimes I wish more languages had Perl's bistable .. operator.
+        if (githubCodeMode) {
+            if (l == '```') {
+                out.push('</pre>');
+                out.push('<p>');
+                githubCodeMode = false;
+            } else {
+                // Sanitize HTML input
+                l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+                out.push(l + "\n");
+            }
+            return;
+        } else {
+            if (l == '```') {
+                out.push('<pre>');
+                githubCodeMode = true;
+                return;
+            }
+        }
+
+        // Create a code block when lines begin with at least four spaces
+        if (l.substr(0, 4) == '    ') {
+            if (!codeMode) {
+                out.push('<pre>');
+                codeMode = true;
+            }
+            // Sanitize HTML input
+            l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+            out.push(l.substr(4) + "\n");
             return;
+        } else {
+            if (codeMode) {
+                out.push('</pre>');
+                codeMode = false;
+            }
         }
 
         // Put quoted material into a special paragraph
@@ -81,9 +129,12 @@ blerg.Util.blergFormat = function(text) {
         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 ~~foo~~ into strikethrough
+        l = l.replace(/([^\w\\]|^)~~(\w[^~]*)~~(\W|$)/g, '$1<s>$2</s>$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>');
+        l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return blerg.Util.qlink(event)">#$2</a>');
+        l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return blerg.Util.qlink(event)">@$2</a>');
 
         // Create lists when lines begin with *
         if (l[0] == '*') {
@@ -109,7 +160,7 @@ blerg.Util.blergFormat = function(text) {
         }
 
         // Remove backslashes from escaped metachars
-        l = l.replace(/\\([*\[\]@#])/g, '$1');
+        l = l.replace(/\\([*\[\]@#~])/g, '$1');
 
         out.push(l);
         if (endpush) {
@@ -119,10 +170,15 @@ blerg.Util.blergFormat = function(text) {
             out.push('<br>');
         }
     });
-    while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')
+    while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>') {
         out.pop();
-    if (listMode)
+    }
+    if (listMode) {
         out.push('</ul>');
+    }
+    if (codeMode || githubCodeMode) {
+        out.push('</pre>');
+    }
 
     return out.join('');
 }