Theme Inspinia
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. // Released under MIT license
  2. // Copyright (c) 2009-2010 Dominic Baggott
  3. // Copyright (c) 2009-2010 Ash Berlin
  4. // Copyright (c) 2011 Christoph Dorn <christoph@christophdorn.com> (http://www.christophdorn.com)
  5. (function( expose ) {
  6. /**
  7. * class Markdown
  8. *
  9. * Markdown processing in Javascript done right. We have very particular views
  10. * on what constitutes 'right' which include:
  11. *
  12. * - produces well-formed HTML (this means that em and strong nesting is
  13. * important)
  14. *
  15. * - has an intermediate representation to allow processing of parsed data (We
  16. * in fact have two, both as [JsonML]: a markdown tree and an HTML tree).
  17. *
  18. * - is easily extensible to add new dialects without having to rewrite the
  19. * entire parsing mechanics
  20. *
  21. * - has a good test suite
  22. *
  23. * This implementation fulfills all of these (except that the test suite could
  24. * do with expanding to automatically run all the fixtures from other Markdown
  25. * implementations.)
  26. *
  27. * ##### Intermediate Representation
  28. *
  29. * *TODO* Talk about this :) Its JsonML, but document the node names we use.
  30. *
  31. * [JsonML]: http://jsonml.org/ "JSON Markup Language"
  32. **/
  33. var Markdown = expose.Markdown = function Markdown(dialect) {
  34. switch (typeof dialect) {
  35. case "undefined":
  36. this.dialect = Markdown.dialects.Gruber;
  37. break;
  38. case "object":
  39. this.dialect = dialect;
  40. break;
  41. default:
  42. if (dialect in Markdown.dialects) {
  43. this.dialect = Markdown.dialects[dialect];
  44. }
  45. else {
  46. throw new Error("Unknown Markdown dialect '" + String(dialect) + "'");
  47. }
  48. break;
  49. }
  50. this.em_state = [];
  51. this.strong_state = [];
  52. this.debug_indent = "";
  53. };
  54. /**
  55. * parse( markdown, [dialect] ) -> JsonML
  56. * - markdown (String): markdown string to parse
  57. * - dialect (String | Dialect): the dialect to use, defaults to gruber
  58. *
  59. * Parse `markdown` and return a markdown document as a Markdown.JsonML tree.
  60. **/
  61. expose.parse = function( source, dialect ) {
  62. // dialect will default if undefined
  63. var md = new Markdown( dialect );
  64. return md.toTree( source );
  65. };
  66. /**
  67. * toHTML( markdown, [dialect] ) -> String
  68. * toHTML( md_tree ) -> String
  69. * - markdown (String): markdown string to parse
  70. * - md_tree (Markdown.JsonML): parsed markdown tree
  71. *
  72. * Take markdown (either as a string or as a JsonML tree) and run it through
  73. * [[toHTMLTree]] then turn it into a well-formated HTML fragment.
  74. **/
  75. expose.toHTML = function toHTML( source , dialect , options ) {
  76. var input = expose.toHTMLTree( source , dialect , options );
  77. return expose.renderJsonML( input );
  78. };
  79. /**
  80. * toHTMLTree( markdown, [dialect] ) -> JsonML
  81. * toHTMLTree( md_tree ) -> JsonML
  82. * - markdown (String): markdown string to parse
  83. * - dialect (String | Dialect): the dialect to use, defaults to gruber
  84. * - md_tree (Markdown.JsonML): parsed markdown tree
  85. *
  86. * Turn markdown into HTML, represented as a JsonML tree. If a string is given
  87. * to this function, it is first parsed into a markdown tree by calling
  88. * [[parse]].
  89. **/
  90. expose.toHTMLTree = function toHTMLTree( input, dialect , options ) {
  91. // convert string input to an MD tree
  92. if ( typeof input ==="string" ) input = this.parse( input, dialect );
  93. // Now convert the MD tree to an HTML tree
  94. // remove references from the tree
  95. var attrs = extract_attr( input ),
  96. refs = {};
  97. if ( attrs && attrs.references ) {
  98. refs = attrs.references;
  99. }
  100. var html = convert_tree_to_html( input, refs , options );
  101. merge_text_nodes( html );
  102. return html;
  103. };
  104. // For Spidermonkey based engines
  105. function mk_block_toSource() {
  106. return "Markdown.mk_block( " +
  107. uneval(this.toString()) +
  108. ", " +
  109. uneval(this.trailing) +
  110. ", " +
  111. uneval(this.lineNumber) +
  112. " )";
  113. }
  114. // node
  115. function mk_block_inspect() {
  116. var util = require('util');
  117. return "Markdown.mk_block( " +
  118. util.inspect(this.toString()) +
  119. ", " +
  120. util.inspect(this.trailing) +
  121. ", " +
  122. util.inspect(this.lineNumber) +
  123. " )";
  124. }
  125. var mk_block = Markdown.mk_block = function(block, trail, line) {
  126. // Be helpful for default case in tests.
  127. if ( arguments.length == 1 ) trail = "\n\n";
  128. var s = new String(block);
  129. s.trailing = trail;
  130. // To make it clear its not just a string
  131. s.inspect = mk_block_inspect;
  132. s.toSource = mk_block_toSource;
  133. if (line != undefined)
  134. s.lineNumber = line;
  135. return s;
  136. };
  137. function count_lines( str ) {
  138. var n = 0, i = -1;
  139. while ( ( i = str.indexOf('\n', i+1) ) !== -1) n++;
  140. return n;
  141. }
  142. // Internal - split source into rough blocks
  143. Markdown.prototype.split_blocks = function splitBlocks( input, startLine ) {
  144. // [\s\S] matches _anything_ (newline or space)
  145. var re = /([\s\S]+?)($|\n(?:\s*\n|$)+)/g,
  146. blocks = [],
  147. m;
  148. var line_no = 1;
  149. if ( ( m = /^(\s*\n)/.exec(input) ) != null ) {
  150. // skip (but count) leading blank lines
  151. line_no += count_lines( m[0] );
  152. re.lastIndex = m[0].length;
  153. }
  154. while ( ( m = re.exec(input) ) !== null ) {
  155. blocks.push( mk_block( m[1], m[2], line_no ) );
  156. line_no += count_lines( m[0] );
  157. }
  158. return blocks;
  159. };
  160. /**
  161. * Markdown#processBlock( block, next ) -> undefined | [ JsonML, ... ]
  162. * - block (String): the block to process
  163. * - next (Array): the following blocks
  164. *
  165. * Process `block` and return an array of JsonML nodes representing `block`.
  166. *
  167. * It does this by asking each block level function in the dialect to process
  168. * the block until one can. Succesful handling is indicated by returning an
  169. * array (with zero or more JsonML nodes), failure by a false value.
  170. *
  171. * Blocks handlers are responsible for calling [[Markdown#processInline]]
  172. * themselves as appropriate.
  173. *
  174. * If the blocks were split incorrectly or adjacent blocks need collapsing you
  175. * can adjust `next` in place using shift/splice etc.
  176. *
  177. * If any of this default behaviour is not right for the dialect, you can
  178. * define a `__call__` method on the dialect that will get invoked to handle
  179. * the block processing.
  180. */
  181. Markdown.prototype.processBlock = function processBlock( block, next ) {
  182. var cbs = this.dialect.block,
  183. ord = cbs.__order__;
  184. if ( "__call__" in cbs ) {
  185. return cbs.__call__.call(this, block, next);
  186. }
  187. for ( var i = 0; i < ord.length; i++ ) {
  188. //D:this.debug( "Testing", ord[i] );
  189. var res = cbs[ ord[i] ].call( this, block, next );
  190. if ( res ) {
  191. //D:this.debug(" matched");
  192. if ( !isArray(res) || ( res.length > 0 && !( isArray(res[0]) ) ) )
  193. this.debug(ord[i], "didn't return a proper array");
  194. //D:this.debug( "" );
  195. return res;
  196. }
  197. }
  198. // Uhoh! no match! Should we throw an error?
  199. return [];
  200. };
  201. Markdown.prototype.processInline = function processInline( block ) {
  202. return this.dialect.inline.__call__.call( this, String( block ) );
  203. };
  204. /**
  205. * Markdown#toTree( source ) -> JsonML
  206. * - source (String): markdown source to parse
  207. *
  208. * Parse `source` into a JsonML tree representing the markdown document.
  209. **/
  210. // custom_tree means set this.tree to `custom_tree` and restore old value on return
  211. Markdown.prototype.toTree = function toTree( source, custom_root ) {
  212. var blocks = source instanceof Array ? source : this.split_blocks( source );
  213. // Make tree a member variable so its easier to mess with in extensions
  214. var old_tree = this.tree;
  215. try {
  216. this.tree = custom_root || this.tree || [ "markdown" ];
  217. blocks:
  218. while ( blocks.length ) {
  219. var b = this.processBlock( blocks.shift(), blocks );
  220. // Reference blocks and the like won't return any content
  221. if ( !b.length ) continue blocks;
  222. this.tree.push.apply( this.tree, b );
  223. }
  224. return this.tree;
  225. }
  226. finally {
  227. if ( custom_root ) {
  228. this.tree = old_tree;
  229. }
  230. }
  231. };
  232. // Noop by default
  233. Markdown.prototype.debug = function () {
  234. var args = Array.prototype.slice.call( arguments);
  235. args.unshift(this.debug_indent);
  236. if (typeof print !== "undefined")
  237. print.apply( print, args );
  238. if (typeof console !== "undefined" && typeof console.log !== "undefined")
  239. console.log.apply( null, args );
  240. }
  241. Markdown.prototype.loop_re_over_block = function( re, block, cb ) {
  242. // Dont use /g regexps with this
  243. var m,
  244. b = block.valueOf();
  245. while ( b.length && (m = re.exec(b) ) != null) {
  246. b = b.substr( m[0].length );
  247. cb.call(this, m);
  248. }
  249. return b;
  250. };
  251. /**
  252. * Markdown.dialects
  253. *
  254. * Namespace of built-in dialects.
  255. **/
  256. Markdown.dialects = {};
  257. /**
  258. * Markdown.dialects.Gruber
  259. *
  260. * The default dialect that follows the rules set out by John Gruber's
  261. * markdown.pl as closely as possible. Well actually we follow the behaviour of
  262. * that script which in some places is not exactly what the syntax web page
  263. * says.
  264. **/
  265. Markdown.dialects.Gruber = {
  266. block: {
  267. atxHeader: function atxHeader( block, next ) {
  268. var m = block.match( /^(#{1,6})\s*(.*?)\s*#*\s*(?:\n|$)/ );
  269. if ( !m ) return undefined;
  270. var header = [ "header", { level: m[ 1 ].length } ];
  271. Array.prototype.push.apply(header, this.processInline(m[ 2 ]));
  272. if ( m[0].length < block.length )
  273. next.unshift( mk_block( block.substr( m[0].length ), block.trailing, block.lineNumber + 2 ) );
  274. return [ header ];
  275. },
  276. setextHeader: function setextHeader( block, next ) {
  277. var m = block.match( /^(.*)\n([-=])\2\2+(?:\n|$)/ );
  278. if ( !m ) return undefined;
  279. var level = ( m[ 2 ] === "=" ) ? 1 : 2;
  280. var header = [ "header", { level : level }, m[ 1 ] ];
  281. if ( m[0].length < block.length )
  282. next.unshift( mk_block( block.substr( m[0].length ), block.trailing, block.lineNumber + 2 ) );
  283. return [ header ];
  284. },
  285. code: function code( block, next ) {
  286. // | Foo
  287. // |bar
  288. // should be a code block followed by a paragraph. Fun
  289. //
  290. // There might also be adjacent code block to merge.
  291. var ret = [],
  292. re = /^(?: {0,3}\t| {4})(.*)\n?/,
  293. lines;
  294. // 4 spaces + content
  295. if ( !block.match( re ) ) return undefined;
  296. block_search:
  297. do {
  298. // Now pull out the rest of the lines
  299. var b = this.loop_re_over_block(
  300. re, block.valueOf(), function( m ) { ret.push( m[1] ); } );
  301. if (b.length) {
  302. // Case alluded to in first comment. push it back on as a new block
  303. next.unshift( mk_block(b, block.trailing) );
  304. break block_search;
  305. }
  306. else if (next.length) {
  307. // Check the next block - it might be code too
  308. if ( !next[0].match( re ) ) break block_search;
  309. // Pull how how many blanks lines follow - minus two to account for .join
  310. ret.push ( block.trailing.replace(/[^\n]/g, '').substring(2) );
  311. block = next.shift();
  312. }
  313. else {
  314. break block_search;
  315. }
  316. } while (true);
  317. return [ [ "code_block", ret.join("\n") ] ];
  318. },
  319. horizRule: function horizRule( block, next ) {
  320. // this needs to find any hr in the block to handle abutting blocks
  321. var m = block.match( /^(?:([\s\S]*?)\n)?[ \t]*([-_*])(?:[ \t]*\2){2,}[ \t]*(?:\n([\s\S]*))?$/ );
  322. if ( !m ) {
  323. return undefined;
  324. }
  325. var jsonml = [ [ "hr" ] ];
  326. // if there's a leading abutting block, process it
  327. if ( m[ 1 ] ) {
  328. jsonml.unshift.apply( jsonml, this.processBlock( m[ 1 ], [] ) );
  329. }
  330. // if there's a trailing abutting block, stick it into next
  331. if ( m[ 3 ] ) {
  332. next.unshift( mk_block( m[ 3 ] ) );
  333. }
  334. return jsonml;
  335. },
  336. // There are two types of lists. Tight and loose. Tight lists have no whitespace
  337. // between the items (and result in text just in the <li>) and loose lists,
  338. // which have an empty line between list items, resulting in (one or more)
  339. // paragraphs inside the <li>.
  340. //
  341. // There are all sorts weird edge cases about the original markdown.pl's
  342. // handling of lists:
  343. //
  344. // * Nested lists are supposed to be indented by four chars per level. But
  345. // if they aren't, you can get a nested list by indenting by less than
  346. // four so long as the indent doesn't match an indent of an existing list
  347. // item in the 'nest stack'.
  348. //
  349. // * The type of the list (bullet or number) is controlled just by the
  350. // first item at the indent. Subsequent changes are ignored unless they
  351. // are for nested lists
  352. //
  353. lists: (function( ) {
  354. // Use a closure to hide a few variables.
  355. var any_list = "[*+-]|\\d+\\.",
  356. bullet_list = /[*+-]/,
  357. number_list = /\d+\./,
  358. // Capture leading indent as it matters for determining nested lists.
  359. is_list_re = new RegExp( "^( {0,3})(" + any_list + ")[ \t]+" ),
  360. indent_re = "(?: {0,3}\\t| {4})";
  361. // TODO: Cache this regexp for certain depths.
  362. // Create a regexp suitable for matching an li for a given stack depth
  363. function regex_for_depth( depth ) {
  364. return new RegExp(
  365. // m[1] = indent, m[2] = list_type
  366. "(?:^(" + indent_re + "{0," + depth + "} {0,3})(" + any_list + ")\\s+)|" +
  367. // m[3] = cont
  368. "(^" + indent_re + "{0," + (depth-1) + "}[ ]{0,4})"
  369. );
  370. }
  371. function expand_tab( input ) {
  372. return input.replace( / {0,3}\t/g, " " );
  373. }
  374. // Add inline content `inline` to `li`. inline comes from processInline
  375. // so is an array of content
  376. function add(li, loose, inline, nl) {
  377. if (loose) {
  378. li.push( [ "para" ].concat(inline) );
  379. return;
  380. }
  381. // Hmmm, should this be any block level element or just paras?
  382. var add_to = li[li.length -1] instanceof Array && li[li.length - 1][0] == "para"
  383. ? li[li.length -1]
  384. : li;
  385. // If there is already some content in this list, add the new line in
  386. if (nl && li.length > 1) inline.unshift(nl);
  387. for (var i=0; i < inline.length; i++) {
  388. var what = inline[i],
  389. is_str = typeof what == "string";
  390. if (is_str && add_to.length > 1 && typeof add_to[add_to.length-1] == "string" ) {
  391. add_to[ add_to.length-1 ] += what;
  392. }
  393. else {
  394. add_to.push( what );
  395. }
  396. }
  397. }
  398. // contained means have an indent greater than the current one. On
  399. // *every* line in the block
  400. function get_contained_blocks( depth, blocks ) {
  401. var re = new RegExp( "^(" + indent_re + "{" + depth + "}.*?\\n?)*$" ),
  402. replace = new RegExp("^" + indent_re + "{" + depth + "}", "gm"),
  403. ret = [];
  404. while ( blocks.length > 0 ) {
  405. if ( re.exec( blocks[0] ) ) {
  406. var b = blocks.shift(),
  407. // Now remove that indent
  408. x = b.replace( replace, "");
  409. ret.push( mk_block( x, b.trailing, b.lineNumber ) );
  410. }
  411. break;
  412. }
  413. return ret;
  414. }
  415. // passed to stack.forEach to turn list items up the stack into paras
  416. function paragraphify(s, i, stack) {
  417. var list = s.list;
  418. var last_li = list[list.length-1];
  419. if (last_li[1] instanceof Array && last_li[1][0] == "para") {
  420. return;
  421. }
  422. if (i+1 == stack.length) {
  423. // Last stack frame
  424. // Keep the same array, but replace the contents
  425. last_li.push( ["para"].concat( last_li.splice(1) ) );
  426. }
  427. else {
  428. var sublist = last_li.pop();
  429. last_li.push( ["para"].concat( last_li.splice(1) ), sublist );
  430. }
  431. }
  432. // The matcher function
  433. return function( block, next ) {
  434. var m = block.match( is_list_re );
  435. if ( !m ) return undefined;
  436. function make_list( m ) {
  437. var list = bullet_list.exec( m[2] )
  438. ? ["bulletlist"]
  439. : ["numberlist"];
  440. stack.push( { list: list, indent: m[1] } );
  441. return list;
  442. }
  443. var stack = [], // Stack of lists for nesting.
  444. list = make_list( m ),
  445. last_li,
  446. loose = false,
  447. ret = [ stack[0].list ],
  448. i;
  449. // Loop to search over block looking for inner block elements and loose lists
  450. loose_search:
  451. while( true ) {
  452. // Split into lines preserving new lines at end of line
  453. var lines = block.split( /(?=\n)/ );
  454. // We have to grab all lines for a li and call processInline on them
  455. // once as there are some inline things that can span lines.
  456. var li_accumulate = "";
  457. // Loop over the lines in this block looking for tight lists.
  458. tight_search:
  459. for (var line_no=0; line_no < lines.length; line_no++) {
  460. var nl = "",
  461. l = lines[line_no].replace(/^\n/, function(n) { nl = n; return ""; });
  462. // TODO: really should cache this
  463. var line_re = regex_for_depth( stack.length );
  464. m = l.match( line_re );
  465. //print( "line:", uneval(l), "\nline match:", uneval(m) );
  466. // We have a list item
  467. if ( m[1] !== undefined ) {
  468. // Process the previous list item, if any
  469. if ( li_accumulate.length ) {
  470. add( last_li, loose, this.processInline( li_accumulate ), nl );
  471. // Loose mode will have been dealt with. Reset it
  472. loose = false;
  473. li_accumulate = "";
  474. }
  475. m[1] = expand_tab( m[1] );
  476. var wanted_depth = Math.floor(m[1].length/4)+1;
  477. //print( "want:", wanted_depth, "stack:", stack.length);
  478. if ( wanted_depth > stack.length ) {
  479. // Deep enough for a nested list outright
  480. //print ( "new nested list" );
  481. list = make_list( m );
  482. last_li.push( list );
  483. last_li = list[1] = [ "listitem" ];
  484. }
  485. else {
  486. // We aren't deep enough to be strictly a new level. This is
  487. // where Md.pl goes nuts. If the indent matches a level in the
  488. // stack, put it there, else put it one deeper then the
  489. // wanted_depth deserves.
  490. var found = false;
  491. for (i = 0; i < stack.length; i++) {
  492. if ( stack[ i ].indent != m[1] ) continue;
  493. list = stack[ i ].list;
  494. stack.splice( i+1 );
  495. found = true;
  496. break;
  497. }
  498. if (!found) {
  499. //print("not found. l:", uneval(l));
  500. wanted_depth++;
  501. if (wanted_depth <= stack.length) {
  502. stack.splice(wanted_depth);
  503. //print("Desired depth now", wanted_depth, "stack:", stack.length);
  504. list = stack[wanted_depth-1].list;
  505. //print("list:", uneval(list) );
  506. }
  507. else {
  508. //print ("made new stack for messy indent");
  509. list = make_list(m);
  510. last_li.push(list);
  511. }
  512. }
  513. //print( uneval(list), "last", list === stack[stack.length-1].list );
  514. last_li = [ "listitem" ];
  515. list.push(last_li);
  516. } // end depth of shenegains
  517. nl = "";
  518. }
  519. // Add content
  520. if (l.length > m[0].length) {
  521. li_accumulate += nl + l.substr( m[0].length );
  522. }
  523. } // tight_search
  524. if ( li_accumulate.length ) {
  525. add( last_li, loose, this.processInline( li_accumulate ), nl );
  526. // Loose mode will have been dealt with. Reset it
  527. loose = false;
  528. li_accumulate = "";
  529. }
  530. // Look at the next block - we might have a loose list. Or an extra
  531. // paragraph for the current li
  532. var contained = get_contained_blocks( stack.length, next );
  533. // Deal with code blocks or properly nested lists
  534. if (contained.length > 0) {
  535. // Make sure all listitems up the stack are paragraphs
  536. forEach( stack, paragraphify, this);
  537. last_li.push.apply( last_li, this.toTree( contained, [] ) );
  538. }
  539. var next_block = next[0] && next[0].valueOf() || "";
  540. if ( next_block.match(is_list_re) || next_block.match( /^ / ) ) {
  541. block = next.shift();
  542. // Check for an HR following a list: features/lists/hr_abutting
  543. var hr = this.dialect.block.horizRule( block, next );
  544. if (hr) {
  545. ret.push.apply(ret, hr);
  546. break;
  547. }
  548. // Make sure all listitems up the stack are paragraphs
  549. forEach( stack, paragraphify, this);
  550. loose = true;
  551. continue loose_search;
  552. }
  553. break;
  554. } // loose_search
  555. return ret;
  556. };
  557. })(),
  558. blockquote: function blockquote( block, next ) {
  559. if ( !block.match( /^>/m ) )
  560. return undefined;
  561. var jsonml = [];
  562. // separate out the leading abutting block, if any
  563. if ( block[ 0 ] != ">" ) {
  564. var lines = block.split( /\n/ ),
  565. prev = [];
  566. // keep shifting lines until you find a crotchet
  567. while ( lines.length && lines[ 0 ][ 0 ] != ">" ) {
  568. prev.push( lines.shift() );
  569. }
  570. // reassemble!
  571. block = lines.join( "\n" );
  572. jsonml.push.apply( jsonml, this.processBlock( prev.join( "\n" ), [] ) );
  573. }
  574. // if the next block is also a blockquote merge it in
  575. while ( next.length && next[ 0 ][ 0 ] == ">" ) {
  576. var b = next.shift();
  577. block = new String(block + block.trailing + b);
  578. block.trailing = b.trailing;
  579. }
  580. // Strip off the leading "> " and re-process as a block.
  581. var input = block.replace( /^> ?/gm, '' ),
  582. old_tree = this.tree;
  583. jsonml.push( this.toTree( input, [ "blockquote" ] ) );
  584. return jsonml;
  585. },
  586. referenceDefn: function referenceDefn( block, next) {
  587. var re = /^\s*\[(.*?)\]:\s*(\S+)(?:\s+(?:(['"])(.*?)\3|\((.*?)\)))?\n?/;
  588. // interesting matches are [ , ref_id, url, , title, title ]
  589. if ( !block.match(re) )
  590. return undefined;
  591. // make an attribute node if it doesn't exist
  592. if ( !extract_attr( this.tree ) ) {
  593. this.tree.splice( 1, 0, {} );
  594. }
  595. var attrs = extract_attr( this.tree );
  596. // make a references hash if it doesn't exist
  597. if ( attrs.references === undefined ) {
  598. attrs.references = {};
  599. }
  600. var b = this.loop_re_over_block(re, block, function( m ) {
  601. if ( m[2] && m[2][0] == '<' && m[2][m[2].length-1] == '>' )
  602. m[2] = m[2].substring( 1, m[2].length - 1 );
  603. var ref = attrs.references[ m[1].toLowerCase() ] = {
  604. href: m[2]
  605. };
  606. if (m[4] !== undefined)
  607. ref.title = m[4];
  608. else if (m[5] !== undefined)
  609. ref.title = m[5];
  610. } );
  611. if (b.length)
  612. next.unshift( mk_block( b, block.trailing ) );
  613. return [];
  614. },
  615. para: function para( block, next ) {
  616. // everything's a para!
  617. return [ ["para"].concat( this.processInline( block ) ) ];
  618. }
  619. }
  620. };
  621. Markdown.dialects.Gruber.inline = {
  622. __oneElement__: function oneElement( text, patterns_or_re, previous_nodes ) {
  623. var m,
  624. res,
  625. lastIndex = 0;
  626. patterns_or_re = patterns_or_re || this.dialect.inline.__patterns__;
  627. var re = new RegExp( "([\\s\\S]*?)(" + (patterns_or_re.source || patterns_or_re) + ")" );
  628. m = re.exec( text );
  629. if (!m) {
  630. // Just boring text
  631. return [ text.length, text ];
  632. }
  633. else if ( m[1] ) {
  634. // Some un-interesting text matched. Return that first
  635. return [ m[1].length, m[1] ];
  636. }
  637. var res;
  638. if ( m[2] in this.dialect.inline ) {
  639. res = this.dialect.inline[ m[2] ].call(
  640. this,
  641. text.substr( m.index ), m, previous_nodes || [] );
  642. }
  643. // Default for now to make dev easier. just slurp special and output it.
  644. res = res || [ m[2].length, m[2] ];
  645. return res;
  646. },
  647. __call__: function inline( text, patterns ) {
  648. var out = [],
  649. res;
  650. function add(x) {
  651. //D:self.debug(" adding output", uneval(x));
  652. if (typeof x == "string" && typeof out[out.length-1] == "string")
  653. out[ out.length-1 ] += x;
  654. else
  655. out.push(x);
  656. }
  657. while ( text.length > 0 ) {
  658. res = this.dialect.inline.__oneElement__.call(this, text, patterns, out );
  659. text = text.substr( res.shift() );
  660. forEach(res, add )
  661. }
  662. return out;
  663. },
  664. // These characters are intersting elsewhere, so have rules for them so that
  665. // chunks of plain text blocks don't include them
  666. "]": function () {},
  667. "}": function () {},
  668. "\\": function escaped( text ) {
  669. // [ length of input processed, node/children to add... ]
  670. // Only esacape: \ ` * _ { } [ ] ( ) # * + - . !
  671. if ( text.match( /^\\[\\`\*_{}\[\]()#\+.!\-]/ ) )
  672. return [ 2, text[1] ];
  673. else
  674. // Not an esacpe
  675. return [ 1, "\\" ];
  676. },
  677. "![": function image( text ) {
  678. // Unlike images, alt text is plain text only. no other elements are
  679. // allowed in there
  680. // ![Alt text](/path/to/img.jpg "Optional title")
  681. // 1 2 3 4 <--- captures
  682. var m = text.match( /^!\[(.*?)\][ \t]*\([ \t]*(\S*)(?:[ \t]+(["'])(.*?)\3)?[ \t]*\)/ );
  683. if ( m ) {
  684. if ( m[2] && m[2][0] == '<' && m[2][m[2].length-1] == '>' )
  685. m[2] = m[2].substring( 1, m[2].length - 1 );
  686. m[2] = this.dialect.inline.__call__.call( this, m[2], /\\/ )[0];
  687. var attrs = { alt: m[1], href: m[2] || "" };
  688. if ( m[4] !== undefined)
  689. attrs.title = m[4];
  690. return [ m[0].length, [ "img", attrs ] ];
  691. }
  692. // ![Alt text][id]
  693. m = text.match( /^!\[(.*?)\][ \t]*\[(.*?)\]/ );
  694. if ( m ) {
  695. // We can't check if the reference is known here as it likely wont be
  696. // found till after. Check it in md tree->hmtl tree conversion
  697. return [ m[0].length, [ "img_ref", { alt: m[1], ref: m[2].toLowerCase(), original: m[0] } ] ];
  698. }
  699. // Just consume the '!['
  700. return [ 2, "![" ];
  701. },
  702. "[": function link( text ) {
  703. var orig = String(text);
  704. // Inline content is possible inside `link text`
  705. var res = Markdown.DialectHelpers.inline_until_char.call( this, text.substr(1), ']' );
  706. // No closing ']' found. Just consume the [
  707. if ( !res ) return [ 1, '[' ];
  708. var consumed = 1 + res[ 0 ],
  709. children = res[ 1 ],
  710. link,
  711. attrs;
  712. // At this point the first [...] has been parsed. See what follows to find
  713. // out which kind of link we are (reference or direct url)
  714. text = text.substr( consumed );
  715. // [link text](/path/to/img.jpg "Optional title")
  716. // 1 2 3 <--- captures
  717. // This will capture up to the last paren in the block. We then pull
  718. // back based on if there a matching ones in the url
  719. // ([here](/url/(test))
  720. // The parens have to be balanced
  721. var m = text.match( /^\s*\([ \t]*(\S+)(?:[ \t]+(["'])(.*?)\2)?[ \t]*\)/ );
  722. if ( m ) {
  723. var url = m[1];
  724. consumed += m[0].length;
  725. if ( url && url[0] == '<' && url[url.length-1] == '>' )
  726. url = url.substring( 1, url.length - 1 );
  727. // If there is a title we don't have to worry about parens in the url
  728. if ( !m[3] ) {
  729. var open_parens = 1; // One open that isn't in the capture
  730. for (var len = 0; len < url.length; len++) {
  731. switch ( url[len] ) {
  732. case '(':
  733. open_parens++;
  734. break;
  735. case ')':
  736. if ( --open_parens == 0) {
  737. consumed -= url.length - len;
  738. url = url.substring(0, len);
  739. }
  740. break;
  741. }
  742. }
  743. }
  744. // Process escapes only
  745. url = this.dialect.inline.__call__.call( this, url, /\\/ )[0];
  746. attrs = { href: url || "" };
  747. if ( m[3] !== undefined)
  748. attrs.title = m[3];
  749. link = [ "link", attrs ].concat( children );
  750. return [ consumed, link ];
  751. }
  752. // [Alt text][id]
  753. // [Alt text] [id]
  754. m = text.match( /^\s*\[(.*?)\]/ );
  755. if ( m ) {
  756. consumed += m[ 0 ].length;
  757. // [links][] uses links as its reference
  758. attrs = { ref: ( m[ 1 ] || String(children) ).toLowerCase(), original: orig.substr( 0, consumed ) };
  759. link = [ "link_ref", attrs ].concat( children );
  760. // We can't check if the reference is known here as it likely wont be
  761. // found till after. Check it in md tree->hmtl tree conversion.
  762. // Store the original so that conversion can revert if the ref isn't found.
  763. return [ consumed, link ];
  764. }
  765. // [id]
  766. // Only if id is plain (no formatting.)
  767. if ( children.length == 1 && typeof children[0] == "string" ) {
  768. attrs = { ref: children[0].toLowerCase(), original: orig.substr( 0, consumed ) };
  769. link = [ "link_ref", attrs, children[0] ];
  770. return [ consumed, link ];
  771. }
  772. // Just consume the '['
  773. return [ 1, "[" ];
  774. },
  775. "<": function autoLink( text ) {
  776. var m;
  777. if ( ( m = text.match( /^<(?:((https?|ftp|mailto):[^>]+)|(.*?@.*?\.[a-zA-Z]+))>/ ) ) != null ) {
  778. if ( m[3] ) {
  779. return [ m[0].length, [ "link", { href: "mailto:" + m[3] }, m[3] ] ];
  780. }
  781. else if ( m[2] == "mailto" ) {
  782. return [ m[0].length, [ "link", { href: m[1] }, m[1].substr("mailto:".length ) ] ];
  783. }
  784. else
  785. return [ m[0].length, [ "link", { href: m[1] }, m[1] ] ];
  786. }
  787. return [ 1, "<" ];
  788. },
  789. "`": function inlineCode( text ) {
  790. // Inline code block. as many backticks as you like to start it
  791. // Always skip over the opening ticks.
  792. var m = text.match( /(`+)(([\s\S]*?)\1)/ );
  793. if ( m && m[2] )
  794. return [ m[1].length + m[2].length, [ "inlinecode", m[3] ] ];
  795. else {
  796. // TODO: No matching end code found - warn!
  797. return [ 1, "`" ];
  798. }
  799. },
  800. " \n": function lineBreak( text ) {
  801. return [ 3, [ "linebreak" ] ];
  802. }
  803. };
  804. // Meta Helper/generator method for em and strong handling
  805. function strong_em( tag, md ) {
  806. var state_slot = tag + "_state",
  807. other_slot = tag == "strong" ? "em_state" : "strong_state";
  808. function CloseTag(len) {
  809. this.len_after = len;
  810. this.name = "close_" + md;
  811. }
  812. return function ( text, orig_match ) {
  813. if (this[state_slot][0] == md) {
  814. // Most recent em is of this type
  815. //D:this.debug("closing", md);
  816. this[state_slot].shift();
  817. // "Consume" everything to go back to the recrusion in the else-block below
  818. return[ text.length, new CloseTag(text.length-md.length) ];
  819. }
  820. else {
  821. // Store a clone of the em/strong states
  822. var other = this[other_slot].slice(),
  823. state = this[state_slot].slice();
  824. this[state_slot].unshift(md);
  825. //D:this.debug_indent += " ";
  826. // Recurse
  827. var res = this.processInline( text.substr( md.length ) );
  828. //D:this.debug_indent = this.debug_indent.substr(2);
  829. var last = res[res.length - 1];
  830. //D:this.debug("processInline from", tag + ": ", uneval( res ) );
  831. var check = this[state_slot].shift();
  832. if (last instanceof CloseTag) {
  833. res.pop();
  834. // We matched! Huzzah.
  835. var consumed = text.length - last.len_after;
  836. return [ consumed, [ tag ].concat(res) ];
  837. }
  838. else {
  839. // Restore the state of the other kind. We might have mistakenly closed it.
  840. this[other_slot] = other;
  841. this[state_slot] = state;
  842. // We can't reuse the processed result as it could have wrong parsing contexts in it.
  843. return [ md.length, md ];
  844. }
  845. }
  846. }; // End returned function
  847. }
  848. Markdown.dialects.Gruber.inline["**"] = strong_em("strong", "**");
  849. Markdown.dialects.Gruber.inline["__"] = strong_em("strong", "__");
  850. Markdown.dialects.Gruber.inline["*"] = strong_em("em", "*");
  851. Markdown.dialects.Gruber.inline["_"] = strong_em("em", "_");
  852. // Build default order from insertion order.
  853. Markdown.buildBlockOrder = function(d) {
  854. var ord = [];
  855. for ( var i in d ) {
  856. if ( i == "__order__" || i == "__call__" ) continue;
  857. ord.push( i );
  858. }
  859. d.__order__ = ord;
  860. };
  861. // Build patterns for inline matcher
  862. Markdown.buildInlinePatterns = function(d) {
  863. var patterns = [];
  864. for ( var i in d ) {
  865. // __foo__ is reserved and not a pattern
  866. if ( i.match( /^__.*__$/) ) continue;
  867. var l = i.replace( /([\\.*+?|()\[\]{}])/g, "\\$1" )
  868. .replace( /\n/, "\\n" );
  869. patterns.push( i.length == 1 ? l : "(?:" + l + ")" );
  870. }
  871. patterns = patterns.join("|");
  872. d.__patterns__ = patterns;
  873. //print("patterns:", uneval( patterns ) );
  874. var fn = d.__call__;
  875. d.__call__ = function(text, pattern) {
  876. if (pattern != undefined) {
  877. return fn.call(this, text, pattern);
  878. }
  879. else
  880. {
  881. return fn.call(this, text, patterns);
  882. }
  883. };
  884. };
  885. Markdown.DialectHelpers = {};
  886. Markdown.DialectHelpers.inline_until_char = function( text, want ) {
  887. var consumed = 0,
  888. nodes = [];
  889. while ( true ) {
  890. if ( text[ consumed ] == want ) {
  891. // Found the character we were looking for
  892. consumed++;
  893. return [ consumed, nodes ];
  894. }
  895. if ( consumed >= text.length ) {
  896. // No closing char found. Abort.
  897. return null;
  898. }
  899. var res = this.dialect.inline.__oneElement__.call(this, text.substr( consumed ) );
  900. consumed += res[ 0 ];
  901. // Add any returned nodes.
  902. nodes.push.apply( nodes, res.slice( 1 ) );
  903. }
  904. }
  905. // Helper function to make sub-classing a dialect easier
  906. Markdown.subclassDialect = function( d ) {
  907. function Block() {}
  908. Block.prototype = d.block;
  909. function Inline() {}
  910. Inline.prototype = d.inline;
  911. return { block: new Block(), inline: new Inline() };
  912. };
  913. Markdown.buildBlockOrder ( Markdown.dialects.Gruber.block );
  914. Markdown.buildInlinePatterns( Markdown.dialects.Gruber.inline );
  915. Markdown.dialects.Maruku = Markdown.subclassDialect( Markdown.dialects.Gruber );
  916. Markdown.dialects.Maruku.processMetaHash = function processMetaHash( meta_string ) {
  917. var meta = split_meta_hash( meta_string ),
  918. attr = {};
  919. for ( var i = 0; i < meta.length; ++i ) {
  920. // id: #foo
  921. if ( /^#/.test( meta[ i ] ) ) {
  922. attr.id = meta[ i ].substring( 1 );
  923. }
  924. // class: .foo
  925. else if ( /^\./.test( meta[ i ] ) ) {
  926. // if class already exists, append the new one
  927. if ( attr['class'] ) {
  928. attr['class'] = attr['class'] + meta[ i ].replace( /./, " " );
  929. }
  930. else {
  931. attr['class'] = meta[ i ].substring( 1 );
  932. }
  933. }
  934. // attribute: foo=bar
  935. else if ( /\=/.test( meta[ i ] ) ) {
  936. var s = meta[ i ].split( /\=/ );
  937. attr[ s[ 0 ] ] = s[ 1 ];
  938. }
  939. }
  940. return attr;
  941. }
  942. function split_meta_hash( meta_string ) {
  943. var meta = meta_string.split( "" ),
  944. parts = [ "" ],
  945. in_quotes = false;
  946. while ( meta.length ) {
  947. var letter = meta.shift();
  948. switch ( letter ) {
  949. case " " :
  950. // if we're in a quoted section, keep it
  951. if ( in_quotes ) {
  952. parts[ parts.length - 1 ] += letter;
  953. }
  954. // otherwise make a new part
  955. else {
  956. parts.push( "" );
  957. }
  958. break;
  959. case "'" :
  960. case '"' :
  961. // reverse the quotes and move straight on
  962. in_quotes = !in_quotes;
  963. break;
  964. case "\\" :
  965. // shift off the next letter to be used straight away.
  966. // it was escaped so we'll keep it whatever it is
  967. letter = meta.shift();
  968. default :
  969. parts[ parts.length - 1 ] += letter;
  970. break;
  971. }
  972. }
  973. return parts;
  974. }
  975. Markdown.dialects.Maruku.block.document_meta = function document_meta( block, next ) {
  976. // we're only interested in the first block
  977. if ( block.lineNumber > 1 ) return undefined;
  978. // document_meta blocks consist of one or more lines of `Key: Value\n`
  979. if ( ! block.match( /^(?:\w+:.*\n)*\w+:.*$/ ) ) return undefined;
  980. // make an attribute node if it doesn't exist
  981. if ( !extract_attr( this.tree ) ) {
  982. this.tree.splice( 1, 0, {} );
  983. }
  984. var pairs = block.split( /\n/ );
  985. for ( p in pairs ) {
  986. var m = pairs[ p ].match( /(\w+):\s*(.*)$/ ),
  987. key = m[ 1 ].toLowerCase(),
  988. value = m[ 2 ];
  989. this.tree[ 1 ][ key ] = value;
  990. }
  991. // document_meta produces no content!
  992. return [];
  993. };
  994. Markdown.dialects.Maruku.block.block_meta = function block_meta( block, next ) {
  995. // check if the last line of the block is an meta hash
  996. var m = block.match( /(^|\n) {0,3}\{:\s*((?:\\\}|[^\}])*)\s*\}$/ );
  997. if ( !m ) return undefined;
  998. // process the meta hash
  999. var attr = this.dialect.processMetaHash( m[ 2 ] );
  1000. var hash;
  1001. // if we matched ^ then we need to apply meta to the previous block
  1002. if ( m[ 1 ] === "" ) {
  1003. var node = this.tree[ this.tree.length - 1 ];
  1004. hash = extract_attr( node );
  1005. // if the node is a string (rather than JsonML), bail
  1006. if ( typeof node === "string" ) return undefined;
  1007. // create the attribute hash if it doesn't exist
  1008. if ( !hash ) {
  1009. hash = {};
  1010. node.splice( 1, 0, hash );
  1011. }
  1012. // add the attributes in
  1013. for ( a in attr ) {
  1014. hash[ a ] = attr[ a ];
  1015. }
  1016. // return nothing so the meta hash is removed
  1017. return [];
  1018. }
  1019. // pull the meta hash off the block and process what's left
  1020. var b = block.replace( /\n.*$/, "" ),
  1021. result = this.processBlock( b, [] );
  1022. // get or make the attributes hash
  1023. hash = extract_attr( result[ 0 ] );
  1024. if ( !hash ) {
  1025. hash = {};
  1026. result[ 0 ].splice( 1, 0, hash );
  1027. }
  1028. // attach the attributes to the block
  1029. for ( a in attr ) {
  1030. hash[ a ] = attr[ a ];
  1031. }
  1032. return result;
  1033. };
  1034. Markdown.dialects.Maruku.block.definition_list = function definition_list( block, next ) {
  1035. // one or more terms followed by one or more definitions, in a single block
  1036. var tight = /^((?:[^\s:].*\n)+):\s+([\s\S]+)$/,
  1037. list = [ "dl" ],
  1038. i;
  1039. // see if we're dealing with a tight or loose block
  1040. if ( ( m = block.match( tight ) ) ) {
  1041. // pull subsequent tight DL blocks out of `next`
  1042. var blocks = [ block ];
  1043. while ( next.length && tight.exec( next[ 0 ] ) ) {
  1044. blocks.push( next.shift() );
  1045. }
  1046. for ( var b = 0; b < blocks.length; ++b ) {
  1047. var m = blocks[ b ].match( tight ),
  1048. terms = m[ 1 ].replace( /\n$/, "" ).split( /\n/ ),
  1049. defns = m[ 2 ].split( /\n:\s+/ );
  1050. // print( uneval( m ) );
  1051. for ( i = 0; i < terms.length; ++i ) {
  1052. list.push( [ "dt", terms[ i ] ] );
  1053. }
  1054. for ( i = 0; i < defns.length; ++i ) {
  1055. // run inline processing over the definition
  1056. list.push( [ "dd" ].concat( this.processInline( defns[ i ].replace( /(\n)\s+/, "$1" ) ) ) );
  1057. }
  1058. }
  1059. }
  1060. else {
  1061. return undefined;
  1062. }
  1063. return [ list ];
  1064. };
  1065. Markdown.dialects.Maruku.inline[ "{:" ] = function inline_meta( text, matches, out ) {
  1066. if ( !out.length ) {
  1067. return [ 2, "{:" ];
  1068. }
  1069. // get the preceeding element
  1070. var before = out[ out.length - 1 ];
  1071. if ( typeof before === "string" ) {
  1072. return [ 2, "{:" ];
  1073. }
  1074. // match a meta hash
  1075. var m = text.match( /^\{:\s*((?:\\\}|[^\}])*)\s*\}/ );
  1076. // no match, false alarm
  1077. if ( !m ) {
  1078. return [ 2, "{:" ];
  1079. }
  1080. // attach the attributes to the preceeding element
  1081. var meta = this.dialect.processMetaHash( m[ 1 ] ),
  1082. attr = extract_attr( before );
  1083. if ( !attr ) {
  1084. attr = {};
  1085. before.splice( 1, 0, attr );
  1086. }
  1087. for ( var k in meta ) {
  1088. attr[ k ] = meta[ k ];
  1089. }
  1090. // cut out the string and replace it with nothing
  1091. return [ m[ 0 ].length, "" ];
  1092. };
  1093. Markdown.buildBlockOrder ( Markdown.dialects.Maruku.block );
  1094. Markdown.buildInlinePatterns( Markdown.dialects.Maruku.inline );
  1095. var isArray = Array.isArray || function(obj) {
  1096. return Object.prototype.toString.call(obj) == '[object Array]';
  1097. };
  1098. var forEach;
  1099. // Don't mess with Array.prototype. Its not friendly
  1100. if ( Array.prototype.forEach ) {
  1101. forEach = function( arr, cb, thisp ) {
  1102. return arr.forEach( cb, thisp );
  1103. };
  1104. }
  1105. else {
  1106. forEach = function(arr, cb, thisp) {
  1107. for (var i = 0; i < arr.length; i++) {
  1108. cb.call(thisp || arr, arr[i], i, arr);
  1109. }
  1110. }
  1111. }
  1112. function extract_attr( jsonml ) {
  1113. return isArray(jsonml)
  1114. && jsonml.length > 1
  1115. && typeof jsonml[ 1 ] === "object"
  1116. && !( isArray(jsonml[ 1 ]) )
  1117. ? jsonml[ 1 ]
  1118. : undefined;
  1119. }
  1120. /**
  1121. * renderJsonML( jsonml[, options] ) -> String
  1122. * - jsonml (Array): JsonML array to render to XML
  1123. * - options (Object): options
  1124. *
  1125. * Converts the given JsonML into well-formed XML.
  1126. *
  1127. * The options currently understood are:
  1128. *
  1129. * - root (Boolean): wether or not the root node should be included in the
  1130. * output, or just its children. The default `false` is to not include the
  1131. * root itself.
  1132. */
  1133. expose.renderJsonML = function( jsonml, options ) {
  1134. options = options || {};
  1135. // include the root element in the rendered output?
  1136. options.root = options.root || false;
  1137. var content = [];
  1138. if ( options.root ) {
  1139. content.push( render_tree( jsonml ) );
  1140. }
  1141. else {
  1142. jsonml.shift(); // get rid of the tag
  1143. if ( jsonml.length && typeof jsonml[ 0 ] === "object" && !( jsonml[ 0 ] instanceof Array ) ) {
  1144. jsonml.shift(); // get rid of the attributes
  1145. }
  1146. while ( jsonml.length ) {
  1147. content.push( render_tree( jsonml.shift() ) );
  1148. }
  1149. }
  1150. return content.join( "\n\n" );
  1151. };
  1152. function escapeHTML( text ) {
  1153. return text.replace( /&/g, "&amp;" )
  1154. .replace( /</g, "&lt;" )
  1155. .replace( />/g, "&gt;" )
  1156. .replace( /"/g, "&quot;" )
  1157. .replace( /'/g, "&#39;" );
  1158. }
  1159. function render_tree( jsonml ) {
  1160. // basic case
  1161. if ( typeof jsonml === "string" ) {
  1162. return escapeHTML( jsonml );
  1163. }
  1164. var tag = jsonml.shift(),
  1165. attributes = {},
  1166. content = [];
  1167. if ( jsonml.length && typeof jsonml[ 0 ] === "object" && !( jsonml[ 0 ] instanceof Array ) ) {
  1168. attributes = jsonml.shift();
  1169. }
  1170. while ( jsonml.length ) {
  1171. content.push( arguments.callee( jsonml.shift() ) );
  1172. }
  1173. var tag_attrs = "";
  1174. for ( var a in attributes ) {
  1175. tag_attrs += " " + a + '="' + escapeHTML( attributes[ a ] ) + '"';
  1176. }
  1177. // be careful about adding whitespace here for inline elements
  1178. if ( tag == "img" || tag == "br" || tag == "hr" ) {
  1179. return "<"+ tag + tag_attrs + "/>";
  1180. }
  1181. else {
  1182. return "<"+ tag + tag_attrs + ">" + content.join( "" ) + "</" + tag + ">";
  1183. }
  1184. }
  1185. function convert_tree_to_html( tree, references, options ) {
  1186. var i;
  1187. options = options || {};
  1188. // shallow clone
  1189. var jsonml = tree.slice( 0 );
  1190. if (typeof options.preprocessTreeNode === "function") {
  1191. jsonml = options.preprocessTreeNode(jsonml, references);
  1192. }
  1193. // Clone attributes if they exist
  1194. var attrs = extract_attr( jsonml );
  1195. if ( attrs ) {
  1196. jsonml[ 1 ] = {};
  1197. for ( i in attrs ) {
  1198. jsonml[ 1 ][ i ] = attrs[ i ];
  1199. }
  1200. attrs = jsonml[ 1 ];
  1201. }
  1202. // basic case
  1203. if ( typeof jsonml === "string" ) {
  1204. return jsonml;
  1205. }
  1206. // convert this node
  1207. switch ( jsonml[ 0 ] ) {
  1208. case "header":
  1209. jsonml[ 0 ] = "h" + jsonml[ 1 ].level;
  1210. delete jsonml[ 1 ].level;
  1211. break;
  1212. case "bulletlist":
  1213. jsonml[ 0 ] = "ul";
  1214. break;
  1215. case "numberlist":
  1216. jsonml[ 0 ] = "ol";
  1217. break;
  1218. case "listitem":
  1219. jsonml[ 0 ] = "li";
  1220. break;
  1221. case "para":
  1222. jsonml[ 0 ] = "p";
  1223. break;
  1224. case "markdown":
  1225. jsonml[ 0 ] = "html";
  1226. if ( attrs ) delete attrs.references;
  1227. break;
  1228. case "code_block":
  1229. jsonml[ 0 ] = "pre";
  1230. i = attrs ? 2 : 1;
  1231. var code = [ "code" ];
  1232. code.push.apply( code, jsonml.splice( i ) );
  1233. jsonml[ i ] = code;
  1234. break;
  1235. case "inlinecode":
  1236. jsonml[ 0 ] = "code";
  1237. break;
  1238. case "img":
  1239. jsonml[ 1 ].src = jsonml[ 1 ].href;
  1240. delete jsonml[ 1 ].href;
  1241. break;
  1242. case "linebreak":
  1243. jsonml[ 0 ] = "br";
  1244. break;
  1245. case "link":
  1246. jsonml[ 0 ] = "a";
  1247. break;
  1248. case "link_ref":
  1249. jsonml[ 0 ] = "a";
  1250. // grab this ref and clean up the attribute node
  1251. var ref = references[ attrs.ref ];
  1252. // if the reference exists, make the link
  1253. if ( ref ) {
  1254. delete attrs.ref;
  1255. // add in the href and title, if present
  1256. attrs.href = ref.href;
  1257. if ( ref.title ) {
  1258. attrs.title = ref.title;
  1259. }
  1260. // get rid of the unneeded original text
  1261. delete attrs.original;
  1262. }
  1263. // the reference doesn't exist, so revert to plain text
  1264. else {
  1265. return attrs.original;
  1266. }
  1267. break;
  1268. case "img_ref":
  1269. jsonml[ 0 ] = "img";
  1270. // grab this ref and clean up the attribute node
  1271. var ref = references[ attrs.ref ];
  1272. // if the reference exists, make the link
  1273. if ( ref ) {
  1274. delete attrs.ref;
  1275. // add in the href and title, if present
  1276. attrs.src = ref.href;
  1277. if ( ref.title ) {
  1278. attrs.title = ref.title;
  1279. }
  1280. // get rid of the unneeded original text
  1281. delete attrs.original;
  1282. }
  1283. // the reference doesn't exist, so revert to plain text
  1284. else {
  1285. return attrs.original;
  1286. }
  1287. break;
  1288. }
  1289. // convert all the children
  1290. i = 1;
  1291. // deal with the attribute node, if it exists
  1292. if ( attrs ) {
  1293. // if there are keys, skip over it
  1294. for ( var key in jsonml[ 1 ] ) {
  1295. i = 2;
  1296. }
  1297. // if there aren't, remove it
  1298. if ( i === 1 ) {
  1299. jsonml.splice( i, 1 );
  1300. }
  1301. }
  1302. for ( ; i < jsonml.length; ++i ) {
  1303. jsonml[ i ] = arguments.callee( jsonml[ i ], references, options );
  1304. }
  1305. return jsonml;
  1306. }
  1307. // merges adjacent text nodes into a single node
  1308. function merge_text_nodes( jsonml ) {
  1309. // skip the tag name and attribute hash
  1310. var i = extract_attr( jsonml ) ? 2 : 1;
  1311. while ( i < jsonml.length ) {
  1312. // if it's a string check the next item too
  1313. if ( typeof jsonml[ i ] === "string" ) {
  1314. if ( i + 1 < jsonml.length && typeof jsonml[ i + 1 ] === "string" ) {
  1315. // merge the second string into the first and remove it
  1316. jsonml[ i ] += jsonml.splice( i + 1, 1 )[ 0 ];
  1317. }
  1318. else {
  1319. ++i;
  1320. }
  1321. }
  1322. // if it's not a string recurse
  1323. else {
  1324. arguments.callee( jsonml[ i ] );
  1325. ++i;
  1326. }
  1327. }
  1328. }
  1329. } )( (function() {
  1330. if ( typeof exports === "undefined" ) {
  1331. window.markdown = {};
  1332. return window.markdown;
  1333. }
  1334. else {
  1335. return exports;
  1336. }
  1337. } )() );