Add User, port simplified paging
[blerg.git] / www / jssrc / blerg / Util.js
1 blerg = window.blerg || {};
2
3 blerg.Util = {};
4
5 blerg.Util.blergFormat = function(text) {
6         var lines = text.split(/\r?\n/);
7         if (lines[lines.length - 1] == '')
8                 lines.pop();
9
10         var out = ['<p>'];
11         var endpush = null;
12         var listMode = false;
13         lines.forEach(function(l) {
14                 if (l == '') {
15                         if (out[out.length - 1] == '<br>') {
16                                 out[out.length - 1] = '<p>';
17                         }
18                         if (out[out.length - 1] == '</li>') {
19                                 out.push('</ul>');
20                                 out.push('<p>');
21                                 listMode = false;
22                         }
23                         return;
24                 }
25
26                 // Put quoted material into a special paragraph
27                 if (l[0] == '>') {
28                         var pi = out.lastIndexOf('<p>');
29                         if (pi != -1) {
30                                 out[pi] = '<p class="quote">';
31                                 l = l.replace(/^>\s*/, '');
32                         }
33                 }
34
35                 // Sanitize HTML input
36                 l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
37
38                 // Turn HTTP URLs into links
39                 l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
40
41                 // Turn markdown links into links
42                 var re;
43
44                 /*
45                 // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.audioExtensions.
46                 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.audioExtensions.join('|') + '))\\)', 'g');
47                 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
48
49                 // Ditto, but use the extended markdown link syntax to specify the format
50                 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+audio:(' + BlergMedia.audioExtensions.join('|') + ')\\)', 'g');
51                 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
52
53                 // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.videoExtensions.
54                 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.videoExtensions.join('|') + '))\\)', 'g');
55                 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
56
57                 // Ditto, but use the extended markdown link syntax to specify the format
58                 re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+video:(' + BlergMedia.videoExtensions.join('|') + ')\\)', 'g');
59                 l = l.replace(re, '$1<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
60                 */
61
62                 // Regular markdown links
63                 l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
64
65                 // Turn *foo* into italics and **foo** into bold
66                 l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
67                 l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
68
69                 // Turn refs and tags into links
70                 l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return qlink()">#$2</a>');
71                 l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return qlink()">@$2</a>');
72
73                 // Create lists when lines begin with *
74                 if (l[0] == '*') {
75                         if (!listMode) {
76                                 var pi = out.lastIndexOf('<p>');
77                                 out[pi] = '<ul>';
78                                 listMode = true;
79                         }
80                         l = l.replace(/^\*\s*/, '');
81                         out.push('<li>');
82                         endpush = '</li>';
83                 }
84
85                 // Create headers when lines begin with = or #
86                 if (l[0] == '=' || l[0] == '#') {
87                         var m = l.match(/^([=#]+)/);
88                         var depth = m[1].length;
89                         if (depth <= 5) {
90                                 l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
91                                 out.push('<h' + depth + '>');
92                                 endpush = '</h' + depth + '>';
93                         }
94                 }
95
96                 // Remove backslashes from escaped metachars
97                 l = l.replace(/\\([*\[\]@#])/g, '$1');
98
99                 out.push(l);
100                 if (endpush) {
101                         out.push(endpush);
102                         endpush = null;
103                 } else {
104                         out.push('<br>');
105                 }
106         });
107         while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')
108                 out.pop();
109         if (listMode)
110                 out.push('</ul>');
111
112         return out.join('');
113 }