X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjssrc%2Fblerg%2FUtil.js;h=6e99f46a5b2c0104f4143dbd72a4a0da8724fb4f;hb=4210d2d20df6635e19c20f5b303d498f4657968b;hp=b3f5369c8bccdeea7ddf4aafd8153441cf57c751;hpb=65a1f7dc82554486ffa9cc03eac29a7a1642f046;p=blerg.git diff --git a/www/jssrc/blerg/Util.js b/www/jssrc/blerg/Util.js index b3f5369..6e99f46 100644 --- a/www/jssrc/blerg/Util.js +++ b/www/jssrc/blerg/Util.js @@ -3,120 +3,182 @@ blerg = window.blerg || {}; blerg.Util = {}; // Dirty, terrible shim for rewritten links below -blerg.Util.qlink = function() { - try { - location.href = event.target.href; - } catch(e) { } - enyo.$.blerg.bubble('onNavigate'); - return false; +blerg.Util.qlink = function(ev) { + try { + location.href = ev.target.href; + } catch(e) { } + enyo.$.blerg.bubble('onNavigate'); + return false; +} + +blerg.Util.formatLinkHooks = [ + BlergMedia +]; + +blerg.Util.formatMarkdownLinks = function(l) { + m = l.match(/(\s[()\[\]{}]?|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?|mailto:[^)"]*?)(\s([^)]*))?\)/); + if (!m) + return l; + var whitespace = m[1]; + var data = { + label: m[2], + url: m[3], + extra: m[5] + }; + + for (var i = 0; i < blerg.Util.formatLinkHooks.length; i++) { + var newdata = blerg.Util.formatLinkHooks[i].process(data); + if (newdata) + data = newdata; + else + console.warn("Format Link Hook " + i + " returned null"); + } + var s = whitespace + '' + data.label + '' + + (data.widget ? ' ' + data.widget : ''); + return l.slice(0, m.index) + s + blerg.Util.formatMarkdownLinks(l.slice(m.index + m[0].length)); } blerg.Util.blergFormat = function(text) { - var lines = text.split(/\r?\n/); - if (lines[lines.length - 1] == '') - lines.pop(); - - var out = ['

']; - var endpush = null; - var listMode = false; - lines.forEach(function(l) { - if (l == '') { - if (out[out.length - 1] == '
') { - out[out.length - 1] = '

'; - } - if (out[out.length - 1] == '') { - out.push(''); - out.push('

'); - listMode = false; - } - return; - } - - // Put quoted material into a special paragraph - if (l[0] == '>') { - var pi = out.lastIndexOf('

'); - if (pi != -1) { - out[pi] = '

'; - l = l.replace(/^>\s*/, ''); - } - } - - // Sanitize HTML input - l = l.replace(/&/g, '&').replace(//g, '>'); - - // Turn HTTP URLs into links - l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1$2'); - - // Turn markdown links into links - var re; - - /* - // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.audioExtensions. - re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.audioExtensions.join('|') + '))\\)', 'g'); - l = l.replace(re, '$1$2 '); - - // Ditto, but use the extended markdown link syntax to specify the format - re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+audio:(' + BlergMedia.audioExtensions.join('|') + ')\\)', 'g'); - l = l.replace(re, '$1$2 '); - - // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.videoExtensions. - re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.videoExtensions.join('|') + '))\\)', 'g'); - l = l.replace(re, '$1$2 '); - - // Ditto, but use the extended markdown link syntax to specify the format - re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+video:(' + BlergMedia.videoExtensions.join('|') + ')\\)', 'g'); - l = l.replace(re, '$1$2 '); - */ - - // Regular markdown links - l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1$2'); - - // Turn *foo* into italics and **foo** into bold - l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1$2$3'); - 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'); - - // Create lists when lines begin with * - if (l[0] == '*') { - if (!listMode) { - var pi = out.lastIndexOf('

'); - out[pi] = '

'); - - return out.join(''); + var lines = text.split(/\r?\n/); + if (lines[lines.length - 1] == '') + lines.pop(); + + 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 { + // Sanitize HTML input + l = l.replace(/&/g, '&').replace(//g, '>'); + 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;
+            }
+            // Sanitize HTML input
+            l = l.replace(/&/g, '&').replace(//g, '>');
+            out.push(l.substr(4) + "\n");
+            return;
+        } else {
+            if (codeMode) {
+                out.push('
'); + codeMode = false; + } + } + + // Put quoted material into a special paragraph + if (l[0] == '>') { + var pi = out.lastIndexOf('

'); + if (pi != -1) { + out[pi] = '

'; + l = l.replace(/^>\s*/, ''); + } + } + + // Sanitize HTML input + l = l.replace(/&/g, '&').replace(//g, '>'); + + // Turn HTTP URLs into links + l = l.replace(/(\s[()\[\]{}]?|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1$2'); + + // Turn markdown links into links + l = blerg.Util.formatMarkdownLinks(l); + + // Turn *foo* into italics and **foo** into bold + 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'); + + // Create lists when lines begin with * + if (l[0] == '*') { + if (!listMode) { + var pi = out.lastIndexOf('

'); + out[pi] = '

'); + } + if (codeMode || githubCodeMode) { + out.push('
'); + } + + return out.join(''); }