X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjssrc%2Fblerg%2FUtil.js;fp=www%2Fjssrc%2Fblerg%2FUtil.js;h=8706f8140c78ef44ed23b41dad2db647597ab95b;hb=17bef4d5d8ed21748bbcd439d47d9b43d28b3501;hp=18f31694f4cc6e0165f707db393209abb9875ab5;hpb=b1acd335f975b9553e68cb66c81eef117821d296;p=blerg.git diff --git a/www/jssrc/blerg/Util.js b/www/jssrc/blerg/Util.js index 18f3169..8706f81 100644 --- a/www/jssrc/blerg/Util.js +++ b/www/jssrc/blerg/Util.js @@ -46,17 +46,61 @@ blerg.Util.blergFormat = function(text) { var out = ['

']; 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] == '
') { out[out.length - 1] = '

'; } + // End bullet list if (out[out.length - 1] == '') { out.push(''); out.push('

'); listMode = false; } + // End code mode + if (codeMode) { + out.push(''); + out.push('

'); + codeMode = false; + } + return; + } + + // Sometimes I wish more languages had Perl's bistable .. operator. + if (githubCodeMode) { + if (l == '```') { + out.push(''); + out.push('

'); + githubCodeMode = false; + } else { + out.push(l + "\n"); + } + return; + } else { + if (l == '```') { + out.push('

');
+                githubCodeMode = true;
+                return;
+            }
+        }
+
+        // Create a code block when lines begin with at least four spaces
+        if (l.substr(0, 4) == '    ') {
+            if (!codeMode) {
+                out.push('
');
+                codeMode = true;
+            }
+            out.push(l.substr(4) + "\n");
             return;
+        } else {
+            if (codeMode) {
+                out.push('
'); + codeMode = false; + } } // Put quoted material into a special paragraph @@ -81,6 +125,9 @@ blerg.Util.blergFormat = function(text) { l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1$2$3'); l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1$2$3'); + // Turn ~~foo~~ into strikethrough + l = l.replace(/([^\w\\]|^)~~(\w[^~]*)~~(\W|$)/g, '$1$2$3'); + // Turn refs and tags into links l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1#$2'); l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1@$2'); @@ -109,7 +156,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 +166,15 @@ blerg.Util.blergFormat = function(text) { out.push('
'); } }); - while (out[out.length - 1] == '
' || out[out.length - 1] == '

') + while (out[out.length - 1] == '
' || out[out.length - 1] == '

') { out.pop(); - if (listMode) + } + if (listMode) { out.push(''); + } + if (codeMode || githubCodeMode) { + out.push('

'); + } return out.join(''); }