markdown.js 50 KB

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