191 lines
4.0 KiB
JavaScript
191 lines
4.0 KiB
JavaScript
var Renderer = require('./renderer');
|
|
var InlineLexer = require('./lex/inline');
|
|
|
|
/**
|
|
* Parsing & Compiling
|
|
*/
|
|
|
|
function Parser(options, renderer) {
|
|
this.tokens = [];
|
|
this.token = null;
|
|
this.options = options ? options : null;
|
|
this.renderer = renderer || (this.options && this.options.renderer) || new Renderer(this.options);
|
|
}
|
|
|
|
/**
|
|
* Static Parse Method
|
|
*/
|
|
|
|
Parser.parse = function(src, options, renderer) {
|
|
var parser = new Parser(options, renderer);
|
|
return parser.parse(src);
|
|
};
|
|
|
|
/**
|
|
* Parse Loop
|
|
*/
|
|
|
|
Parser.prototype.parse = function(src) {
|
|
this.inline = new InlineLexer(src.links, this.options, this.renderer);
|
|
this.tokens = src.reverse();
|
|
|
|
var out = '';
|
|
while (this.next()) {
|
|
out += this.tok();
|
|
}
|
|
|
|
return out;
|
|
};
|
|
|
|
/**
|
|
* Next Token
|
|
*/
|
|
|
|
Parser.prototype.next = function() {
|
|
return this.token = this.tokens.pop();
|
|
};
|
|
|
|
/**
|
|
* Preview Next Token
|
|
*/
|
|
|
|
Parser.prototype.peek = function() {
|
|
return this.tokens[this.tokens.length - 1] || 0;
|
|
};
|
|
|
|
/**
|
|
* Parse Text Tokens
|
|
*/
|
|
|
|
Parser.prototype.parseText = function() {
|
|
var body = this.token.text;
|
|
|
|
while (this.peek().type === 'text') {
|
|
body += '\n' + this.next().text;
|
|
}
|
|
|
|
return this.inline.output(body);
|
|
};
|
|
|
|
/**
|
|
* Parse Current Token
|
|
*/
|
|
|
|
Parser.prototype.tok = function() {
|
|
if(typeof this.token === 'undefined' || !this.token.hasOwnProperty('type')) {
|
|
return '';
|
|
}
|
|
switch (this.token.type) {
|
|
case 'space': {
|
|
return '';
|
|
}
|
|
case 'hr': {
|
|
return this.renderer.hr();
|
|
}
|
|
case 'heading': {
|
|
return this.renderer.heading(
|
|
this.inline.output(this.token.text),
|
|
this.token.depth,
|
|
this.token.text);
|
|
}
|
|
case 'footnote': {
|
|
return this.renderer.footnote(
|
|
this.token.refname,
|
|
this.inline.output(this.token.text));
|
|
}
|
|
case 'code': {
|
|
return this.renderer.code(this.token.text,
|
|
this.token.lang,
|
|
this.token.escaped);
|
|
}
|
|
case 'math': {
|
|
return this.renderer.math(this.token.text, 'math/tex', true);
|
|
}
|
|
case 'table': {
|
|
var header = ''
|
|
, body = ''
|
|
, i
|
|
, row
|
|
, cell
|
|
, flags
|
|
, j;
|
|
|
|
// header
|
|
cell = '';
|
|
for (i = 0; i < this.token.header.length; i++) {
|
|
flags = { header: true, align: this.token.align[i] };
|
|
cell += this.renderer.tablecell(
|
|
this.inline.output(this.token.header[i]),
|
|
{ header: true, align: this.token.align[i] }
|
|
);
|
|
}
|
|
header += this.renderer.tablerow(cell);
|
|
|
|
for (i = 0; i < this.token.cells.length; i++) {
|
|
row = this.token.cells[i];
|
|
|
|
cell = '';
|
|
for (j = 0; j < row.length; j++) {
|
|
cell += this.renderer.tablecell(
|
|
this.inline.output(row[j]),
|
|
{ header: false, align: this.token.align[j] }
|
|
);
|
|
}
|
|
|
|
body += this.renderer.tablerow(cell);
|
|
}
|
|
return this.renderer.table(header, body);
|
|
}
|
|
case 'blockquote_start': {
|
|
var body = '';
|
|
|
|
while (this.next().type !== 'blockquote_end') {
|
|
body += this.tok();
|
|
}
|
|
|
|
return this.renderer.blockquote(body);
|
|
}
|
|
case 'list_start': {
|
|
var body = ''
|
|
, ordered = this.token.ordered;
|
|
|
|
while (this.next().type !== 'list_end') {
|
|
body += this.tok();
|
|
}
|
|
|
|
return this.renderer.list(body, ordered);
|
|
}
|
|
case 'list_item_start': {
|
|
var body = '';
|
|
|
|
while (this.next().type !== 'list_item_end') {
|
|
body += this.token.type === 'text'
|
|
? this.parseText()
|
|
: this.tok();
|
|
}
|
|
|
|
return this.renderer.listitem(body);
|
|
}
|
|
case 'loose_item_start': {
|
|
var body = '';
|
|
|
|
while (this.next().type !== 'list_item_end') {
|
|
body += this.tok();
|
|
}
|
|
|
|
return this.renderer.listitem(body);
|
|
}
|
|
case 'html': {
|
|
return this.renderer.html(this.token.text);
|
|
}
|
|
case 'paragraph': {
|
|
return this.renderer.paragraph(this.inline.output(this.token.text));
|
|
}
|
|
case 'text': {
|
|
return this.renderer.paragraph(this.parseText());
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = Parser;
|