Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#588 Add option to allow json output instead of a string #589

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 107 additions & 37 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ var inline = {
autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
url: noop,
tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
link: /^!?\[(inside)\]\(href\)/,
link: /^!?\[(inside)\]\(href\)(:.+)/,
reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
Expand Down Expand Up @@ -515,12 +515,13 @@ inline.breaks = merge({}, inline.gfm, {
* Inline Lexer & Compiler
*/

function InlineLexer(links, options) {
function InlineLexer(links, options, renderer, lib) {
this.options = options || marked.defaults;
this.links = links;
this.rules = inline.normal;
this.renderer = this.options.renderer || new Renderer;
this.renderer.options = this.options;
this.lib = lib;

if (!this.links) {
throw new
Expand Down Expand Up @@ -558,17 +559,18 @@ InlineLexer.output = function(src, links, options) {
*/

InlineLexer.prototype.output = function(src) {
var out = ''
var out = this.lib.getEmptyResult()
, link
, text
, href
, cap;
, cap
, res;

while (src) {
// escape
if (cap = this.rules.escape.exec(src)) {
src = src.substring(cap[0].length);
out += cap[1];
out = this.lib.addToResult(out, cap[1]);
continue;
}

Expand All @@ -584,7 +586,8 @@ InlineLexer.prototype.output = function(src) {
text = escape(cap[1]);
href = text;
}
out += this.renderer.link(href, null, text);
res = this.renderer.link(href, null, text);
out = this.lib.addToResult(out, res);
continue;
}

Expand All @@ -593,7 +596,8 @@ InlineLexer.prototype.output = function(src) {
src = src.substring(cap[0].length);
text = escape(cap[1]);
href = text;
out += this.renderer.link(href, null, text);
res = this.renderer.link(href, null, text);
out = this.lib.addToResult(out, res);
continue;
}

Expand All @@ -605,20 +609,22 @@ InlineLexer.prototype.output = function(src) {
this.inLink = false;
}
src = src.substring(cap[0].length);
out += this.options.sanitize
res = this.options.sanitize
? escape(cap[0])
: cap[0];
out = this.lib.addToResult(out, res);
continue;
}

// link
if (cap = this.rules.link.exec(src)) {
src = src.substring(cap[0].length);
this.inLink = true;
out += this.outputLink(cap, {
res = this.outputLink(cap, {
href: cap[2],
title: cap[3]
});
out = this.lib.addToResult(out, res);
this.inLink = false;
continue;
}
Expand All @@ -630,55 +636,63 @@ InlineLexer.prototype.output = function(src) {
link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
link = this.links[link.toLowerCase()];
if (!link || !link.href) {
out += cap[0].charAt(0);
res = cap[0].charAt(0);
out = this.lib.addToResult(out, res);
src = cap[0].substring(1) + src;
continue;
}
this.inLink = true;
out += this.outputLink(cap, link);
res = this.outputLink(cap, link);
out = this.lib.addToResult(out, res);
this.inLink = false;
continue;
}

// strong
if (cap = this.rules.strong.exec(src)) {
src = src.substring(cap[0].length);
out += this.renderer.strong(this.output(cap[2] || cap[1]));
res = this.renderer.strong(this.output(cap[2] || cap[1]));
out = this.lib.addToResult(out, res);
continue;
}

// em
if (cap = this.rules.em.exec(src)) {
src = src.substring(cap[0].length);
out += this.renderer.em(this.output(cap[2] || cap[1]));
res = this.renderer.em(this.output(cap[2] || cap[1]));
out = this.lib.addToResult(out, res);
continue;
}

// code
if (cap = this.rules.code.exec(src)) {
src = src.substring(cap[0].length);
out += this.renderer.codespan(escape(cap[2], true));
res = this.renderer.codespan(escape(cap[2], true));
out = this.lib.addToResult(out, res);
continue;
}

// br
if (cap = this.rules.br.exec(src)) {
src = src.substring(cap[0].length);
out += this.renderer.br();
res = this.renderer.br();
out = this.lib.addToResult(out, res);
continue;
}

// del (gfm)
if (cap = this.rules.del.exec(src)) {
src = src.substring(cap[0].length);
out += this.renderer.del(this.output(cap[1]));
res = this.renderer.del(this.output(cap[1]));
out = this.lib.addToResult(out, res);
continue;
}

// text
if (cap = this.rules.text.exec(src)) {
src = src.substring(cap[0].length);
out += escape(this.smartypants(cap[0]));
res = escape(this.smartypants(cap[0]));
out = this.lib.addToResult(out, res);
continue;
}

Expand Down Expand Up @@ -889,6 +903,51 @@ Renderer.prototype.image = function(href, title, text) {
return out;
};

/**
* Common functions
*/
var Lib = function(options) {
this.options = options || {};
};

/**
* Return empty result out
*/

Lib.prototype.getEmptyResult = function() {
var result;

switch (this.options.output) {
case 'json': {
result = [];
break;
}
default: {
result = '';
}
}

return result;
};

/**
* Add to result out
*/

Lib.prototype.addToResult = function(result, data) {
switch (this.options.output) {
case 'json': {
result.push(data);
break;
}
default: {
result += data;
}
}

return result;
};

/**
* Parsing & Compiling
*/
Expand All @@ -900,6 +959,7 @@ function Parser(options) {
this.options.renderer = this.options.renderer || new Renderer;
this.renderer = this.options.renderer;
this.renderer.options = this.options;
this.lib = new Lib(this.options);
}

/**
Expand All @@ -916,12 +976,13 @@ Parser.parse = function(src, options, renderer) {
*/

Parser.prototype.parse = function(src) {
this.inline = new InlineLexer(src.links, this.options, this.renderer);
this.inline = new InlineLexer(src.links, this.options, this.renderer, this.lib);
this.tokens = src.reverse();

var out = '';
var out = this.lib.getEmptyResult();

while (this.next()) {
out += this.tok();
out = this.lib.addToResult(out, this.tok());
}

return out;
Expand Down Expand Up @@ -981,75 +1042,83 @@ Parser.prototype.tok = function() {
this.token.escaped);
}
case 'table': {
var header = ''
, body = ''
var header = this.lib.getEmptyResult()
, body = this.lib.getEmptyResult()
, i
, row
, tablerow
, tablecell
, cell
, flags
, j;

// header
cell = '';
cell = this.lib.getEmptyResult();
for (i = 0; i < this.token.header.length; i++) {
flags = { header: true, align: this.token.align[i] };
cell += this.renderer.tablecell(
tablecell = this.renderer.tablecell(
this.inline.output(this.token.header[i]),
{ header: true, align: this.token.align[i] }
);
cell = this.lib.addToResult(cell, tablecell);
}
header += this.renderer.tablerow(cell);
tablerow = this.renderer.tablerow(cell);
header = this.lib.addToResult(header, tablerow);

for (i = 0; i < this.token.cells.length; i++) {
row = this.token.cells[i];

cell = '';
cell = this.lib.getEmptyResult();
for (j = 0; j < row.length; j++) {
cell += this.renderer.tablecell(
tablecell = this.renderer.tablecell(
this.inline.output(row[j]),
{ header: false, align: this.token.align[j] }
);
cell = this.lib.addToResult(cell, tablecell);
}

body += this.renderer.tablerow(cell);
tablerow = this.renderer.tablerow(cell);
body = this.lib.addToResult(body, tablerow);
}
return this.renderer.table(header, body);
}
case 'blockquote_start': {
var body = '';
body = this.lib.getEmptyResult();

while (this.next().type !== 'blockquote_end') {
body += this.tok();
body = this.lib.addToResult(body, this.tok());
}

return this.renderer.blockquote(body);
}
case 'list_start': {
var body = ''
body = this.lib.getEmptyResult()
, ordered = this.token.ordered;

while (this.next().type !== 'list_end') {
body += this.tok();
body = this.lib.addToResult(body, this.tok());
}

return this.renderer.list(body, ordered);
}
case 'list_item_start': {
var body = '';
body = this.lib.getEmptyResult();

while (this.next().type !== 'list_item_end') {
body += this.token.type === 'text'
var res = this.token.type === 'text'
? this.parseText()
: this.tok();

body = this.lib.addToResult(body, res);
}

return this.renderer.listitem(body);
}
case 'loose_item_start': {
var body = '';
body = this.lib.getEmptyResult();

while (this.next().type !== 'list_item_end') {
body += this.tok();
body = this.lib.addToResult(body, this.tok());
}

return this.renderer.listitem(body);
Expand Down Expand Up @@ -1239,7 +1308,8 @@ marked.defaults = {
smartypants: false,
headerPrefix: '',
renderer: new Renderer,
xhtml: false
xhtml: false,
output: 'string'
};

/**
Expand Down