PageRenderTime 804ms CodeModel.GetById 32ms RepoModel.GetById 2ms app.codeStats 0ms

/build/js/mdown.js

http://mdown.googlecode.com/
JavaScript | 975 lines | 973 code | 2 blank | 0 comment | 169 complexity | 69f3a2f38547ddc867d24291912b67bb MD5 | raw file
Possible License(s): Apache-2.0
  1. this['mdown']=(function(){var md,$_,$Main,$closure,$estr,js,$o;
  2. $estr = function() { return js.Boot.__string_rec(this,''); }
  3. if(typeof md=='undefined') md = {}
  4. if(typeof js=='undefined') js = {}
  5. js.Boot = function() { }
  6. js.Boot.__name__ = ["js","Boot"];
  7. js.Boot.__unhtml = function(s) {
  8. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  9. }
  10. js.Boot.__trace = function(v,i) {
  11. var msg = (i != null?((i.fileName + ":") + i.lineNumber) + ": ":"");
  12. msg += js.Boot.__unhtml(js.Boot.__string_rec(v,"")) + "<br/>";
  13. var d = document.getElementById("haxe:trace");
  14. if(d == null) alert("No haxe:trace element defined\n" + msg);
  15. else d.innerHTML += msg;
  16. }
  17. js.Boot.__clear_trace = function() {
  18. var d = document.getElementById("haxe:trace");
  19. if(d != null) d.innerHTML = "";
  20. }
  21. js.Boot.__closure = function(o,f) {
  22. var m=f;
  23. if(m == null) return null;
  24. var f1 = function() {
  25. return m.apply(o,arguments);
  26. }
  27. f1.scope = o;
  28. f1.method = m;
  29. return f1;
  30. }
  31. js.Boot.__string_rec = function(o,s) {
  32. if(o == null) return "null";
  33. if(s.length >= 5) return "<...>";
  34. var t = typeof(o);
  35. if(t == "function" && (o.__name__ != null || o.__ename__ != null)) t = "object";
  36. switch(t) {
  37. case "object":{
  38. if(o instanceof Array) {
  39. if(o.__enum__ != null) {
  40. if(o.length == 2) return o[0];
  41. var str = o[0] + "(";
  42. s += "\t";
  43. {
  44. var _g1 = 2, _g = o.length;
  45. while(_g1 < _g) {
  46. var i = _g1++;
  47. if(i != 2) str += "," + js.Boot.__string_rec(o[i],s);
  48. else str += js.Boot.__string_rec(o[i],s);
  49. }
  50. }
  51. return str + ")";
  52. }
  53. var l = o.length;
  54. var i;
  55. var str = "[";
  56. s += "\t";
  57. {
  58. var _g = 0;
  59. while(_g < l) {
  60. var i1 = _g++;
  61. str += ((i1 > 0?",":"")) + js.Boot.__string_rec(o[i1],s);
  62. }
  63. }
  64. str += "]";
  65. return str;
  66. }
  67. var tostr;
  68. try {
  69. tostr = o.toString;
  70. }
  71. catch( $e0 ) {
  72. {
  73. var e = $e0;
  74. {
  75. return "???";
  76. }
  77. }
  78. }
  79. if(tostr != null && tostr != Object.toString) {
  80. var s2 = o.toString();
  81. if(s2 != "[object Object]") return s2;
  82. }
  83. var k = null;
  84. var str = "{\n";
  85. s += "\t";
  86. var hasp = (o.hasOwnProperty != null);
  87. for( var k in o ) { ;
  88. if(hasp && !o.hasOwnProperty(k)) continue;
  89. if(k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__") continue;
  90. if(str.length != 2) str += ", \n";
  91. str += ((s + k) + " : ") + js.Boot.__string_rec(o[k],s);
  92. }
  93. s = s.substring(1);
  94. str += ("\n" + s) + "}";
  95. return str;
  96. }break;
  97. case "function":{
  98. return "<function>";
  99. }break;
  100. case "string":{
  101. return o;
  102. }break;
  103. default:{
  104. return md.String(o);
  105. }break;
  106. }
  107. }
  108. js.Boot.__interfLoop = function(cc,cl) {
  109. if(cc == null) return false;
  110. if(cc == cl) return true;
  111. var intf = cc.__interfaces__;
  112. if(intf != null) {
  113. var _g1 = 0, _g = intf.length;
  114. while(_g1 < _g) {
  115. var i = _g1++;
  116. var i1 = intf[i];
  117. if(i1 == cl || js.Boot.__interfLoop(i1,cl)) return true;
  118. }
  119. }
  120. return js.Boot.__interfLoop(cc.__super__,cl);
  121. }
  122. js.Boot.__instanceof = function(o,cl) {
  123. try {
  124. if(o instanceof cl) {
  125. if(cl == md.Array) return (o.__enum__ == null);
  126. return true;
  127. }
  128. if(js.Boot.__interfLoop(o.__class__,cl)) return true;
  129. }
  130. catch( $e0 ) {
  131. {
  132. var e = $e0;
  133. {
  134. if(cl == null) return false;
  135. }
  136. }
  137. }
  138. switch(cl) {
  139. case md.Int:{
  140. return Math.ceil(o%2147483648.0) === o;
  141. }break;
  142. case md.Float:{
  143. return typeof(o) == "number";
  144. }break;
  145. case md.Bool:{
  146. return o === true || o === false;
  147. }break;
  148. case md.String:{
  149. return typeof(o) == "string";
  150. }break;
  151. case md.Dynamic:{
  152. return true;
  153. }break;
  154. default:{
  155. if(o == null) return false;
  156. return o.__enum__ == cl || (cl == md.Class && o.__name__ != null) || (cl == md.Enum && o.__ename__ != null);
  157. }break;
  158. }
  159. }
  160. js.Boot.__init = function() {
  161. js.Lib.isIE = (typeof document!='undefined' && document.all != null && typeof window!='undefined' && window.opera == null);
  162. js.Lib.isOpera = (typeof window!='undefined' && window.opera != null);
  163. md.Array = Array;
  164. md.String = String;
  165. md.Math = Math;
  166. md.Date = Date;
  167. md.Array.prototype.copy = md.Array.prototype.slice;
  168. md.Array.prototype.insert = function(i,x) {
  169. this.splice(i,0,x);
  170. }
  171. md.Array.prototype.remove = (md.Array.prototype.indexOf?function(obj) {
  172. var idx = this.indexOf(obj);
  173. if(idx == -1) return false;
  174. this.splice(idx,1);
  175. return true;
  176. }:function(obj) {
  177. var i = 0;
  178. var l = this.length;
  179. while(i < l) {
  180. if(this[i] == obj) {
  181. this.splice(i,1);
  182. return true;
  183. }
  184. i++;
  185. }
  186. return false;
  187. });
  188. md.Array.prototype.iterator = function() {
  189. return { cur : 0, arr : this, hasNext : function() {
  190. return this.cur < this.arr.length;
  191. }, next : function() {
  192. return this.arr[this.cur++];
  193. }}
  194. }
  195. var cca = md.String.prototype.charCodeAt;
  196. md.String.prototype.cca = cca;
  197. md.String.prototype.charCodeAt = function(i) {
  198. var x = cca.call(this,i);
  199. if(isNaN(x)) return null;
  200. return x;
  201. }
  202. var oldsub = md.String.prototype.substr;
  203. md.String.prototype.substr = function(pos,len) {
  204. if(pos != null && pos != 0 && len != null && len < 0) return "";
  205. if(len == null) len = this.length;
  206. if(pos < 0) {
  207. pos = this.length + pos;
  208. if(pos < 0) pos = 0;
  209. }
  210. else if(len < 0) {
  211. len = (this.length + len) - pos;
  212. }
  213. return oldsub.apply(this,[pos,len]);
  214. }
  215. $closure = js.Boot.__closure;
  216. }
  217. js.Boot.prototype.__class__ = js.Boot;
  218. js.Lib = function() { }
  219. js.Lib.__name__ = ["js","Lib"];
  220. js.Lib.isIE = null;
  221. js.Lib.isOpera = null;
  222. js.Lib.document = null;
  223. js.Lib.window = null;
  224. js.Lib.alert = function(v) {
  225. alert(js.Boot.__string_rec(v,""));
  226. }
  227. js.Lib.eval = function(code) {
  228. return eval(code);
  229. }
  230. js.Lib.setErrorHandler = function(f) {
  231. js.Lib.onerror = f;
  232. }
  233. js.Lib.prototype.__class__ = js.Lib;
  234. md.Markdown = function(p) { if( p === $_ ) return; {
  235. this.init();
  236. }}
  237. md.Markdown.__name__ = ["Markdown"];
  238. md.Markdown.instance = null;
  239. md.Markdown.main = function() {
  240. md.Markdown.instance = new md.Markdown();
  241. }
  242. md.Markdown.prototype.detab = function(text) {
  243. text = this.replaceText(text,new md.EReg("\\t(?=\\t)","g")," ");
  244. text = this.replaceText(text,new md.EReg("\\t","g"),"~A~B");
  245. text = this.replaceFn(text,new md.EReg("~B(.+?)~A",""),$closure(this, this.detab_cb));
  246. text = this.replaceText(text,new md.EReg("~A","g")," ");
  247. text = this.replaceText(text,new md.EReg("~B","g"),"");
  248. return text;
  249. }
  250. md.Markdown.prototype.detab_cb = function(re) {
  251. var leadingText = re.matched(1);
  252. var numSpaces = 4 - leadingText.length % 4;
  253. {
  254. var _g = 0;
  255. while(_g < numSpaces) {
  256. var i = _g++;
  257. leadingText += " ";
  258. }
  259. }
  260. return leadingText;
  261. }
  262. md.Markdown.prototype.doAnchors = function(text) {
  263. text = this.replaceFn(text,new md.EReg("(\\[((?:\\[[^\\]]*\\]|[^\\[\\]])*)\\][ ]?(?:\\n[ ]*)?\\[(.*?)\\])()()()()",""),$closure(this, this.writeAnchorTag));
  264. text = this.replaceFn(text,new md.EReg("(\\[((?:\\[[^\\]]*\\]|[^\\[\\]])*)\\]\\([ \\t]*()<?(.*?)>?[ \\t]*(([\\'\"])(.*?)\\6[ \\t]*)?\\))",""),$closure(this, this.writeAnchorTag));
  265. text = this.replaceFn(text,new md.EReg("(\\[([^\\[\\]]+)\\])()()()()()",""),$closure(this, this.writeAnchorTag));
  266. return text;
  267. }
  268. md.Markdown.prototype.doAutoLinks = function(text) {
  269. text = this.replaceText(text,new md.EReg("<((https?|ftp|dict):[^\\'\">\\s]+)>","gi"),"<a href=\"$1\">$1</a>");
  270. text = this.replaceFn(text,new md.EReg("<(?:mailto:)?([-.\\w]+\\@[-a-z0-9]+(\\.[-a-z0-9]+)*\\.[a-z]+)>","i"),$closure(this, this.doAutoLinks_cb));
  271. return text;
  272. }
  273. md.Markdown.prototype.doAutoLinks_cb = function(re) {
  274. return this.encodeEmail(this.unescapeSpecial(re.matched(1)));
  275. }
  276. md.Markdown.prototype.doBlockQuotes = function(text) {
  277. text = this.replaceFn(text,new md.EReg("((^[ \\t]*>[ \\t]?.+\\n(.+\\n)*\\n*)+)","m"),$closure(this, this.doBlockQuotes_cb));
  278. return text;
  279. }
  280. md.Markdown.prototype.doBlockQuotes_cb = function(re) {
  281. var bq = re.matched(1);
  282. bq = this.replaceText(bq,new md.EReg("^[ \\t]*>[ \\t]?","gm"),"~0");
  283. bq = this.replaceText(bq,new md.EReg("~0","g"),"");
  284. bq = this.replaceText(bq,new md.EReg("^[ \\t]+$","gm"),"");
  285. bq = this.runBlockGamut(bq);
  286. bq = this.replaceText(bq,new md.EReg("(^|\\n)","g"),"$1 ");
  287. bq = this.replaceFn(bq,new md.EReg("(\\s*<pre>[^\\r]+?</pre>)","m"),$closure(this, this.doBlockQuotes_cb_cb));
  288. return this.hashBlock(("<blockquote>\n" + bq) + "\n</blockquote>");
  289. }
  290. md.Markdown.prototype.doBlockQuotes_cb_cb = function(re) {
  291. var pre = re.matched(1);
  292. pre = this.replaceText(pre,new md.EReg("^ ","mg"),"~0");
  293. pre = this.replaceText(pre,new md.EReg("~0","g"),"");
  294. return pre;
  295. }
  296. md.Markdown.prototype.doCodeBlocks = function(text) {
  297. text += "~0";
  298. text = this.replaceFn(text,new md.EReg("(?:\\n\\n|^)((?:(?:[ ]{4}|\\t).*\\n+)+)(\\n*[ ]{0,3}[^ \\t\\n]|(?=~0))",""),$closure(this, this.doCodeBlocks_cb));
  299. text = this.replaceText(text,new md.EReg("~0",""),"");
  300. return text;
  301. }
  302. md.Markdown.prototype.doCodeBlocks_cb = function(re) {
  303. var codeblock = re.matched(1);
  304. var nextChar = re.matched(2);
  305. codeblock = this.encodeCode(this.outdent(codeblock));
  306. codeblock = this.detab(codeblock);
  307. codeblock = this.replaceText(codeblock,new md.EReg("^\\n+","g"),"");
  308. codeblock = this.replaceText(codeblock,new md.EReg("\\n+$","g"),"");
  309. codeblock = ("<pre><code>" + codeblock) + "\n</code></pre>";
  310. return this.hashBlock(codeblock) + nextChar;
  311. }
  312. md.Markdown.prototype.doCodeSpans = function(text) {
  313. text = this.replaceFn(text,new md.EReg("(^|[^\\\\])(`+)([^\\r]*?[^`])\\2(?!`)","m"),$closure(this, this.doCodeSpans_cb));
  314. return text;
  315. }
  316. md.Markdown.prototype.doCodeSpans_cb = function(re) {
  317. var c = re.matched(3);
  318. c = this.replaceText(c,new md.EReg("^([ \\t]*)","g"),"");
  319. c = this.replaceText(c,new md.EReg("[ \\t]*$","g"),"");
  320. c = this.encodeCode(c);
  321. return ((re.matched(1) + "<code>") + c) + "</code>";
  322. }
  323. md.Markdown.prototype.doHardBreaks = function(text) {
  324. return this.replaceText(text,new md.EReg(" +\\n","g")," <br />\n");
  325. }
  326. md.Markdown.prototype.doHeaders = function(text) {
  327. text = this.replaceFn(text,new md.EReg("^(.+)[ \\t]*\\n=+[ \\t]*\\n+","m"),$closure(this, this.doHeaders_h1_cb));
  328. text = this.replaceFn(text,new md.EReg("^(.+)[ \\t]*\\n-+[ \\t]*\\n+","m"),$closure(this, this.doHeaders_h2_cb));
  329. text = this.replaceFn(text,new md.EReg("^(\\#{1,6})[ \\t]*(.+?)[ \\t]*\\#*\\n+","m"),$closure(this, this.doHeaders_atx_cb));
  330. return text;
  331. }
  332. md.Markdown.prototype.doHeaders_atx_cb = function(re) {
  333. var h_level = re.matched(1).length;
  334. return this.hashBlock(((((("<h" + h_level) + ">") + this.runSpanGamut(re.matched(2))) + "</h") + h_level) + ">");
  335. }
  336. md.Markdown.prototype.doHeaders_h1_cb = function(re) {
  337. return this.hashBlock(("<h1>" + this.runSpanGamut(re.matched(1))) + "</h1>");
  338. }
  339. md.Markdown.prototype.doHeaders_h2_cb = function(re) {
  340. return this.hashBlock(("<h2>" + this.runSpanGamut(re.matched(1))) + "</h2>");
  341. }
  342. md.Markdown.prototype.doHorizontalRules = function(text) {
  343. var key = this.hashBlock("<hr />");
  344. text = this.replaceText(text,new md.EReg("^[ ]{0,2}([ ]?\\*[ ]?){3,}[ \\t]*$","gm"),key);
  345. text = this.replaceText(text,new md.EReg("^[ ]{0,2}([ ]?-[ ]?){3,}[ \\t]*$","gm"),key);
  346. text = this.replaceText(text,new md.EReg("^[ ]{0,2}([ ]?_[ ]?){3,}[ \\t]*$","gm"),key);
  347. return text;
  348. }
  349. md.Markdown.prototype.doImages = function(text) {
  350. text = this.replaceFn(text,new md.EReg("(!\\[(.*?)\\][ ]?(?:\\n[ ]*)?\\[(.*?)\\])()()()()",""),$closure(this, this.writeImageTag));
  351. text = this.replaceFn(text,new md.EReg("(!\\[(.*?)\\]\\s?\\([ \\t]*()<?(\\S+?)>?[ \\t]*(([\\'\"])(.*?)\\6[ \\t]*)?\\))",""),$closure(this, this.writeImageTag));
  352. return text;
  353. }
  354. md.Markdown.prototype.doItalicsAndBold = function(text) {
  355. text = this.replaceText(text,new md.EReg("(\\*\\*|__)(?=\\S)([^\\r]*?\\S[\\*_]*)\\1","g"),"<strong>$2</strong>");
  356. text = this.replaceText(text,new md.EReg("(\\*|_)(?=\\S)([^\\r]*?\\S)\\1","g"),"<em>$2</em>");
  357. return text;
  358. }
  359. md.Markdown.prototype.doLists = function(text) {
  360. text += "~0";
  361. var whole_list = new md.EReg("^(([ ]{0,3}([*+-]|\\d+[.])[ \\t]+)[^\\r]+?(~0|\\n{2,}(?=\\S)(?![ \\t]*(?:[*+-]|\\d+[.])[ \\t]+)))","m");
  362. if(this.g_list_level > 0) {
  363. text = this.replaceFn(text,whole_list,$closure(this, this.doLists_outer_cb));
  364. }
  365. else {
  366. whole_list = new md.EReg("(\\n\\n|^\\n?)(([ ]{0,3}([*+-]|\\d+[.])[ \\t]+)[^\\r]+?(~0|\\n{2,}(?=\\S)(?![ \\t]*(?:[*+-]|\\d+[.])[ \\t]+)))","");
  367. text = this.replaceFn(text,whole_list,$closure(this, this.doLists_inner_cb));
  368. }
  369. text = this.replaceText(text,new md.EReg("~0",""),"");
  370. return text;
  371. }
  372. md.Markdown.prototype.doLists_inner_cb = function(re) {
  373. var runup = re.matched(1);
  374. var list = re.matched(2);
  375. var list_type = ((new md.EReg("[*+-]","g").match(re.matched(3)))?"ul":"ol");
  376. var list1 = this.replaceText(list,new md.EReg("\\n{2,}","g"),"\n\n\n");
  377. var result = this.processListItems(list1);
  378. result = ((((((runup + "<") + list_type) + ">\n") + result) + "</") + list_type) + ">\n";
  379. return result;
  380. }
  381. md.Markdown.prototype.doLists_outer_cb = function(re) {
  382. var list = re.matched(1);
  383. var list_type = ((new md.EReg("[*+-]","").match(re.matched(2)))?"ul":"ol");
  384. list = this.replaceText(list,new md.EReg("\\n{2,}","g"),"\n\n\n");
  385. var result = this.processListItems(list);
  386. result = this.replaceText(result,new md.EReg("\\s+$",""),"");
  387. result = ((((("<" + list_type) + ">") + result) + "</") + list_type) + ">\n";
  388. return result;
  389. }
  390. md.Markdown.prototype.encodeAmpsAndAngles = function(text) {
  391. text = this.replaceText(text,new md.EReg("&(?!#?[xX]?(?:[0-9a-fA-F]+|\\w+);)","g"),"&amp;");
  392. text = this.replaceText(text,new md.EReg("<(?![a-z/?\\$!])","gi"),"&lt;");
  393. return text;
  394. }
  395. md.Markdown.prototype.encodeBackslashEscapes = function(text) {
  396. text = this.replaceFn(text,new md.EReg("\\\\(\\\\)",""),$closure(this, this.escapeCharactersCallback));
  397. text = this.replaceFn(text,new md.EReg("\\\\([`*_{}\\[\\]()>#+-.!])",""),$closure(this, this.escapeCharactersCallback));
  398. return text;
  399. }
  400. md.Markdown.prototype.encodeCode = function(text) {
  401. text = this.replaceText(text,new md.EReg("&","g"),"&amp;");
  402. text = this.replaceText(text,new md.EReg("<","g"),"&lt;");
  403. text = this.replaceText(text,new md.EReg(">","g"),"&gt;");
  404. text = this.escapeCharacters(text,"*_{}[]\\",false);
  405. return text;
  406. }
  407. md.Markdown.prototype.encodeEmail = function(addr) {
  408. addr = "mailto:" + addr;
  409. addr = this.replaceFn(addr,new md.EReg(".",""),$closure(this, this.encodeEmail_cb));
  410. addr = ((("<a href=\"" + addr) + "\">") + addr) + "</a>";
  411. addr = this.replaceText(addr,new md.EReg("\">.+:","g"),"\">");
  412. return addr;
  413. }
  414. md.Markdown.prototype.encodeEmail_cb = function(re) {
  415. var char2hex = function(ch) {
  416. var hexDigits = "0123456789ABCDEF";
  417. var dec = ch.charCodeAt(0);
  418. return (hexDigits.charAt(dec >> 4) + hexDigits.charAt(dec & 15));
  419. }
  420. var encode = [function(ch) {
  421. return ("&#" + ch.charCodeAt(0)) + ";";
  422. },function(ch) {
  423. return ("&#x" + char2hex(ch)) + ";";
  424. },function(ch) {
  425. return ch;
  426. }];
  427. var ch = re.matched(0);
  428. if(ch == "@") {
  429. ch = encode[md.Math.floor(md.Math.random() * 2)](ch);
  430. }
  431. else if(ch != ":") {
  432. var r = md.Math.random();
  433. ch = ((r > .9?encode[2](ch):(r > .45?encode[1](ch):encode[0](ch))));
  434. }
  435. return ch;
  436. }
  437. md.Markdown.prototype.escapeCharacters = function(text,charsToEscape,afterBackslash) {
  438. var regexString = ("([" + this.replaceText(charsToEscape,new md.EReg("([\\[\\]\\\\])","g"),"\\$1")) + "])";
  439. if(afterBackslash) {
  440. regexString = "\\\\" + regexString;
  441. }
  442. var regex = new md.EReg(regexString,"");
  443. text = this.replaceFn(text,regex,$closure(this, this.escapeCharactersCallback));
  444. return text;
  445. }
  446. md.Markdown.prototype.escapeCharactersCallback = function(re) {
  447. var charCodeToEscape = re.matched(1).charCodeAt(0);
  448. return ("~E" + charCodeToEscape) + "E";
  449. }
  450. md.Markdown.prototype.escapeSpecialInAttributes = function(text) {
  451. var regex = new md.EReg("(<[a-z/!$](\"[^\"]*\"|\\'[^\\']*\\'|[^\\'\">])*>|<!(--.*?--\\s*)+>)","i");
  452. text = this.replaceFn(text,regex,$closure(this, this.escapeSpecialInAttributes_cb));
  453. return text;
  454. }
  455. md.Markdown.prototype.escapeSpecialInAttributes_cb = function(re) {
  456. var tag = this.replaceText(re.matched(0),new md.EReg("(.)</?code>(?=.)","g"),"$1`");
  457. tag = this.escapeCharacters(tag,"\\`*_");
  458. return tag;
  459. }
  460. md.Markdown.prototype.formParagraphs = function(text) {
  461. text = this.replaceText(text,new md.EReg("^\\n+","g"),"");
  462. text = this.replaceText(text,new md.EReg("\\n+$","g"),"");
  463. var grafs = new md.EReg("\\n{2,}","g").split(text);
  464. var grafsOut = new md.Array();
  465. var end = grafs.length;
  466. {
  467. var _g = 0;
  468. while(_g < end) {
  469. var i = _g++;
  470. var str = grafs[i];
  471. if(new md.EReg("~K(\\d+)K","g").match(str)) {
  472. grafsOut.push(str);
  473. }
  474. else if(new md.EReg("\\S","").match(str)) {
  475. str = this.runSpanGamut(str);
  476. str = this.replaceText(str,new md.EReg("^([ \\t]*)","g"),"<p>");
  477. str += "</p>";
  478. grafsOut.push(str);
  479. }
  480. }
  481. }
  482. end = grafsOut.length;
  483. {
  484. var _g = 0;
  485. while(_g < end) {
  486. var i = _g++;
  487. var re = new md.EReg("~K(\\d+)K","");
  488. while(re.match(grafsOut[i])) {
  489. var blockText = this.g_html_blocks[md.Std.parseInt(re.matched(1))];
  490. blockText = this.replaceText(blockText,new md.EReg("\\$","g"),"$$$$");
  491. grafsOut[i] = this.replaceText(grafsOut[i],new md.EReg("~K\\d+K",""),blockText);
  492. }
  493. }
  494. }
  495. return grafsOut.join("\n\n");
  496. }
  497. md.Markdown.prototype.g_block_filters = null;
  498. md.Markdown.prototype.g_html_blocks = null;
  499. md.Markdown.prototype.g_list_level = null;
  500. md.Markdown.prototype.g_span_filters = null;
  501. md.Markdown.prototype.g_titles = null;
  502. md.Markdown.prototype.g_urls = null;
  503. md.Markdown.prototype.hashBlock = function(text) {
  504. text = this.replaceText(text,new md.EReg("(^\\n+|\\n+$)","g"),"");
  505. this.g_html_blocks.push(text);
  506. return ("\n\n~K" + (this.g_html_blocks.length - 1)) + "K\n\n";
  507. }
  508. md.Markdown.prototype.hashElement = function(re) {
  509. var blockText = re.matched(1);
  510. blockText = this.replaceText(blockText,new md.EReg("\\n\\n","g"),"\n");
  511. blockText = this.replaceText(blockText,new md.EReg("^\\n",""),"");
  512. blockText = this.replaceText(blockText,new md.EReg("\\n+$","g"),"");
  513. this.g_html_blocks.push(blockText);
  514. blockText = ("\n\n~K" + (this.g_html_blocks.length - 1)) + "K\n\n";
  515. return blockText;
  516. }
  517. md.Markdown.prototype.hashHTMLBlocks = function(text) {
  518. text = this.replaceText(text,new md.EReg("\\n","g"),"\n\n");
  519. var block_tags_a = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del";
  520. var block_tags_b = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math";
  521. text = this.replaceFn(text,new md.EReg("^(<(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+))","m"),$closure(this, this.hashElement));
  522. text = this.replaceFn(text,new md.EReg("^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math)\\b[^\\r]*?.*</\\2>[ \\t]*(?=\\n+)\\n)","m"),$closure(this, this.hashElement));
  523. text = this.replaceFn(text,new md.EReg("(\\n[ ]{0,3}(<(hr)\\b([^<>])*?/?>)[ \\t]*(?=\\n{2,}))",""),$closure(this, this.hashElement));
  524. text = this.replaceFn(text,new md.EReg("(\\n\\n[ ]{0,3}<!(--[^\\r]*?--\\s*)+>[ \\t]*(?=\\n{2,}))",""),$closure(this, this.hashElement));
  525. text = this.replaceFn(text,new md.EReg("(?:\\n\\n)([ ]{0,3}(?:<([?%])[^\\r]*?\\2>)[ \\t]*(?=\\n{2,}))",""),$closure(this, this.hashElement));
  526. text = this.replaceText(text,new md.EReg("\\n\\n","g"),"\n");
  527. return text;
  528. }
  529. md.Markdown.prototype.init = function() {
  530. this.g_block_filters = new md.FilterList();
  531. this.g_block_filters.add(10,$closure(this, this.doHeaders));
  532. this.g_block_filters.add(20,$closure(this, this.doHorizontalRules));
  533. this.g_block_filters.add(30,$closure(this, this.doLists));
  534. this.g_block_filters.add(40,$closure(this, this.doHeaders));
  535. this.g_block_filters.add(50,$closure(this, this.doCodeBlocks));
  536. this.g_block_filters.add(60,$closure(this, this.doBlockQuotes));
  537. this.g_block_filters.add(70,$closure(this, this.hashHTMLBlocks));
  538. this.g_block_filters.add(80,$closure(this, this.formParagraphs));
  539. this.g_span_filters = new md.FilterList();
  540. this.g_span_filters.add(10,$closure(this, this.doCodeSpans));
  541. this.g_span_filters.add(20,$closure(this, this.escapeSpecialInAttributes));
  542. this.g_span_filters.add(30,$closure(this, this.encodeBackslashEscapes));
  543. this.g_span_filters.add(40,$closure(this, this.doImages));
  544. this.g_span_filters.add(50,$closure(this, this.doAnchors));
  545. this.g_span_filters.add(60,$closure(this, this.doAutoLinks));
  546. this.g_span_filters.add(70,$closure(this, this.encodeAmpsAndAngles));
  547. this.g_span_filters.add(80,$closure(this, this.doItalicsAndBold));
  548. this.g_span_filters.add(90,$closure(this, this.doHardBreaks));
  549. }
  550. md.Markdown.prototype.makeHtml = function(text) {
  551. this.g_urls = new md.Hash();
  552. this.g_titles = new md.Hash();
  553. this.g_html_blocks = new md.Array();
  554. text = this.replaceText(text,new md.EReg("~","g"),"~T");
  555. text = this.replaceText(text,new md.EReg("\\$","g"),"~D");
  556. text = this.replaceText(text,new md.EReg("\\r\\n","g"),"\n");
  557. text = this.replaceText(text,new md.EReg("\\r","g"),"\n");
  558. text = ("\n\n" + text) + "\n\n";
  559. text = this.detab(text);
  560. text = this.replaceText(text,new md.EReg("^[ \\t]+$","mg"),"");
  561. text = this.hashHTMLBlocks(text);
  562. text = this.stripLinkDefs(text);
  563. text = this.runBlockGamut(text);
  564. text = this.unescapeSpecial(text);
  565. text = this.replaceText(text,new md.EReg("~D","g"),"$$");
  566. text = this.replaceText(text,new md.EReg("~T","g"),"~");
  567. return text;
  568. }
  569. md.Markdown.prototype.outdent = function(text) {
  570. text = this.replaceText(text,new md.EReg("^(\\t|[ ]{1,4})","gm"),"~0");
  571. text = this.replaceText(text,new md.EReg("~0","g"),"");
  572. return text;
  573. }
  574. md.Markdown.prototype.processListItems = function(list_str) {
  575. this.g_list_level++;
  576. list_str = this.replaceText(list_str,new md.EReg("\\n{2,}$",""),"\n");
  577. list_str += "~0";
  578. list_str = this.replaceFn(list_str,new md.EReg("(\\n)?(^[ \\t]*)([*+-]|\\d+[.])[ \\t]+([^\\r]+?(\\n{1,2}))(?=\\n*(~0|\\2([*+-]|\\d+[.])[ \\t]+))","m"),$closure(this, this.processListItems_cb));
  579. list_str = this.replaceText(list_str,new md.EReg("~0","g"),"");
  580. this.g_list_level--;
  581. return list_str;
  582. }
  583. md.Markdown.prototype.processListItems_cb = function(re) {
  584. var item = re.matched(4);
  585. var leading_line = re.matched(1);
  586. var leading_space = re.matched(2);
  587. if(leading_line == "" || (new md.EReg("\\n{2,}","").match(item))) {
  588. item = this.runBlockGamut(this.outdent(item));
  589. }
  590. else {
  591. item = this.doLists(this.outdent(item));
  592. item = this.replaceText(item,new md.EReg("\\n$",""),"");
  593. item = this.runSpanGamut(item);
  594. }
  595. return ("<li>" + item) + "</li>\n";
  596. }
  597. md.Markdown.prototype.replaceFn = function(orig,regex,fn) {
  598. return regex.customReplace(orig,fn);
  599. }
  600. md.Markdown.prototype.replaceText = function(orig,regex,replacement) {
  601. return regex.replace(orig,replacement);
  602. }
  603. md.Markdown.prototype.runBlockGamut = function(text) {
  604. return this.g_block_filters.run(text);
  605. }
  606. md.Markdown.prototype.runSpanGamut = function(text) {
  607. return this.g_span_filters.run(text);
  608. }
  609. md.Markdown.prototype.stripLinkDefs = function(text) {
  610. var text1 = this.replaceFn(text,new md.EReg("^[ ]{0,3}\\[(.+)\\]:[ \\t]*\\n?[ \\t]*<?(\\S+?)>?[ \\t]*\\n?[ \\t]*(?:(\\n*)[\"(](.+?)[\")][ \\t]*)?(?:\\n+)","m"),$closure(this, this.stripLinkDefs_cb));
  611. return text1;
  612. }
  613. md.Markdown.prototype.stripLinkDefs_cb = function(re) {
  614. var m1 = re.matched(1).toLowerCase();
  615. var m3 = re.matched(3);
  616. var m4 = re.matched(4);
  617. this.g_urls.set(m1,this.encodeAmpsAndAngles(re.matched(2)));
  618. if(m3 != "") {
  619. return m3 + m4;
  620. }
  621. else if(m4 != "") {
  622. this.g_titles.set(m1,this.replaceText(m4,new md.EReg("\"","g"),"&quot;"));
  623. }
  624. return "";
  625. }
  626. md.Markdown.prototype.unescapeSpecial = function(text) {
  627. text = this.replaceFn(text,new md.EReg("~E(\\d+)E",""),$closure(this, this.unescapeSpecial_cb));
  628. return text;
  629. }
  630. md.Markdown.prototype.unescapeSpecial_cb = function(re) {
  631. var charCodeToReplace = md.Std.parseInt(re.matched(1));
  632. return md.String.fromCharCode(charCodeToReplace);
  633. }
  634. md.Markdown.prototype.writeAnchorTag = function(re) {
  635. var whole_match = re.matched(1);
  636. var link_text = re.matched(2);
  637. var link_id = re.matched(3).toLowerCase();
  638. var url = re.matched(4);
  639. var title = new md.String(re.matched(7));
  640. if(url == "") {
  641. if(link_id == "") {
  642. link_id = this.replaceText(link_text.toLowerCase(),new md.EReg(" ?\\n","g")," ");
  643. }
  644. url = "#" + link_id;
  645. if(this.g_urls.exists(link_id)) {
  646. url = this.g_urls.get(link_id);
  647. if(this.g_titles.exists(link_id)) {
  648. title = this.g_titles.get(link_id);
  649. }
  650. }
  651. else {
  652. if(new md.EReg("\\(\\s*\\)$","m").match(whole_match)) {
  653. url = "";
  654. }
  655. else {
  656. return whole_match;
  657. }
  658. }
  659. }
  660. url = this.escapeCharacters(url,"*_");
  661. var result = ("<a href=\"" + url) + "\"";
  662. if(title != "") {
  663. title = this.replaceText(title,new md.EReg("\"","g"),"&quot;");
  664. title = this.escapeCharacters(title,"*_");
  665. result += (" title=\"" + title) + "\"";
  666. }
  667. result += (">" + link_text) + "</a>";
  668. return result;
  669. }
  670. md.Markdown.prototype.writeImageTag = function(re) {
  671. var whole_match = re.matched(1);
  672. var alt_text = re.matched(2);
  673. var link_id = re.matched(3).toLowerCase();
  674. var url = re.matched(4);
  675. var title = new md.String(re.matched(7));
  676. if(url == "") {
  677. if(link_id == "") {
  678. link_id = this.replaceText(alt_text.toLowerCase(),new md.EReg(" ?\\n","g")," ");
  679. }
  680. url = "#" + link_id;
  681. if(this.g_urls.exists(link_id)) {
  682. url = this.g_urls.get(link_id);
  683. if(this.g_titles.exists(link_id)) {
  684. title = this.g_titles.get(link_id);
  685. }
  686. }
  687. else {
  688. return whole_match;
  689. }
  690. }
  691. alt_text = this.replaceText(alt_text,new md.EReg("\"","g"),"&quot;");
  692. url = this.escapeCharacters(url,"*_");
  693. var result = ((("<img src=\"" + url) + "\" alt=\"") + alt_text) + "\"";
  694. title = this.replaceText(title,new md.EReg("\"","g"),"&quot;");
  695. title = this.escapeCharacters(title,"*_");
  696. result += (" title=\"" + title) + "\"";
  697. result += " />";
  698. return result;
  699. }
  700. md.Markdown.prototype.__class__ = md.Markdown;
  701. md.FilterList = function(p) { if( p === $_ ) return; {
  702. this.filters = [];
  703. }}
  704. md.FilterList.__name__ = ["FilterList"];
  705. md.FilterList.prototype.add = function(p,f) {
  706. this.sorted = false;
  707. this.filters.push(new md.Filter(p,f));
  708. }
  709. md.FilterList.prototype.filters = null;
  710. md.FilterList.prototype.run = function(text) {
  711. if(!this.sorted) {
  712. this.sorted = true;
  713. this.filters.sort($closure(this, this.sort));
  714. }
  715. var end = this.filters.length;
  716. {
  717. var _g = 0;
  718. while(_g < end) {
  719. var i = _g++;
  720. text = this.filters[i].fn(text);
  721. }
  722. }
  723. return text;
  724. }
  725. md.FilterList.prototype.sort = function(a,b) {
  726. return (a.priority > b.priority?1:-1);
  727. }
  728. md.FilterList.prototype.sorted = null;
  729. md.FilterList.prototype.__class__ = md.FilterList;
  730. md.Filter = function(p,f) { if( p === $_ ) return; {
  731. this.priority = p;
  732. this.fn = f;
  733. }}
  734. md.Filter.__name__ = ["Filter"];
  735. md.Filter.prototype.fn = null;
  736. md.Filter.prototype.priority = null;
  737. md.Filter.prototype.__class__ = md.Filter;
  738. md.Test = function() { }
  739. md.Test.__name__ = ["Test"];
  740. md.Test.prototype.a = function(re) {
  741. return "a";
  742. }
  743. md.Test.prototype.go = function() {
  744. new md.EReg("y","g").customReplace("xyz",$closure(this, this.a));
  745. }
  746. md.Test.prototype.__class__ = md.Test;
  747. md.Std = function() { }
  748. md.Std.__name__ = ["Std"];
  749. md.Std["is"] = function(v,t) {
  750. return js.Boot.__instanceof(v,t);
  751. }
  752. md.Std.string = function(s) {
  753. return js.Boot.__string_rec(s,"");
  754. }
  755. md.Std["int"] = function(x) {
  756. if(x < 0) return md.Math.ceil(x);
  757. return md.Math.floor(x);
  758. }
  759. md.Std.parseInt = function(x) {
  760. var v = parseInt(x);
  761. if(md.Math.isNaN(v)) return null;
  762. return v;
  763. }
  764. md.Std.parseFloat = function(x) {
  765. return parseFloat(x);
  766. }
  767. md.Std.random = function(x) {
  768. return md.Math.floor(md.Math.random() * x);
  769. }
  770. md.Std.prototype.__class__ = md.Std;
  771. md.Hash = function(p) { if( p === $_ ) return; {
  772. this.h = {}
  773. if(this.h.__proto__ != null) {
  774. this.h.__proto__ = null;
  775. delete(this.h.__proto__);
  776. }
  777. }}
  778. md.Hash.__name__ = ["Hash"];
  779. md.Hash.prototype.exists = function(key) {
  780. try {
  781. key = "$" + key;
  782. return this.hasOwnProperty.call(this.h,key);
  783. }
  784. catch( $e0 ) {
  785. {
  786. var e = $e0;
  787. {
  788. for(var i in this.h)
  789. if( i == key ) return true;
  790. ;
  791. return false;
  792. }
  793. }
  794. }
  795. }
  796. md.Hash.prototype.get = function(key) {
  797. return this.h["$" + key];
  798. }
  799. md.Hash.prototype.h = null;
  800. md.Hash.prototype.iterator = function() {
  801. return { ref : this.h, it : this.keys(), hasNext : function() {
  802. return this.it.hasNext();
  803. }, next : function() {
  804. var i = this.it.next();
  805. return this.ref["$" + i];
  806. }}
  807. }
  808. md.Hash.prototype.keys = function() {
  809. var a = new md.Array();
  810. for(var i in this.h)
  811. a.push(i.substr(1));
  812. ;
  813. return a.iterator();
  814. }
  815. md.Hash.prototype.remove = function(key) {
  816. if(!this.exists(key)) return false;
  817. delete(this.h["$" + key]);
  818. return true;
  819. }
  820. md.Hash.prototype.set = function(key,value) {
  821. this.h["$" + key] = value;
  822. }
  823. md.Hash.prototype.toString = function() {
  824. var s = new md.StringBuf();
  825. s.b[s.b.length] = "{";
  826. var it = this.keys();
  827. { var $it0 = it;
  828. while( $it0.hasNext() ) { var i = $it0.next();
  829. {
  830. s.b[s.b.length] = i;
  831. s.b[s.b.length] = " => ";
  832. s.b[s.b.length] = md.Std.string(this.get(i));
  833. if(it.hasNext()) s.b[s.b.length] = ", ";
  834. }
  835. }}
  836. s.b[s.b.length] = "}";
  837. return s.b.join("");
  838. }
  839. md.Hash.prototype.__class__ = md.Hash;
  840. md.EReg = function(r,opt) { if( r === $_ ) return; {
  841. opt = opt.split("u").join("");
  842. this.r = new RegExp(r,opt);
  843. }}
  844. md.EReg.__name__ = ["EReg"];
  845. md.EReg.prototype.customReplace = function(s,f) {
  846. var buf = new md.StringBuf();
  847. while(true) {
  848. if(!this.match(s)) break;
  849. buf.b[buf.b.length] = this.matchedLeft();
  850. buf.b[buf.b.length] = f(this);
  851. s = this.matchedRight();
  852. }
  853. buf.b[buf.b.length] = s;
  854. return buf.b.join("");
  855. }
  856. md.EReg.prototype.match = function(s) {
  857. this.r.m = this.r.exec(s);
  858. this.r.s = s;
  859. this.r.l = RegExp.leftContext;
  860. this.r.r = RegExp.rightContext;
  861. return (this.r.m != null);
  862. }
  863. md.EReg.prototype.matched = function(n) {
  864. return (this.r.m != null && n >= 0 && n < this.r.m.length?this.r.m[n]:(function($this) {
  865. var $r;
  866. throw "EReg::matched";
  867. return $r;
  868. }(this)));
  869. }
  870. md.EReg.prototype.matchedLeft = function() {
  871. if(this.r.m == null) throw "No string matched";
  872. if(this.r.l == null) return this.r.s.substr(0,this.r.m.index);
  873. return this.r.l;
  874. }
  875. md.EReg.prototype.matchedPos = function() {
  876. if(this.r.m == null) throw "No string matched";
  877. return { pos : this.r.m.index, len : this.r.m[0].length}
  878. }
  879. md.EReg.prototype.matchedRight = function() {
  880. if(this.r.m == null) throw "No string matched";
  881. if(this.r.r == null) {
  882. var sz = this.r.m.index + this.r.m[0].length;
  883. return this.r.s.substr(sz,this.r.s.length - sz);
  884. }
  885. return this.r.r;
  886. }
  887. md.EReg.prototype.r = null;
  888. md.EReg.prototype.replace = function(s,by) {
  889. return s.replace(this.r,by);
  890. }
  891. md.EReg.prototype.split = function(s) {
  892. var d = "#__delim__#";
  893. return s.replace(this.r,d).split(d);
  894. }
  895. md.EReg.prototype.__class__ = md.EReg;
  896. md.StringBuf = function(p) { if( p === $_ ) return; {
  897. this.b = new md.Array();
  898. }}
  899. md.StringBuf.__name__ = ["StringBuf"];
  900. md.StringBuf.prototype.add = function(x) {
  901. this.b[this.b.length] = x;
  902. }
  903. md.StringBuf.prototype.addChar = function(c) {
  904. this.b[this.b.length] = md.String.fromCharCode(c);
  905. }
  906. md.StringBuf.prototype.addSub = function(s,pos,len) {
  907. this.b[this.b.length] = s.substr(pos,len);
  908. }
  909. md.StringBuf.prototype.b = null;
  910. md.StringBuf.prototype.toString = function() {
  911. return this.b.join("");
  912. }
  913. md.StringBuf.prototype.__class__ = md.StringBuf;
  914. md.IntIter = function(min,max) { if( min === $_ ) return; {
  915. this.min = min;
  916. this.max = max;
  917. }}
  918. md.IntIter.__name__ = ["IntIter"];
  919. md.IntIter.prototype.hasNext = function() {
  920. return this.min < this.max;
  921. }
  922. md.IntIter.prototype.max = null;
  923. md.IntIter.prototype.min = null;
  924. md.IntIter.prototype.next = function() {
  925. return this.min++;
  926. }
  927. md.IntIter.prototype.__class__ = md.IntIter;
  928. $Main = function() { }
  929. $Main.__name__ = ["@Main"];
  930. $Main.prototype.__class__ = $Main;
  931. $_ = {}
  932. js.Boot.__res = {}
  933. js.Boot.__ns = 'md';
  934. js.Boot.__init();
  935. {
  936. js.Lib.document = document;
  937. js.Lib.window = window;
  938. onerror = function(msg,url,line) {
  939. var f = js.Lib.onerror;
  940. if( f == null )
  941. return false;
  942. return f(msg,[url+":"+line]);
  943. }
  944. }
  945. {
  946. md.String.prototype.__class__ = md.String;
  947. md.String.__name__ = ["String"];
  948. md.Array.prototype.__class__ = md.Array;
  949. md.Array.__name__ = ["Array"];
  950. md.Int = { __name__ : ["Int"]}
  951. md.Dynamic = { __name__ : ["Dynamic"]}
  952. md.Float = Number;
  953. md.Float.__name__ = ["Float"];
  954. md.Bool = { __ename__ : ["Bool"]}
  955. md.Class = { __name__ : ["Class"]}
  956. md.Enum = { }
  957. md.Void = { __ename__ : ["Void"]}
  958. }
  959. {
  960. md.Math.__name__ = ["Math"];
  961. md.Math.NaN = Number["NaN"];
  962. md.Math.NEGATIVE_INFINITY = Number["NEGATIVE_INFINITY"];
  963. md.Math.POSITIVE_INFINITY = Number["POSITIVE_INFINITY"];
  964. md.Math.isFinite = function(i) {
  965. return isFinite(i);
  966. }
  967. md.Math.isNaN = function(i) {
  968. return isNaN(i);
  969. }
  970. }
  971. js.Lib.onerror = null;
  972. $Main.init = md.Markdown.main();
  973. return function(s){return md.Markdown.instance.makeHtml(s)};}());