kuji-mercury-rails 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (231) hide show
  1. data/LICENSE +25 -0
  2. data/POST_INSTALL +15 -0
  3. data/app/controllers/mercury/images_controller.rb +19 -0
  4. data/app/controllers/mercury_controller.rb +34 -0
  5. data/app/models/mercury/image.rb +16 -0
  6. data/app/views/layouts/mercury.html.erb +33 -0
  7. data/app/views/mercury/lightviews/about.html +11 -0
  8. data/app/views/mercury/modals/character.html +255 -0
  9. data/app/views/mercury/modals/htmleditor.html +13 -0
  10. data/app/views/mercury/modals/link.html +75 -0
  11. data/app/views/mercury/modals/media.html +82 -0
  12. data/app/views/mercury/modals/table.html +84 -0
  13. data/app/views/mercury/palettes/backcolor.html +73 -0
  14. data/app/views/mercury/palettes/forecolor.html +73 -0
  15. data/app/views/mercury/panels/history.html +3 -0
  16. data/app/views/mercury/panels/notes.html +3 -0
  17. data/app/views/mercury/panels/snippets.html +12 -0
  18. data/app/views/mercury/selects/formatblock.html +11 -0
  19. data/app/views/mercury/selects/style.html +5 -0
  20. data/app/views/mercury/snippets/example/options.html.erb +34 -0
  21. data/app/views/mercury/snippets/example/preview.html.erb +1 -0
  22. data/config/engine.rb +41 -0
  23. data/db/migrate/20110526035601_create_mercury_images.rb +11 -0
  24. data/features/loading/loading.feature +22 -0
  25. data/features/loading/navigating.feature +77 -0
  26. data/features/loading/user_interface.feature +67 -0
  27. data/features/regions/editable/advanced_editing.feature +0 -0
  28. data/features/regions/editable/basic_editing.feature +195 -0
  29. data/features/regions/editable/inserting_links.feature +98 -0
  30. data/features/regions/editable/inserting_media.feature +110 -0
  31. data/features/regions/editable/inserting_snippets.feature +102 -0
  32. data/features/regions/editable/inserting_special_characters.feature +24 -0
  33. data/features/regions/editable/inserting_tables.feature +109 -0
  34. data/features/regions/editable/pasting.feature +0 -0
  35. data/features/regions/editable/uploading_images.feature +0 -0
  36. data/features/regions/image/uploading_images.feature +0 -0
  37. data/features/regions/markupable/advanced_editing.feature +0 -0
  38. data/features/regions/markupable/basic_editing.feature +0 -0
  39. data/features/regions/markupable/inserting_links.feature +0 -0
  40. data/features/regions/markupable/inserting_media.feature +0 -0
  41. data/features/regions/markupable/inserting_snippets.feature +0 -0
  42. data/features/regions/markupable/inserting_special_characters.feature +0 -0
  43. data/features/regions/markupable/inserting_tables.feature +0 -0
  44. data/features/regions/markupable/uploading_images.feature +0 -0
  45. data/features/regions/simple/basic_editing.feature +5 -0
  46. data/features/regions/snippetable/advanced_editing.feature +0 -0
  47. data/features/regions/snippetable/basic_editing.feature +0 -0
  48. data/features/regions/snippetable/inserting_snippets.feature +0 -0
  49. data/features/saving/saving.feature +33 -0
  50. data/features/step_definitions/debug_steps.rb +14 -0
  51. data/features/step_definitions/mercury_steps.rb +438 -0
  52. data/features/step_definitions/web_steps.rb +211 -0
  53. data/features/support/env.rb +46 -0
  54. data/features/support/mercury_contents.rb +25 -0
  55. data/features/support/mercury_selectors.rb +148 -0
  56. data/features/support/paths.rb +38 -0
  57. data/features/support/selectors.rb +44 -0
  58. data/lib/generators/mercury/install/install_generator.rb +49 -0
  59. data/lib/generators/mercury/install/templates/mongoid_paperclip_image.rb +17 -0
  60. data/lib/mercury-rails.rb +3 -0
  61. data/lib/mercury/authentication.rb +8 -0
  62. data/spec/javascripts/mercury/dialog_spec.js.coffee +281 -0
  63. data/spec/javascripts/mercury/dialogs/backcolor_spec.js.coffee +37 -0
  64. data/spec/javascripts/mercury/dialogs/forecolor_spec.js.coffee +37 -0
  65. data/spec/javascripts/mercury/dialogs/formatblock_spec.js.coffee +25 -0
  66. data/spec/javascripts/mercury/dialogs/snippetpanel_spec.js.coffee +30 -0
  67. data/spec/javascripts/mercury/dialogs/style_spec.js.coffee +25 -0
  68. data/spec/javascripts/mercury/history_buffer_spec.js.coffee +76 -0
  69. data/spec/javascripts/mercury/lightview_spec.js.coffee +497 -0
  70. data/spec/javascripts/mercury/mercury_spec.js.coffee +132 -0
  71. data/spec/javascripts/mercury/modal_spec.js.coffee +504 -0
  72. data/spec/javascripts/mercury/modals/htmleditor_spec.js.coffee +30 -0
  73. data/spec/javascripts/mercury/modals/insertcharacter_spec.js.coffee +29 -0
  74. data/spec/javascripts/mercury/modals/insertlink_spec.js.coffee +220 -0
  75. data/spec/javascripts/mercury/modals/insertmedia_spec.js.coffee +167 -0
  76. data/spec/javascripts/mercury/modals/insertsnippet_spec.js.coffee +52 -0
  77. data/spec/javascripts/mercury/modals/inserttable_spec.js.coffee +160 -0
  78. data/spec/javascripts/mercury/native_extensions_spec.js.coffee +60 -0
  79. data/spec/javascripts/mercury/page_editor_spec.js.coffee +757 -0
  80. data/spec/javascripts/mercury/palette_spec.js.coffee +49 -0
  81. data/spec/javascripts/mercury/panel_spec.js.coffee +183 -0
  82. data/spec/javascripts/mercury/region_spec.js.coffee +298 -0
  83. data/spec/javascripts/mercury/regions/editable_spec.js.coffee +561 -0
  84. data/spec/javascripts/mercury/regions/image_spec.js.coffee +34 -0
  85. data/spec/javascripts/mercury/regions/markupable_spec.js.coffee +367 -0
  86. data/spec/javascripts/mercury/regions/simple_spec.js.coffee +33 -0
  87. data/spec/javascripts/mercury/regions/snippetable_spec.js.coffee +370 -0
  88. data/spec/javascripts/mercury/select_spec.js.coffee +49 -0
  89. data/spec/javascripts/mercury/snippet_spec.js.coffee +253 -0
  90. data/spec/javascripts/mercury/snippet_toolbar_spec.js.coffee +184 -0
  91. data/spec/javascripts/mercury/statusbar_spec.js.coffee +150 -0
  92. data/spec/javascripts/mercury/table_editor_spec.js.coffee +194 -0
  93. data/spec/javascripts/mercury/toolbar.button_group_spec.js.coffee +90 -0
  94. data/spec/javascripts/mercury/toolbar.button_spec.js.coffee +360 -0
  95. data/spec/javascripts/mercury/toolbar.expander_spec.js.coffee +118 -0
  96. data/spec/javascripts/mercury/toolbar_spec.js.coffee +222 -0
  97. data/spec/javascripts/mercury/tooltip_spec.js.coffee +186 -0
  98. data/spec/javascripts/mercury/uploader_spec.js.coffee +530 -0
  99. data/spec/javascripts/spec_helper.js +513 -0
  100. data/spec/javascripts/templates/mercury/dialog.html +2 -0
  101. data/spec/javascripts/templates/mercury/dialogs/backcolor.html +5 -0
  102. data/spec/javascripts/templates/mercury/dialogs/forecolor.html +5 -0
  103. data/spec/javascripts/templates/mercury/dialogs/formatblock.html +3 -0
  104. data/spec/javascripts/templates/mercury/dialogs/snippetpanel.html +16 -0
  105. data/spec/javascripts/templates/mercury/dialogs/style.html +3 -0
  106. data/spec/javascripts/templates/mercury/lightview.html +13 -0
  107. data/spec/javascripts/templates/mercury/modal.html +13 -0
  108. data/spec/javascripts/templates/mercury/modals/htmleditor.html +5 -0
  109. data/spec/javascripts/templates/mercury/modals/insertcharacter.html +5 -0
  110. data/spec/javascripts/templates/mercury/modals/insertlink.html +30 -0
  111. data/spec/javascripts/templates/mercury/modals/insertmedia.html +35 -0
  112. data/spec/javascripts/templates/mercury/modals/insertsnippet.html +6 -0
  113. data/spec/javascripts/templates/mercury/modals/inserttable.html +27 -0
  114. data/spec/javascripts/templates/mercury/page_editor.html +35 -0
  115. data/spec/javascripts/templates/mercury/palette.html +16 -0
  116. data/spec/javascripts/templates/mercury/panel.html +16 -0
  117. data/spec/javascripts/templates/mercury/region.html +2 -0
  118. data/spec/javascripts/templates/mercury/regions/editable.html +3 -0
  119. data/spec/javascripts/templates/mercury/regions/image.html +1 -0
  120. data/spec/javascripts/templates/mercury/regions/simple.html +3 -0
  121. data/spec/javascripts/templates/mercury/regions/snippetable.html +4 -0
  122. data/spec/javascripts/templates/mercury/select.html +16 -0
  123. data/spec/javascripts/templates/mercury/snippet.html +1 -0
  124. data/spec/javascripts/templates/mercury/snippet_toolbar.html +16 -0
  125. data/spec/javascripts/templates/mercury/statusbar.html +8 -0
  126. data/spec/javascripts/templates/mercury/table_editor.html +65 -0
  127. data/spec/javascripts/templates/mercury/toolbar.button.html +64 -0
  128. data/spec/javascripts/templates/mercury/toolbar.button_group.html +9 -0
  129. data/spec/javascripts/templates/mercury/toolbar.expander.html +18 -0
  130. data/spec/javascripts/templates/mercury/toolbar.html +11 -0
  131. data/spec/javascripts/templates/mercury/tooltip.html +12 -0
  132. data/spec/javascripts/templates/mercury/uploader.html +11 -0
  133. data/vendor/assets/images/mercury/button.png +0 -0
  134. data/vendor/assets/images/mercury/close.png +0 -0
  135. data/vendor/assets/images/mercury/default-snippet.png +0 -0
  136. data/vendor/assets/images/mercury/loading-dark.gif +0 -0
  137. data/vendor/assets/images/mercury/loading-light.gif +0 -0
  138. data/vendor/assets/images/mercury/missing-image.png +0 -0
  139. data/vendor/assets/images/mercury/search-icon.png +0 -0
  140. data/vendor/assets/images/mercury/temp-logo.png +0 -0
  141. data/vendor/assets/images/mercury/toolbar/editable/buttons.png +0 -0
  142. data/vendor/assets/images/mercury/toolbar/primary/_expander.png +0 -0
  143. data/vendor/assets/images/mercury/toolbar/primary/_pressed.png +0 -0
  144. data/vendor/assets/images/mercury/toolbar/primary/historypanel.png +0 -0
  145. data/vendor/assets/images/mercury/toolbar/primary/insertcharacter.png +0 -0
  146. data/vendor/assets/images/mercury/toolbar/primary/insertlink.png +0 -0
  147. data/vendor/assets/images/mercury/toolbar/primary/insertmedia.png +0 -0
  148. data/vendor/assets/images/mercury/toolbar/primary/inserttable.png +0 -0
  149. data/vendor/assets/images/mercury/toolbar/primary/inspectorpanel.png +0 -0
  150. data/vendor/assets/images/mercury/toolbar/primary/notespanel.png +0 -0
  151. data/vendor/assets/images/mercury/toolbar/primary/preview.png +0 -0
  152. data/vendor/assets/images/mercury/toolbar/primary/redo.png +0 -0
  153. data/vendor/assets/images/mercury/toolbar/primary/save.png +0 -0
  154. data/vendor/assets/images/mercury/toolbar/primary/snippetpanel.png +0 -0
  155. data/vendor/assets/images/mercury/toolbar/primary/undo.png +0 -0
  156. data/vendor/assets/images/mercury/toolbar/snippetable/buttons.png +0 -0
  157. data/vendor/assets/javascripts/mercury.js +479 -0
  158. data/vendor/assets/javascripts/mercury/dependencies/jquery-1.7.js +9300 -0
  159. data/vendor/assets/javascripts/mercury/dependencies/jquery-ui-1.8.13.custom.js +1328 -0
  160. data/vendor/assets/javascripts/mercury/dependencies/jquery.additions.js +206 -0
  161. data/vendor/assets/javascripts/mercury/dependencies/jquery.htmlClean.js +527 -0
  162. data/vendor/assets/javascripts/mercury/dependencies/liquidmetal.js +88 -0
  163. data/vendor/assets/javascripts/mercury/dependencies/showdown.js +1340 -0
  164. data/vendor/assets/javascripts/mercury/dialog.js.coffee +159 -0
  165. data/vendor/assets/javascripts/mercury/dialogs/backcolor.js.coffee +6 -0
  166. data/vendor/assets/javascripts/mercury/dialogs/forecolor.js.coffee +6 -0
  167. data/vendor/assets/javascripts/mercury/dialogs/formatblock.js.coffee +4 -0
  168. data/vendor/assets/javascripts/mercury/dialogs/snippetpanel.js.coffee +10 -0
  169. data/vendor/assets/javascripts/mercury/dialogs/style.js.coffee +4 -0
  170. data/vendor/assets/javascripts/mercury/finalize.js.coffee +3 -0
  171. data/vendor/assets/javascripts/mercury/history_buffer.js.coffee +30 -0
  172. data/vendor/assets/javascripts/mercury/lightview.js.coffee +205 -0
  173. data/vendor/assets/javascripts/mercury/locales/ar.locale.js.coffee +207 -0
  174. data/vendor/assets/javascripts/mercury/locales/da.locale.js.coffee +211 -0
  175. data/vendor/assets/javascripts/mercury/locales/de.locale.js.coffee +206 -0
  176. data/vendor/assets/javascripts/mercury/locales/es.locale.js.coffee +211 -0
  177. data/vendor/assets/javascripts/mercury/locales/example.local.js.coffee +211 -0
  178. data/vendor/assets/javascripts/mercury/locales/fr.locale.js.coffee +211 -0
  179. data/vendor/assets/javascripts/mercury/locales/it.locale.js.coffee +208 -0
  180. data/vendor/assets/javascripts/mercury/locales/ko.local.js.coffee +206 -0
  181. data/vendor/assets/javascripts/mercury/locales/nl.locale.js.coffee +206 -0
  182. data/vendor/assets/javascripts/mercury/locales/pt.locale.js.coffee +211 -0
  183. data/vendor/assets/javascripts/mercury/locales/sv.local.js.coffee +209 -0
  184. data/vendor/assets/javascripts/mercury/locales/swedish_chef.locale.js.coffee +213 -0
  185. data/vendor/assets/javascripts/mercury/mercury.js.coffee +109 -0
  186. data/vendor/assets/javascripts/mercury/modal.js.coffee +198 -0
  187. data/vendor/assets/javascripts/mercury/modals/htmleditor.js.coffee +11 -0
  188. data/vendor/assets/javascripts/mercury/modals/insertcharacter.js.coffee +4 -0
  189. data/vendor/assets/javascripts/mercury/modals/insertlink.js.coffee +92 -0
  190. data/vendor/assets/javascripts/mercury/modals/insertmedia.js.coffee +81 -0
  191. data/vendor/assets/javascripts/mercury/modals/insertsnippet.js.coffee +12 -0
  192. data/vendor/assets/javascripts/mercury/modals/inserttable.js.coffee +54 -0
  193. data/vendor/assets/javascripts/mercury/native_extensions.js.coffee +55 -0
  194. data/vendor/assets/javascripts/mercury/page_editor.js.coffee +225 -0
  195. data/vendor/assets/javascripts/mercury/palette.js.coffee +29 -0
  196. data/vendor/assets/javascripts/mercury/panel.js.coffee +115 -0
  197. data/vendor/assets/javascripts/mercury/plugins/save_as_xml/mercury/page_editor.js.coffee +28 -0
  198. data/vendor/assets/javascripts/mercury/plugins/save_as_xml/plugin.js +9 -0
  199. data/vendor/assets/javascripts/mercury/region.js.coffee +107 -0
  200. data/vendor/assets/javascripts/mercury/regions/editable.js.coffee +600 -0
  201. data/vendor/assets/javascripts/mercury/regions/image.js.coffee +114 -0
  202. data/vendor/assets/javascripts/mercury/regions/markupable.js.coffee +398 -0
  203. data/vendor/assets/javascripts/mercury/regions/simple.js.coffee +325 -0
  204. data/vendor/assets/javascripts/mercury/regions/snippetable.js.coffee +124 -0
  205. data/vendor/assets/javascripts/mercury/select.js.coffee +44 -0
  206. data/vendor/assets/javascripts/mercury/snippet.js.coffee +106 -0
  207. data/vendor/assets/javascripts/mercury/snippet_toolbar.js.coffee +72 -0
  208. data/vendor/assets/javascripts/mercury/statusbar.js.coffee +51 -0
  209. data/vendor/assets/javascripts/mercury/support/history.js +1 -0
  210. data/vendor/assets/javascripts/mercury/table_editor.js.coffee +265 -0
  211. data/vendor/assets/javascripts/mercury/toolbar.button.js.coffee +173 -0
  212. data/vendor/assets/javascripts/mercury/toolbar.button_group.js.coffee +42 -0
  213. data/vendor/assets/javascripts/mercury/toolbar.expander.js.coffee +56 -0
  214. data/vendor/assets/javascripts/mercury/toolbar.js.coffee +86 -0
  215. data/vendor/assets/javascripts/mercury/tooltip.js.coffee +74 -0
  216. data/vendor/assets/javascripts/mercury/uploader.js.coffee +244 -0
  217. data/vendor/assets/javascripts/mercury_loader.js +193 -0
  218. data/vendor/assets/javascripts/mercury_overrides.js +6 -0
  219. data/vendor/assets/stylesheets/mercury.css +28 -0
  220. data/vendor/assets/stylesheets/mercury/all_images.css.erb +89 -0
  221. data/vendor/assets/stylesheets/mercury/dialog.css +212 -0
  222. data/vendor/assets/stylesheets/mercury/form.css +118 -0
  223. data/vendor/assets/stylesheets/mercury/lightview.css +151 -0
  224. data/vendor/assets/stylesheets/mercury/mercury.css +37 -0
  225. data/vendor/assets/stylesheets/mercury/modal.css +183 -0
  226. data/vendor/assets/stylesheets/mercury/statusbar.css +32 -0
  227. data/vendor/assets/stylesheets/mercury/toolbar.css +304 -0
  228. data/vendor/assets/stylesheets/mercury/tooltip.css +26 -0
  229. data/vendor/assets/stylesheets/mercury/uploader.css +111 -0
  230. data/vendor/assets/stylesheets/mercury_overrides.css +17 -0
  231. metadata +572 -0
@@ -0,0 +1,88 @@
1
+ /*
2
+ * LiquidMetal, version: 0.1 (2009-02-05)
3
+ *
4
+ * A mimetic poly-alloy of Quicksilver's scoring algorithm, essentially
5
+ * LiquidMetal.
6
+ *
7
+ * For usage and examples, visit:
8
+ * http://github.com/rmm5t/liquidmetal
9
+ *
10
+ * Licensed under the MIT:
11
+ * http://www.opensource.org/licenses/mit-license.php
12
+ *
13
+ * Copyright (c) 2009, Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org)
14
+ */
15
+ var LiquidMetal = function() {
16
+ var SCORE_NO_MATCH = 0.0;
17
+ var SCORE_MATCH = 1.0;
18
+ var SCORE_TRAILING = 0.8;
19
+ var SCORE_TRAILING_BUT_STARTED = 0.9;
20
+ var SCORE_BUFFER = 0.85;
21
+
22
+ return {
23
+ score: function(string, abbreviation) {
24
+ // Short circuits
25
+ if (abbreviation.length == 0) return SCORE_TRAILING;
26
+ if (abbreviation.length > string.length) return SCORE_NO_MATCH;
27
+
28
+ var scores = this.buildScoreArray(string, abbreviation);
29
+
30
+ var sum = 0.0;
31
+ for (var i =0; i < scores.length; i++) {
32
+ sum += scores[i];
33
+ }
34
+
35
+ return (sum / scores.length);
36
+ },
37
+
38
+ buildScoreArray: function(string, abbreviation) {
39
+ var scores = new Array(string.length);
40
+ var lower = string.toLowerCase();
41
+ var chars = abbreviation.toLowerCase().split("");
42
+
43
+ var lastIndex = -1;
44
+ var started = false;
45
+ for (var i =0; i < chars.length; i++) {
46
+ var c = chars[i];
47
+ var index = lower.indexOf(c, lastIndex+1);
48
+ if (index < 0) return fillArray(scores, SCORE_NO_MATCH);
49
+ if (index == 0) started = true;
50
+
51
+ if (isNewWord(string, index)) {
52
+ scores[index-1] = 1;
53
+ fillArray(scores, SCORE_BUFFER, lastIndex+1, index-1);
54
+ }
55
+ else if (isUpperCase(string, index)) {
56
+ fillArray(scores, SCORE_BUFFER, lastIndex+1, index);
57
+ }
58
+ else {
59
+ fillArray(scores, SCORE_NO_MATCH, lastIndex+1, index);
60
+ }
61
+
62
+ scores[index] = SCORE_MATCH;
63
+ lastIndex = index;
64
+ }
65
+
66
+ var trailingScore = started ? SCORE_TRAILING_BUT_STARTED : SCORE_TRAILING;
67
+ fillArray(scores, trailingScore, lastIndex+1);
68
+ return scores;
69
+ }
70
+ };
71
+
72
+ function isUpperCase(string, index) {
73
+ var c = string.charAt(index);
74
+ return ("A" <= c && c <= "Z");
75
+ }
76
+
77
+ function isNewWord(string, index) {
78
+ var c = string.charAt(index-1);
79
+ return (c == " " || c == "\t");
80
+ }
81
+
82
+ function fillArray(array, value, from, to) {
83
+ from = Math.max(from || 0, 0);
84
+ to = Math.min(to || array.length, array.length);
85
+ for (var i = from; i < to; i++) { array[i] = value; }
86
+ return array;
87
+ }
88
+ }();
@@ -0,0 +1,1340 @@
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
+ // GitHub Flavored Markdown modifications by Tekkub
66
+ // http://github.github.com/github-flavored-markdown/
67
+ //
68
+ // Modifications are tagged with "GFM"
69
+ // **************************************************
70
+
71
+ //
72
+ // Showdown namespace
73
+ //
74
+ var Showdown = {};
75
+
76
+ //
77
+ // converter
78
+ //
79
+ // Wraps all "globals" so that the only thing
80
+ // exposed is makeHtml().
81
+ //
82
+ Showdown.converter = function() {
83
+
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
110
+ // magic in Markdown will work.
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
+
1334
+
1335
+ var escapeCharacters_callback = function(wholeMatch, m1) {
1336
+ var charCodeToEscape = m1.charCodeAt(0);
1337
+ return "~E" + charCodeToEscape + "E";
1338
+ };
1339
+
1340
+ };