12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616 |
- (function( expose ) {
- var Markdown = expose.Markdown = function Markdown(dialect) {
- switch (typeof dialect) {
- case "undefined":
- this.dialect = Markdown.dialects.Gruber;
- break;
- case "object":
- this.dialect = dialect;
- break;
- default:
- if (dialect in Markdown.dialects) {
- this.dialect = Markdown.dialects[dialect];
- }
- else {
- throw new Error("Unknown Markdown dialect '" + String(dialect) + "'");
- }
- break;
- }
- this.em_state = [];
- this.strong_state = [];
- this.debug_indent = "";
- };
- expose.parse = function( source, dialect ) {
-
- var md = new Markdown( dialect );
- return md.toTree( source );
- };
- expose.toHTML = function toHTML( source , dialect , options ) {
- var input = expose.toHTMLTree( source , dialect , options );
- return expose.renderJsonML( input );
- };
- expose.toHTMLTree = function toHTMLTree( input, dialect , options ) {
-
- if ( typeof input ==="string" ) input = this.parse( input, dialect );
-
-
- var attrs = extract_attr( input ),
- refs = {};
- if ( attrs && attrs.references ) {
- refs = attrs.references;
- }
- var html = convert_tree_to_html( input, refs , options );
- merge_text_nodes( html );
- return html;
- };
- function mk_block_toSource() {
- return "Markdown.mk_block( " +
- uneval(this.toString()) +
- ", " +
- uneval(this.trailing) +
- ", " +
- uneval(this.lineNumber) +
- " )";
- }
- function mk_block_inspect() {
- var util = require('util');
- return "Markdown.mk_block( " +
- util.inspect(this.toString()) +
- ", " +
- util.inspect(this.trailing) +
- ", " +
- util.inspect(this.lineNumber) +
- " )";
- }
- var mk_block = Markdown.mk_block = function(block, trail, line) {
-
- if ( arguments.length == 1 ) trail = "\n\n";
- var s = new String(block);
- s.trailing = trail;
-
- s.inspect = mk_block_inspect;
- s.toSource = mk_block_toSource;
- if (line != undefined)
- s.lineNumber = line;
- return s;
- };
- function count_lines( str ) {
- var n = 0, i = -1;
- while ( ( i = str.indexOf('\n', i+1) ) !== -1) n++;
- return n;
- }
- Markdown.prototype.split_blocks = function splitBlocks( input, startLine ) {
-
- var re = /([\s\S]+?)($|\n(?:\s*\n|$)+)/g,
- blocks = [],
- m;
- var line_no = 1;
- if ( ( m = /^(\s*\n)/.exec(input) ) != null ) {
-
- line_no += count_lines( m[0] );
- re.lastIndex = m[0].length;
- }
- while ( ( m = re.exec(input) ) !== null ) {
- blocks.push( mk_block( m[1], m[2], line_no ) );
- line_no += count_lines( m[0] );
- }
- return blocks;
- };
- Markdown.prototype.processBlock = function processBlock( block, next ) {
- var cbs = this.dialect.block,
- ord = cbs.__order__;
- if ( "__call__" in cbs ) {
- return cbs.__call__.call(this, block, next);
- }
- for ( var i = 0; i < ord.length; i++ ) {
-
- var res = cbs[ ord[i] ].call( this, block, next );
- if ( res ) {
-
- if ( !isArray(res) || ( res.length > 0 && !( isArray(res[0]) ) ) )
- this.debug(ord[i], "didn't return a proper array");
-
- return res;
- }
- }
-
- return [];
- };
- Markdown.prototype.processInline = function processInline( block ) {
- return this.dialect.inline.__call__.call( this, String( block ) );
- };
- Markdown.prototype.toTree = function toTree( source, custom_root ) {
- var blocks = source instanceof Array ? source : this.split_blocks( source );
-
- var old_tree = this.tree;
- try {
- this.tree = custom_root || this.tree || [ "markdown" ];
- blocks:
- while ( blocks.length ) {
- var b = this.processBlock( blocks.shift(), blocks );
-
- if ( !b.length ) continue blocks;
- this.tree.push.apply( this.tree, b );
- }
- return this.tree;
- }
- finally {
- if ( custom_root ) {
- this.tree = old_tree;
- }
- }
- };
- Markdown.prototype.debug = function () {
- var args = Array.prototype.slice.call( arguments);
- args.unshift(this.debug_indent);
- if (typeof print !== "undefined")
- print.apply( print, args );
- if (typeof console !== "undefined" && typeof console.log !== "undefined")
- console.log.apply( null, args );
- }
- Markdown.prototype.loop_re_over_block = function( re, block, cb ) {
-
- var m,
- b = block.valueOf();
- while ( b.length && (m = re.exec(b) ) != null) {
- b = b.substr( m[0].length );
- cb.call(this, m);
- }
- return b;
- };
- Markdown.dialects = {};
- Markdown.dialects.Gruber = {
- block: {
- atxHeader: function atxHeader( block, next ) {
- var m = block.match( /^(#{1,6})\s*(.*?)\s*#*\s*(?:\n|$)/ );
- if ( !m ) return undefined;
- var header = [ "header", { level: m[ 1 ].length } ];
- Array.prototype.push.apply(header, this.processInline(m[ 2 ]));
- if ( m[0].length < block.length )
- next.unshift( mk_block( block.substr( m[0].length ), block.trailing, block.lineNumber + 2 ) );
- return [ header ];
- },
- setextHeader: function setextHeader( block, next ) {
- var m = block.match( /^(.*)\n([-=])\2\2+(?:\n|$)/ );
- if ( !m ) return undefined;
- var level = ( m[ 2 ] === "=" ) ? 1 : 2;
- var header = [ "header", { level : level }, m[ 1 ] ];
- if ( m[0].length < block.length )
- next.unshift( mk_block( block.substr( m[0].length ), block.trailing, block.lineNumber + 2 ) );
- return [ header ];
- },
- code: function code( block, next ) {
-
-
-
-
-
- var ret = [],
- re = /^(?: {0,3}\t| {4})(.*)\n?/,
- lines;
-
- if ( !block.match( re ) ) return undefined;
- block_search:
- do {
-
- var b = this.loop_re_over_block(
- re, block.valueOf(), function( m ) { ret.push( m[1] ); } );
- if (b.length) {
-
- next.unshift( mk_block(b, block.trailing) );
- break block_search;
- }
- else if (next.length) {
-
- if ( !next[0].match( re ) ) break block_search;
-
- ret.push ( block.trailing.replace(/[^\n]/g, '').substring(2) );
- block = next.shift();
- }
- else {
- break block_search;
- }
- } while (true);
- return [ [ "code_block", ret.join("\n") ] ];
- },
- horizRule: function horizRule( block, next ) {
-
- var m = block.match( /^(?:([\s\S]*?)\n)?[ \t]*([-_*])(?:[ \t]*\2){2,}[ \t]*(?:\n([\s\S]*))?$/ );
- if ( !m ) {
- return undefined;
- }
- var jsonml = [ [ "hr" ] ];
-
- if ( m[ 1 ] ) {
- jsonml.unshift.apply( jsonml, this.processBlock( m[ 1 ], [] ) );
- }
-
- if ( m[ 3 ] ) {
- next.unshift( mk_block( m[ 3 ] ) );
- }
- return jsonml;
- },
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- lists: (function( ) {
-
- var any_list = "[*+-]|\\d+\\.",
- bullet_list = /[*+-]/,
- number_list = /\d+\./,
-
- is_list_re = new RegExp( "^( {0,3})(" + any_list + ")[ \t]+" ),
- indent_re = "(?: {0,3}\\t| {4})";
-
-
- function regex_for_depth( depth ) {
- return new RegExp(
-
- "(?:^(" + indent_re + "{0," + depth + "} {0,3})(" + any_list + ")\\s+)|" +
-
- "(^" + indent_re + "{0," + (depth-1) + "}[ ]{0,4})"
- );
- }
- function expand_tab( input ) {
- return input.replace( / {0,3}\t/g, " " );
- }
-
-
- function add(li, loose, inline, nl) {
- if (loose) {
- li.push( [ "para" ].concat(inline) );
- return;
- }
-
- var add_to = li[li.length -1] instanceof Array && li[li.length - 1][0] == "para"
- ? li[li.length -1]
- : li;
-
- if (nl && li.length > 1) inline.unshift(nl);
- for (var i=0; i < inline.length; i++) {
- var what = inline[i],
- is_str = typeof what == "string";
- if (is_str && add_to.length > 1 && typeof add_to[add_to.length-1] == "string" ) {
- add_to[ add_to.length-1 ] += what;
- }
- else {
- add_to.push( what );
- }
- }
- }
-
-
- function get_contained_blocks( depth, blocks ) {
- var re = new RegExp( "^(" + indent_re + "{" + depth + "}.*?\\n?)*$" ),
- replace = new RegExp("^" + indent_re + "{" + depth + "}", "gm"),
- ret = [];
- while ( blocks.length > 0 ) {
- if ( re.exec( blocks[0] ) ) {
- var b = blocks.shift(),
-
- x = b.replace( replace, "");
- ret.push( mk_block( x, b.trailing, b.lineNumber ) );
- }
- break;
- }
- return ret;
- }
-
- function paragraphify(s, i, stack) {
- var list = s.list;
- var last_li = list[list.length-1];
- if (last_li[1] instanceof Array && last_li[1][0] == "para") {
- return;
- }
- if (i+1 == stack.length) {
-
-
- last_li.push( ["para"].concat( last_li.splice(1) ) );
- }
- else {
- var sublist = last_li.pop();
- last_li.push( ["para"].concat( last_li.splice(1) ), sublist );
- }
- }
-
- return function( block, next ) {
- var m = block.match( is_list_re );
- if ( !m ) return undefined;
- function make_list( m ) {
- var list = bullet_list.exec( m[2] )
- ? ["bulletlist"]
- : ["numberlist"];
- stack.push( { list: list, indent: m[1] } );
- return list;
- }
- var stack = [],
- list = make_list( m ),
- last_li,
- loose = false,
- ret = [ stack[0].list ],
- i;
-
- loose_search:
- while( true ) {
-
- var lines = block.split( /(?=\n)/ );
-
-
- var li_accumulate = "";
-
- tight_search:
- for (var line_no=0; line_no < lines.length; line_no++) {
- var nl = "",
- l = lines[line_no].replace(/^\n/, function(n) { nl = n; return ""; });
-
- var line_re = regex_for_depth( stack.length );
- m = l.match( line_re );
-
-
- if ( m[1] !== undefined ) {
-
- if ( li_accumulate.length ) {
- add( last_li, loose, this.processInline( li_accumulate ), nl );
-
- loose = false;
- li_accumulate = "";
- }
- m[1] = expand_tab( m[1] );
- var wanted_depth = Math.floor(m[1].length/4)+1;
-
- if ( wanted_depth > stack.length ) {
-
-
- list = make_list( m );
- last_li.push( list );
- last_li = list[1] = [ "listitem" ];
- }
- else {
-
-
-
-
- var found = false;
- for (i = 0; i < stack.length; i++) {
- if ( stack[ i ].indent != m[1] ) continue;
- list = stack[ i ].list;
- stack.splice( i+1 );
- found = true;
- break;
- }
- if (!found) {
-
- wanted_depth++;
- if (wanted_depth <= stack.length) {
- stack.splice(wanted_depth);
-
- list = stack[wanted_depth-1].list;
-
- }
- else {
-
- list = make_list(m);
- last_li.push(list);
- }
- }
-
- last_li = [ "listitem" ];
- list.push(last_li);
- }
- nl = "";
- }
-
- if (l.length > m[0].length) {
- li_accumulate += nl + l.substr( m[0].length );
- }
- }
- if ( li_accumulate.length ) {
- add( last_li, loose, this.processInline( li_accumulate ), nl );
-
- loose = false;
- li_accumulate = "";
- }
-
-
- var contained = get_contained_blocks( stack.length, next );
-
- if (contained.length > 0) {
-
- forEach( stack, paragraphify, this);
- last_li.push.apply( last_li, this.toTree( contained, [] ) );
- }
- var next_block = next[0] && next[0].valueOf() || "";
- if ( next_block.match(is_list_re) || next_block.match( /^ / ) ) {
- block = next.shift();
-
- var hr = this.dialect.block.horizRule( block, next );
- if (hr) {
- ret.push.apply(ret, hr);
- break;
- }
-
- forEach( stack, paragraphify, this);
- loose = true;
- continue loose_search;
- }
- break;
- }
- return ret;
- };
- })(),
- blockquote: function blockquote( block, next ) {
- if ( !block.match( /^>/m ) )
- return undefined;
- var jsonml = [];
-
- if ( block[ 0 ] != ">" ) {
- var lines = block.split( /\n/ ),
- prev = [];
-
- while ( lines.length && lines[ 0 ][ 0 ] != ">" ) {
- prev.push( lines.shift() );
- }
-
- block = lines.join( "\n" );
- jsonml.push.apply( jsonml, this.processBlock( prev.join( "\n" ), [] ) );
- }
-
- while ( next.length && next[ 0 ][ 0 ] == ">" ) {
- var b = next.shift();
- block = new String(block + block.trailing + b);
- block.trailing = b.trailing;
- }
-
- var input = block.replace( /^> ?/gm, '' ),
- old_tree = this.tree;
- jsonml.push( this.toTree( input, [ "blockquote" ] ) );
- return jsonml;
- },
- referenceDefn: function referenceDefn( block, next) {
- var re = /^\s*\[(.*?)\]:\s*(\S+)(?:\s+(?:(['"])(.*?)\3|\((.*?)\)))?\n?/;
-
- if ( !block.match(re) )
- return undefined;
-
- if ( !extract_attr( this.tree ) ) {
- this.tree.splice( 1, 0, {} );
- }
- var attrs = extract_attr( this.tree );
-
- if ( attrs.references === undefined ) {
- attrs.references = {};
- }
- var b = this.loop_re_over_block(re, block, function( m ) {
- if ( m[2] && m[2][0] == '<' && m[2][m[2].length-1] == '>' )
- m[2] = m[2].substring( 1, m[2].length - 1 );
- var ref = attrs.references[ m[1].toLowerCase() ] = {
- href: m[2]
- };
- if (m[4] !== undefined)
- ref.title = m[4];
- else if (m[5] !== undefined)
- ref.title = m[5];
- } );
- if (b.length)
- next.unshift( mk_block( b, block.trailing ) );
- return [];
- },
- para: function para( block, next ) {
-
- return [ ["para"].concat( this.processInline( block ) ) ];
- }
- }
- };
- Markdown.dialects.Gruber.inline = {
- __oneElement__: function oneElement( text, patterns_or_re, previous_nodes ) {
- var m,
- res,
- lastIndex = 0;
- patterns_or_re = patterns_or_re || this.dialect.inline.__patterns__;
- var re = new RegExp( "([\\s\\S]*?)(" + (patterns_or_re.source || patterns_or_re) + ")" );
- m = re.exec( text );
- if (!m) {
-
- return [ text.length, text ];
- }
- else if ( m[1] ) {
-
- return [ m[1].length, m[1] ];
- }
- var res;
- if ( m[2] in this.dialect.inline ) {
- res = this.dialect.inline[ m[2] ].call(
- this,
- text.substr( m.index ), m, previous_nodes || [] );
- }
-
- res = res || [ m[2].length, m[2] ];
- return res;
- },
- __call__: function inline( text, patterns ) {
- var out = [],
- res;
- function add(x) {
-
- if (typeof x == "string" && typeof out[out.length-1] == "string")
- out[ out.length-1 ] += x;
- else
- out.push(x);
- }
- while ( text.length > 0 ) {
- res = this.dialect.inline.__oneElement__.call(this, text, patterns, out );
- text = text.substr( res.shift() );
- forEach(res, add )
- }
- return out;
- },
-
-
- "]": function () {},
- "}": function () {},
- "\\": function escaped( text ) {
-
-
- if ( text.match( /^\\[\\`\*_{}\[\]()#\+.!\-]/ ) )
- return [ 2, text[1] ];
- else
-
- return [ 1, "\\" ];
- },
- "![": function image( text ) {
-
-
-
-
- var m = text.match( /^!\[(.*?)\][ \t]*\([ \t]*(\S*)(?:[ \t]+(["'])(.*?)\3)?[ \t]*\)/ );
- if ( m ) {
- if ( m[2] && m[2][0] == '<' && m[2][m[2].length-1] == '>' )
- m[2] = m[2].substring( 1, m[2].length - 1 );
- m[2] = this.dialect.inline.__call__.call( this, m[2], /\\/ )[0];
- var attrs = { alt: m[1], href: m[2] || "" };
- if ( m[4] !== undefined)
- attrs.title = m[4];
- return [ m[0].length, [ "img", attrs ] ];
- }
-
- m = text.match( /^!\[(.*?)\][ \t]*\[(.*?)\]/ );
- if ( m ) {
-
-
- return [ m[0].length, [ "img_ref", { alt: m[1], ref: m[2].toLowerCase(), original: m[0] } ] ];
- }
-
- return [ 2, "![" ];
- },
- "[": function link( text ) {
- var orig = String(text);
-
- var res = Markdown.DialectHelpers.inline_until_char.call( this, text.substr(1), ']' );
-
- if ( !res ) return [ 1, '[' ];
- var consumed = 1 + res[ 0 ],
- children = res[ 1 ],
- link,
- attrs;
-
-
- text = text.substr( consumed );
-
-
-
-
-
-
- var m = text.match( /^\s*\([ \t]*(\S+)(?:[ \t]+(["'])(.*?)\2)?[ \t]*\)/ );
- if ( m ) {
- var url = m[1];
- consumed += m[0].length;
- if ( url && url[0] == '<' && url[url.length-1] == '>' )
- url = url.substring( 1, url.length - 1 );
-
- if ( !m[3] ) {
- var open_parens = 1;
- for (var len = 0; len < url.length; len++) {
- switch ( url[len] ) {
- case '(':
- open_parens++;
- break;
- case ')':
- if ( --open_parens == 0) {
- consumed -= url.length - len;
- url = url.substring(0, len);
- }
- break;
- }
- }
- }
-
- url = this.dialect.inline.__call__.call( this, url, /\\/ )[0];
- attrs = { href: url || "" };
- if ( m[3] !== undefined)
- attrs.title = m[3];
- link = [ "link", attrs ].concat( children );
- return [ consumed, link ];
- }
-
-
- m = text.match( /^\s*\[(.*?)\]/ );
- if ( m ) {
- consumed += m[ 0 ].length;
-
- attrs = { ref: ( m[ 1 ] || String(children) ).toLowerCase(), original: orig.substr( 0, consumed ) };
- link = [ "link_ref", attrs ].concat( children );
-
-
-
- return [ consumed, link ];
- }
-
-
- if ( children.length == 1 && typeof children[0] == "string" ) {
- attrs = { ref: children[0].toLowerCase(), original: orig.substr( 0, consumed ) };
- link = [ "link_ref", attrs, children[0] ];
- return [ consumed, link ];
- }
-
- return [ 1, "[" ];
- },
- "<": function autoLink( text ) {
- var m;
- if ( ( m = text.match( /^<(?:((https?|ftp|mailto):[^>]+)|(.*?@.*?\.[a-zA-Z]+))>/ ) ) != null ) {
- if ( m[3] ) {
- return [ m[0].length, [ "link", { href: "mailto:" + m[3] }, m[3] ] ];
- }
- else if ( m[2] == "mailto" ) {
- return [ m[0].length, [ "link", { href: m[1] }, m[1].substr("mailto:".length ) ] ];
- }
- else
- return [ m[0].length, [ "link", { href: m[1] }, m[1] ] ];
- }
- return [ 1, "<" ];
- },
- "`": function inlineCode( text ) {
-
-
- var m = text.match( /(`+)(([\s\S]*?)\1)/ );
- if ( m && m[2] )
- return [ m[1].length + m[2].length, [ "inlinecode", m[3] ] ];
- else {
-
- return [ 1, "`" ];
- }
- },
- " \n": function lineBreak( text ) {
- return [ 3, [ "linebreak" ] ];
- }
- };
- function strong_em( tag, md ) {
- var state_slot = tag + "_state",
- other_slot = tag == "strong" ? "em_state" : "strong_state";
- function CloseTag(len) {
- this.len_after = len;
- this.name = "close_" + md;
- }
- return function ( text, orig_match ) {
- if (this[state_slot][0] == md) {
-
-
- this[state_slot].shift();
-
- return[ text.length, new CloseTag(text.length-md.length) ];
- }
- else {
-
- var other = this[other_slot].slice(),
- state = this[state_slot].slice();
- this[state_slot].unshift(md);
-
-
- var res = this.processInline( text.substr( md.length ) );
-
- var last = res[res.length - 1];
-
- var check = this[state_slot].shift();
- if (last instanceof CloseTag) {
- res.pop();
-
- var consumed = text.length - last.len_after;
- return [ consumed, [ tag ].concat(res) ];
- }
- else {
-
- this[other_slot] = other;
- this[state_slot] = state;
-
- return [ md.length, md ];
- }
- }
- };
- }
- Markdown.dialects.Gruber.inline["**"] = strong_em("strong", "**");
- Markdown.dialects.Gruber.inline["__"] = strong_em("strong", "__");
- Markdown.dialects.Gruber.inline["*"] = strong_em("em", "*");
- Markdown.dialects.Gruber.inline["_"] = strong_em("em", "_");
- Markdown.buildBlockOrder = function(d) {
- var ord = [];
- for ( var i in d ) {
- if ( i == "__order__" || i == "__call__" ) continue;
- ord.push( i );
- }
- d.__order__ = ord;
- };
- Markdown.buildInlinePatterns = function(d) {
- var patterns = [];
- for ( var i in d ) {
-
- if ( i.match( /^__.*__$/) ) continue;
- var l = i.replace( /([\\.*+?|()\[\]{}])/g, "\\$1" )
- .replace( /\n/, "\\n" );
- patterns.push( i.length == 1 ? l : "(?:" + l + ")" );
- }
- patterns = patterns.join("|");
- d.__patterns__ = patterns;
-
- var fn = d.__call__;
- d.__call__ = function(text, pattern) {
- if (pattern != undefined) {
- return fn.call(this, text, pattern);
- }
- else
- {
- return fn.call(this, text, patterns);
- }
- };
- };
- Markdown.DialectHelpers = {};
- Markdown.DialectHelpers.inline_until_char = function( text, want ) {
- var consumed = 0,
- nodes = [];
- while ( true ) {
- if ( text[ consumed ] == want ) {
-
- consumed++;
- return [ consumed, nodes ];
- }
- if ( consumed >= text.length ) {
-
- return null;
- }
- var res = this.dialect.inline.__oneElement__.call(this, text.substr( consumed ) );
- consumed += res[ 0 ];
-
- nodes.push.apply( nodes, res.slice( 1 ) );
- }
- }
- Markdown.subclassDialect = function( d ) {
- function Block() {}
- Block.prototype = d.block;
- function Inline() {}
- Inline.prototype = d.inline;
- return { block: new Block(), inline: new Inline() };
- };
- Markdown.buildBlockOrder ( Markdown.dialects.Gruber.block );
- Markdown.buildInlinePatterns( Markdown.dialects.Gruber.inline );
- Markdown.dialects.Maruku = Markdown.subclassDialect( Markdown.dialects.Gruber );
- Markdown.dialects.Maruku.processMetaHash = function processMetaHash( meta_string ) {
- var meta = split_meta_hash( meta_string ),
- attr = {};
- for ( var i = 0; i < meta.length; ++i ) {
-
- if ( /^#/.test( meta[ i ] ) ) {
- attr.id = meta[ i ].substring( 1 );
- }
-
- else if ( /^\./.test( meta[ i ] ) ) {
-
- if ( attr['class'] ) {
- attr['class'] = attr['class'] + meta[ i ].replace( /./, " " );
- }
- else {
- attr['class'] = meta[ i ].substring( 1 );
- }
- }
-
- else if ( /\=/.test( meta[ i ] ) ) {
- var s = meta[ i ].split( /\=/ );
- attr[ s[ 0 ] ] = s[ 1 ];
- }
- }
- return attr;
- }
- function split_meta_hash( meta_string ) {
- var meta = meta_string.split( "" ),
- parts = [ "" ],
- in_quotes = false;
- while ( meta.length ) {
- var letter = meta.shift();
- switch ( letter ) {
- case " " :
-
- if ( in_quotes ) {
- parts[ parts.length - 1 ] += letter;
- }
-
- else {
- parts.push( "" );
- }
- break;
- case "'" :
- case '"' :
-
- in_quotes = !in_quotes;
- break;
- case "\\" :
-
-
- letter = meta.shift();
- default :
- parts[ parts.length - 1 ] += letter;
- break;
- }
- }
- return parts;
- }
- Markdown.dialects.Maruku.block.document_meta = function document_meta( block, next ) {
-
- if ( block.lineNumber > 1 ) return undefined;
-
- if ( ! block.match( /^(?:\w+:.*\n)*\w+:.*$/ ) ) return undefined;
-
- if ( !extract_attr( this.tree ) ) {
- this.tree.splice( 1, 0, {} );
- }
- var pairs = block.split( /\n/ );
- for ( p in pairs ) {
- var m = pairs[ p ].match( /(\w+):\s*(.*)$/ ),
- key = m[ 1 ].toLowerCase(),
- value = m[ 2 ];
- this.tree[ 1 ][ key ] = value;
- }
-
- return [];
- };
- Markdown.dialects.Maruku.block.block_meta = function block_meta( block, next ) {
-
- var m = block.match( /(^|\n) {0,3}\{:\s*((?:\\\}|[^\}])*)\s*\}$/ );
- if ( !m ) return undefined;
-
- var attr = this.dialect.processMetaHash( m[ 2 ] );
- var hash;
-
- if ( m[ 1 ] === "" ) {
- var node = this.tree[ this.tree.length - 1 ];
- hash = extract_attr( node );
-
- if ( typeof node === "string" ) return undefined;
-
- if ( !hash ) {
- hash = {};
- node.splice( 1, 0, hash );
- }
-
- for ( a in attr ) {
- hash[ a ] = attr[ a ];
- }
-
- return [];
- }
-
- var b = block.replace( /\n.*$/, "" ),
- result = this.processBlock( b, [] );
-
- hash = extract_attr( result[ 0 ] );
- if ( !hash ) {
- hash = {};
- result[ 0 ].splice( 1, 0, hash );
- }
-
- for ( a in attr ) {
- hash[ a ] = attr[ a ];
- }
- return result;
- };
- Markdown.dialects.Maruku.block.definition_list = function definition_list( block, next ) {
-
- var tight = /^((?:[^\s:].*\n)+):\s+([\s\S]+)$/,
- list = [ "dl" ],
- i;
-
- if ( ( m = block.match( tight ) ) ) {
-
- var blocks = [ block ];
- while ( next.length && tight.exec( next[ 0 ] ) ) {
- blocks.push( next.shift() );
- }
- for ( var b = 0; b < blocks.length; ++b ) {
- var m = blocks[ b ].match( tight ),
- terms = m[ 1 ].replace( /\n$/, "" ).split( /\n/ ),
- defns = m[ 2 ].split( /\n:\s+/ );
-
- for ( i = 0; i < terms.length; ++i ) {
- list.push( [ "dt", terms[ i ] ] );
- }
- for ( i = 0; i < defns.length; ++i ) {
-
- list.push( [ "dd" ].concat( this.processInline( defns[ i ].replace( /(\n)\s+/, "$1" ) ) ) );
- }
- }
- }
- else {
- return undefined;
- }
- return [ list ];
- };
- Markdown.dialects.Maruku.inline[ "{:" ] = function inline_meta( text, matches, out ) {
- if ( !out.length ) {
- return [ 2, "{:" ];
- }
-
- var before = out[ out.length - 1 ];
- if ( typeof before === "string" ) {
- return [ 2, "{:" ];
- }
-
- var m = text.match( /^\{:\s*((?:\\\}|[^\}])*)\s*\}/ );
-
- if ( !m ) {
- return [ 2, "{:" ];
- }
-
- var meta = this.dialect.processMetaHash( m[ 1 ] ),
- attr = extract_attr( before );
- if ( !attr ) {
- attr = {};
- before.splice( 1, 0, attr );
- }
- for ( var k in meta ) {
- attr[ k ] = meta[ k ];
- }
-
- return [ m[ 0 ].length, "" ];
- };
- Markdown.buildBlockOrder ( Markdown.dialects.Maruku.block );
- Markdown.buildInlinePatterns( Markdown.dialects.Maruku.inline );
- var isArray = Array.isArray || function(obj) {
- return Object.prototype.toString.call(obj) == '[object Array]';
- };
- var forEach;
- if ( Array.prototype.forEach ) {
- forEach = function( arr, cb, thisp ) {
- return arr.forEach( cb, thisp );
- };
- }
- else {
- forEach = function(arr, cb, thisp) {
- for (var i = 0; i < arr.length; i++) {
- cb.call(thisp || arr, arr[i], i, arr);
- }
- }
- }
- function extract_attr( jsonml ) {
- return isArray(jsonml)
- && jsonml.length > 1
- && typeof jsonml[ 1 ] === "object"
- && !( isArray(jsonml[ 1 ]) )
- ? jsonml[ 1 ]
- : undefined;
- }
- expose.renderJsonML = function( jsonml, options ) {
- options = options || {};
-
- options.root = options.root || false;
- var content = [];
- if ( options.root ) {
- content.push( render_tree( jsonml ) );
- }
- else {
- jsonml.shift();
- if ( jsonml.length && typeof jsonml[ 0 ] === "object" && !( jsonml[ 0 ] instanceof Array ) ) {
- jsonml.shift();
- }
- while ( jsonml.length ) {
- content.push( render_tree( jsonml.shift() ) );
- }
- }
- return content.join( "\n\n" );
- };
- function escapeHTML( text ) {
- return text.replace( /&/g, "&" )
- .replace( /</g, "<" )
- .replace( />/g, ">" )
- .replace( /"/g, """ )
- .replace( /'/g, "'" );
- }
- function render_tree( jsonml ) {
-
- if ( typeof jsonml === "string" ) {
- return escapeHTML( jsonml );
- }
- var tag = jsonml.shift(),
- attributes = {},
- content = [];
- if ( jsonml.length && typeof jsonml[ 0 ] === "object" && !( jsonml[ 0 ] instanceof Array ) ) {
- attributes = jsonml.shift();
- }
- while ( jsonml.length ) {
- content.push( arguments.callee( jsonml.shift() ) );
- }
- var tag_attrs = "";
- for ( var a in attributes ) {
- tag_attrs += " " + a + '="' + escapeHTML( attributes[ a ] ) + '"';
- }
-
- if ( tag == "img" || tag == "br" || tag == "hr" ) {
- return "<"+ tag + tag_attrs + "/>";
- }
- else {
- return "<"+ tag + tag_attrs + ">" + content.join( "" ) + "</" + tag + ">";
- }
- }
- function convert_tree_to_html( tree, references, options ) {
- var i;
- options = options || {};
-
- var jsonml = tree.slice( 0 );
- if (typeof options.preprocessTreeNode === "function") {
- jsonml = options.preprocessTreeNode(jsonml, references);
- }
-
- var attrs = extract_attr( jsonml );
- if ( attrs ) {
- jsonml[ 1 ] = {};
- for ( i in attrs ) {
- jsonml[ 1 ][ i ] = attrs[ i ];
- }
- attrs = jsonml[ 1 ];
- }
-
- if ( typeof jsonml === "string" ) {
- return jsonml;
- }
-
- switch ( jsonml[ 0 ] ) {
- case "header":
- jsonml[ 0 ] = "h" + jsonml[ 1 ].level;
- delete jsonml[ 1 ].level;
- break;
- case "bulletlist":
- jsonml[ 0 ] = "ul";
- break;
- case "numberlist":
- jsonml[ 0 ] = "ol";
- break;
- case "listitem":
- jsonml[ 0 ] = "li";
- break;
- case "para":
- jsonml[ 0 ] = "p";
- break;
- case "markdown":
- jsonml[ 0 ] = "html";
- if ( attrs ) delete attrs.references;
- break;
- case "code_block":
- jsonml[ 0 ] = "pre";
- i = attrs ? 2 : 1;
- var code = [ "code" ];
- code.push.apply( code, jsonml.splice( i ) );
- jsonml[ i ] = code;
- break;
- case "inlinecode":
- jsonml[ 0 ] = "code";
- break;
- case "img":
- jsonml[ 1 ].src = jsonml[ 1 ].href;
- delete jsonml[ 1 ].href;
- break;
- case "linebreak":
- jsonml[ 0 ] = "br";
- break;
- case "link":
- jsonml[ 0 ] = "a";
- break;
- case "link_ref":
- jsonml[ 0 ] = "a";
-
- var ref = references[ attrs.ref ];
-
- if ( ref ) {
- delete attrs.ref;
-
- attrs.href = ref.href;
- if ( ref.title ) {
- attrs.title = ref.title;
- }
-
- delete attrs.original;
- }
-
- else {
- return attrs.original;
- }
- break;
- case "img_ref":
- jsonml[ 0 ] = "img";
-
- var ref = references[ attrs.ref ];
-
- if ( ref ) {
- delete attrs.ref;
-
- attrs.src = ref.href;
- if ( ref.title ) {
- attrs.title = ref.title;
- }
-
- delete attrs.original;
- }
-
- else {
- return attrs.original;
- }
- break;
- }
-
- i = 1;
-
- if ( attrs ) {
-
- for ( var key in jsonml[ 1 ] ) {
- i = 2;
- }
-
- if ( i === 1 ) {
- jsonml.splice( i, 1 );
- }
- }
- for ( ; i < jsonml.length; ++i ) {
- jsonml[ i ] = arguments.callee( jsonml[ i ], references, options );
- }
- return jsonml;
- }
- function merge_text_nodes( jsonml ) {
-
- var i = extract_attr( jsonml ) ? 2 : 1;
- while ( i < jsonml.length ) {
-
- if ( typeof jsonml[ i ] === "string" ) {
- if ( i + 1 < jsonml.length && typeof jsonml[ i + 1 ] === "string" ) {
-
- jsonml[ i ] += jsonml.splice( i + 1, 1 )[ 0 ];
- }
- else {
- ++i;
- }
- }
-
- else {
- arguments.callee( jsonml[ i ] );
- ++i;
- }
- }
- }
- } )( (function() {
- if ( typeof exports === "undefined" ) {
- window.markdown = {};
- return window.markdown;
- }
- else {
- return exports;
- }
- } )() );
|