mercury-rails 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. data/README.rdoc +81 -50
  2. data/VERSION +1 -1
  3. data/app/assets/javascripts/mercury/dialog.js.coffee +4 -4
  4. data/app/assets/javascripts/mercury/dialogs/backcolor.js.coffee +3 -3
  5. data/app/assets/javascripts/mercury/dialogs/forecolor.js.coffee +3 -3
  6. data/app/assets/javascripts/mercury/dialogs/formatblock.js.coffee +1 -1
  7. data/app/assets/javascripts/mercury/dialogs/objectspanel.js.coffee +3 -3
  8. data/app/assets/javascripts/mercury/dialogs/style.js.coffee +1 -1
  9. data/app/assets/javascripts/mercury/history_buffer.js.coffee +2 -2
  10. data/app/assets/javascripts/mercury/mercury.js.coffee +47 -54
  11. data/app/assets/javascripts/mercury/modal.js.coffee +13 -13
  12. data/app/assets/javascripts/mercury/modals/htmleditor.js.coffee +2 -2
  13. data/app/assets/javascripts/mercury/modals/insertcharacter.js.coffee +2 -2
  14. data/app/assets/javascripts/mercury/modals/insertlink.js.coffee +7 -7
  15. data/app/assets/javascripts/mercury/modals/insertmedia.js.coffee +5 -5
  16. data/app/assets/javascripts/mercury/modals/insertsnippet.js.coffee +2 -2
  17. data/app/assets/javascripts/mercury/modals/inserttable.js.coffee +14 -15
  18. data/app/assets/javascripts/mercury/native_extensions.js.coffee +2 -1
  19. data/app/assets/javascripts/mercury/page_editor.js.coffee +27 -27
  20. data/app/assets/javascripts/mercury/palette.js.coffee +6 -6
  21. data/app/assets/javascripts/mercury/panel.js.coffee +4 -4
  22. data/app/assets/javascripts/mercury/region.js.coffee +9 -9
  23. data/app/assets/javascripts/mercury/regions/editable.js.coffee +71 -71
  24. data/app/assets/javascripts/mercury/regions/markupable.js.coffee +51 -42
  25. data/app/assets/javascripts/mercury/regions/snippetable.js.coffee +7 -8
  26. data/app/assets/javascripts/mercury/select.js.coffee +8 -8
  27. data/app/assets/javascripts/mercury/snippet.js.coffee +6 -6
  28. data/app/assets/javascripts/mercury/snippet_toolbar.js.coffee +7 -7
  29. data/app/assets/javascripts/mercury/statusbar.js.coffee +7 -3
  30. data/app/assets/javascripts/mercury/table_editor.js.coffee +24 -24
  31. data/app/assets/javascripts/mercury/toolbar.button.js.coffee +20 -21
  32. data/app/assets/javascripts/mercury/toolbar.button_group.js.coffee +2 -2
  33. data/app/assets/javascripts/mercury/toolbar.expander.js.coffee +9 -9
  34. data/app/assets/javascripts/mercury/toolbar.js.coffee +12 -13
  35. data/app/assets/javascripts/mercury/tooltip.js.coffee +7 -7
  36. data/app/assets/javascripts/mercury/uploader.js.coffee +12 -13
  37. data/app/assets/javascripts/mercury_loader.js +98 -0
  38. data/app/assets/stylesheets/mercury/modal.scss +5 -5
  39. data/app/assets/stylesheets/mercury/toolbar.scss +62 -64
  40. data/app/views/layouts/mercury.html.haml +0 -4
  41. data/app/views/mercury/modals/character.html.haml +1 -1
  42. data/app/views/mercury/modals/htmleditor.html.haml +1 -1
  43. data/app/views/mercury/modals/table.html.haml +10 -10
  44. data/config/routes.rb +2 -2
  45. data/mercury-rails.gemspec +17 -20
  46. data/spec/javascripts/mercury/dialogs/backcolor_spec.js.coffee +3 -3
  47. data/spec/javascripts/mercury/dialogs/forecolor_spec.js.coffee +3 -3
  48. data/spec/javascripts/mercury/dialogs/objectspanel_spec.js.coffee +3 -3
  49. data/spec/javascripts/mercury/modals/htmleditor_spec.js.coffee +4 -4
  50. data/spec/javascripts/mercury/modals/insertcharacter_spec.js.coffee +2 -2
  51. data/spec/javascripts/mercury/modals/insertlink_spec.js.coffee +10 -10
  52. data/spec/javascripts/mercury/modals/insertmedia_spec.js.coffee +7 -7
  53. data/spec/javascripts/mercury/modals/insertsnippet_spec.js.coffee +4 -4
  54. data/spec/javascripts/mercury/modals/inserttable_spec.js.coffee +17 -17
  55. data/spec/javascripts/mercury/page_editor_spec.js.coffee +5 -6
  56. data/spec/javascripts/mercury/region_spec.js.coffee +5 -5
  57. data/spec/javascripts/mercury/regions/editable_spec.js.coffee +398 -0
  58. data/spec/javascripts/mercury/regions/snippetable_spec.js.coffee +18 -14
  59. data/spec/javascripts/mercury/snippet_spec.js.coffee +1 -1
  60. data/spec/javascripts/mercury/toolbar.button_spec.js.coffee +16 -16
  61. data/spec/javascripts/templates/mercury/modals/inserttable.html +11 -11
  62. data/spec/javascripts/templates/mercury/regions/editable.html +3 -0
  63. data/spec/javascripts/templates/mercury/toolbar.button.html +8 -8
  64. data/vendor/assets/javascripts/{jquery-ui-1.8.13.sortable.custom.js → jquery-ui-1.8.13.custom.js} +250 -0
  65. data/vendor/assets/javascripts/jquery.additions.js +151 -0
  66. data/vendor/assets/javascripts/showdown.js +1254 -1276
  67. metadata +26 -29
  68. data/app/assets/javascripts/mercury/websocket.js.coffee +0 -34
  69. data/spec/javascripts/mercury/regions/_editable_.js.coffee +0 -0
  70. data/vendor/assets/javascripts/jquery-ui-1.8.13.custom.min.js +0 -249
  71. data/vendor/assets/javascripts/jquery.easing.js +0 -173
  72. data/vendor/assets/javascripts/jquery.json2.js +0 -178
  73. data/vendor/assets/javascripts/jquery.serialize_object.js +0 -16
  74. data/vendor/assets/javascripts/jquery.ujs.js +0 -289
@@ -0,0 +1,151 @@
1
+ /*
2
+ * jQuery serializeObject Plugin
3
+ *
4
+ */
5
+ (function($) {
6
+ $.fn.serializeObject = function() {
7
+ var o = {};
8
+ var a = this.serializeArray();
9
+ jQuery.each(a, function() {
10
+ if (o[this.name] !== undefined) {
11
+ if (!o[this.name].push) o[this.name] = [o[this.name]];
12
+ o[this.name].push(this.value || '');
13
+ } else {
14
+ o[this.name] = this.value || '';
15
+ }
16
+ });
17
+ return o;
18
+ };
19
+ })(jQuery);
20
+
21
+ /*
22
+ * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
23
+ *
24
+ * Uses the built in easing capabilities added In jQuery 1.1 to offer multiple easing options
25
+ *
26
+ * TERMS OF USE - jQuery Easing
27
+ *
28
+ * Open source under the BSD License.
29
+ *
30
+ * Copyright © 2008 George McGinley Smith
31
+ * All rights reserved.
32
+ *
33
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
34
+ * following conditions are met:
35
+ *
36
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
37
+ * disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
38
+ * the following disclaimer in the documentation and/or other materials provided with the distribution.
39
+ *
40
+ * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from
41
+ * this software without specific prior written permission.
42
+ *
43
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
44
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
48
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
+ */
51
+ jQuery.extend(jQuery.easing, {
52
+ easeInSine: function (x, t, b, c, d) {
53
+ return -c * Math.cos(t / d * (Math.PI / 2)) + c + b
54
+ },
55
+ easeOutSine: function (x, t, b, c, d) {
56
+ return c * Math.sin(t / d * (Math.PI / 2)) + b
57
+ },
58
+ easeInOutSine: function (x, t, b, c, d) {
59
+ return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b
60
+ }
61
+ });
62
+
63
+ /*
64
+ * jQuery JSON Plugin version: 2.1 (2009-08-14)
65
+ *
66
+ * This document is licensed as free software under the terms of the MIT License:
67
+ * http://www.opensource.org/licenses/mit-license.php
68
+ *
69
+ * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org website's http://www.json.org/json2.js,
70
+ * which proclaims: "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that I uphold.
71
+ *
72
+ * It is also influenced heavily by MochiKit's serializeJSON, which is copyrighted 2005 by Bob Ippolito.
73
+ */
74
+ (function($) {
75
+ $.toJSON = function(o) {
76
+ if (typeof(JSON) == 'object' && JSON.stringify) return JSON.stringify(o);
77
+
78
+ var type = typeof(o);
79
+
80
+ if (o === null) return "null";
81
+ if (type == "undefined") return undefined;
82
+ if (type == "number" || type == "boolean") return o + "";
83
+ if (type == "string") return $.quoteString(o);
84
+
85
+ if (type == 'object') {
86
+ if (typeof(o.toJSON) == "function") return $.toJSON(o.toJSON());
87
+
88
+ if (o.constructor === Date) {
89
+ var year = o.getUTCFullYear();
90
+
91
+ var month = o.getUTCMonth() + 1;
92
+ if (month < 10) month = '0' + month;
93
+
94
+ var day = o.getUTCDate();
95
+ if (day < 10) day = '0' + day;
96
+
97
+ var hours = o.getUTCHours();
98
+ if (hours < 10) hours = '0' + hours;
99
+
100
+ var minutes = o.getUTCMinutes();
101
+ if (minutes < 10) minutes = '0' + minutes;
102
+
103
+ var seconds = o.getUTCSeconds();
104
+ if (seconds < 10) seconds = '0' + seconds;
105
+
106
+ var milli = o.getUTCMilliseconds();
107
+ if (milli < 100) milli = '0' + milli;
108
+ if (milli < 10) milli = '0' + milli;
109
+
110
+ return '"' + year + '-' + month + '-' + day + 'T' + hours + ':' + minutes + ':' + seconds + '.' + milli + 'Z"';
111
+ }
112
+
113
+ if (o.constructor === Array) {
114
+ var ret = [];
115
+ for (var i = 0; i < o.length; i++) ret.push($.toJSON(o[i]) || "null");
116
+ return "[" + ret.join(",") + "]";
117
+ }
118
+
119
+ var pairs = [];
120
+ for (var k in o) {
121
+ var name;
122
+ type = typeof(k);
123
+
124
+ if (type == "number") name = '"' + k + '"';
125
+ else if (type == "string") name = $.quoteString(k);
126
+ else continue; //skip non-string or number keys
127
+
128
+ if (typeof o[k] == "function") continue; //skip pairs where the value is a function.
129
+ var val = $.toJSON(o[k]);
130
+ pairs.push(name + ":" + val);
131
+ }
132
+
133
+ return "{" + pairs.join(", ") + "}";
134
+ }
135
+ };
136
+
137
+ $.quoteString = function(string) {
138
+ if (string.match(_escapeable)) {
139
+ return '"' + string.replace(_escapeable, function (a) {
140
+ var c = _meta[a];
141
+ if (typeof c === 'string') return c;
142
+ c = a.charCodeAt();
143
+ return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
144
+ }) + '"';
145
+ }
146
+ return '"' + string + '"';
147
+ };
148
+
149
+ var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
150
+ var _meta = {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'};
151
+ })(jQuery);
@@ -81,1282 +81,1260 @@ var Showdown = {};
81
81
  //
82
82
  Showdown.converter = function() {
83
83
 
84
- //
85
- // Globals:
86
- //
87
-
88
- // Global hashes, used by various utility routines
89
- var g_urls;
90
- var g_titles;
91
- var g_html_blocks;
92
-
93
- // Used to track when we're inside an ordered or unordered list
94
- // (see _ProcessListItems() for details):
95
- var g_list_level = 0;
96
-
97
-
98
- this.makeHtml = function(text) {
99
- //
100
- // Main function. The order in which other subs are called here is
101
- // essential. Link and image substitutions need to happen before
102
- // _EscapeSpecialCharsWithinTagAttributes(), so that any *'s or _'s in the <a>
103
- // and <img> tags get encoded.
104
- //
105
-
106
- // Clear the global hashes. If we don't clear these, you get conflicts
107
- // from other articles when generating a page which contains more than
108
- // one article (e.g. an index page that shows the N most recent
109
- // articles):
110
- g_urls = new Array();
111
- g_titles = new Array();
112
- g_html_blocks = new Array();
113
-
114
- // attacklab: Replace ~ with ~T
115
- // This lets us use tilde as an escape char to avoid md5 hashes
116
- // The choice of character is arbitray; anything that isn't
84
+ // Global hashes, used by various utility routines
85
+ var g_urls;
86
+ var g_titles;
87
+ var g_html_blocks;
88
+
89
+ // Used to track when we're inside an ordered or unordered list
90
+ // (see _ProcessListItems() for details):
91
+ var g_list_level = 0;
92
+
93
+ // Main function. The order in which other subs are called here is
94
+ // essential. Link and image substitutions need to happen before
95
+ // _EscapeSpecialCharsWithinTagAttributes(), so that any *'s or _'s in the <a>
96
+ // and <img> tags get encoded.
97
+ this.makeHtml = function(text) {
98
+
99
+ // Clear the global hashes. If we don't clear these, you get conflicts
100
+ // from other articles when generating a page which contains more than
101
+ // one article (e.g. an index page that shows the N most recent
102
+ // articles):
103
+ g_urls = new Array();
104
+ g_titles = new Array();
105
+ g_html_blocks = new Array();
106
+
107
+ // attacklab: Replace ~ with ~T
108
+ // This lets us use tilde as an escape char to avoid md5 hashes
109
+ // The choice of character is arbitray; anything that isn't
117
110
  // magic in Markdown will work.
118
- text = text.replace(/~/g,"~T");
119
-
120
- // attacklab: Replace $ with ~D
121
- // RegExp interprets $ as a special character
122
- // when it's in a replacement string
123
- text = text.replace(/\$/g,"~D");
124
-
125
- // Standardize line endings
126
- text = text.replace(/\r\n/g,"\n"); // DOS to Unix
127
- text = text.replace(/\r/g,"\n"); // Mac to Unix
128
-
129
- // Make sure text begins and ends with a couple of newlines:
130
- text = "\n\n" + text + "\n\n";
131
-
132
- // Convert all tabs to spaces.
133
- text = _Detab(text);
134
-
135
- // Strip any lines consisting only of spaces and tabs.
136
- // This makes subsequent regexen easier to write, because we can
137
- // match consecutive blank lines with /\n+/ instead of something
138
- // contorted like /[ \t]*\n+/ .
139
- text = text.replace(/^[ \t]+$/mg,"");
140
-
141
- // Turn block-level HTML blocks into hash entries
142
- text = _HashHTMLBlocks(text);
143
-
144
- // Strip link definitions, store in hashes.
145
- text = _StripLinkDefinitions(text);
146
-
147
- text = _RunBlockGamut(text);
148
-
149
- text = _UnescapeSpecialChars(text);
150
-
151
- // attacklab: Restore dollar signs
152
- text = text.replace(/~D/g,"$$");
153
-
154
- // attacklab: Restore tildes
155
- text = text.replace(/~T/g,"~");
156
-
157
- // ** GFM ** Auto-link URLs and emails
158
- text = text.replace(/https?\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!]/g, function(wholeMatch,matchIndex){
159
- var left = text.slice(0, matchIndex), right = text.slice(matchIndex)
160
- if (left.match(/<[^>]+$/) && right.match(/^[^>]*>/)) {return wholeMatch}
161
- href = wholeMatch.replace(/^http:\/\/github.com\//, "https://github.com/")
162
- return "<a href='" + href + "'>" + wholeMatch + "</a>";
163
- });
164
- text = text.replace(/[a-z0-9_\-+=.]+@[a-z0-9\-]+(\.[a-z0-9-]+)+/ig, function(wholeMatch){return "<a href='mailto:" + wholeMatch + "'>" + wholeMatch + "</a>";});
165
-
166
- // ** GFM ** Auto-link sha1 if GitHub.nameWithOwner is defined
167
- text = text.replace(/[a-f0-9]{40}/ig, function(wholeMatch,matchIndex){
168
- if (typeof(GitHub) == "undefined" || typeof(GitHub.nameWithOwner) == "undefined") {return wholeMatch;}
169
- var left = text.slice(0, matchIndex), right = text.slice(matchIndex)
170
- if (left.match(/@$/) || (left.match(/<[^>]+$/) && right.match(/^[^>]*>/))) {return wholeMatch;}
171
- return "<a href='http://github.com/" + GitHub.nameWithOwner + "/commit/" + wholeMatch + "'>" + wholeMatch.substring(0,7) + "</a>";
172
- });
173
-
174
- // ** GFM ** Auto-link user@sha1 if GitHub.nameWithOwner is defined
175
- text = text.replace(/([a-z0-9_\-+=.]+)@([a-f0-9]{40})/ig, function(wholeMatch,username,sha,matchIndex){
176
- if (typeof(GitHub) == "undefined" || typeof(GitHub.nameWithOwner) == "undefined") {return wholeMatch;}
177
- GitHub.repoName = GitHub.repoName || _GetRepoName()
178
- var left = text.slice(0, matchIndex), right = text.slice(matchIndex)
179
- if (left.match(/\/$/) || (left.match(/<[^>]+$/) && right.match(/^[^>]*>/))) {return wholeMatch;}
180
- return "<a href='http://github.com/" + username + "/" + GitHub.repoName + "/commit/" + sha + "'>" + username + "@" + sha.substring(0,7) + "</a>";
181
- });
182
-
183
- // ** GFM ** Auto-link user/repo@sha1
184
- text = text.replace(/([a-z0-9_\-+=.]+\/[a-z0-9_\-+=.]+)@([a-f0-9]{40})/ig, function(wholeMatch,repo,sha){
185
- return "<a href='http://github.com/" + repo + "/commit/" + sha + "'>" + repo + "@" + sha.substring(0,7) + "</a>";
186
- });
187
-
188
- // ** GFM ** Auto-link #issue if GitHub.nameWithOwner is defined
189
- text = text.replace(/#([0-9]+)/ig, function(wholeMatch,issue,matchIndex){
190
- if (typeof(GitHub) == "undefined" || typeof(GitHub.nameWithOwner) == "undefined") {return wholeMatch;}
191
- var left = text.slice(0, matchIndex), right = text.slice(matchIndex)
192
- if (left == "" || left.match(/[a-z0-9_\-+=.]$/) || (left.match(/<[^>]+$/) && right.match(/^[^>]*>/))) {return wholeMatch;}
193
- return "<a href='http://github.com/" + GitHub.nameWithOwner + "/issues/#issue/" + issue + "'>" + wholeMatch + "</a>";
194
- });
195
-
196
- // ** GFM ** Auto-link user#issue if GitHub.nameWithOwner is defined
197
- text = text.replace(/([a-z0-9_\-+=.]+)#([0-9]+)/ig, function(wholeMatch,username,issue,matchIndex){
198
- if (typeof(GitHub) == "undefined" || typeof(GitHub.nameWithOwner) == "undefined") {return wholeMatch;}
199
- GitHub.repoName = GitHub.repoName || _GetRepoName()
200
- var left = text.slice(0, matchIndex), right = text.slice(matchIndex)
201
- if (left.match(/\/$/) || (left.match(/<[^>]+$/) && right.match(/^[^>]*>/))) {return wholeMatch;}
202
- return "<a href='http://github.com/" + username + "/" + GitHub.repoName + "/issues/#issue/" + issue + "'>" + wholeMatch + "</a>";
203
- });
204
-
205
- // ** GFM ** Auto-link user/repo#issue
206
- text = text.replace(/([a-z0-9_\-+=.]+\/[a-z0-9_\-+=.]+)#([0-9]+)/ig, function(wholeMatch,repo,issue){
207
- return "<a href='http://github.com/" + repo + "/issues/#issue/" + issue + "'>" + wholeMatch + "</a>";
208
- });
209
-
210
- return text;
211
- }
212
-
213
-
214
- var _GetRepoName = function() {
215
- return GitHub.nameWithOwner.match(/^.+\/(.+)$/)[1]
216
- }
217
-
218
- var _StripLinkDefinitions = function(text) {
219
- //
220
- // Strips link definitions from text, stores the URLs and titles in
221
- // hash references.
222
- //
223
-
224
- // Link defs are in the form: ^[id]: url "optional title"
225
-
226
- /*
227
- var text = text.replace(/
228
- ^[ ]{0,3}\[(.+)\]: // id = $1 attacklab: g_tab_width - 1
229
- [ \t]*
230
- \n? // maybe *one* newline
231
- [ \t]*
232
- <?(\S+?)>? // url = $2
233
- [ \t]*
234
- \n? // maybe one newline
235
- [ \t]*
236
- (?:
237
- (\n*) // any lines skipped = $3 attacklab: lookbehind removed
238
- ["(]
239
- (.+?) // title = $4
240
- [")]
241
- [ \t]*
242
- )? // title is optional
243
- (?:\n+|$)
244
- /gm,
245
- function(){...});
246
- */
247
- var text = text.replace(/^[ ]{0,3}\[(.+)\]:[ \t]*\n?[ \t]*<?(\S+?)>?[ \t]*\n?[ \t]*(?:(\n*)["(](.+?)[")][ \t]*)?(?:\n+|\Z)/gm,
248
- function (wholeMatch,m1,m2,m3,m4) {
249
- m1 = m1.toLowerCase();
250
- g_urls[m1] = _EncodeAmpsAndAngles(m2); // Link IDs are case-insensitive
251
- if (m3) {
252
- // Oops, found blank lines, so it's not a title.
253
- // Put back the parenthetical statement we stole.
254
- return m3+m4;
255
- } else if (m4) {
256
- g_titles[m1] = m4.replace(/"/g,"&quot;");
257
- }
258
-
259
- // Completely remove the definition from the text
260
- return "";
261
- }
262
- );
263
-
264
- return text;
265
- }
266
-
267
-
268
- var _HashHTMLBlocks = function(text) {
269
- // attacklab: Double up blank lines to reduce lookaround
270
- text = text.replace(/\n/g,"\n\n");
271
-
272
- // Hashify HTML blocks:
273
- // We only want to do this for block-level HTML tags, such as headers,
274
- // lists, and tables. That's because we still want to wrap <p>s around
275
- // "paragraphs" that are wrapped in non-block-level tags, such as anchors,
276
- // phrase emphasis, and spans. The list of tags we're looking for is
277
- // hard-coded:
278
- var block_tags_a = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del"
279
- var block_tags_b = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math"
280
-
281
- // First, look for nested blocks, e.g.:
282
- // <div>
283
- // <div>
284
- // tags for inner block must be indented.
285
- // </div>
286
- // </div>
287
- //
288
- // The outermost tags must start at the left margin for this to match, and
289
- // the inner nested divs must be indented.
290
- // We need to do this before the next, more liberal match, because the next
291
- // match will start at the first `<div>` and stop at the first `</div>`.
292
-
293
- // attacklab: This regex can be expensive when it fails.
294
- /*
295
- var text = text.replace(/
296
- ( // save in $1
297
- ^ // start of line (with /m)
298
- <($block_tags_a) // start tag = $2
299
- \b // word break
300
- // attacklab: hack around khtml/pcre bug...
301
- [^\r]*?\n // any number of lines, minimally matching
302
- </\2> // the matching end tag
303
- [ \t]* // trailing spaces/tabs
304
- (?=\n+) // followed by a newline
305
- ) // attacklab: there are sentinel newlines at end of document
306
- /gm,function(){...}};
307
- */
308
- text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del)\b[^\r]*?\n<\/\2>[ \t]*(?=\n+))/gm,hashElement);
309
-
310
- //
311
- // Now match more liberally, simply from `\n<tag>` to `</tag>\n`
312
- //
313
-
314
- /*
315
- var text = text.replace(/
316
- ( // save in $1
317
- ^ // start of line (with /m)
318
- <($block_tags_b) // start tag = $2
319
- \b // word break
320
- // attacklab: hack around khtml/pcre bug...
321
- [^\r]*? // any number of lines, minimally matching
322
- .*</\2> // the matching end tag
323
- [ \t]* // trailing spaces/tabs
324
- (?=\n+) // followed by a newline
325
- ) // attacklab: there are sentinel newlines at end of document
326
- /gm,function(){...}};
327
- */
328
- text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math)\b[^\r]*?.*<\/\2>[ \t]*(?=\n+)\n)/gm,hashElement);
329
-
330
- // Special case just for <hr />. It was easier to make a special case than
331
- // to make the other regex more complicated.
332
-
333
- /*
334
- text = text.replace(/
335
- ( // save in $1
336
- \n\n // Starting after a blank line
337
- [ ]{0,3}
338
- (<(hr) // start tag = $2
339
- \b // word break
340
- ([^<>])*? //
341
- \/?>) // the matching end tag
342
- [ \t]*
343
- (?=\n{2,}) // followed by a blank line
344
- )
345
- /g,hashElement);
346
- */
347
- text = text.replace(/(\n[ ]{0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g,hashElement);
348
-
349
- // Special case for standalone HTML comments:
350
-
351
- /*
352
- text = text.replace(/
353
- ( // save in $1
354
- \n\n // Starting after a blank line
355
- [ ]{0,3} // attacklab: g_tab_width - 1
356
- <!
357
- (--[^\r]*?--\s*)+
358
- >
359
- [ \t]*
360
- (?=\n{2,}) // followed by a blank line
361
- )
362
- /g,hashElement);
363
- */
364
- text = text.replace(/(\n\n[ ]{0,3}<!(--[^\r]*?--\s*)+>[ \t]*(?=\n{2,}))/g,hashElement);
365
-
366
- // PHP and ASP-style processor instructions (<?...?> and <%...%>)
367
-
368
- /*
369
- text = text.replace(/
370
- (?:
371
- \n\n // Starting after a blank line
372
- )
373
- ( // save in $1
374
- [ ]{0,3} // attacklab: g_tab_width - 1
375
- (?:
376
- <([?%]) // $2
377
- [^\r]*?
378
- \2>
379
- )
380
- [ \t]*
381
- (?=\n{2,}) // followed by a blank line
382
- )
383
- /g,hashElement);
384
- */
385
- text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g,hashElement);
386
-
387
- // attacklab: Undo double lines (see comment at top of this function)
388
- text = text.replace(/\n\n/g,"\n");
389
- return text;
390
- }
391
-
392
- var hashElement = function(wholeMatch,m1) {
393
- var blockText = m1;
394
-
395
- // Undo double lines
396
- blockText = blockText.replace(/\n\n/g,"\n");
397
- blockText = blockText.replace(/^\n/,"");
398
-
399
- // strip trailing blank lines
400
- blockText = blockText.replace(/\n+$/g,"");
401
-
402
- // Replace the element text with a marker ("~KxK" where x is its key)
403
- blockText = "\n\n~K" + (g_html_blocks.push(blockText)-1) + "K\n\n";
404
-
405
- return blockText;
406
- };
407
-
408
- var _RunBlockGamut = function(text) {
409
- //
410
- // These are all the transformations that form block-level
411
- // tags like paragraphs, headers, and list items.
412
- //
413
- text = _DoHeaders(text);
414
-
415
- // Do Horizontal Rules:
416
- var key = hashBlock("<hr />");
417
- text = text.replace(/^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$/gm,key);
418
- text = text.replace(/^[ ]{0,2}([ ]?\-[ ]?){3,}[ \t]*$/gm,key);
419
- text = text.replace(/^[ ]{0,2}([ ]?\_[ ]?){3,}[ \t]*$/gm,key);
420
-
421
- text = _DoLists(text);
422
- text = _DoCodeBlocks(text);
423
- text = _DoBlockQuotes(text);
424
-
425
- // We already ran _HashHTMLBlocks() before, in Markdown(), but that
426
- // was to escape raw HTML in the original Markdown source. This time,
427
- // we're escaping the markup we've just created, so that we don't wrap
428
- // <p> tags around block-level tags.
429
- text = _HashHTMLBlocks(text);
430
- text = _FormParagraphs(text);
431
-
432
- return text;
433
- }
434
-
435
-
436
- var _RunSpanGamut = function(text) {
437
- //
438
- // These are all the transformations that occur *within* block-level
439
- // tags like paragraphs, headers, and list items.
440
- //
441
-
442
- text = _DoCodeSpans(text);
443
- text = _EscapeSpecialCharsWithinTagAttributes(text);
444
- text = _EncodeBackslashEscapes(text);
445
-
446
- // Process anchor and image tags. Images must come first,
447
- // because ![foo][f] looks like an anchor.
448
- text = _DoImages(text);
449
- text = _DoAnchors(text);
450
-
451
- // Make links out of things like `<http://example.com/>`
452
- // Must come after _DoAnchors(), because you can use < and >
453
- // delimiters in inline links like [this](<url>).
454
- text = _DoAutoLinks(text);
455
- text = _EncodeAmpsAndAngles(text);
456
- text = _DoItalicsAndBold(text);
457
-
458
- // Do hard breaks:
459
- text = text.replace(/ +\n/g," <br />\n");
460
-
461
- return text;
462
- }
463
-
464
- var _EscapeSpecialCharsWithinTagAttributes = function(text) {
465
- //
466
- // Within tags -- meaning between < and > -- encode [\ ` * _] so they
467
- // don't conflict with their use in Markdown for code, italics and strong.
468
- //
469
-
470
- // Build a regex to find HTML tags and comments. See Friedl's
471
- // "Mastering Regular Expressions", 2nd Ed., pp. 200-201.
472
- var regex = /(<[a-z\/!$]("[^"]*"|'[^']*'|[^'">])*>|<!(--.*?--\s*)+>)/gi;
473
-
474
- text = text.replace(regex, function(wholeMatch) {
475
- var tag = wholeMatch.replace(/(.)<\/?code>(?=.)/g,"$1`");
476
- tag = escapeCharacters(tag,"\\`*_");
477
- return tag;
478
- });
479
-
480
- return text;
481
- }
482
-
483
- var _DoAnchors = function(text) {
484
- //
485
- // Turn Markdown link shortcuts into XHTML <a> tags.
486
- //
487
- //
488
- // First, handle reference-style links: [link text] [id]
489
- //
490
-
491
- /*
492
- text = text.replace(/
493
- ( // wrap whole match in $1
494
- \[
495
- (
496
- (?:
497
- \[[^\]]*\] // allow brackets nested one level
498
- |
499
- [^\[] // or anything else
500
- )*
501
- )
502
- \]
503
-
504
- [ ]? // one optional space
505
- (?:\n[ ]*)? // one optional newline followed by spaces
506
-
507
- \[
508
- (.*?) // id = $3
509
- \]
510
- )()()()() // pad remaining backreferences
511
- /g,_DoAnchors_callback);
512
- */
513
- text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g,writeAnchorTag);
514
-
515
- //
516
- // Next, inline-style links: [link text](url "optional title")
517
- //
518
-
519
- /*
520
- text = text.replace(/
521
- ( // wrap whole match in $1
522
- \[
523
- (
524
- (?:
525
- \[[^\]]*\] // allow brackets nested one level
526
- |
527
- [^\[\]] // or anything else
528
- )
529
- )
530
- \]
531
- \( // literal paren
532
- [ \t]*
533
- () // no id, so leave $3 empty
534
- <?(.*?)>? // href = $4
535
- [ \t]*
536
- ( // $5
537
- (['"]) // quote char = $6
538
- (.*?) // Title = $7
539
- \6 // matching quote
540
- [ \t]* // ignore any spaces/tabs between closing quote and )
541
- )? // title is optional
542
- \)
543
- )
544
- /g,writeAnchorTag);
545
- */
546
- text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?(.*?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,writeAnchorTag);
547
-
548
- //
549
- // Last, handle reference-style shortcuts: [link text]
550
- // These must come last in case you've also got [link test][1]
551
- // or [link test](/foo)
552
- //
553
-
554
- /*
555
- text = text.replace(/
556
- ( // wrap whole match in $1
557
- \[
558
- ([^\[\]]+) // link text = $2; can't contain '[' or ']'
559
- \]
560
- )()()()()() // pad rest of backreferences
561
- /g, writeAnchorTag);
562
- */
563
- text = text.replace(/(\[([^\[\]]+)\])()()()()()/g, writeAnchorTag);
564
-
565
- return text;
566
- }
567
-
568
- var writeAnchorTag = function(wholeMatch,m1,m2,m3,m4,m5,m6,m7) {
569
- if (m7 == undefined) m7 = "";
570
- var whole_match = m1;
571
- var link_text = m2;
572
- var link_id = m3.toLowerCase();
573
- var url = m4;
574
- var title = m7;
575
-
576
- if (url == "") {
577
- if (link_id == "") {
578
- // lower-case and turn embedded newlines into spaces
579
- link_id = link_text.toLowerCase().replace(/ ?\n/g," ");
580
- }
581
- url = "#"+link_id;
582
-
583
- if (g_urls[link_id] != undefined) {
584
- url = g_urls[link_id];
585
- if (g_titles[link_id] != undefined) {
586
- title = g_titles[link_id];
587
- }
588
- }
589
- else {
590
- if (whole_match.search(/\(\s*\)$/m)>-1) {
591
- // Special case for explicit empty url
592
- url = "";
593
- } else {
594
- return whole_match;
595
- }
596
- }
597
- }
598
-
599
- url = escapeCharacters(url,"*_");
600
- var result = "<a href=\"" + url + "\"";
601
-
602
- if (title != "") {
603
- title = title.replace(/"/g,"&quot;");
604
- title = escapeCharacters(title,"*_");
605
- result += " title=\"" + title + "\"";
606
- }
607
-
608
- result += ">" + link_text + "</a>";
609
-
610
- return result;
611
- }
612
-
613
-
614
- var _DoImages = function(text) {
615
- //
616
- // Turn Markdown image shortcuts into <img> tags.
617
- //
618
-
619
- //
620
- // First, handle reference-style labeled images: ![alt text][id]
621
- //
622
-
623
- /*
624
- text = text.replace(/
625
- ( // wrap whole match in $1
626
- !\[
627
- (.*?) // alt text = $2
628
- \]
629
-
630
- [ ]? // one optional space
631
- (?:\n[ ]*)? // one optional newline followed by spaces
632
-
633
- \[
634
- (.*?) // id = $3
635
- \]
636
- )()()()() // pad rest of backreferences
637
- /g,writeImageTag);
638
- */
639
- text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g,writeImageTag);
640
-
641
- //
642
- // Next, handle inline images: ![alt text](url "optional title")
643
- // Don't forget: encode * and _
644
-
645
- /*
646
- text = text.replace(/
647
- ( // wrap whole match in $1
648
- !\[
649
- (.*?) // alt text = $2
650
- \]
651
- \s? // One optional whitespace character
652
- \( // literal paren
653
- [ \t]*
654
- () // no id, so leave $3 empty
655
- <?(\S+?)>? // src url = $4
656
- [ \t]*
657
- ( // $5
658
- (['"]) // quote char = $6
659
- (.*?) // title = $7
660
- \6 // matching quote
661
- [ \t]*
662
- )? // title is optional
663
- \)
664
- )
665
- /g,writeImageTag);
666
- */
667
- text = text.replace(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,writeImageTag);
668
-
669
- return text;
670
- }
671
-
672
- var writeImageTag = function(wholeMatch,m1,m2,m3,m4,m5,m6,m7) {
673
- var whole_match = m1;
674
- var alt_text = m2;
675
- var link_id = m3.toLowerCase();
676
- var url = m4;
677
- var title = m7;
678
-
679
- if (!title) title = "";
680
-
681
- if (url == "") {
682
- if (link_id == "") {
683
- // lower-case and turn embedded newlines into spaces
684
- link_id = alt_text.toLowerCase().replace(/ ?\n/g," ");
685
- }
686
- url = "#"+link_id;
687
-
688
- if (g_urls[link_id] != undefined) {
689
- url = g_urls[link_id];
690
- if (g_titles[link_id] != undefined) {
691
- title = g_titles[link_id];
692
- }
693
- }
694
- else {
695
- return whole_match;
696
- }
697
- }
698
-
699
- alt_text = alt_text.replace(/"/g,"&quot;");
700
- url = escapeCharacters(url,"*_");
701
- var result = "<img src=\"" + url + "\" alt=\"" + alt_text + "\"";
702
-
703
- // attacklab: Markdown.pl adds empty title attributes to images.
704
- // Replicate this bug.
705
-
706
- //if (title != "") {
707
- title = title.replace(/"/g,"&quot;");
708
- title = escapeCharacters(title,"*_");
709
- result += " title=\"" + title + "\"";
710
- //}
711
-
712
- result += " />";
713
-
714
- return result;
715
- }
716
-
717
-
718
- var _DoHeaders = function(text) {
719
-
720
- // Setext-style headers:
721
- // Header 1
722
- // ========
723
- //
724
- // Header 2
725
- // --------
726
- //
727
- text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm,
728
- function(wholeMatch,m1){return hashBlock("<h1>" + _RunSpanGamut(m1) + "</h1>");});
729
-
730
- text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm,
731
- function(matchFound,m1){return hashBlock("<h2>" + _RunSpanGamut(m1) + "</h2>");});
732
-
733
- // atx-style headers:
734
- // # Header 1
735
- // ## Header 2
736
- // ## Header 2 with closing hashes ##
737
- // ...
738
- // ###### Header 6
739
- //
740
-
741
- /*
742
- text = text.replace(/
743
- ^(\#{1,6}) // $1 = string of #'s
744
- [ \t]*
745
- (.+?) // $2 = Header text
746
- [ \t]*
747
- \#* // optional closing #'s (not counted)
748
- \n+
749
- /gm, function() {...});
750
- */
751
-
752
- text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm,
753
- function(wholeMatch,m1,m2) {
754
- var h_level = m1.length;
755
- return hashBlock("<h" + h_level + ">" + _RunSpanGamut(m2) + "</h" + h_level + ">");
756
- });
757
-
758
- return text;
759
- }
760
-
761
- // This declaration keeps Dojo compressor from outputting garbage:
762
- var _ProcessListItems;
763
-
764
- var _DoLists = function(text) {
765
- //
766
- // Form HTML ordered (numbered) and unordered (bulleted) lists.
767
- //
768
-
769
- // attacklab: add sentinel to hack around khtml/safari bug:
770
- // http://bugs.webkit.org/show_bug.cgi?id=11231
771
- text += "~0";
772
-
773
- // Re-usable pattern to match any entirel ul or ol list:
774
-
775
- /*
776
- var whole_list = /
777
- ( // $1 = whole list
778
- ( // $2
779
- [ ]{0,3} // attacklab: g_tab_width - 1
780
- ([*+-]|\d+[.]) // $3 = first list item marker
781
- [ \t]+
782
- )
783
- [^\r]+?
784
- ( // $4
785
- ~0 // sentinel for workaround; should be $
786
- |
787
- \n{2,}
788
- (?=\S)
789
- (?! // Negative lookahead for another list item marker
790
- [ \t]*
791
- (?:[*+-]|\d+[.])[ \t]+
792
- )
793
- )
794
- )/g
795
- */
796
- var whole_list = /^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
797
-
798
- if (g_list_level) {
799
- text = text.replace(whole_list,function(wholeMatch,m1,m2) {
800
- var list = m1;
801
- var list_type = (m2.search(/[*+-]/g)>-1) ? "ul" : "ol";
802
-
803
- // Turn double returns into triple returns, so that we can make a
804
- // paragraph for the last item in a list, if necessary:
805
- list = list.replace(/\n{2,}/g,"\n\n\n");;
806
- var result = _ProcessListItems(list);
807
-
808
- // Trim any trailing whitespace, to put the closing `</$list_type>`
809
- // up on the preceding line, to get it past the current stupid
810
- // HTML block parser. This is a hack to work around the terrible
811
- // hack that is the HTML block parser.
812
- result = result.replace(/\s+$/,"");
813
- result = "<"+list_type+">" + result + "</"+list_type+">\n";
814
- return result;
815
- });
816
- } else {
817
- whole_list = /(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/g;
818
- text = text.replace(whole_list,function(wholeMatch,m1,m2,m3) {
819
- var runup = m1;
820
- var list = m2;
821
-
822
- var list_type = (m3.search(/[*+-]/g)>-1) ? "ul" : "ol";
823
- // Turn double returns into triple returns, so that we can make a
824
- // paragraph for the last item in a list, if necessary:
825
- var list = list.replace(/\n{2,}/g,"\n\n\n");;
826
- var result = _ProcessListItems(list);
827
- result = runup + "<"+list_type+">\n" + result + "</"+list_type+">\n";
828
- return result;
829
- });
830
- }
831
-
832
- // attacklab: strip sentinel
833
- text = text.replace(/~0/,"");
834
-
835
- return text;
836
- }
837
-
838
- _ProcessListItems = function(list_str) {
839
- //
840
- // Process the contents of a single ordered or unordered list, splitting it
841
- // into individual list items.
842
- //
843
- // The $g_list_level global keeps track of when we're inside a list.
844
- // Each time we enter a list, we increment it; when we leave a list,
845
- // we decrement. If it's zero, we're not in a list anymore.
846
- //
847
- // We do this because when we're not inside a list, we want to treat
848
- // something like this:
849
- //
850
- // I recommend upgrading to version
851
- // 8. Oops, now this line is treated
852
- // as a sub-list.
853
- //
854
- // As a single paragraph, despite the fact that the second line starts
855
- // with a digit-period-space sequence.
856
- //
857
- // Whereas when we're inside a list (or sub-list), that line will be
858
- // treated as the start of a sub-list. What a kludge, huh? This is
859
- // an aspect of Markdown's syntax that's hard to parse perfectly
860
- // without resorting to mind-reading. Perhaps the solution is to
861
- // change the syntax rules such that sub-lists must start with a
862
- // starting cardinal number; e.g. "1." or "a.".
863
-
864
- g_list_level++;
865
-
866
- // trim trailing blank lines:
867
- list_str = list_str.replace(/\n{2,}$/,"\n");
868
-
869
- // attacklab: add sentinel to emulate \z
870
- list_str += "~0";
871
-
872
- /*
873
- list_str = list_str.replace(/
874
- (\n)? // leading line = $1
875
- (^[ \t]*) // leading whitespace = $2
876
- ([*+-]|\d+[.]) [ \t]+ // list marker = $3
877
- ([^\r]+? // list item text = $4
878
- (\n{1,2}))
879
- (?= \n* (~0 | \2 ([*+-]|\d+[.]) [ \t]+))
880
- /gm, function(){...});
881
- */
882
- list_str = list_str.replace(/(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+([^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm,
883
- function(wholeMatch,m1,m2,m3,m4){
884
- var item = m4;
885
- var leading_line = m1;
886
- var leading_space = m2;
887
-
888
- if (leading_line || (item.search(/\n{2,}/)>-1)) {
889
- item = _RunBlockGamut(_Outdent(item));
890
- }
891
- else {
892
- // Recursion for sub-lists:
893
- item = _DoLists(_Outdent(item));
894
- item = item.replace(/\n$/,""); // chomp(item)
895
- item = _RunSpanGamut(item);
896
- }
897
-
898
- return "<li>" + item + "</li>\n";
899
- }
900
- );
901
-
902
- // attacklab: strip sentinel
903
- list_str = list_str.replace(/~0/g,"");
904
-
905
- g_list_level--;
906
- return list_str;
907
- }
908
-
909
-
910
- var _DoCodeBlocks = function(text) {
911
- //
912
- // Process Markdown `<pre><code>` blocks.
913
- //
914
-
915
- /*
916
- text = text.replace(text,
917
- /(?:\n\n|^)
918
- ( // $1 = the code block -- one or more lines, starting with a space/tab
919
- (?:
920
- (?:[ ]{4}|\t) // Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width
921
- .*\n+
922
- )+
923
- )
924
- (\n*[ ]{0,3}[^ \t\n]|(?=~0)) // attacklab: g_tab_width
925
- /g,function(){...});
926
- */
927
-
928
- // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
929
- text += "~0";
930
-
931
- text = text.replace(/(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g,
932
- function(wholeMatch,m1,m2) {
933
- var codeblock = m1;
934
- var nextChar = m2;
935
-
936
- codeblock = _EncodeCode( _Outdent(codeblock));
937
- codeblock = _Detab(codeblock);
938
- codeblock = codeblock.replace(/^\n+/g,""); // trim leading newlines
939
- codeblock = codeblock.replace(/\n+$/g,""); // trim trailing whitespace
940
-
941
- codeblock = "<pre><code>" + codeblock + "\n</code></pre>";
942
-
943
- return hashBlock(codeblock) + nextChar;
944
- }
945
- );
946
-
947
- // attacklab: strip sentinel
948
- text = text.replace(/~0/,"");
949
-
950
- return text;
951
- }
952
-
953
- var hashBlock = function(text) {
954
- text = text.replace(/(^\n+|\n+$)/g,"");
955
- return "\n\n~K" + (g_html_blocks.push(text)-1) + "K\n\n";
956
- }
957
-
958
-
959
- var _DoCodeSpans = function(text) {
960
- //
961
- // * Backtick quotes are used for <code></code> spans.
962
- //
963
- // * You can use multiple backticks as the delimiters if you want to
964
- // include literal backticks in the code span. So, this input:
965
- //
966
- // Just type ``foo `bar` baz`` at the prompt.
967
- //
968
- // Will translate to:
969
- //
970
- // <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
971
- //
972
- // There's no arbitrary limit to the number of backticks you
973
- // can use as delimters. If you need three consecutive backticks
974
- // in your code, use four for delimiters, etc.
975
- //
976
- // * You can use spaces to get literal backticks at the edges:
977
- //
978
- // ... type `` `bar` `` ...
979
- //
980
- // Turns to:
981
- //
982
- // ... type <code>`bar`</code> ...
983
- //
984
-
985
- /*
986
- text = text.replace(/
987
- (^|[^\\]) // Character before opening ` can't be a backslash
988
- (`+) // $2 = Opening run of `
989
- ( // $3 = The code block
990
- [^\r]*?
991
- [^`] // attacklab: work around lack of lookbehind
992
- )
993
- \2 // Matching closer
994
- (?!`)
995
- /gm, function(){...});
996
- */
997
-
998
- text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm,
999
- function(wholeMatch,m1,m2,m3,m4) {
1000
- var c = m3;
1001
- c = c.replace(/^([ \t]*)/g,""); // leading whitespace
1002
- c = c.replace(/[ \t]*$/g,""); // trailing whitespace
1003
- c = _EncodeCode(c);
1004
- return m1+"<code>"+c+"</code>";
1005
- });
1006
-
1007
- return text;
1008
- }
1009
-
1010
-
1011
- var _EncodeCode = function(text) {
1012
- //
1013
- // Encode/escape certain characters inside Markdown code runs.
1014
- // The point is that in code, these characters are literals,
1015
- // and lose their special Markdown meanings.
1016
- //
1017
- // Encode all ampersands; HTML entities are not
1018
- // entities within a Markdown code span.
1019
- text = text.replace(/&/g,"&amp;");
1020
-
1021
- // Do the angle bracket song and dance:
1022
- text = text.replace(/</g,"&lt;");
1023
- text = text.replace(/>/g,"&gt;");
1024
-
1025
- // Now, escape characters that are magic in Markdown:
1026
- text = escapeCharacters(text,"\*_{}[]\\",false);
1027
-
1028
- // jj the line above breaks this:
1029
- //---
1030
-
1031
- //* Item
1032
-
1033
- // 1. Subitem
1034
-
1035
- // special char: *
1036
- //---
1037
-
1038
- return text;
1039
- }
1040
-
1041
-
1042
- var _DoItalicsAndBold = function(text) {
1043
-
1044
- // <strong> must go first:
1045
- text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g,
1046
- "<strong>$2</strong>");
1047
-
1048
- text = text.replace(/(\w)_(\w)/g, "$1~E95E$2") // ** GFM ** "~E95E" == escaped "_"
1049
- text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g,
1050
- "<em>$2</em>");
1051
-
1052
- return text;
1053
- }
1054
-
1055
-
1056
- var _DoBlockQuotes = function(text) {
1057
-
1058
- /*
1059
- text = text.replace(/
1060
- ( // Wrap whole match in $1
1061
- (
1062
- ^[ \t]*>[ \t]? // '>' at the start of a line
1063
- .+\n // rest of the first line
1064
- (.+\n)* // subsequent consecutive lines
1065
- \n* // blanks
1066
- )+
1067
- )
1068
- /gm, function(){...});
1069
- */
1070
-
1071
- text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,
1072
- function(wholeMatch,m1) {
1073
- var bq = m1;
1074
-
1075
- // attacklab: hack around Konqueror 3.5.4 bug:
1076
- // "----------bug".replace(/^-/g,"") == "bug"
1077
-
1078
- bq = bq.replace(/^[ \t]*>[ \t]?/gm,"~0"); // trim one level of quoting
1079
-
1080
- // attacklab: clean up hack
1081
- bq = bq.replace(/~0/g,"");
1082
-
1083
- bq = bq.replace(/^[ \t]+$/gm,""); // trim whitespace-only lines
1084
- bq = _RunBlockGamut(bq); // recurse
1085
-
1086
- bq = bq.replace(/(^|\n)/g,"$1 ");
1087
- // These leading spaces screw with <pre> content, so we need to fix that:
1088
- bq = bq.replace(
1089
- /(\s*<pre>[^\r]+?<\/pre>)/gm,
1090
- function(wholeMatch,m1) {
1091
- var pre = m1;
1092
- // attacklab: hack around Konqueror 3.5.4 bug:
1093
- pre = pre.replace(/^ /mg,"~0");
1094
- pre = pre.replace(/~0/g,"");
1095
- return pre;
1096
- });
1097
-
1098
- return hashBlock("<blockquote>\n" + bq + "\n</blockquote>");
1099
- });
1100
- return text;
1101
- }
1102
-
1103
-
1104
- var _FormParagraphs = function(text) {
1105
- //
1106
- // Params:
1107
- // $text - string to process with html <p> tags
1108
- //
1109
-
1110
- // Strip leading and trailing lines:
1111
- text = text.replace(/^\n+/g,"");
1112
- text = text.replace(/\n+$/g,"");
1113
-
1114
- var grafs = text.split(/\n{2,}/g);
1115
- var grafsOut = new Array();
1116
-
1117
- //
1118
- // Wrap <p> tags.
1119
- //
1120
- var end = grafs.length;
1121
- for (var i=0; i<end; i++) {
1122
- var str = grafs[i];
1123
-
1124
- // if this is an HTML marker, copy it
1125
- if (str.search(/~K(\d+)K/g) >= 0) {
1126
- grafsOut.push(str);
1127
- }
1128
- else if (str.search(/\S/) >= 0) {
1129
- str = _RunSpanGamut(str);
1130
- str = str.replace(/\n/g,"<br />"); // ** GFM **
1131
- str = str.replace(/^([ \t]*)/g,"<p>");
1132
- str += "</p>"
1133
- grafsOut.push(str);
1134
- }
1135
-
1136
- }
1137
-
1138
- //
1139
- // Unhashify HTML blocks
1140
- //
1141
- end = grafsOut.length;
1142
- for (var i=0; i<end; i++) {
1143
- // if this is a marker for an html block...
1144
- while (grafsOut[i].search(/~K(\d+)K/) >= 0) {
1145
- var blockText = g_html_blocks[RegExp.$1];
1146
- blockText = blockText.replace(/\$/g,"$$$$"); // Escape any dollar signs
1147
- grafsOut[i] = grafsOut[i].replace(/~K\d+K/,blockText);
1148
- }
1149
- }
1150
-
1151
- return grafsOut.join("\n\n");
1152
- }
1153
-
1154
-
1155
- var _EncodeAmpsAndAngles = function(text) {
1156
- // Smart processing for ampersands and angle brackets that need to be encoded.
1157
-
1158
- // Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
1159
- // http://bumppo.net/projects/amputator/
1160
- text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g,"&amp;");
1161
-
1162
- // Encode naked <'s
1163
- text = text.replace(/<(?![a-z\/?\$!])/gi,"&lt;");
1164
-
1165
- return text;
1166
- }
1167
-
1168
-
1169
- var _EncodeBackslashEscapes = function(text) {
1170
- //
1171
- // Parameter: String.
1172
- // Returns: The string, with after processing the following backslash
1173
- // escape sequences.
1174
- //
1175
-
1176
- // attacklab: The polite way to do this is with the new
1177
- // escapeCharacters() function:
1178
- //
1179
- // text = escapeCharacters(text,"\\",true);
1180
- // text = escapeCharacters(text,"`*_{}[]()>#+-.!",true);
1181
- //
1182
- // ...but we're sidestepping its use of the (slow) RegExp constructor
1183
- // as an optimization for Firefox. This function gets called a LOT.
1184
-
1185
- text = text.replace(/\\(\\)/g,escapeCharacters_callback);
1186
- text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g,escapeCharacters_callback);
1187
- return text;
1188
- }
1189
-
1190
-
1191
- var _DoAutoLinks = function(text) {
1192
-
1193
- text = text.replace(/<((https?|ftp|dict):[^'">\s]+)>/gi,"<a href=\"$1\">$1</a>");
1194
-
1195
- // Email addresses: <address@domain.foo>
1196
-
1197
- /*
1198
- text = text.replace(/
1199
- <
1200
- (?:mailto:)?
1201
- (
1202
- [-.\w]+
1203
- \@
1204
- [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+
1205
- )
1206
- >
1207
- /gi, _DoAutoLinks_callback());
1208
- */
1209
- text = text.replace(/<(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi,
1210
- function(wholeMatch,m1) {
1211
- return _EncodeEmailAddress( _UnescapeSpecialChars(m1) );
1212
- }
1213
- );
1214
-
1215
- return text;
1216
- }
1217
-
1218
-
1219
- var _EncodeEmailAddress = function(addr) {
1220
- //
1221
- // Input: an email address, e.g. "foo@example.com"
1222
- //
1223
- // Output: the email address as a mailto link, with each character
1224
- // of the address encoded as either a decimal or hex entity, in
1225
- // the hopes of foiling most address harvesting spam bots. E.g.:
1226
- //
1227
- // <a href="&#x6D;&#97;&#105;&#108;&#x74;&#111;:&#102;&#111;&#111;&#64;&#101;
1228
- // x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;">&#102;&#111;&#111;
1229
- // &#64;&#101;x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;</a>
1230
- //
1231
- // Based on a filter by Matthew Wickline, posted to the BBEdit-Talk
1232
- // mailing list: <http://tinyurl.com/yu7ue>
1233
- //
1234
-
1235
- // attacklab: why can't javascript speak hex?
1236
- function char2hex(ch) {
1237
- var hexDigits = '0123456789ABCDEF';
1238
- var dec = ch.charCodeAt(0);
1239
- return(hexDigits.charAt(dec>>4) + hexDigits.charAt(dec&15));
1240
- }
1241
-
1242
- var encode = [
1243
- function(ch){return "&#"+ch.charCodeAt(0)+";";},
1244
- function(ch){return "&#x"+char2hex(ch)+";";},
1245
- function(ch){return ch;}
1246
- ];
1247
-
1248
- addr = "mailto:" + addr;
1249
-
1250
- addr = addr.replace(/./g, function(ch) {
1251
- if (ch == "@") {
1252
- // this *must* be encoded. I insist.
1253
- ch = encode[Math.floor(Math.random()*2)](ch);
1254
- } else if (ch !=":") {
1255
- // leave ':' alone (to spot mailto: later)
1256
- var r = Math.random();
1257
- // roughly 10% raw, 45% hex, 45% dec
1258
- ch = (
1259
- r > .9 ? encode[2](ch) :
1260
- r > .45 ? encode[1](ch) :
1261
- encode[0](ch)
1262
- );
1263
- }
1264
- return ch;
1265
- });
1266
-
1267
- addr = "<a href=\"" + addr + "\">" + addr + "</a>";
1268
- addr = addr.replace(/">.+:/g,"\">"); // strip the mailto: from the visible part
1269
-
1270
- return addr;
1271
- }
1272
-
1273
-
1274
- var _UnescapeSpecialChars = function(text) {
1275
- //
1276
- // Swap back in all the special characters we've hidden.
1277
- //
1278
- text = text.replace(/~E(\d+)E/g,
1279
- function(wholeMatch,m1) {
1280
- var charCodeToReplace = parseInt(m1);
1281
- return String.fromCharCode(charCodeToReplace);
1282
- }
1283
- );
1284
- return text;
1285
- }
1286
-
1287
-
1288
- var _Outdent = function(text) {
1289
- //
1290
- // Remove one level of line-leading tabs or spaces
1291
- //
1292
-
1293
- // attacklab: hack around Konqueror 3.5.4 bug:
1294
- // "----------bug".replace(/^-/g,"") == "bug"
1295
-
1296
- text = text.replace(/^(\t|[ ]{1,4})/gm,"~0"); // attacklab: g_tab_width
1297
-
1298
- // attacklab: clean up hack
1299
- text = text.replace(/~0/g,"")
1300
-
1301
- return text;
1302
- }
1303
-
1304
- var _Detab = function(text) {
1305
- // attacklab: Detab's completely rewritten for speed.
1306
- // In perl we could fix it by anchoring the regexp with \G.
1307
- // In javascript we're less fortunate.
1308
-
1309
- // expand first n-1 tabs
1310
- text = text.replace(/\t(?=\t)/g," "); // attacklab: g_tab_width
1311
-
1312
- // replace the nth with two sentinels
1313
- text = text.replace(/\t/g,"~A~B");
1314
-
1315
- // use the sentinel to anchor our regex so it doesn't explode
1316
- text = text.replace(/~B(.+?)~A/g,
1317
- function(wholeMatch,m1,m2) {
1318
- var leadingText = m1;
1319
- var numSpaces = 4 - leadingText.length % 4; // attacklab: g_tab_width
1320
-
1321
- // there *must* be a better way to do this:
1322
- for (var i=0; i<numSpaces; i++) leadingText+=" ";
1323
-
1324
- return leadingText;
1325
- }
1326
- );
1327
-
1328
- // clean up sentinels
1329
- text = text.replace(/~A/g," "); // attacklab: g_tab_width
1330
- text = text.replace(/~B/g,"");
1331
-
1332
- return text;
1333
- }
1334
-
1335
-
1336
- //
1337
- // attacklab: Utility functions
1338
- //
1339
-
1340
-
1341
- var escapeCharacters = function(text, charsToEscape, afterBackslash) {
1342
- // First we have to escape the escape characters so that
1343
- // we can build a character class out of them
1344
- var regexString = "([" + charsToEscape.replace(/([\[\]\\])/g,"\\$1") + "])";
1345
-
1346
- if (afterBackslash) {
1347
- regexString = "\\\\" + regexString;
1348
- }
1349
-
1350
- var regex = new RegExp(regexString,"g");
1351
- text = text.replace(regex,escapeCharacters_callback);
1352
-
1353
- return text;
1354
- }
1355
-
111
+ text = text.replace(/~/g, "~T");
112
+
113
+ // attacklab: Replace $ with ~D
114
+ // RegExp interprets $ as a special character
115
+ // when it's in a replacement string
116
+ text = text.replace(/\$/g, "~D");
117
+
118
+ // Standardize line endings
119
+ text = text.replace(/\r\n/g, "\n"); // DOS to Unix
120
+ text = text.replace(/\r/g, "\n"); // Mac to Unix
121
+
122
+ // Make sure text begins and ends with a couple of newlines:
123
+ text = "\n\n" + text + "\n\n";
124
+
125
+ // Convert all tabs to spaces.
126
+ text = _Detab(text);
127
+
128
+ // Strip any lines consisting only of spaces and tabs.
129
+ // This makes subsequent regexen easier to write, because we can
130
+ // match consecutive blank lines with /\n+/ instead of something
131
+ // contorted like /[ \t]*\n+/ .
132
+ text = text.replace(/^[ \t]+$/mg, "");
133
+
134
+ // Turn block-level HTML blocks into hash entries
135
+ text = _HashHTMLBlocks(text);
136
+
137
+ // Strip link definitions, store in hashes.
138
+ text = _StripLinkDefinitions(text);
139
+
140
+ text = _RunBlockGamut(text);
141
+
142
+ text = _UnescapeSpecialChars(text);
143
+
144
+ // attacklab: Restore dollar signs
145
+ text = text.replace(/~D/g, "$$");
146
+
147
+ // attacklab: Restore tildes
148
+ text = text.replace(/~T/g, "~");
149
+
150
+ // ** GFM ** Auto-link URLs and emails
151
+ text = text.replace(/https?\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!]/g, function(wholeMatch, matchIndex) {
152
+ var left = text.slice(0, matchIndex), right = text.slice(matchIndex);
153
+ if (left.match(/<[^>]+$/) && right.match(/^[^>]*>/)) {
154
+ return wholeMatch
155
+ }
156
+ var href = wholeMatch.replace(/^http:\/\/github.com\//, "https://github.com/");
157
+ return "<a href='" + href + "'>" + wholeMatch + "</a>";
158
+ });
159
+ text = text.replace(/[a-z0-9_\-+=.]+@[a-z0-9\-]+(\.[a-z0-9-]+)+/ig, function(wholeMatch) {
160
+ return "<a href='mailto:" + wholeMatch + "'>" + wholeMatch + "</a>";
161
+ });
162
+
163
+ // ** GFM ** Auto-link sha1 if GitHub.nameWithOwner is defined
164
+ text = text.replace(/[a-f0-9]{40}/ig, function(wholeMatch, matchIndex) {
165
+ if (typeof(GitHub) == "undefined" || typeof(GitHub.nameWithOwner) == "undefined") {
166
+ return wholeMatch;
167
+ }
168
+ var left = text.slice(0, matchIndex), right = text.slice(matchIndex);
169
+ if (left.match(/@$/) || (left.match(/<[^>]+$/) && right.match(/^[^>]*>/))) {
170
+ return wholeMatch;
171
+ }
172
+ return "<a href='http://github.com/" + GitHub.nameWithOwner + "/commit/" + wholeMatch + "'>" + wholeMatch.substring(0, 7) + "</a>";
173
+ });
174
+
175
+ // ** GFM ** Auto-link user@sha1 if GitHub.nameWithOwner is defined
176
+ text = text.replace(/([a-z0-9_\-+=.]+)@([a-f0-9]{40})/ig, function(wholeMatch, username, sha, matchIndex) {
177
+ if (typeof(GitHub) == "undefined" || typeof(GitHub.nameWithOwner) == "undefined") {
178
+ return wholeMatch;
179
+ }
180
+ GitHub.repoName = GitHub.repoName || _GetRepoName();
181
+ var left = text.slice(0, matchIndex), right = text.slice(matchIndex);
182
+ if (left.match(/\/$/) || (left.match(/<[^>]+$/) && right.match(/^[^>]*>/))) {
183
+ return wholeMatch;
184
+ }
185
+ return "<a href='http://github.com/" + username + "/" + GitHub.repoName + "/commit/" + sha + "'>" + username + "@" + sha.substring(0, 7) + "</a>";
186
+ });
187
+
188
+ // ** GFM ** Auto-link user/repo@sha1
189
+ text = text.replace(/([a-z0-9_\-+=.]+\/[a-z0-9_\-+=.]+)@([a-f0-9]{40})/ig, function(wholeMatch, repo, sha) {
190
+ return "<a href='http://github.com/" + repo + "/commit/" + sha + "'>" + repo + "@" + sha.substring(0, 7) + "</a>";
191
+ });
192
+
193
+ // ** GFM ** Auto-link #issue if GitHub.nameWithOwner is defined
194
+ text = text.replace(/#([0-9]+)/ig, function(wholeMatch, issue, matchIndex) {
195
+ if (typeof(GitHub) == "undefined" || typeof(GitHub.nameWithOwner) == "undefined") {
196
+ return wholeMatch;
197
+ }
198
+ var left = text.slice(0, matchIndex), right = text.slice(matchIndex);
199
+ if (left == "" || left.match(/[a-z0-9_\-+=.]$/) || (left.match(/<[^>]+$/) && right.match(/^[^>]*>/))) {
200
+ return wholeMatch;
201
+ }
202
+ return "<a href='http://github.com/" + GitHub.nameWithOwner + "/issues/#issue/" + issue + "'>" + wholeMatch + "</a>";
203
+ });
204
+
205
+ // ** GFM ** Auto-link user#issue if GitHub.nameWithOwner is defined
206
+ text = text.replace(/([a-z0-9_\-+=.]+)#([0-9]+)/ig, function(wholeMatch, username, issue, matchIndex) {
207
+ if (typeof(GitHub) == "undefined" || typeof(GitHub.nameWithOwner) == "undefined") {
208
+ return wholeMatch;
209
+ }
210
+ GitHub.repoName = GitHub.repoName || _GetRepoName();
211
+ var left = text.slice(0, matchIndex), right = text.slice(matchIndex);
212
+ if (left.match(/\/$/) || (left.match(/<[^>]+$/) && right.match(/^[^>]*>/))) {
213
+ return wholeMatch;
214
+ }
215
+ return "<a href='http://github.com/" + username + "/" + GitHub.repoName + "/issues/#issue/" + issue + "'>" + wholeMatch + "</a>";
216
+ });
217
+
218
+ // ** GFM ** Auto-link user/repo#issue
219
+ text = text.replace(/([a-z0-9_\-+=.]+\/[a-z0-9_\-+=.]+)#([0-9]+)/ig, function(wholeMatch, repo, issue) {
220
+ return "<a href='http://github.com/" + repo + "/issues/#issue/" + issue + "'>" + wholeMatch + "</a>";
221
+ });
222
+
223
+ return text;
224
+ };
225
+
226
+
227
+ var _GetRepoName = function() {
228
+ return GitHub.nameWithOwner.match(/^.+\/(.+)$/)[1]
229
+ };
230
+
231
+ //
232
+ // Strips link definitions from text, stores the URLs and titles in
233
+ // hash references.
234
+ //
235
+ var _StripLinkDefinitions = function(text) {
236
+
237
+ // Link defs are in the form: ^[id]: url "optional title"
238
+
239
+ /*
240
+ var text = text.replace(/
241
+ ^[ ]{0,3}\[(.+)\]: // id = $1 attacklab: g_tab_width - 1
242
+ [ \t]*
243
+ \n? // maybe *one* newline
244
+ [ \t]*
245
+ <?(\S+?)>? // url = $2
246
+ [ \t]*
247
+ \n? // maybe one newline
248
+ [ \t]*
249
+ (?:
250
+ (\n*) // any lines skipped = $3 attacklab: lookbehind removed
251
+ ["(]
252
+ (.+?) // title = $4
253
+ [")]
254
+ [ \t]*
255
+ )? // title is optional
256
+ (?:\n+|$)
257
+ /gm,
258
+ function(){...});
259
+ */
260
+ var text = text.replace(/^[ ]{0,3}\[(.+)\]:[ \t]*\n?[ \t]*<?(\S+?)>?[ \t]*\n?[ \t]*(?:(\n*)["(](.+?)[")][ \t]*)?(?:\n+|\Z)/gm, function (wholeMatch, m1, m2, m3, m4) {
261
+ m1 = m1.toLowerCase();
262
+ g_urls[m1] = _EncodeAmpsAndAngles(m2); // Link IDs are case-insensitive
263
+ if (m3) {
264
+ // Oops, found blank lines, so it's not a title.
265
+ // Put back the parenthetical statement we stole.
266
+ return m3 + m4;
267
+ } else if (m4) {
268
+ g_titles[m1] = m4.replace(/"/g, "&quot;");
269
+ }
270
+
271
+ // Completely remove the definition from the text
272
+ return "";
273
+ });
274
+
275
+ return text;
276
+ };
277
+
278
+
279
+ var _HashHTMLBlocks = function(text) {
280
+ // attacklab: Double up blank lines to reduce lookaround
281
+ text = text.replace(/\n/g, "\n\n");
282
+
283
+ // Hashify HTML blocks:
284
+ // We only want to do this for block-level HTML tags, such as headers,
285
+ // lists, and tables. That's because we still want to wrap <p>s around
286
+ // "paragraphs" that are wrapped in non-block-level tags, such as anchors,
287
+ // phrase emphasis, and spans. The list of tags we're looking for is
288
+ // hard-coded:
289
+ var block_tags_a = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del";
290
+ var block_tags_b = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math";
291
+
292
+ // First, look for nested blocks, e.g.:
293
+ // <div>
294
+ // <div>
295
+ // tags for inner block must be indented.
296
+ // </div>
297
+ // </div>
298
+ //
299
+ // The outermost tags must start at the left margin for this to match, and
300
+ // the inner nested divs must be indented.
301
+ // We need to do this before the next, more liberal match, because the next
302
+ // match will start at the first `<div>` and stop at the first `</div>`.
303
+
304
+ // attacklab: This regex can be expensive when it fails.
305
+ /*
306
+ var text = text.replace(/
307
+ ( // save in $1
308
+ ^ // start of line (with /m)
309
+ <($block_tags_a) // start tag = $2
310
+ \b // word break
311
+ // attacklab: hack around khtml/pcre bug...
312
+ [^\r]*?\n // any number of lines, minimally matching
313
+ </\2> // the matching end tag
314
+ [ \t]* // trailing spaces/tabs
315
+ (?=\n+) // followed by a newline
316
+ ) // attacklab: there are sentinel newlines at end of document
317
+ /gm,function(){...}};
318
+ */
319
+ text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del)\b[^\r]*?\n<\/\2>[ \t]*(?=\n+))/gm, hashElement);
320
+
321
+ //
322
+ // Now match more liberally, simply from `\n<tag>` to `</tag>\n`
323
+ //
324
+
325
+ /*
326
+ var text = text.replace(/
327
+ ( // save in $1
328
+ ^ // start of line (with /m)
329
+ <($block_tags_b) // start tag = $2
330
+ \b // word break
331
+ // attacklab: hack around khtml/pcre bug...
332
+ [^\r]*? // any number of lines, minimally matching
333
+ .*</\2> // the matching end tag
334
+ [ \t]* // trailing spaces/tabs
335
+ (?=\n+) // followed by a newline
336
+ ) // attacklab: there are sentinel newlines at end of document
337
+ /gm,function(){...}};
338
+ */
339
+ text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math)\b[^\r]*?.*<\/\2>[ \t]*(?=\n+)\n)/gm, hashElement);
340
+
341
+ // Special case just for <hr />. It was easier to make a special case than
342
+ // to make the other regex more complicated.
343
+
344
+ /*
345
+ text = text.replace(/
346
+ ( // save in $1
347
+ \n\n // Starting after a blank line
348
+ [ ]{0,3}
349
+ (<(hr) // start tag = $2
350
+ \b // word break
351
+ ([^<>])*? //
352
+ \/?>) // the matching end tag
353
+ [ \t]*
354
+ (?=\n{2,}) // followed by a blank line
355
+ )
356
+ /g,hashElement);
357
+ */
358
+ text = text.replace(/(\n[ ]{0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g, hashElement);
359
+
360
+ // Special case for standalone HTML comments:
361
+
362
+ /*
363
+ text = text.replace(/
364
+ ( // save in $1
365
+ \n\n // Starting after a blank line
366
+ [ ]{0,3} // attacklab: g_tab_width - 1
367
+ <!
368
+ (--[^\r]*?--\s*)+
369
+ >
370
+ [ \t]*
371
+ (?=\n{2,}) // followed by a blank line
372
+ )
373
+ /g,hashElement);
374
+ */
375
+ text = text.replace(/(\n\n[ ]{0,3}<!(--[^\r]*?--\s*)+>[ \t]*(?=\n{2,}))/g, hashElement);
376
+
377
+ // PHP and ASP-style processor instructions (<?...?> and <%...%>)
378
+
379
+ /*
380
+ text = text.replace(/
381
+ (?:
382
+ \n\n // Starting after a blank line
383
+ )
384
+ ( // save in $1
385
+ [ ]{0,3} // attacklab: g_tab_width - 1
386
+ (?:
387
+ <([?%]) // $2
388
+ [^\r]*?
389
+ \2>
390
+ )
391
+ [ \t]*
392
+ (?=\n{2,}) // followed by a blank line
393
+ )
394
+ /g,hashElement);
395
+ */
396
+ text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g, hashElement);
397
+
398
+ // attacklab: Undo double lines (see comment at top of this function)
399
+ text = text.replace(/\n\n/g, "\n");
400
+ return text;
401
+ };
402
+
403
+ var hashElement = function(wholeMatch, m1) {
404
+ var blockText = m1;
405
+
406
+ // Undo double lines
407
+ blockText = blockText.replace(/\n\n/g, "\n");
408
+ blockText = blockText.replace(/^\n/, "");
409
+
410
+ // strip trailing blank lines
411
+ blockText = blockText.replace(/\n+$/g, "");
412
+
413
+ // Replace the element text with a marker ("~KxK" where x is its key)
414
+ blockText = "\n\n~K" + (g_html_blocks.push(blockText) - 1) + "K\n\n";
415
+
416
+ return blockText;
417
+ };
418
+
419
+ //
420
+ // These are all the transformations that form block-level
421
+ // tags like paragraphs, headers, and list items.
422
+ //
423
+ var _RunBlockGamut = function(text) {
424
+ text = _DoHeaders(text);
425
+
426
+ // Do Horizontal Rules:
427
+ var key = hashBlock("<hr />");
428
+ text = text.replace(/^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$/gm, key);
429
+ text = text.replace(/^[ ]{0,2}([ ]?\-[ ]?){3,}[ \t]*$/gm, key);
430
+ text = text.replace(/^[ ]{0,2}([ ]?\_[ ]?){3,}[ \t]*$/gm, key);
431
+
432
+ text = _DoLists(text);
433
+ text = _DoCodeBlocks(text);
434
+ text = _DoBlockQuotes(text);
435
+
436
+ // We already ran _HashHTMLBlocks() before, in Markdown(), but that
437
+ // was to escape raw HTML in the original Markdown source. This time,
438
+ // we're escaping the markup we've just created, so that we don't wrap
439
+ // <p> tags around block-level tags.
440
+ text = _HashHTMLBlocks(text);
441
+ text = _FormParagraphs(text);
442
+
443
+ return text;
444
+ };
445
+
446
+ //
447
+ // These are all the transformations that occur *within* block-level
448
+ // tags like paragraphs, headers, and list items.
449
+ //
450
+ var _RunSpanGamut = function(text) {
451
+
452
+ text = _DoCodeSpans(text);
453
+ text = _EscapeSpecialCharsWithinTagAttributes(text);
454
+ text = _EncodeBackslashEscapes(text);
455
+
456
+ // Process anchor and image tags. Images must come first,
457
+ // because ![foo][f] looks like an anchor.
458
+ text = _DoImages(text);
459
+ text = _DoAnchors(text);
460
+
461
+ // Make links out of things like `<http://example.com/>`
462
+ // Must come after _DoAnchors(), because you can use < and >
463
+ // delimiters in inline links like [this](<url>).
464
+ text = _DoAutoLinks(text);
465
+ text = _EncodeAmpsAndAngles(text);
466
+ text = _DoItalicsAndBold(text);
467
+
468
+ // Do hard breaks:
469
+ text = text.replace(/ +\n/g, " <br />\n");
470
+
471
+ return text;
472
+ };
473
+
474
+ //
475
+ // Within tags -- meaning between < and > -- encode [\ ` * _] so they
476
+ // don't conflict with their use in Markdown for code, italics and strong.
477
+ //
478
+ var _EscapeSpecialCharsWithinTagAttributes = function(text) {
479
+
480
+ // Build a regex to find HTML tags and comments. See Friedl's
481
+ // "Mastering Regular Expressions", 2nd Ed., pp. 200-201.
482
+ var regex = /(<[a-z\/!$]("[^"]*"|'[^']*'|[^'">])*>|<!(--.*?--\s*)+>)/gi;
483
+
484
+ text = text.replace(regex, function(wholeMatch) {
485
+ var tag = wholeMatch.replace(/(.)<\/?code>(?=.)/g, "$1`");
486
+ tag = escapeCharacters(tag, "\\`*_");
487
+ return tag;
488
+ });
489
+
490
+ return text;
491
+ };
492
+
493
+ //
494
+ // Turn Markdown link shortcuts into XHTML <a> tags.
495
+ //
496
+ var _DoAnchors = function(text) {
497
+ //
498
+ // First, handle reference-style links: [link text] [id]
499
+ //
500
+
501
+ /*
502
+ text = text.replace(/
503
+ ( // wrap whole match in $1
504
+ \[
505
+ (
506
+ (?:
507
+ \[[^\]]*\] // allow brackets nested one level
508
+ |
509
+ [^\[] // or anything else
510
+ )*
511
+ )
512
+ \]
513
+
514
+ [ ]? // one optional space
515
+ (?:\n[ ]*)? // one optional newline followed by spaces
516
+
517
+ \[
518
+ (.*?) // id = $3
519
+ \]
520
+ )()()()() // pad remaining backreferences
521
+ /g,_DoAnchors_callback);
522
+ */
523
+ text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeAnchorTag);
524
+
525
+ //
526
+ // Next, inline-style links: [link text](url "optional title")
527
+ //
528
+
529
+ /*
530
+ text = text.replace(/
531
+ ( // wrap whole match in $1
532
+ \[
533
+ (
534
+ (?:
535
+ \[[^\]]*\] // allow brackets nested one level
536
+ |
537
+ [^\[\]] // or anything else
538
+ )
539
+ )
540
+ \]
541
+ \( // literal paren
542
+ [ \t]*
543
+ () // no id, so leave $3 empty
544
+ <?(.*?)>? // href = $4
545
+ [ \t]*
546
+ ( // $5
547
+ (['"]) // quote char = $6
548
+ (.*?) // Title = $7
549
+ \6 // matching quote
550
+ [ \t]* // ignore any spaces/tabs between closing quote and )
551
+ )? // title is optional
552
+ \)
553
+ )
554
+ /g,writeAnchorTag);
555
+ */
556
+ text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?(.*?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeAnchorTag);
557
+
558
+ //
559
+ // Last, handle reference-style shortcuts: [link text]
560
+ // These must come last in case you've also got [link test][1]
561
+ // or [link test](/foo)
562
+ //
563
+
564
+ /*
565
+ text = text.replace(/
566
+ ( // wrap whole match in $1
567
+ \[
568
+ ([^\[\]]+) // link text = $2; can't contain '[' or ']'
569
+ \]
570
+ )()()()()() // pad rest of backreferences
571
+ /g, writeAnchorTag);
572
+ */
573
+ text = text.replace(/(\[([^\[\]]+)\])()()()()()/g, writeAnchorTag);
574
+
575
+ return text;
576
+ };
577
+
578
+ var writeAnchorTag = function(wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
579
+ if (m7 == undefined) m7 = "";
580
+ var whole_match = m1;
581
+ var link_text = m2;
582
+ var link_id = m3.toLowerCase();
583
+ var url = m4;
584
+ var title = m7;
585
+
586
+ if (url == "") {
587
+ if (link_id == "") {
588
+ // lower-case and turn embedded newlines into spaces
589
+ link_id = link_text.toLowerCase().replace(/ ?\n/g, " ");
590
+ }
591
+ url = "#" + link_id;
592
+
593
+ if (g_urls[link_id] != undefined) {
594
+ url = g_urls[link_id];
595
+ if (g_titles[link_id] != undefined) {
596
+ title = g_titles[link_id];
597
+ }
598
+ }
599
+ else {
600
+ if (whole_match.search(/\(\s*\)$/m) > -1) {
601
+ // Special case for explicit empty url
602
+ url = "";
603
+ } else {
604
+ return whole_match;
605
+ }
606
+ }
607
+ }
608
+
609
+ url = escapeCharacters(url, "*_");
610
+ var result = "<a href=\"" + url + "\"";
611
+
612
+ if (title != "") {
613
+ title = title.replace(/"/g, "&quot;");
614
+ title = escapeCharacters(title, "*_");
615
+ result += " title=\"" + title + "\"";
616
+ }
617
+
618
+ result += ">" + link_text + "</a>";
619
+
620
+ return result;
621
+ };
622
+
623
+
624
+ //
625
+ // Turn Markdown image shortcuts into <img> tags.
626
+ //
627
+ var _DoImages = function(text) {
628
+ //
629
+ // First, handle reference-style labeled images: ![alt text][id]
630
+ //
631
+
632
+ /*
633
+ text = text.replace(/
634
+ ( // wrap whole match in $1
635
+ !\[
636
+ (.*?) // alt text = $2
637
+ \]
638
+
639
+ [ ]? // one optional space
640
+ (?:\n[ ]*)? // one optional newline followed by spaces
641
+
642
+ \[
643
+ (.*?) // id = $3
644
+ \]
645
+ )()()()() // pad rest of backreferences
646
+ /g,writeImageTag);
647
+ */
648
+ text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeImageTag);
649
+
650
+ //
651
+ // Next, handle inline images: ![alt text](url "optional title")
652
+ // Don't forget: encode * and _
653
+
654
+ /*
655
+ text = text.replace(/
656
+ ( // wrap whole match in $1
657
+ !\[
658
+ (.*?) // alt text = $2
659
+ \]
660
+ \s? // One optional whitespace character
661
+ \( // literal paren
662
+ [ \t]*
663
+ () // no id, so leave $3 empty
664
+ <?(\S+?)>? // src url = $4
665
+ [ \t]*
666
+ ( // $5
667
+ (['"]) // quote char = $6
668
+ (.*?) // title = $7
669
+ \6 // matching quote
670
+ [ \t]*
671
+ )? // title is optional
672
+ \)
673
+ )
674
+ /g,writeImageTag);
675
+ */
676
+ text = text.replace(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeImageTag);
677
+
678
+ return text;
679
+ };
680
+
681
+ var writeImageTag = function(wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
682
+ var whole_match = m1;
683
+ var alt_text = m2;
684
+ var link_id = m3.toLowerCase();
685
+ var url = m4;
686
+ var title = m7;
687
+
688
+ if (!title) title = "";
689
+
690
+ if (url == "") {
691
+ if (link_id == "") {
692
+ // lower-case and turn embedded newlines into spaces
693
+ link_id = alt_text.toLowerCase().replace(/ ?\n/g, " ");
694
+ }
695
+ url = "#" + link_id;
696
+
697
+ if (g_urls[link_id] != undefined) {
698
+ url = g_urls[link_id];
699
+ if (g_titles[link_id] != undefined) {
700
+ title = g_titles[link_id];
701
+ }
702
+ }
703
+ else {
704
+ return whole_match;
705
+ }
706
+ }
707
+
708
+ alt_text = alt_text.replace(/"/g, "&quot;");
709
+ url = escapeCharacters(url, "*_");
710
+ var result = "<img src=\"" + url + "\" alt=\"" + alt_text + "\"";
711
+
712
+ // attacklab: Markdown.pl adds empty title attributes to images.
713
+ // Replicate this bug.
714
+
715
+ //if (title != "") {
716
+ title = title.replace(/"/g, "&quot;");
717
+ title = escapeCharacters(title, "*_");
718
+ result += " title=\"" + title + "\"";
719
+ //}
720
+
721
+ result += " />";
722
+
723
+ return result;
724
+ };
725
+
726
+
727
+ var _DoHeaders = function(text) {
728
+
729
+ // Setext-style headers:
730
+ // Header 1
731
+ // ========
732
+ //
733
+ // Header 2
734
+ // --------
735
+ //
736
+ text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm,
737
+ function(wholeMatch, m1) {
738
+ return hashBlock("<h1>" + _RunSpanGamut(m1) + "</h1>");
739
+ });
740
+
741
+ text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm,
742
+ function(matchFound, m1) {
743
+ return hashBlock("<h2>" + _RunSpanGamut(m1) + "</h2>");
744
+ });
745
+
746
+ // atx-style headers:
747
+ // # Header 1
748
+ // ## Header 2
749
+ // ## Header 2 with closing hashes ##
750
+ // ...
751
+ // ###### Header 6
752
+ //
753
+
754
+ /*
755
+ text = text.replace(/
756
+ ^(\#{1,6}) // $1 = string of #'s
757
+ [ \t]*
758
+ (.+?) // $2 = Header text
759
+ [ \t]*
760
+ \#* // optional closing #'s (not counted)
761
+ \n+
762
+ /gm, function() {...});
763
+ */
764
+
765
+ text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm,
766
+ function(wholeMatch, m1, m2) {
767
+ var h_level = m1.length;
768
+ return hashBlock("<h" + h_level + ">" + _RunSpanGamut(m2) + "</h" + h_level + ">");
769
+ });
770
+
771
+ return text;
772
+ };
773
+
774
+ // This declaration keeps Dojo compressor from outputting garbage:
775
+ var _ProcessListItems;
776
+
777
+ //
778
+ // Form HTML ordered (numbered) and unordered (bulleted) lists.
779
+ //
780
+ var _DoLists = function(text) {
781
+
782
+ // attacklab: add sentinel to hack around khtml/safari bug:
783
+ // http://bugs.webkit.org/show_bug.cgi?id=11231
784
+ text += "~0";
785
+
786
+ // Re-usable pattern to match any entirel ul or ol list:
787
+
788
+ /*
789
+ var whole_list = /
790
+ ( // $1 = whole list
791
+ ( // $2
792
+ [ ]{0,3} // attacklab: g_tab_width - 1
793
+ ([*+-]|\d+[.]) // $3 = first list item marker
794
+ [ \t]+
795
+ )
796
+ [^\r]+?
797
+ ( // $4
798
+ ~0 // sentinel for workaround; should be $
799
+ |
800
+ \n{2,}
801
+ (?=\S)
802
+ (?! // Negative lookahead for another list item marker
803
+ [ \t]*
804
+ (?:[*+-]|\d+[.])[ \t]+
805
+ )
806
+ )
807
+ )/g
808
+ */
809
+ var whole_list = /^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
810
+
811
+ if (g_list_level) {
812
+ text = text.replace(whole_list, function(wholeMatch, m1, m2) {
813
+ var list = m1;
814
+ var list_type = (m2.search(/[*+-]/g) > -1) ? "ul" : "ol";
815
+
816
+ // Turn double returns into triple returns, so that we can make a
817
+ // paragraph for the last item in a list, if necessary:
818
+ list = list.replace(/\n{2,}/g, "\n\n\n");
819
+ var result = _ProcessListItems(list);
820
+
821
+ // Trim any trailing whitespace, to put the closing `</$list_type>`
822
+ // up on the preceding line, to get it past the current stupid
823
+ // HTML block parser. This is a hack to work around the terrible
824
+ // hack that is the HTML block parser.
825
+ result = result.replace(/\s+$/, "");
826
+ result = "<" + list_type + ">" + result + "</" + list_type + ">\n";
827
+ return result;
828
+ });
829
+ } else {
830
+ whole_list = /(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/g;
831
+ text = text.replace(whole_list, function(wholeMatch, m1, m2, m3) {
832
+ var runup = m1;
833
+ var list = m2;
834
+
835
+ var list_type = (m3.search(/[*+-]/g) > -1) ? "ul" : "ol";
836
+ // Turn double returns into triple returns, so that we can make a
837
+ // paragraph for the last item in a list, if necessary:
838
+ var list = list.replace(/\n{2,}/g, "\n\n\n");
839
+ var result = _ProcessListItems(list);
840
+ result = runup + "<" + list_type + ">\n" + result + "</" + list_type + ">\n";
841
+ return result;
842
+ });
843
+ }
844
+
845
+ // attacklab: strip sentinel
846
+ text = text.replace(/~0/, "");
847
+
848
+ return text;
849
+ };
850
+
851
+ //
852
+ // Process the contents of a single ordered or unordered list, splitting it
853
+ // into individual list items.
854
+ //
855
+ _ProcessListItems = function(list_str) {
856
+ // The $g_list_level global keeps track of when we're inside a list.
857
+ // Each time we enter a list, we increment it; when we leave a list,
858
+ // we decrement. If it's zero, we're not in a list anymore.
859
+ //
860
+ // We do this because when we're not inside a list, we want to treat
861
+ // something like this:
862
+ //
863
+ // I recommend upgrading to version
864
+ // 8. Oops, now this line is treated
865
+ // as a sub-list.
866
+ //
867
+ // As a single paragraph, despite the fact that the second line starts
868
+ // with a digit-period-space sequence.
869
+ //
870
+ // Whereas when we're inside a list (or sub-list), that line will be
871
+ // treated as the start of a sub-list. What a kludge, huh? This is
872
+ // an aspect of Markdown's syntax that's hard to parse perfectly
873
+ // without resorting to mind-reading. Perhaps the solution is to
874
+ // change the syntax rules such that sub-lists must start with a
875
+ // starting cardinal number; e.g. "1." or "a.".
876
+
877
+ g_list_level++;
878
+
879
+ // trim trailing blank lines:
880
+ list_str = list_str.replace(/\n{2,}$/, "\n");
881
+
882
+ // attacklab: add sentinel to emulate \z
883
+ list_str += "~0";
884
+
885
+ /*
886
+ list_str = list_str.replace(/
887
+ (\n)? // leading line = $1
888
+ (^[ \t]*) // leading whitespace = $2
889
+ ([*+-]|\d+[.]) [ \t]+ // list marker = $3
890
+ ([^\r]+? // list item text = $4
891
+ (\n{1,2}))
892
+ (?= \n* (~0 | \2 ([*+-]|\d+[.]) [ \t]+))
893
+ /gm, function(){...});
894
+ */
895
+ list_str = list_str.replace(/(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+([^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm,
896
+ function(wholeMatch, m1, m2, m3, m4) {
897
+ var item = m4;
898
+ var leading_line = m1;
899
+ var leading_space = m2;
900
+
901
+ if (leading_line || (item.search(/\n{2,}/) > -1)) {
902
+ item = _RunBlockGamut(_Outdent(item));
903
+ }
904
+ else {
905
+ // Recursion for sub-lists:
906
+ item = _DoLists(_Outdent(item));
907
+ item = item.replace(/\n$/, ""); // chomp(item)
908
+ item = _RunSpanGamut(item);
909
+ }
910
+
911
+ return "<li>" + item + "</li>\n";
912
+ }
913
+ );
914
+
915
+ // attacklab: strip sentinel
916
+ list_str = list_str.replace(/~0/g, "");
917
+
918
+ g_list_level--;
919
+ return list_str;
920
+ };
921
+
922
+
923
+ //
924
+ // Process Markdown `<pre><code>` blocks.
925
+ //
926
+ var _DoCodeBlocks = function(text) {
927
+
928
+ /*
929
+ text = text.replace(text,
930
+ /(?:\n\n|^)
931
+ ( // $1 = the code block -- one or more lines, starting with a space/tab
932
+ (?:
933
+ (?:[ ]{4}|\t) // Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width
934
+ .*\n+
935
+ )+
936
+ )
937
+ (\n*[ ]{0,3}[^ \t\n]|(?=~0)) // attacklab: g_tab_width
938
+ /g,function(){...});
939
+ */
940
+
941
+ // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
942
+ text += "~0";
943
+
944
+ text = text.replace(/(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g, function(wholeMatch, m1, m2) {
945
+ var codeblock = m1;
946
+ var nextChar = m2;
947
+
948
+ codeblock = _EncodeCode(_Outdent(codeblock));
949
+ codeblock = _Detab(codeblock);
950
+ codeblock = codeblock.replace(/^\n+/g, ""); // trim leading newlines
951
+ codeblock = codeblock.replace(/\n+$/g, ""); // trim trailing whitespace
952
+
953
+ codeblock = "<pre><code>" + codeblock + "\n</code></pre>";
954
+
955
+ return hashBlock(codeblock) + nextChar;
956
+ });
957
+
958
+ // attacklab: strip sentinel
959
+ text = text.replace(/~0/, "");
960
+
961
+ return text;
962
+ };
963
+
964
+ var hashBlock = function(text) {
965
+ text = text.replace(/(^\n+|\n+$)/g, "");
966
+ return "\n\n~K" + (g_html_blocks.push(text) - 1) + "K\n\n";
967
+ };
968
+
969
+
970
+ //
971
+ // * Backtick quotes are used for <code></code> spans.
972
+ //
973
+ // * You can use multiple backticks as the delimiters if you want to
974
+ // include literal backticks in the code span. So, this input:
975
+ //
976
+ // Just type ``foo `bar` baz`` at the prompt.
977
+ //
978
+ // Will translate to:
979
+ //
980
+ // <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
981
+ //
982
+ // There's no arbitrary limit to the number of backticks you
983
+ // can use as delimters. If you need three consecutive backticks
984
+ // in your code, use four for delimiters, etc.
985
+ //
986
+ // * You can use spaces to get literal backticks at the edges:
987
+ //
988
+ // ... type `` `bar` `` ...
989
+ //
990
+ // Turns to:
991
+ //
992
+ // ... type <code>`bar`</code> ...
993
+ //
994
+ var _DoCodeSpans = function(text) {
995
+ /*
996
+ text = text.replace(/
997
+ (^|[^\\]) // Character before opening ` can't be a backslash
998
+ (`+) // $2 = Opening run of `
999
+ ( // $3 = The code block
1000
+ [^\r]*?
1001
+ [^`] // attacklab: work around lack of lookbehind
1002
+ )
1003
+ \2 // Matching closer
1004
+ (?!`)
1005
+ /gm, function(){...});
1006
+ */
1007
+
1008
+ text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm,
1009
+ function(wholeMatch, m1, m2, m3, m4) {
1010
+ var c = m3;
1011
+ c = c.replace(/^([ \t]*)/g, ""); // leading whitespace
1012
+ c = c.replace(/[ \t]*$/g, ""); // trailing whitespace
1013
+ c = _EncodeCode(c);
1014
+ return m1 + "<code>" + c + "</code>";
1015
+ });
1016
+
1017
+ return text;
1018
+ };
1019
+
1020
+
1021
+ //
1022
+ // Encode/escape certain characters inside Markdown code runs.
1023
+ // The point is that in code, these characters are literals,
1024
+ // and lose their special Markdown meanings.
1025
+ //
1026
+ var _EncodeCode = function(text) {
1027
+ // Encode all ampersands; HTML entities are not
1028
+ // entities within a Markdown code span.
1029
+ text = text.replace(/&/g, "&amp;");
1030
+
1031
+ // Do the angle bracket song and dance:
1032
+ text = text.replace(/</g, "&lt;");
1033
+ text = text.replace(/>/g, "&gt;");
1034
+
1035
+ // Now, escape characters that are magic in Markdown:
1036
+ text = escapeCharacters(text, "\*_{}[]\\", false);
1037
+
1038
+ return text;
1039
+ };
1040
+
1041
+
1042
+ var _DoItalicsAndBold = function(text) {
1043
+
1044
+ // <strong> must go first:
1045
+ text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g,
1046
+ "<strong>$2</strong>");
1047
+
1048
+ text = text.replace(/(\w)_(\w)/g, "$1~E95E$2"); // ** GFM ** "~E95E" == escaped "_"
1049
+ text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g,
1050
+ "<em>$2</em>");
1051
+
1052
+ return text;
1053
+ };
1054
+
1055
+
1056
+ var _DoBlockQuotes = function(text) {
1057
+
1058
+ /*
1059
+ text = text.replace(/
1060
+ ( // Wrap whole match in $1
1061
+ (
1062
+ ^[ \t]*>[ \t]? // '>' at the start of a line
1063
+ .+\n // rest of the first line
1064
+ (.+\n)* // subsequent consecutive lines
1065
+ \n* // blanks
1066
+ )+
1067
+ )
1068
+ /gm, function(){...});
1069
+ */
1070
+
1071
+ text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,
1072
+ function(wholeMatch, m1) {
1073
+ var bq = m1;
1074
+
1075
+ // attacklab: hack around Konqueror 3.5.4 bug:
1076
+ // "----------bug".replace(/^-/g,"") == "bug"
1077
+
1078
+ bq = bq.replace(/^[ \t]*>[ \t]?/gm, "~0"); // trim one level of quoting
1079
+
1080
+ // attacklab: clean up hack
1081
+ bq = bq.replace(/~0/g, "");
1082
+
1083
+ bq = bq.replace(/^[ \t]+$/gm, ""); // trim whitespace-only lines
1084
+ bq = _RunBlockGamut(bq); // recurse
1085
+
1086
+ bq = bq.replace(/(^|\n)/g, "$1 ");
1087
+ // These leading spaces screw with <pre> content, so we need to fix that:
1088
+ bq = bq.replace(
1089
+ /(\s*<pre>[^\r]+?<\/pre>)/gm,
1090
+ function(wholeMatch, m1) {
1091
+ var pre = m1;
1092
+ // attacklab: hack around Konqueror 3.5.4 bug:
1093
+ pre = pre.replace(/^ /mg, "~0");
1094
+ pre = pre.replace(/~0/g, "");
1095
+ return pre;
1096
+ });
1097
+
1098
+ return hashBlock("<blockquote>\n" + bq + "\n</blockquote>");
1099
+ });
1100
+ return text;
1101
+ };
1102
+
1103
+
1104
+ //
1105
+ // Params:
1106
+ // $text - string to process with html <p> tags
1107
+ //
1108
+ var _FormParagraphs = function(text) {
1109
+
1110
+ // Strip leading and trailing lines:
1111
+ text = text.replace(/^\n+/g, "");
1112
+ text = text.replace(/\n+$/g, "");
1113
+
1114
+ var grafs = text.split(/\n{2,}/g);
1115
+ var grafsOut = new Array();
1116
+
1117
+ //
1118
+ // Wrap <p> tags.
1119
+ //
1120
+ var end = grafs.length;
1121
+ for (var i = 0; i < end; i++) {
1122
+ var str = grafs[i];
1123
+
1124
+ // if this is an HTML marker, copy it
1125
+ if (str.search(/~K(\d+)K/g) >= 0) {
1126
+ grafsOut.push(str);
1127
+ }
1128
+ else if (str.search(/\S/) >= 0) {
1129
+ str = _RunSpanGamut(str);
1130
+ str = str.replace(/\n/g, "<br />"); // ** GFM **
1131
+ str = str.replace(/^([ \t]*)/g, "<p>");
1132
+ str += "</p>";
1133
+ grafsOut.push(str);
1134
+ }
1135
+
1136
+ }
1137
+
1138
+ //
1139
+ // Unhashify HTML blocks
1140
+ //
1141
+ end = grafsOut.length;
1142
+ for (var i = 0; i < end; i++) {
1143
+ // if this is a marker for an html block...
1144
+ while (grafsOut[i].search(/~K(\d+)K/) >= 0) {
1145
+ var blockText = g_html_blocks[RegExp.$1];
1146
+ blockText = blockText.replace(/\$/g, "$$$$"); // Escape any dollar signs
1147
+ grafsOut[i] = grafsOut[i].replace(/~K\d+K/, blockText);
1148
+ }
1149
+ }
1150
+
1151
+ return grafsOut.join("\n\n");
1152
+ };
1153
+
1154
+ // Smart processing for ampersands and angle brackets that need to be encoded.
1155
+ var _EncodeAmpsAndAngles = function(text) {
1156
+
1157
+ // Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
1158
+ // http://bumppo.net/projects/amputator/
1159
+ text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, "&amp;");
1160
+
1161
+ // Encode naked <'s
1162
+ text = text.replace(/<(?![a-z\/?\$!])/gi, "&lt;");
1163
+
1164
+ return text;
1165
+ };
1166
+
1167
+ //
1168
+ // Parameter: String.
1169
+ // Returns: The string, with after processing the following backslash
1170
+ // escape sequences.
1171
+ //
1172
+ var _EncodeBackslashEscapes = function(text) {
1173
+ // attacklab: The polite way to do this is with the new
1174
+ // escapeCharacters() function:
1175
+ //
1176
+ // text = escapeCharacters(text,"\\",true);
1177
+ // text = escapeCharacters(text,"`*_{}[]()>#+-.!",true);
1178
+ //
1179
+ // ...but we're sidestepping its use of the (slow) RegExp constructor
1180
+ // as an optimization for Firefox. This function gets called a LOT.
1181
+
1182
+ text = text.replace(/\\(\\)/g, escapeCharacters_callback);
1183
+ text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g, escapeCharacters_callback);
1184
+ return text;
1185
+ };
1186
+
1187
+ var _DoAutoLinks = function(text) {
1188
+
1189
+ text = text.replace(/<((https?|ftp|dict):[^'">\s]+)>/gi, "<a href=\"$1\">$1</a>");
1190
+
1191
+ // Email addresses: <address@domain.foo>
1192
+
1193
+ /*
1194
+ text = text.replace(/
1195
+ <
1196
+ (?:mailto:)?
1197
+ (
1198
+ [-.\w]+
1199
+ \@
1200
+ [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+
1201
+ )
1202
+ >
1203
+ /gi, _DoAutoLinks_callback());
1204
+ */
1205
+ text = text.replace(/<(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi, function(wholeMatch, m1) {
1206
+ return _EncodeEmailAddress(_UnescapeSpecialChars(m1));
1207
+ });
1208
+
1209
+ return text;
1210
+ };
1211
+
1212
+ //
1213
+ // Input: an email address, e.g. "foo@example.com"
1214
+ //
1215
+ // Output: the email address as a mailto link, with each character
1216
+ // of the address encoded as either a decimal or hex entity, in
1217
+ // the hopes of foiling most address harvesting spam bots. E.g.:
1218
+ //
1219
+ // <a href="&#x6D;&#97;&#105;&#108;&#x74;&#111;:&#102;&#111;&#111;&#64;&#101;
1220
+ // x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;">&#102;&#111;&#111;
1221
+ // &#64;&#101;x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;</a>
1222
+ //
1223
+ // Based on a filter by Matthew Wickline, posted to the BBEdit-Talk
1224
+ // mailing list: <http://tinyurl.com/yu7ue>
1225
+ //
1226
+ var _EncodeEmailAddress = function(addr) {
1227
+
1228
+ // attacklab: why can't javascript speak hex?
1229
+ function char2hex(ch) {
1230
+ var hexDigits = '0123456789ABCDEF';
1231
+ var dec = ch.charCodeAt(0);
1232
+ return(hexDigits.charAt(dec >> 4) + hexDigits.charAt(dec & 15));
1233
+ }
1234
+
1235
+ var encode = [
1236
+ function(ch) { return "&#" + ch.charCodeAt(0) + ";" },
1237
+ function(ch) { return "&#x" + char2hex(ch) + ";" },
1238
+ function(ch) { return ch }
1239
+ ];
1240
+
1241
+ addr = "mailto:" + addr;
1242
+
1243
+ addr = addr.replace(/./g, function(ch) {
1244
+ if (ch == "@") {
1245
+ // this *must* be encoded. I insist.
1246
+ ch = encode[Math.floor(Math.random() * 2)](ch);
1247
+ } else if (ch != ":") {
1248
+ // leave ':' alone (to spot mailto: later)
1249
+ var r = Math.random();
1250
+ // roughly 10% raw, 45% hex, 45% dec
1251
+ ch = (r > .9 ? encode[2](ch) : r > .45 ? encode[1](ch) : encode[0](ch));
1252
+ }
1253
+ return ch;
1254
+ });
1255
+
1256
+ addr = "<a href=\"" + addr + "\">" + addr + "</a>";
1257
+ addr = addr.replace(/">.+:/g, "\">"); // strip the mailto: from the visible part
1258
+
1259
+ return addr;
1260
+ };
1261
+
1262
+ //
1263
+ // Swap back in all the special characters we've hidden.
1264
+ //
1265
+ var _UnescapeSpecialChars = function(text) {
1266
+ text = text.replace(/~E(\d+)E/g, function(wholeMatch, m1) {
1267
+ var charCodeToReplace = parseInt(m1);
1268
+ return String.fromCharCode(charCodeToReplace);
1269
+ });
1270
+ return text;
1271
+ };
1272
+
1273
+ //
1274
+ // Remove one level of line-leading tabs or spaces
1275
+ //
1276
+ var _Outdent = function(text) {
1277
+
1278
+ // attacklab: hack around Konqueror 3.5.4 bug:
1279
+ // "----------bug".replace(/^-/g,"") == "bug"
1280
+
1281
+ text = text.replace(/^(\t|[ ]{1,4})/gm, "~0"); // attacklab: g_tab_width
1282
+
1283
+ // attacklab: clean up hack
1284
+ text = text.replace(/~0/g, "");
1285
+
1286
+ return text;
1287
+ };
1288
+
1289
+ // attacklab: Detab's completely rewritten for speed.
1290
+ // In perl we could fix it by anchoring the regexp with \G.
1291
+ // In javascript we're less fortunate.
1292
+ var _Detab = function(text) {
1293
+ // expand first n-1 tabs
1294
+ text = text.replace(/\t(?=\t)/g, " "); // attacklab: g_tab_width
1295
+
1296
+ // replace the nth with two sentinels
1297
+ text = text.replace(/\t/g, "~A~B");
1298
+
1299
+ // use the sentinel to anchor our regex so it doesn't explode
1300
+ text = text.replace(/~B(.+?)~A/g, function(wholeMatch, m1, m2) {
1301
+ var leadingText = m1;
1302
+ var numSpaces = 4 - leadingText.length % 4; // attacklab: g_tab_width
1303
+
1304
+ // there *must* be a better way to do this:
1305
+ for (var i = 0; i < numSpaces; i++) leadingText += " ";
1306
+
1307
+ return leadingText;
1308
+ });
1309
+
1310
+ // clean up sentinels
1311
+ text = text.replace(/~A/g, " "); // attacklab: g_tab_width
1312
+ text = text.replace(/~B/g, "");
1313
+
1314
+ return text;
1315
+ };
1316
+
1317
+
1318
+ // attacklab: Utility functions
1319
+ var escapeCharacters = function(text, charsToEscape, afterBackslash) {
1320
+ // First we have to escape the escape characters so that
1321
+ // we can build a character class out of them
1322
+ var regexString = "([" + charsToEscape.replace(/([\[\]\\])/g, "\\$1") + "])";
1323
+
1324
+ if (afterBackslash) {
1325
+ regexString = "\\\\" + regexString;
1326
+ }
1327
+
1328
+ var regex = new RegExp(regexString, "g");
1329
+ text = text.replace(regex, escapeCharacters_callback);
1330
+
1331
+ return text;
1332
+ };
1333
+
1356
1334
 
1357
- var escapeCharacters_callback = function(wholeMatch,m1) {
1358
- var charCodeToEscape = m1.charCodeAt(0);
1359
- return "~E"+charCodeToEscape+"E";
1360
- }
1335
+ var escapeCharacters_callback = function(wholeMatch, m1) {
1336
+ var charCodeToEscape = m1.charCodeAt(0);
1337
+ return "~E" + charCodeToEscape + "E";
1338
+ };
1361
1339
 
1362
- } // end of Showdown.converter
1340
+ };