More markdown syntax!
authorChip Black <bytex64@bytex64.net>
Mon, 28 Feb 2011 11:59:00 +0000 (03:59 -0800)
committerChip Black <bytex64@bytex64.net>
Mon, 28 Feb 2011 11:59:00 +0000 (03:59 -0800)
www/css/blerg.css
www/js/blerg.js

index 5c33f5e..6fe2054 100644 (file)
@@ -165,3 +165,8 @@ h1, h2, h3 {
 #usercontrols {
        font-weight: bold;
 }
+
+.quote {
+       border-left: 2px solid blue;
+       padding-left: 6px;
+}
index 4efad6a..9f92594 100644 (file)
@@ -272,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;
+        }
+
+        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 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>');
+        // 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
-    record.data = record.data.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$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
-    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 *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
-    record.data = record.data.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#tag/$2" class="ref">#$2</a>');
-    record.data = record.data.replace(/(\s|^)@([A-Za-z0-9_-]+)/g, '$1<a href="#$2" class="ref">@$2</a>');
+        // 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>');
 
-    // Turn newlines into linebreaks and paragraphs
-    record.data = record.data.replace(/\r?\n\r?\n/g, "<p>").replace(/\r?\n/g, "<br>");
+        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);
 }
@@ -568,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);