praxis 0.13.0 → 0.14.0

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 (135) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -1
  3. data/.travis.yml +15 -2
  4. data/CHANGELOG.md +54 -1
  5. data/bin/praxis +49 -2
  6. data/lib/api_browser/Gruntfile.js +247 -90
  7. data/lib/api_browser/app/bower_components/angular-mocks/.bower.json +19 -0
  8. data/lib/api_browser/app/bower_components/angular-mocks/README.md +57 -0
  9. data/lib/api_browser/app/bower_components/angular-mocks/angular-mocks.js +2193 -0
  10. data/lib/api_browser/app/bower_components/angular-mocks/bower.json +9 -0
  11. data/lib/api_browser/app/bower_components/angular-mocks/package.json +27 -0
  12. data/lib/api_browser/app/bower_components/angular/.bower.json +6 -5
  13. data/lib/api_browser/app/bower_components/angular/README.md +23 -4
  14. data/lib/api_browser/app/bower_components/angular/angular-csp.css +6 -0
  15. data/lib/api_browser/app/bower_components/angular/angular.js +2287 -1597
  16. data/lib/api_browser/app/bower_components/angular/angular.min.js +212 -205
  17. data/lib/api_browser/app/bower_components/angular/angular.min.js.gzip +0 -0
  18. data/lib/api_browser/app/bower_components/angular/angular.min.js.map +3 -3
  19. data/lib/api_browser/app/bower_components/angular/bower.json +2 -1
  20. data/lib/api_browser/app/bower_components/angular/package.json +25 -0
  21. data/lib/api_browser/app/bower_components/showdown/.bower.json +39 -0
  22. data/lib/api_browser/app/bower_components/showdown/.jshintignore +2 -0
  23. data/lib/api_browser/app/bower_components/showdown/.travis.yml +8 -0
  24. data/lib/api_browser/app/bower_components/showdown/Gruntfile.js +100 -0
  25. data/lib/api_browser/app/bower_components/showdown/README.md +317 -0
  26. data/lib/api_browser/app/bower_components/showdown/bower.json +26 -0
  27. data/lib/api_browser/app/bower_components/showdown/compressed/Showdown.js +1606 -0
  28. data/lib/api_browser/app/bower_components/showdown/compressed/Showdown.js.map +1 -0
  29. data/lib/api_browser/app/bower_components/showdown/compressed/Showdown.min.js +2 -0
  30. data/lib/api_browser/app/bower_components/showdown/compressed/extensions/github.min.js +2 -0
  31. data/lib/api_browser/app/bower_components/showdown/compressed/extensions/github.min.js.map +1 -0
  32. data/lib/api_browser/app/bower_components/showdown/compressed/extensions/prettify.min.js +2 -0
  33. data/lib/api_browser/app/bower_components/showdown/compressed/extensions/prettify.min.js.map +1 -0
  34. data/lib/api_browser/app/bower_components/showdown/compressed/extensions/table.min.js +2 -0
  35. data/lib/api_browser/app/bower_components/showdown/compressed/extensions/table.min.js.map +1 -0
  36. data/lib/api_browser/app/bower_components/showdown/compressed/extensions/twitter.min.js +2 -0
  37. data/lib/api_browser/app/bower_components/showdown/compressed/extensions/twitter.min.js.map +1 -0
  38. data/lib/api_browser/app/bower_components/showdown/license.txt +34 -0
  39. data/lib/api_browser/app/bower_components/showdown/package.json +47 -0
  40. data/lib/api_browser/app/bower_components/showdown/src/extensions/github.js +25 -0
  41. data/lib/api_browser/app/bower_components/showdown/src/extensions/prettify.js +29 -0
  42. data/lib/api_browser/app/bower_components/showdown/src/extensions/table.js +106 -0
  43. data/lib/api_browser/app/bower_components/showdown/src/extensions/twitter.js +42 -0
  44. data/lib/api_browser/app/bower_components/showdown/src/ng-showdown.js +150 -0
  45. data/lib/api_browser/app/bower_components/showdown/src/showdown.js +1454 -0
  46. data/lib/api_browser/app/index.html +6 -4
  47. data/lib/api_browser/app/js/app.js +1 -2
  48. data/lib/api_browser/app/js/controllers/action.js +4 -4
  49. data/lib/api_browser/app/js/controllers/controller.js +1 -1
  50. data/lib/api_browser/app/js/controllers/menu.js +5 -3
  51. data/lib/api_browser/app/js/controllers/type.js +5 -5
  52. data/lib/api_browser/app/js/directives/attribute_description.js +5 -5
  53. data/lib/api_browser/app/js/directives/attribute_table.js +1 -1
  54. data/lib/api_browser/app/js/directives/attribute_table_row.js +2 -2
  55. data/lib/api_browser/app/js/directives/no_container.js +1 -1
  56. data/lib/api_browser/app/js/directives/request_body.js +5 -5
  57. data/lib/api_browser/app/js/directives/request_headers.js +3 -6
  58. data/lib/api_browser/app/js/directives/request_parameters.js +3 -6
  59. data/lib/api_browser/app/js/directives/type_label.js +4 -5
  60. data/lib/api_browser/app/js/factories/Documentation.js +4 -4
  61. data/lib/api_browser/app/js/factories/PayloadTemplates.js +2 -2
  62. data/lib/api_browser/app/js/factories/TypeTemplates.js +3 -3
  63. data/lib/api_browser/app/js/filters/markdown.js +6 -0
  64. data/lib/api_browser/app/js/filters/resource_name.js +2 -2
  65. data/lib/api_browser/app/sass/modules/_header.scss +2 -7
  66. data/lib/api_browser/app/sass/{main.scss → praxis.scss} +0 -0
  67. data/lib/api_browser/app/sass/variables/_bootstrap-variables.scss +370 -367
  68. data/lib/api_browser/app/views/action.html +2 -2
  69. data/lib/api_browser/app/views/controller.html +2 -2
  70. data/lib/api_browser/app/views/directives/attribute_description.html +1 -1
  71. data/lib/api_browser/app/views/layout.html +2 -11
  72. data/lib/api_browser/app/views/navbar.html +9 -0
  73. data/lib/api_browser/app/views/resource/_actions.html +1 -1
  74. data/lib/api_browser/app/views/type.html +2 -2
  75. data/lib/api_browser/app/views/type/_details.html +2 -1
  76. data/lib/api_browser/bower.json +5 -0
  77. data/lib/api_browser/package.json +18 -7
  78. data/lib/praxis.rb +8 -3
  79. data/lib/praxis/action_definition.rb +28 -6
  80. data/lib/praxis/api_definition.rb +30 -2
  81. data/lib/praxis/api_general_info.rb +36 -0
  82. data/lib/praxis/bootloader.rb +1 -0
  83. data/lib/praxis/collection.rb +34 -0
  84. data/lib/praxis/controller.rb +7 -0
  85. data/lib/praxis/dispatcher.rb +3 -0
  86. data/lib/praxis/links.rb +2 -8
  87. data/lib/praxis/media_type.rb +6 -24
  88. data/lib/praxis/media_type_collection.rb +6 -2
  89. data/lib/praxis/plugin_concern.rb +2 -1
  90. data/lib/praxis/request.rb +24 -15
  91. data/lib/praxis/request_stages/request_stage.rb +19 -4
  92. data/lib/praxis/request_stages/validate_params_and_headers.rb +1 -1
  93. data/lib/praxis/request_stages/validate_payload.rb +1 -1
  94. data/lib/praxis/resource_definition.rb +45 -10
  95. data/lib/praxis/response_definition.rb +46 -27
  96. data/lib/praxis/restful_doc_generator.rb +94 -7
  97. data/lib/praxis/simple_media_type.rb +2 -9
  98. data/lib/praxis/stage.rb +1 -4
  99. data/lib/praxis/tasks/api_docs.rb +51 -19
  100. data/lib/praxis/tasks/routes.rb +19 -15
  101. data/lib/praxis/types/media_type_common.rb +31 -0
  102. data/lib/praxis/types/multipart.rb +4 -4
  103. data/lib/praxis/version.rb +1 -1
  104. data/praxis.gemspec +2 -2
  105. data/spec/api_browser/factories/documentation_spec.js +50 -0
  106. data/spec/api_browser/filters/attribute_name_spec.js +23 -0
  107. data/spec/functional_spec.rb +62 -10
  108. data/spec/praxis/action_definition_spec.rb +12 -4
  109. data/spec/praxis/api_definition_spec.rb +159 -0
  110. data/spec/praxis/api_general_info_spec.rb +36 -0
  111. data/spec/praxis/bootloader_spec.rb +10 -1
  112. data/spec/praxis/media_type_collection_spec.rb +46 -53
  113. data/spec/praxis/media_type_spec.rb +6 -6
  114. data/spec/praxis/request_stage_spec.rb +7 -2
  115. data/spec/praxis/request_stages_validate_spec.rb +12 -7
  116. data/spec/praxis/resource_definition_spec.rb +62 -0
  117. data/spec/praxis/response_definition_spec.rb +26 -16
  118. data/spec/praxis/stage_spec.rb +4 -8
  119. data/spec/praxis/types/collection_spec.rb +144 -0
  120. data/spec/spec_app/app/controllers/instances.rb +8 -2
  121. data/spec/spec_app/design/api.rb +11 -0
  122. data/spec/spec_app/design/media_types/instance.rb +12 -0
  123. data/spec/spec_app/design/media_types/volume.rb +9 -2
  124. data/spec/spec_app/design/media_types/volume_snapshot.rb +9 -6
  125. data/spec/spec_app/design/resources/instances.rb +25 -10
  126. data/spec/support/spec_media_types.rb +1 -1
  127. data/spec/support/spec_resource_definitions.rb +2 -0
  128. data/tasks/thor/app.rb +15 -10
  129. data/tasks/thor/example.rb +115 -115
  130. data/tasks/thor/templates/generator/empty_app/.gitignore +2 -0
  131. data/tasks/thor/templates/generator/empty_app/docs/app.js +1 -0
  132. data/tasks/thor/templates/generator/empty_app/docs/styles.scss +3 -0
  133. metadata +50 -9
  134. data/lib/api_browser/app/css/main.css +0 -4511
  135. data/lib/praxis/types/collection.rb +0 -17
@@ -0,0 +1,1454 @@
1
+ //
2
+ // showdown.js -- A javascript port of Markdown.
3
+ //
4
+ // Copyright (c) 2007 John Fraser.
5
+ //
6
+ // Original Markdown Copyright (c) 2004-2005 John Gruber
7
+ // <http://daringfireball.net/projects/markdown/>
8
+ //
9
+ // Redistributable under a BSD-style open source license.
10
+ // See license.txt for more information.
11
+ //
12
+ // The full source distribution is at:
13
+ //
14
+ // A A L
15
+ // T C A
16
+ // T K B
17
+ //
18
+ // <http://www.attacklab.net/>
19
+ //
20
+
21
+ //
22
+ // Wherever possible, Showdown is a straight, line-by-line port
23
+ // of the Perl version of Markdown.
24
+ //
25
+ // This is not a normal parser design; it's basically just a
26
+ // series of string substitutions. It's hard to read and
27
+ // maintain this way, but keeping Showdown close to the original
28
+ // design makes it easier to port new features.
29
+ //
30
+ // More importantly, Showdown behaves like markdown.pl in most
31
+ // edge cases. So web applications can do client-side preview
32
+ // in Javascript, and then build identical HTML on the server.
33
+ //
34
+ // This port needs the new RegExp functionality of ECMA 262,
35
+ // 3rd Edition (i.e. Javascript 1.5). Most modern web browsers
36
+ // should do fine. Even with the new regular expression features,
37
+ // We do a lot of work to emulate Perl's regex functionality.
38
+ // The tricky changes in this file mostly have the "attacklab:"
39
+ // label. Major or self-explanatory changes don't.
40
+ //
41
+ // Smart diff tools like Araxis Merge will be able to match up
42
+ // this file with markdown.pl in a useful way. A little tweaking
43
+ // helps: in a copy of markdown.pl, replace "#" with "//" and
44
+ // replace "$text" with "text". Be sure to ignore whitespace
45
+ // and line endings.
46
+ //
47
+
48
+
49
+ //
50
+ // Showdown usage:
51
+ //
52
+ // var text = "Markdown *rocks*.";
53
+ //
54
+ // var converter = new Showdown.converter();
55
+ // var html = converter.makeHtml(text);
56
+ //
57
+ // alert(html);
58
+ //
59
+ // Note: move the sample code to the bottom of this
60
+ // file before uncommenting it.
61
+ //
62
+
63
+
64
+ //
65
+ // Showdown namespace
66
+ //
67
+ var Showdown = {extensions: {}};
68
+
69
+ //
70
+ // forEach
71
+ //
72
+ var forEach = Showdown.forEach = function (obj, callback) {
73
+ if (typeof obj.forEach === 'function') {
74
+ obj.forEach(callback);
75
+ } else {
76
+ var i, len = obj.length;
77
+ for (i = 0; i < len; i++) {
78
+ callback(obj[i], i, obj);
79
+ }
80
+ }
81
+ };
82
+
83
+ //
84
+ // Standard extension naming
85
+ //
86
+ var stdExtName = function (s) {
87
+ return s.replace(/[_-]||\s/g, '').toLowerCase();
88
+ };
89
+
90
+ //
91
+ // converter
92
+ //
93
+ // Wraps all "globals" so that the only thing
94
+ // exposed is makeHtml().
95
+ //
96
+ Showdown.converter = function (converter_options) {
97
+
98
+ //
99
+ // Globals:
100
+ //
101
+
102
+ // Global hashes, used by various utility routines
103
+ var g_urls;
104
+ var g_titles;
105
+ var g_html_blocks;
106
+
107
+ // Used to track when we're inside an ordered or unordered list
108
+ // (see _ProcessListItems() for details):
109
+ var g_list_level = 0;
110
+
111
+ // Global extensions
112
+ var g_lang_extensions = [];
113
+ var g_output_modifiers = [];
114
+
115
+
116
+ //
117
+ // Automatic Extension Loading (node only):
118
+ //
119
+ if (typeof module !== 'undefined' && typeof exports !== 'undefined' && typeof require !== 'undefined') {
120
+ var fs = require('fs');
121
+
122
+ if (fs) {
123
+ // Search extensions folder
124
+ var extensions = fs.readdirSync((__dirname || '.') + '/extensions').filter(function (file) {
125
+ return ~file.indexOf('.js');
126
+ }).map(function (file) {
127
+ return file.replace(/\.js$/, '');
128
+ });
129
+ // Load extensions into Showdown namespace
130
+ Showdown.forEach(extensions, function (ext) {
131
+ var name = stdExtName(ext);
132
+ Showdown.extensions[name] = require('./extensions/' + ext);
133
+ });
134
+ }
135
+ }
136
+
137
+ this.makeHtml = function (text) {
138
+ //
139
+ // Main function. The order in which other subs are called here is
140
+ // essential. Link and image substitutions need to happen before
141
+ // _EscapeSpecialCharsWithinTagAttributes(), so that any *'s or _'s in the <a>
142
+ // and <img> tags get encoded.
143
+ //
144
+
145
+ // Clear the global hashes. If we don't clear these, you get conflicts
146
+ // from other articles when generating a page which contains more than
147
+ // one article (e.g. an index page that shows the N most recent
148
+ // articles):
149
+ g_urls = {};
150
+ g_titles = {};
151
+ g_html_blocks = [];
152
+
153
+ // attacklab: Replace ~ with ~T
154
+ // This lets us use tilde as an escape char to avoid md5 hashes
155
+ // The choice of character is arbitray; anything that isn't
156
+ // magic in Markdown will work.
157
+ text = text.replace(/~/g, "~T");
158
+
159
+ // attacklab: Replace $ with ~D
160
+ // RegExp interprets $ as a special character
161
+ // when it's in a replacement string
162
+ text = text.replace(/\$/g, "~D");
163
+
164
+ // Standardize line endings
165
+ text = text.replace(/\r\n/g, "\n"); // DOS to Unix
166
+ text = text.replace(/\r/g, "\n"); // Mac to Unix
167
+
168
+ // Make sure text begins and ends with a couple of newlines:
169
+ text = "\n\n" + text + "\n\n";
170
+
171
+ // Convert all tabs to spaces.
172
+ text = _Detab(text);
173
+
174
+ // Strip any lines consisting only of spaces and tabs.
175
+ // This makes subsequent regexen easier to write, because we can
176
+ // match consecutive blank lines with /\n+/ instead of something
177
+ // contorted like /[ \t]*\n+/ .
178
+ text = text.replace(/^[ \t]+$/mg, "");
179
+
180
+ // Run language extensions
181
+ Showdown.forEach(g_lang_extensions, function (x) {
182
+ text = _ExecuteExtension(x, text);
183
+ });
184
+
185
+ // Handle github codeblocks prior to running HashHTML so that
186
+ // HTML contained within the codeblock gets escaped propertly
187
+ text = _DoGithubCodeBlocks(text);
188
+
189
+ // Turn block-level HTML blocks into hash entries
190
+ text = _HashHTMLBlocks(text);
191
+
192
+ // Strip link definitions, store in hashes.
193
+ text = _StripLinkDefinitions(text);
194
+
195
+ text = _RunBlockGamut(text);
196
+
197
+ text = _UnescapeSpecialChars(text);
198
+
199
+ // attacklab: Restore dollar signs
200
+ text = text.replace(/~D/g, "$$");
201
+
202
+ // attacklab: Restore tildes
203
+ text = text.replace(/~T/g, "~");
204
+
205
+ // Run output modifiers
206
+ Showdown.forEach(g_output_modifiers, function (x) {
207
+ text = _ExecuteExtension(x, text);
208
+ });
209
+
210
+ return text;
211
+ };
212
+
213
+
214
+ //
215
+ // Options:
216
+ //
217
+
218
+ // Parse extensions options into separate arrays
219
+ if (converter_options && converter_options.extensions) {
220
+
221
+ var self = this;
222
+
223
+ // Iterate over each plugin
224
+ Showdown.forEach(converter_options.extensions, function (plugin) {
225
+
226
+ // Assume it's a bundled plugin if a string is given
227
+ if (typeof plugin === 'string') {
228
+ plugin = Showdown.extensions[stdExtName(plugin)];
229
+ }
230
+
231
+ if (typeof plugin === 'function') {
232
+ // Iterate over each extension within that plugin
233
+ Showdown.forEach(plugin(self), function (ext) {
234
+ // Sort extensions by type
235
+ if (ext.type) {
236
+ if (ext.type === 'language' || ext.type === 'lang') {
237
+ g_lang_extensions.push(ext);
238
+ } else if (ext.type === 'output' || ext.type === 'html') {
239
+ g_output_modifiers.push(ext);
240
+ }
241
+ } else {
242
+ // Assume language extension
243
+ g_output_modifiers.push(ext);
244
+ }
245
+ });
246
+ } else {
247
+ throw "Extension '" + plugin + "' could not be loaded. It was either not found or is not a valid extension.";
248
+ }
249
+ });
250
+ }
251
+
252
+
253
+ var _ExecuteExtension = function (ext, text) {
254
+ if (ext.regex) {
255
+ var re = new RegExp(ext.regex, 'g');
256
+ return text.replace(re, ext.replace);
257
+ } else if (ext.filter) {
258
+ return ext.filter(text);
259
+ }
260
+ };
261
+
262
+ var _StripLinkDefinitions = function (text) {
263
+ //
264
+ // Strips link definitions from text, stores the URLs and titles in
265
+ // hash references.
266
+ //
267
+
268
+ // Link defs are in the form: ^[id]: url "optional title"
269
+
270
+ /*
271
+ var text = text.replace(/
272
+ ^[ ]{0,3}\[(.+)\]: // id = $1 attacklab: g_tab_width - 1
273
+ [ \t]*
274
+ \n? // maybe *one* newline
275
+ [ \t]*
276
+ <?(\S+?)>? // url = $2
277
+ [ \t]*
278
+ \n? // maybe one newline
279
+ [ \t]*
280
+ (?:
281
+ (\n*) // any lines skipped = $3 attacklab: lookbehind removed
282
+ ["(]
283
+ (.+?) // title = $4
284
+ [")]
285
+ [ \t]*
286
+ )? // title is optional
287
+ (?:\n+|$)
288
+ /gm,
289
+ function(){...});
290
+ */
291
+
292
+ // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
293
+ text += "~0";
294
+
295
+ text = text.replace(/^[ ]{0,3}\[(.+)\]:[ \t]*\n?[ \t]*<?(\S+?)>?[ \t]*\n?[ \t]*(?:(\n*)["(](.+?)[")][ \t]*)?(?:\n+|(?=~0))/gm,
296
+ function (wholeMatch, m1, m2, m3, m4) {
297
+ m1 = m1.toLowerCase();
298
+ g_urls[m1] = _EncodeAmpsAndAngles(m2); // Link IDs are case-insensitive
299
+ if (m3) {
300
+ // Oops, found blank lines, so it's not a title.
301
+ // Put back the parenthetical statement we stole.
302
+ return m3 + m4;
303
+ } else if (m4) {
304
+ g_titles[m1] = m4.replace(/"/g, "&quot;");
305
+ }
306
+
307
+ // Completely remove the definition from the text
308
+ return "";
309
+ }
310
+ );
311
+
312
+ // attacklab: strip sentinel
313
+ text = text.replace(/~0/, "");
314
+
315
+ return text;
316
+ }
317
+
318
+ var _HashHTMLBlocks = function (text) {
319
+ // attacklab: Double up blank lines to reduce lookaround
320
+ text = text.replace(/\n/g, "\n\n");
321
+
322
+ // Hashify HTML blocks:
323
+ // We only want to do this for block-level HTML tags, such as headers,
324
+ // lists, and tables. That's because we still want to wrap <p>s around
325
+ // "paragraphs" that are wrapped in non-block-level tags, such as anchors,
326
+ // phrase emphasis, and spans. The list of tags we're looking for is
327
+ // hard-coded:
328
+ var block_tags_a = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del|style|section|header|footer|nav|article|aside";
329
+ var block_tags_b = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|style|section|header|footer|nav|article|aside";
330
+
331
+ // First, look for nested blocks, e.g.:
332
+ // <div>
333
+ // <div>
334
+ // tags for inner block must be indented.
335
+ // </div>
336
+ // </div>
337
+ //
338
+ // The outermost tags must start at the left margin for this to match, and
339
+ // the inner nested divs must be indented.
340
+ // We need to do this before the next, more liberal match, because the next
341
+ // match will start at the first `<div>` and stop at the first `</div>`.
342
+
343
+ // attacklab: This regex can be expensive when it fails.
344
+ /*
345
+ var text = text.replace(/
346
+ ( // save in $1
347
+ ^ // start of line (with /m)
348
+ <($block_tags_a) // start tag = $2
349
+ \b // word break
350
+ // attacklab: hack around khtml/pcre bug...
351
+ [^\r]*?\n // any number of lines, minimally matching
352
+ </\2> // the matching end tag
353
+ [ \t]* // trailing spaces/tabs
354
+ (?=\n+) // followed by a newline
355
+ ) // attacklab: there are sentinel newlines at end of document
356
+ /gm,function(){...}};
357
+ */
358
+ 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);
359
+
360
+ //
361
+ // Now match more liberally, simply from `\n<tag>` to `</tag>\n`
362
+ //
363
+
364
+ /*
365
+ var text = text.replace(/
366
+ ( // save in $1
367
+ ^ // start of line (with /m)
368
+ <($block_tags_b) // start tag = $2
369
+ \b // word break
370
+ // attacklab: hack around khtml/pcre bug...
371
+ [^\r]*? // any number of lines, minimally matching
372
+ </\2> // the matching end tag
373
+ [ \t]* // trailing spaces/tabs
374
+ (?=\n+) // followed by a newline
375
+ ) // attacklab: there are sentinel newlines at end of document
376
+ /gm,function(){...}};
377
+ */
378
+ text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|style|section|header|footer|nav|article|aside)\b[^\r]*?<\/\2>[ \t]*(?=\n+)\n)/gm, hashElement);
379
+
380
+ // Special case just for <hr />. It was easier to make a special case than
381
+ // to make the other regex more complicated.
382
+
383
+ /*
384
+ text = text.replace(/
385
+ ( // save in $1
386
+ \n\n // Starting after a blank line
387
+ [ ]{0,3}
388
+ (<(hr) // start tag = $2
389
+ \b // word break
390
+ ([^<>])*? //
391
+ \/?>) // the matching end tag
392
+ [ \t]*
393
+ (?=\n{2,}) // followed by a blank line
394
+ )
395
+ /g,hashElement);
396
+ */
397
+ text = text.replace(/(\n[ ]{0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g, hashElement);
398
+
399
+ // Special case for standalone HTML comments:
400
+
401
+ /*
402
+ text = text.replace(/
403
+ ( // save in $1
404
+ \n\n // Starting after a blank line
405
+ [ ]{0,3} // attacklab: g_tab_width - 1
406
+ <!
407
+ (--[^\r]*?--\s*)+
408
+ >
409
+ [ \t]*
410
+ (?=\n{2,}) // followed by a blank line
411
+ )
412
+ /g,hashElement);
413
+ */
414
+ text = text.replace(/(\n\n[ ]{0,3}<!(--[^\r]*?--\s*)+>[ \t]*(?=\n{2,}))/g, hashElement);
415
+
416
+ // PHP and ASP-style processor instructions (<?...?> and <%...%>)
417
+
418
+ /*
419
+ text = text.replace(/
420
+ (?:
421
+ \n\n // Starting after a blank line
422
+ )
423
+ ( // save in $1
424
+ [ ]{0,3} // attacklab: g_tab_width - 1
425
+ (?:
426
+ <([?%]) // $2
427
+ [^\r]*?
428
+ \2>
429
+ )
430
+ [ \t]*
431
+ (?=\n{2,}) // followed by a blank line
432
+ )
433
+ /g,hashElement);
434
+ */
435
+ text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g, hashElement);
436
+
437
+ // attacklab: Undo double lines (see comment at top of this function)
438
+ text = text.replace(/\n\n/g, "\n");
439
+ return text;
440
+ }
441
+
442
+ var hashElement = function (wholeMatch, m1) {
443
+ var blockText = m1;
444
+
445
+ // Undo double lines
446
+ blockText = blockText.replace(/\n\n/g, "\n");
447
+ blockText = blockText.replace(/^\n/, "");
448
+
449
+ // strip trailing blank lines
450
+ blockText = blockText.replace(/\n+$/g, "");
451
+
452
+ // Replace the element text with a marker ("~KxK" where x is its key)
453
+ blockText = "\n\n~K" + (g_html_blocks.push(blockText) - 1) + "K\n\n";
454
+
455
+ return blockText;
456
+ };
457
+
458
+ var _RunBlockGamut = function (text) {
459
+ //
460
+ // These are all the transformations that form block-level
461
+ // tags like paragraphs, headers, and list items.
462
+ //
463
+ text = _DoHeaders(text);
464
+
465
+ // Do Horizontal Rules:
466
+ var key = hashBlock("<hr />");
467
+ text = text.replace(/^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$/gm, key);
468
+ text = text.replace(/^[ ]{0,2}([ ]?\-[ ]?){3,}[ \t]*$/gm, key);
469
+ text = text.replace(/^[ ]{0,2}([ ]?\_[ ]?){3,}[ \t]*$/gm, key);
470
+
471
+ text = _DoLists(text);
472
+ text = _DoCodeBlocks(text);
473
+ text = _DoBlockQuotes(text);
474
+
475
+ // We already ran _HashHTMLBlocks() before, in Markdown(), but that
476
+ // was to escape raw HTML in the original Markdown source. This time,
477
+ // we're escaping the markup we've just created, so that we don't wrap
478
+ // <p> tags around block-level tags.
479
+ text = _HashHTMLBlocks(text);
480
+ text = _FormParagraphs(text);
481
+
482
+ return text;
483
+ };
484
+
485
+ var _RunSpanGamut = function (text) {
486
+ //
487
+ // These are all the transformations that occur *within* block-level
488
+ // tags like paragraphs, headers, and list items.
489
+ //
490
+
491
+ text = _DoCodeSpans(text);
492
+ text = _EscapeSpecialCharsWithinTagAttributes(text);
493
+ text = _EncodeBackslashEscapes(text);
494
+
495
+ // Process anchor and image tags. Images must come first,
496
+ // because ![foo][f] looks like an anchor.
497
+ text = _DoImages(text);
498
+ text = _DoAnchors(text);
499
+
500
+ // Make links out of things like `<http://example.com/>`
501
+ // Must come after _DoAnchors(), because you can use < and >
502
+ // delimiters in inline links like [this](<url>).
503
+ text = _DoAutoLinks(text);
504
+ text = _EncodeAmpsAndAngles(text);
505
+ text = _DoItalicsAndBold(text);
506
+
507
+ // Do hard breaks:
508
+ text = text.replace(/ +\n/g, " <br />\n");
509
+
510
+ return text;
511
+ }
512
+
513
+ var _EscapeSpecialCharsWithinTagAttributes = function (text) {
514
+ //
515
+ // Within tags -- meaning between < and > -- encode [\ ` * _] so they
516
+ // don't conflict with their use in Markdown for code, italics and strong.
517
+ //
518
+
519
+ // Build a regex to find HTML tags and comments. See Friedl's
520
+ // "Mastering Regular Expressions", 2nd Ed., pp. 200-201.
521
+ var regex = /(<[a-z\/!$]("[^"]*"|'[^']*'|[^'">])*>|<!(--.*?--\s*)+>)/gi;
522
+
523
+ text = text.replace(regex, function (wholeMatch) {
524
+ var tag = wholeMatch.replace(/(.)<\/?code>(?=.)/g, "$1`");
525
+ tag = escapeCharacters(tag, "\\`*_");
526
+ return tag;
527
+ });
528
+
529
+ return text;
530
+ }
531
+
532
+ var _DoAnchors = function (text) {
533
+ //
534
+ // Turn Markdown link shortcuts into XHTML <a> tags.
535
+ //
536
+ //
537
+ // First, handle reference-style links: [link text] [id]
538
+ //
539
+
540
+ /*
541
+ text = text.replace(/
542
+ ( // wrap whole match in $1
543
+ \[
544
+ (
545
+ (?:
546
+ \[[^\]]*\] // allow brackets nested one level
547
+ |
548
+ [^\[] // or anything else
549
+ )*
550
+ )
551
+ \]
552
+
553
+ [ ]? // one optional space
554
+ (?:\n[ ]*)? // one optional newline followed by spaces
555
+
556
+ \[
557
+ (.*?) // id = $3
558
+ \]
559
+ )()()()() // pad remaining backreferences
560
+ /g,_DoAnchors_callback);
561
+ */
562
+ text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeAnchorTag);
563
+
564
+ //
565
+ // Next, inline-style links: [link text](url "optional title")
566
+ //
567
+
568
+ /*
569
+ text = text.replace(/
570
+ ( // wrap whole match in $1
571
+ \[
572
+ (
573
+ (?:
574
+ \[[^\]]*\] // allow brackets nested one level
575
+ |
576
+ [^\[\]] // or anything else
577
+ )
578
+ )
579
+ \]
580
+ \( // literal paren
581
+ [ \t]*
582
+ () // no id, so leave $3 empty
583
+ <?(.*?)>? // href = $4
584
+ [ \t]*
585
+ ( // $5
586
+ (['"]) // quote char = $6
587
+ (.*?) // Title = $7
588
+ \6 // matching quote
589
+ [ \t]* // ignore any spaces/tabs between closing quote and )
590
+ )? // title is optional
591
+ \)
592
+ )
593
+ /g,writeAnchorTag);
594
+ */
595
+ text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeAnchorTag);
596
+
597
+ //
598
+ // Last, handle reference-style shortcuts: [link text]
599
+ // These must come last in case you've also got [link test][1]
600
+ // or [link test](/foo)
601
+ //
602
+
603
+ /*
604
+ text = text.replace(/
605
+ ( // wrap whole match in $1
606
+ \[
607
+ ([^\[\]]+) // link text = $2; can't contain '[' or ']'
608
+ \]
609
+ )()()()()() // pad rest of backreferences
610
+ /g, writeAnchorTag);
611
+ */
612
+ text = text.replace(/(\[([^\[\]]+)\])()()()()()/g, writeAnchorTag);
613
+
614
+ return text;
615
+ }
616
+
617
+ var writeAnchorTag = function (wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
618
+ if (m7 == undefined) m7 = "";
619
+ var whole_match = m1;
620
+ var link_text = m2;
621
+ var link_id = m3.toLowerCase();
622
+ var url = m4;
623
+ var title = m7;
624
+
625
+ if (url == "") {
626
+ if (link_id == "") {
627
+ // lower-case and turn embedded newlines into spaces
628
+ link_id = link_text.toLowerCase().replace(/ ?\n/g, " ");
629
+ }
630
+ url = "#" + link_id;
631
+
632
+ if (g_urls[link_id] != undefined) {
633
+ url = g_urls[link_id];
634
+ if (g_titles[link_id] != undefined) {
635
+ title = g_titles[link_id];
636
+ }
637
+ }
638
+ else {
639
+ if (whole_match.search(/\(\s*\)$/m) > -1) {
640
+ // Special case for explicit empty url
641
+ url = "";
642
+ } else {
643
+ return whole_match;
644
+ }
645
+ }
646
+ }
647
+
648
+ url = escapeCharacters(url, "*_");
649
+ var result = "<a href=\"" + url + "\"";
650
+
651
+ if (title != "") {
652
+ title = title.replace(/"/g, "&quot;");
653
+ title = escapeCharacters(title, "*_");
654
+ result += " title=\"" + title + "\"";
655
+ }
656
+
657
+ result += ">" + link_text + "</a>";
658
+
659
+ return result;
660
+ }
661
+
662
+ var _DoImages = function (text) {
663
+ //
664
+ // Turn Markdown image shortcuts into <img> tags.
665
+ //
666
+
667
+ //
668
+ // First, handle reference-style labeled images: ![alt text][id]
669
+ //
670
+
671
+ /*
672
+ text = text.replace(/
673
+ ( // wrap whole match in $1
674
+ !\[
675
+ (.*?) // alt text = $2
676
+ \]
677
+
678
+ [ ]? // one optional space
679
+ (?:\n[ ]*)? // one optional newline followed by spaces
680
+
681
+ \[
682
+ (.*?) // id = $3
683
+ \]
684
+ )()()()() // pad rest of backreferences
685
+ /g,writeImageTag);
686
+ */
687
+ text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeImageTag);
688
+
689
+ //
690
+ // Next, handle inline images: ![alt text](url "optional title")
691
+ // Don't forget: encode * and _
692
+
693
+ /*
694
+ text = text.replace(/
695
+ ( // wrap whole match in $1
696
+ !\[
697
+ (.*?) // alt text = $2
698
+ \]
699
+ \s? // One optional whitespace character
700
+ \( // literal paren
701
+ [ \t]*
702
+ () // no id, so leave $3 empty
703
+ <?(\S+?)>? // src url = $4
704
+ [ \t]*
705
+ ( // $5
706
+ (['"]) // quote char = $6
707
+ (.*?) // title = $7
708
+ \6 // matching quote
709
+ [ \t]*
710
+ )? // title is optional
711
+ \)
712
+ )
713
+ /g,writeImageTag);
714
+ */
715
+ text = text.replace(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeImageTag);
716
+
717
+ return text;
718
+ }
719
+
720
+ var writeImageTag = function (wholeMatch, m1, m2, m3, m4, m5, m6, m7) {
721
+ var whole_match = m1;
722
+ var alt_text = m2;
723
+ var link_id = m3.toLowerCase();
724
+ var url = m4;
725
+ var title = m7;
726
+
727
+ if (!title) title = "";
728
+
729
+ if (url == "") {
730
+ if (link_id == "") {
731
+ // lower-case and turn embedded newlines into spaces
732
+ link_id = alt_text.toLowerCase().replace(/ ?\n/g, " ");
733
+ }
734
+ url = "#" + link_id;
735
+
736
+ if (g_urls[link_id] != undefined) {
737
+ url = g_urls[link_id];
738
+ if (g_titles[link_id] != undefined) {
739
+ title = g_titles[link_id];
740
+ }
741
+ }
742
+ else {
743
+ return whole_match;
744
+ }
745
+ }
746
+
747
+ alt_text = alt_text.replace(/"/g, "&quot;");
748
+ url = escapeCharacters(url, "*_");
749
+ var result = "<img src=\"" + url + "\" alt=\"" + alt_text + "\"";
750
+
751
+ // attacklab: Markdown.pl adds empty title attributes to images.
752
+ // Replicate this bug.
753
+
754
+ //if (title != "") {
755
+ title = title.replace(/"/g, "&quot;");
756
+ title = escapeCharacters(title, "*_");
757
+ result += " title=\"" + title + "\"";
758
+ //}
759
+
760
+ result += " />";
761
+
762
+ return result;
763
+ }
764
+
765
+ var _DoHeaders = function (text) {
766
+
767
+ // Setext-style headers:
768
+ // Header 1
769
+ // ========
770
+ //
771
+ // Header 2
772
+ // --------
773
+ //
774
+ text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm,
775
+ function (wholeMatch, m1) {
776
+ return hashBlock('<h1 id="' + headerId(m1) + '">' + _RunSpanGamut(m1) + "</h1>");
777
+ });
778
+
779
+ text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm,
780
+ function (matchFound, m1) {
781
+ return hashBlock('<h2 id="' + headerId(m1) + '">' + _RunSpanGamut(m1) + "</h2>");
782
+ });
783
+
784
+ // atx-style headers:
785
+ // # Header 1
786
+ // ## Header 2
787
+ // ## Header 2 with closing hashes ##
788
+ // ...
789
+ // ###### Header 6
790
+ //
791
+
792
+ /*
793
+ text = text.replace(/
794
+ ^(\#{1,6}) // $1 = string of #'s
795
+ [ \t]*
796
+ (.+?) // $2 = Header text
797
+ [ \t]*
798
+ \#* // optional closing #'s (not counted)
799
+ \n+
800
+ /gm, function() {...});
801
+ */
802
+
803
+ text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm,
804
+ function (wholeMatch, m1, m2) {
805
+ var h_level = m1.length;
806
+ return hashBlock("<h" + h_level + ' id="' + headerId(m2) + '">' + _RunSpanGamut(m2) + "</h" + h_level + ">");
807
+ });
808
+
809
+ function headerId(m) {
810
+ return m.replace(/[^\w]/g, '').toLowerCase();
811
+ }
812
+
813
+ return text;
814
+ }
815
+
816
+ // This declaration keeps Dojo compressor from outputting garbage:
817
+ var _ProcessListItems;
818
+
819
+ var _DoLists = function (text) {
820
+ //
821
+ // Form HTML ordered (numbered) and unordered (bulleted) lists.
822
+ //
823
+
824
+ // attacklab: add sentinel to hack around khtml/safari bug:
825
+ // http://bugs.webkit.org/show_bug.cgi?id=11231
826
+ text += "~0";
827
+
828
+ // Re-usable pattern to match any entirel ul or ol list:
829
+
830
+ /*
831
+ var whole_list = /
832
+ ( // $1 = whole list
833
+ ( // $2
834
+ [ ]{0,3} // attacklab: g_tab_width - 1
835
+ ([*+-]|\d+[.]) // $3 = first list item marker
836
+ [ \t]+
837
+ )
838
+ [^\r]+?
839
+ ( // $4
840
+ ~0 // sentinel for workaround; should be $
841
+ |
842
+ \n{2,}
843
+ (?=\S)
844
+ (?! // Negative lookahead for another list item marker
845
+ [ \t]*
846
+ (?:[*+-]|\d+[.])[ \t]+
847
+ )
848
+ )
849
+ )/g
850
+ */
851
+ var whole_list = /^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
852
+
853
+ if (g_list_level) {
854
+ text = text.replace(whole_list, function (wholeMatch, m1, m2) {
855
+ var list = m1;
856
+ var list_type = (m2.search(/[*+-]/g) > -1) ? "ul" : "ol";
857
+
858
+ // Turn double returns into triple returns, so that we can make a
859
+ // paragraph for the last item in a list, if necessary:
860
+ list = list.replace(/\n{2,}/g, "\n\n\n");
861
+ ;
862
+ var result = _ProcessListItems(list);
863
+
864
+ // Trim any trailing whitespace, to put the closing `</$list_type>`
865
+ // up on the preceding line, to get it past the current stupid
866
+ // HTML block parser. This is a hack to work around the terrible
867
+ // hack that is the HTML block parser.
868
+ result = result.replace(/\s+$/, "");
869
+ result = "<" + list_type + ">" + result + "</" + list_type + ">\n";
870
+ return result;
871
+ });
872
+ } else {
873
+ whole_list = /(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/g;
874
+ text = text.replace(whole_list, function (wholeMatch, m1, m2, m3) {
875
+ var runup = m1;
876
+ var list = m2;
877
+
878
+ var list_type = (m3.search(/[*+-]/g) > -1) ? "ul" : "ol";
879
+ // Turn double returns into triple returns, so that we can make a
880
+ // paragraph for the last item in a list, if necessary:
881
+ var list = list.replace(/\n{2,}/g, "\n\n\n");
882
+ ;
883
+ var result = _ProcessListItems(list);
884
+ result = runup + "<" + list_type + ">\n" + result + "</" + list_type + ">\n";
885
+ return result;
886
+ });
887
+ }
888
+
889
+ // attacklab: strip sentinel
890
+ text = text.replace(/~0/, "");
891
+
892
+ return text;
893
+ }
894
+
895
+ _ProcessListItems = function (list_str) {
896
+ //
897
+ // Process the contents of a single ordered or unordered list, splitting it
898
+ // into individual list items.
899
+ //
900
+ // The $g_list_level global keeps track of when we're inside a list.
901
+ // Each time we enter a list, we increment it; when we leave a list,
902
+ // we decrement. If it's zero, we're not in a list anymore.
903
+ //
904
+ // We do this because when we're not inside a list, we want to treat
905
+ // something like this:
906
+ //
907
+ // I recommend upgrading to version
908
+ // 8. Oops, now this line is treated
909
+ // as a sub-list.
910
+ //
911
+ // As a single paragraph, despite the fact that the second line starts
912
+ // with a digit-period-space sequence.
913
+ //
914
+ // Whereas when we're inside a list (or sub-list), that line will be
915
+ // treated as the start of a sub-list. What a kludge, huh? This is
916
+ // an aspect of Markdown's syntax that's hard to parse perfectly
917
+ // without resorting to mind-reading. Perhaps the solution is to
918
+ // change the syntax rules such that sub-lists must start with a
919
+ // starting cardinal number; e.g. "1." or "a.".
920
+
921
+ g_list_level++;
922
+
923
+ // trim trailing blank lines:
924
+ list_str = list_str.replace(/\n{2,}$/, "\n");
925
+
926
+ // attacklab: add sentinel to emulate \z
927
+ list_str += "~0";
928
+
929
+ /*
930
+ list_str = list_str.replace(/
931
+ (\n)? // leading line = $1
932
+ (^[ \t]*) // leading whitespace = $2
933
+ ([*+-]|\d+[.]) [ \t]+ // list marker = $3
934
+ ([^\r]+? // list item text = $4
935
+ (\n{1,2}))
936
+ (?= \n* (~0 | \2 ([*+-]|\d+[.]) [ \t]+))
937
+ /gm, function(){...});
938
+ */
939
+ list_str = list_str.replace(/(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+([^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm,
940
+ function (wholeMatch, m1, m2, m3, m4) {
941
+ var item = m4;
942
+ var leading_line = m1;
943
+ var leading_space = m2;
944
+
945
+ if (leading_line || (item.search(/\n{2,}/) > -1)) {
946
+ item = _RunBlockGamut(_Outdent(item));
947
+ }
948
+ else {
949
+ // Recursion for sub-lists:
950
+ item = _DoLists(_Outdent(item));
951
+ item = item.replace(/\n$/, ""); // chomp(item)
952
+ item = _RunSpanGamut(item);
953
+ }
954
+
955
+ return "<li>" + item + "</li>\n";
956
+ }
957
+ );
958
+
959
+ // attacklab: strip sentinel
960
+ list_str = list_str.replace(/~0/g, "");
961
+
962
+ g_list_level--;
963
+ return list_str;
964
+ }
965
+
966
+ var _DoCodeBlocks = function (text) {
967
+ //
968
+ // Process Markdown `<pre><code>` blocks.
969
+ //
970
+
971
+ /*
972
+ text = text.replace(text,
973
+ /(?:\n\n|^)
974
+ ( // $1 = the code block -- one or more lines, starting with a space/tab
975
+ (?:
976
+ (?:[ ]{4}|\t) // Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width
977
+ .*\n+
978
+ )+
979
+ )
980
+ (\n*[ ]{0,3}[^ \t\n]|(?=~0)) // attacklab: g_tab_width
981
+ /g,function(){...});
982
+ */
983
+
984
+ // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
985
+ text += "~0";
986
+
987
+ text = text.replace(/(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g,
988
+ function (wholeMatch, m1, m2) {
989
+ var codeblock = m1;
990
+ var nextChar = m2;
991
+
992
+ codeblock = _EncodeCode(_Outdent(codeblock));
993
+ codeblock = _Detab(codeblock);
994
+ codeblock = codeblock.replace(/^\n+/g, ""); // trim leading newlines
995
+ codeblock = codeblock.replace(/\n+$/g, ""); // trim trailing whitespace
996
+
997
+ codeblock = "<pre><code>" + codeblock + "\n</code></pre>";
998
+
999
+ return hashBlock(codeblock) + nextChar;
1000
+ }
1001
+ );
1002
+
1003
+ // attacklab: strip sentinel
1004
+ text = text.replace(/~0/, "");
1005
+
1006
+ return text;
1007
+ };
1008
+
1009
+ var _DoGithubCodeBlocks = function (text) {
1010
+ //
1011
+ // Process Github-style code blocks
1012
+ // Example:
1013
+ // ```ruby
1014
+ // def hello_world(x)
1015
+ // puts "Hello, #{x}"
1016
+ // end
1017
+ // ```
1018
+ //
1019
+
1020
+
1021
+ // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
1022
+ text += "~0";
1023
+
1024
+ text = text.replace(/(?:^|\n)```(.*)\n([\s\S]*?)\n```/g,
1025
+ function (wholeMatch, m1, m2) {
1026
+ var language = m1;
1027
+ var codeblock = m2;
1028
+
1029
+ codeblock = _EncodeCode(codeblock);
1030
+ codeblock = _Detab(codeblock);
1031
+ codeblock = codeblock.replace(/^\n+/g, ""); // trim leading newlines
1032
+ codeblock = codeblock.replace(/\n+$/g, ""); // trim trailing whitespace
1033
+
1034
+ codeblock = "<pre><code" + (language ? " class=\"" + language + '"' : "") + ">" + codeblock + "\n</code></pre>";
1035
+
1036
+ return hashBlock(codeblock);
1037
+ }
1038
+ );
1039
+
1040
+ // attacklab: strip sentinel
1041
+ text = text.replace(/~0/, "");
1042
+
1043
+ return text;
1044
+ }
1045
+
1046
+ var hashBlock = function (text) {
1047
+ text = text.replace(/(^\n+|\n+$)/g, "");
1048
+ return "\n\n~K" + (g_html_blocks.push(text) - 1) + "K\n\n";
1049
+ }
1050
+
1051
+ var _DoCodeSpans = function (text) {
1052
+ //
1053
+ // * Backtick quotes are used for <code></code> spans.
1054
+ //
1055
+ // * You can use multiple backticks as the delimiters if you want to
1056
+ // include literal backticks in the code span. So, this input:
1057
+ //
1058
+ // Just type ``foo `bar` baz`` at the prompt.
1059
+ //
1060
+ // Will translate to:
1061
+ //
1062
+ // <p>Just type <code>foo `bar` baz</code> at the prompt.</p>
1063
+ //
1064
+ // There's no arbitrary limit to the number of backticks you
1065
+ // can use as delimters. If you need three consecutive backticks
1066
+ // in your code, use four for delimiters, etc.
1067
+ //
1068
+ // * You can use spaces to get literal backticks at the edges:
1069
+ //
1070
+ // ... type `` `bar` `` ...
1071
+ //
1072
+ // Turns to:
1073
+ //
1074
+ // ... type <code>`bar`</code> ...
1075
+ //
1076
+
1077
+ /*
1078
+ text = text.replace(/
1079
+ (^|[^\\]) // Character before opening ` can't be a backslash
1080
+ (`+) // $2 = Opening run of `
1081
+ ( // $3 = The code block
1082
+ [^\r]*?
1083
+ [^`] // attacklab: work around lack of lookbehind
1084
+ )
1085
+ \2 // Matching closer
1086
+ (?!`)
1087
+ /gm, function(){...});
1088
+ */
1089
+
1090
+ text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm,
1091
+ function (wholeMatch, m1, m2, m3, m4) {
1092
+ var c = m3;
1093
+ c = c.replace(/^([ \t]*)/g, ""); // leading whitespace
1094
+ c = c.replace(/[ \t]*$/g, ""); // trailing whitespace
1095
+ c = _EncodeCode(c);
1096
+ return m1 + "<code>" + c + "</code>";
1097
+ });
1098
+
1099
+ return text;
1100
+ }
1101
+
1102
+ var _EncodeCode = function (text) {
1103
+ //
1104
+ // Encode/escape certain characters inside Markdown code runs.
1105
+ // The point is that in code, these characters are literals,
1106
+ // and lose their special Markdown meanings.
1107
+ //
1108
+ // Encode all ampersands; HTML entities are not
1109
+ // entities within a Markdown code span.
1110
+ text = text.replace(/&/g, "&amp;");
1111
+
1112
+ // Do the angle bracket song and dance:
1113
+ text = text.replace(/</g, "&lt;");
1114
+ text = text.replace(/>/g, "&gt;");
1115
+
1116
+ // Now, escape characters that are magic in Markdown:
1117
+ text = escapeCharacters(text, "\*_{}[]\\", false);
1118
+
1119
+ // jj the line above breaks this:
1120
+ //---
1121
+
1122
+ //* Item
1123
+
1124
+ // 1. Subitem
1125
+
1126
+ // special char: *
1127
+ //---
1128
+
1129
+ return text;
1130
+ }
1131
+
1132
+ var _DoItalicsAndBold = function (text) {
1133
+
1134
+ // <strong> must go first:
1135
+ text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g,
1136
+ "<strong>$2</strong>");
1137
+
1138
+ text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g,
1139
+ "<em>$2</em>");
1140
+
1141
+ return text;
1142
+ }
1143
+
1144
+ var _DoBlockQuotes = function (text) {
1145
+
1146
+ /*
1147
+ text = text.replace(/
1148
+ ( // Wrap whole match in $1
1149
+ (
1150
+ ^[ \t]*>[ \t]? // '>' at the start of a line
1151
+ .+\n // rest of the first line
1152
+ (.+\n)* // subsequent consecutive lines
1153
+ \n* // blanks
1154
+ )+
1155
+ )
1156
+ /gm, function(){...});
1157
+ */
1158
+
1159
+ text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,
1160
+ function (wholeMatch, m1) {
1161
+ var bq = m1;
1162
+
1163
+ // attacklab: hack around Konqueror 3.5.4 bug:
1164
+ // "----------bug".replace(/^-/g,"") == "bug"
1165
+
1166
+ bq = bq.replace(/^[ \t]*>[ \t]?/gm, "~0"); // trim one level of quoting
1167
+
1168
+ // attacklab: clean up hack
1169
+ bq = bq.replace(/~0/g, "");
1170
+
1171
+ bq = bq.replace(/^[ \t]+$/gm, ""); // trim whitespace-only lines
1172
+ bq = _RunBlockGamut(bq); // recurse
1173
+
1174
+ bq = bq.replace(/(^|\n)/g, "$1 ");
1175
+ // These leading spaces screw with <pre> content, so we need to fix that:
1176
+ bq = bq.replace(
1177
+ /(\s*<pre>[^\r]+?<\/pre>)/gm,
1178
+ function (wholeMatch, m1) {
1179
+ var pre = m1;
1180
+ // attacklab: hack around Konqueror 3.5.4 bug:
1181
+ pre = pre.replace(/^ /mg, "~0");
1182
+ pre = pre.replace(/~0/g, "");
1183
+ return pre;
1184
+ });
1185
+
1186
+ return hashBlock("<blockquote>\n" + bq + "\n</blockquote>");
1187
+ });
1188
+ return text;
1189
+ }
1190
+
1191
+ var _FormParagraphs = function (text) {
1192
+ //
1193
+ // Params:
1194
+ // $text - string to process with html <p> tags
1195
+ //
1196
+
1197
+ // Strip leading and trailing lines:
1198
+ text = text.replace(/^\n+/g, "");
1199
+ text = text.replace(/\n+$/g, "");
1200
+
1201
+ var grafs = text.split(/\n{2,}/g);
1202
+ var grafsOut = [];
1203
+
1204
+ //
1205
+ // Wrap <p> tags.
1206
+ //
1207
+ var end = grafs.length;
1208
+ for (var i = 0; i < end; i++) {
1209
+ var str = grafs[i];
1210
+
1211
+ // if this is an HTML marker, copy it
1212
+ if (str.search(/~K(\d+)K/g) >= 0) {
1213
+ grafsOut.push(str);
1214
+ }
1215
+ else if (str.search(/\S/) >= 0) {
1216
+ str = _RunSpanGamut(str);
1217
+ str = str.replace(/^([ \t]*)/g, "<p>");
1218
+ str += "</p>"
1219
+ grafsOut.push(str);
1220
+ }
1221
+
1222
+ }
1223
+
1224
+ //
1225
+ // Unhashify HTML blocks
1226
+ //
1227
+ end = grafsOut.length;
1228
+ for (var i = 0; i < end; i++) {
1229
+ // if this is a marker for an html block...
1230
+ while (grafsOut[i].search(/~K(\d+)K/) >= 0) {
1231
+ var blockText = g_html_blocks[RegExp.$1];
1232
+ blockText = blockText.replace(/\$/g, "$$$$"); // Escape any dollar signs
1233
+ grafsOut[i] = grafsOut[i].replace(/~K\d+K/, blockText);
1234
+ }
1235
+ }
1236
+
1237
+ return grafsOut.join("\n\n");
1238
+ }
1239
+
1240
+ var _EncodeAmpsAndAngles = function (text) {
1241
+ // Smart processing for ampersands and angle brackets that need to be encoded.
1242
+
1243
+ // Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
1244
+ // http://bumppo.net/projects/amputator/
1245
+ text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, "&amp;");
1246
+
1247
+ // Encode naked <'s
1248
+ text = text.replace(/<(?![a-z\/?\$!])/gi, "&lt;");
1249
+
1250
+ return text;
1251
+ }
1252
+
1253
+ var _EncodeBackslashEscapes = function (text) {
1254
+ //
1255
+ // Parameter: String.
1256
+ // Returns: The string, with after processing the following backslash
1257
+ // escape sequences.
1258
+ //
1259
+
1260
+ // attacklab: The polite way to do this is with the new
1261
+ // escapeCharacters() function:
1262
+ //
1263
+ // text = escapeCharacters(text,"\\",true);
1264
+ // text = escapeCharacters(text,"`*_{}[]()>#+-.!",true);
1265
+ //
1266
+ // ...but we're sidestepping its use of the (slow) RegExp constructor
1267
+ // as an optimization for Firefox. This function gets called a LOT.
1268
+
1269
+ text = text.replace(/\\(\\)/g, escapeCharacters_callback);
1270
+ text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g, escapeCharacters_callback);
1271
+ return text;
1272
+ }
1273
+
1274
+ var _DoAutoLinks = function (text) {
1275
+
1276
+ text = text.replace(/<((https?|ftp|dict):[^'">\s]+)>/gi, "<a href=\"$1\">$1</a>");
1277
+
1278
+ // Email addresses: <address@domain.foo>
1279
+
1280
+ /*
1281
+ text = text.replace(/
1282
+ <
1283
+ (?:mailto:)?
1284
+ (
1285
+ [-.\w]+
1286
+ \@
1287
+ [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+
1288
+ )
1289
+ >
1290
+ /gi, _DoAutoLinks_callback());
1291
+ */
1292
+ text = text.replace(/<(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi,
1293
+ function (wholeMatch, m1) {
1294
+ return _EncodeEmailAddress(_UnescapeSpecialChars(m1));
1295
+ }
1296
+ );
1297
+
1298
+ return text;
1299
+ }
1300
+
1301
+ var _EncodeEmailAddress = function (addr) {
1302
+ //
1303
+ // Input: an email address, e.g. "foo@example.com"
1304
+ //
1305
+ // Output: the email address as a mailto link, with each character
1306
+ // of the address encoded as either a decimal or hex entity, in
1307
+ // the hopes of foiling most address harvesting spam bots. E.g.:
1308
+ //
1309
+ // <a href="&#x6D;&#97;&#105;&#108;&#x74;&#111;:&#102;&#111;&#111;&#64;&#101;
1310
+ // x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;">&#102;&#111;&#111;
1311
+ // &#64;&#101;x&#x61;&#109;&#x70;&#108;&#x65;&#x2E;&#99;&#111;&#109;</a>
1312
+ //
1313
+ // Based on a filter by Matthew Wickline, posted to the BBEdit-Talk
1314
+ // mailing list: <http://tinyurl.com/yu7ue>
1315
+ //
1316
+
1317
+ var encode = [
1318
+ function (ch) {
1319
+ return "&#" + ch.charCodeAt(0) + ";";
1320
+ },
1321
+ function (ch) {
1322
+ return "&#x" + ch.charCodeAt(0).toString(16) + ";";
1323
+ },
1324
+ function (ch) {
1325
+ return ch;
1326
+ }
1327
+ ];
1328
+
1329
+ addr = "mailto:" + addr;
1330
+
1331
+ addr = addr.replace(/./g, function (ch) {
1332
+ if (ch == "@") {
1333
+ // this *must* be encoded. I insist.
1334
+ ch = encode[Math.floor(Math.random() * 2)](ch);
1335
+ } else if (ch != ":") {
1336
+ // leave ':' alone (to spot mailto: later)
1337
+ var r = Math.random();
1338
+ // roughly 10% raw, 45% hex, 45% dec
1339
+ ch = (
1340
+ r > .9 ? encode[2](ch) :
1341
+ r > .45 ? encode[1](ch) :
1342
+ encode[0](ch)
1343
+ );
1344
+ }
1345
+ return ch;
1346
+ });
1347
+
1348
+ addr = "<a href=\"" + addr + "\">" + addr + "</a>";
1349
+ addr = addr.replace(/">.+:/g, "\">"); // strip the mailto: from the visible part
1350
+
1351
+ return addr;
1352
+ }
1353
+
1354
+ var _UnescapeSpecialChars = function (text) {
1355
+ //
1356
+ // Swap back in all the special characters we've hidden.
1357
+ //
1358
+ text = text.replace(/~E(\d+)E/g,
1359
+ function (wholeMatch, m1) {
1360
+ var charCodeToReplace = parseInt(m1);
1361
+ return String.fromCharCode(charCodeToReplace);
1362
+ }
1363
+ );
1364
+ return text;
1365
+ }
1366
+
1367
+ var _Outdent = function (text) {
1368
+ //
1369
+ // Remove one level of line-leading tabs or spaces
1370
+ //
1371
+
1372
+ // attacklab: hack around Konqueror 3.5.4 bug:
1373
+ // "----------bug".replace(/^-/g,"") == "bug"
1374
+
1375
+ text = text.replace(/^(\t|[ ]{1,4})/gm, "~0"); // attacklab: g_tab_width
1376
+
1377
+ // attacklab: clean up hack
1378
+ text = text.replace(/~0/g, "")
1379
+
1380
+ return text;
1381
+ }
1382
+
1383
+ var _Detab = function (text) {
1384
+ // attacklab: Detab's completely rewritten for speed.
1385
+ // In perl we could fix it by anchoring the regexp with \G.
1386
+ // In javascript we're less fortunate.
1387
+
1388
+ // expand first n-1 tabs
1389
+ text = text.replace(/\t(?=\t)/g, " "); // attacklab: g_tab_width
1390
+
1391
+ // replace the nth with two sentinels
1392
+ text = text.replace(/\t/g, "~A~B");
1393
+
1394
+ // use the sentinel to anchor our regex so it doesn't explode
1395
+ text = text.replace(/~B(.+?)~A/g,
1396
+ function (wholeMatch, m1, m2) {
1397
+ var leadingText = m1;
1398
+ var numSpaces = 4 - leadingText.length % 4; // attacklab: g_tab_width
1399
+
1400
+ // there *must* be a better way to do this:
1401
+ for (var i = 0; i < numSpaces; i++) leadingText += " ";
1402
+
1403
+ return leadingText;
1404
+ }
1405
+ );
1406
+
1407
+ // clean up sentinels
1408
+ text = text.replace(/~A/g, " "); // attacklab: g_tab_width
1409
+ text = text.replace(/~B/g, "");
1410
+
1411
+ return text;
1412
+ }
1413
+
1414
+
1415
+ //
1416
+ // attacklab: Utility functions
1417
+ //
1418
+
1419
+
1420
+ var escapeCharacters = function (text, charsToEscape, afterBackslash) {
1421
+ // First we have to escape the escape characters so that
1422
+ // we can build a character class out of them
1423
+ var regexString = "([" + charsToEscape.replace(/([\[\]\\])/g, "\\$1") + "])";
1424
+
1425
+ if (afterBackslash) {
1426
+ regexString = "\\\\" + regexString;
1427
+ }
1428
+
1429
+ var regex = new RegExp(regexString, "g");
1430
+ text = text.replace(regex, escapeCharacters_callback);
1431
+
1432
+ return text;
1433
+ }
1434
+
1435
+
1436
+ var escapeCharacters_callback = function (wholeMatch, m1) {
1437
+ var charCodeToEscape = m1.charCodeAt(0);
1438
+ return "~E" + charCodeToEscape + "E";
1439
+ }
1440
+
1441
+ } // end of Showdown.converter
1442
+
1443
+
1444
+ // export
1445
+ if (typeof module !== 'undefined') module.exports = Showdown;
1446
+
1447
+ // stolen from AMD branch of underscore
1448
+ // AMD define happens at the end for compatibility with AMD loaders
1449
+ // that don't enforce next-turn semantics on modules.
1450
+ if (typeof define === 'function' && define.amd) {
1451
+ define('showdown', function () {
1452
+ return Showdown;
1453
+ });
1454
+ }