Skip to content

Commit

Permalink
fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiang Shang committed Nov 9, 2015
1 parent 2bb46b9 commit 3870345
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 146 deletions.
129 changes: 62 additions & 67 deletions dist/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,25 @@ var Parser = (function () {
}, {
key: 'makeFootnotes',
value: function makeFootnotes(html) {
if (this.footnotes.length > 0) {
html += '<div class="footnotes"><hr><ol>';
var index = 1;
var val = this.footnotes.pop();
while (val) {
if (typeof val === 'string') {
val += ' <a href="#fnref-' + index + '" class="footnote-backref">&#8617;</a>';
} else {
val[val.length - 1] += ' <a href="#fnref-' + index + '" class="footnote-backref">&#8617;</a>';
val = val.length > 1 ? this.parse(val.join("\n")) : this.parseInline(val[0]);
}
var _this2 = this;

html += '<li id="fn-' + index + '">' + val + '</li>';
if (this.footnotes.length > 0) {
(function () {
html += '<div class="footnotes"><hr><ol>';
var index = 1;
_this2.footnotes.forEach(function (val) {
if (typeof val === 'string') {
val += ' <a href="#fnref-' + index + '" class="footnote-backref">&#8617;</a>';
} else {
val[val.length - 1] += ' <a href="#fnref-' + index + '" class="footnote-backref">&#8617;</a>';
val = val.length > 1 ? _this2.parse(val.join("\n")) : _this2.parseInline(val[0]);
}

index++;
val = this.footnotes.pop();
}
html += '</ol></div>';
html += '<li id="fn-' + index + '">' + val + '</li>';
index++;
});
html += '</ol></div>';
})();
}
return html;
}
Expand All @@ -141,7 +142,7 @@ var Parser = (function () {
}, {
key: 'parse',
value: function parse(text) {
var _this2 = this;
var _this3 = this;

var lines = text.split("\n");
var blocks = this.parseBlock(text, lines);
Expand All @@ -158,9 +159,9 @@ var Parser = (function () {
var extract = lines.slice(start, end + 1);
var method = 'parse' + type.slice(0, 1).toUpperCase() + type.slice(1);
var beforeMethod = 'beforeParse' + type.slice(0, 1).toUpperCase() + type.slice(1);
extract = _this2.call(beforeMethod, extract, value);
var result = _this2[method](extract, value);
result = _this2.call('after' + method.slice(0, 1).toUpperCase() + method.slice(1), result, value);
extract = _this3.call(beforeMethod, extract, value);
var result = _this3[method](extract, value);
result = _this3.call('after' + method.slice(0, 1).toUpperCase() + method.slice(1), result, value);

html += result;
});
Expand All @@ -177,23 +178,20 @@ var Parser = (function () {
*/
}, {
key: 'call',
value: function call(type) {
for (var _len = arguments.length, value = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
value[_key - 1] = arguments[_key];
}

value: function call(type, value) {
if (!this.hooks[type]) {
return value[0];
return value;
}

var args = value;
var args = [].slice.call(arguments);
args = args.slice(1);

this.hooks[type].forEach(function (callback) {
value = callback(args);
value = callback.apply(null, args);
args[0] = value;
});

return value[0];
return value;
}

/**
Expand Down Expand Up @@ -231,7 +229,7 @@ var Parser = (function () {
}, {
key: 'parseInline',
value: function parseInline(text) {
var _this3 = this;
var _this4 = this;

var whiteList = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
var clearHolders = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
Expand All @@ -245,15 +243,15 @@ var Parser = (function () {

// link
text = text.replace(/<(https?:\/\/.+)>/ig, function (match, p1) {
return _this3.makeHolder('<a href="' + p1 + '">' + p1 + '</a>');
return _this4.makeHolder('<a href="' + p1 + '">' + p1 + '</a>');
});

text = text.replace(/<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/ig, function (match, p1, p2, p3) {
var whiteLists = _this3.commonWhiteList + '|' + whiteList;
var whiteLists = _this4.commonWhiteList + '|' + whiteList;
if (whiteLists.toLowerCase().indexOf(p2.toLowerCase()) !== -1) {
return _this3.makeHolder(match);
return _this4.makeHolder(match);
} else {
return _this3.htmlspecialchars(match);
return _this4.htmlspecialchars(match);
}
});

Expand All @@ -266,11 +264,11 @@ var Parser = (function () {
var id = _this.footnotes.indexOf(p1);

if (id === -1) {
id = _this.footnotes.length + 1;
_this.footnotes[id] = _this3.parseInline(p1, '', false);
id = _this.footnotes.length;
_this.footnotes.push(_this4.parseInline(p1, '', false));
}

return _this.makeHolder('<sup id="fnref-' + id + '"><a href="#fn-' + id + '" class="footnote-ref">' + id + '</a></sup>');
return _this.makeHolder('<sup id="fnref-' + (id + 1) + '"><a href="#fn-' + (id + 1) + '" class="footnote-ref">' + (id + 1) + '</a></sup>');
});

// image
Expand Down Expand Up @@ -339,34 +337,34 @@ var Parser = (function () {
}, {
key: 'parseInlineCallback',
value: function parseInlineCallback(text) {
var _this4 = this;
var _this5 = this;

text = text.replace(/(\*{3})(.+?)\1/g, function (match, p1, p2) {
return '<strong><em>' + _this4.parseInlineCallback(p2) + '</em></strong>';
return '<strong><em>' + _this5.parseInlineCallback(p2) + '</em></strong>';
});

text = text.replace(/(\*{2})(.+?)\1/g, function (match, p1, p2) {
return '<strong>' + _this4.parseInlineCallback(p2) + '</strong>';
return '<strong>' + _this5.parseInlineCallback(p2) + '</strong>';
});

text = text.replace(/(\*)(.+?)\1/g, function (match, p1, p2) {
return '<em>' + _this4.parseInlineCallback(p2) + '</em>';
return '<em>' + _this5.parseInlineCallback(p2) + '</em>';
});

text = text.replace(/(\s+|^)(_{3})(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) {
return p1 + '<strong><em>' + _this4.parseInlineCallback(p3) + '</em></strong>' + p4;
return p1 + '<strong><em>' + _this5.parseInlineCallback(p3) + '</em></strong>' + p4;
});

text = text.replace(/(\s+|^)(_{2})(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) {
return p1 + '<strong>' + _this4.parseInlineCallback(p3) + '</strong>' + p4;
return p1 + '<strong>' + _this5.parseInlineCallback(p3) + '</strong>' + p4;
});

text = text.replace(/(\s+|^)(_)(.+?)\2(\s+|$)/g, function (match, p1, p2, p3, p4) {
return p1 + '<em>' + _this4.parseInlineCallback(p3) + '</em>' + p4;
return p1 + '<em>' + _this5.parseInlineCallback(p3) + '</em>' + p4;
});

text = text.replace(/(~{2})(.+?)\1/g, function (match, p1, p2) {
return '<del>' + _this4.parseInlineCallback(p2) + '</del>';
return '<del>' + _this5.parseInlineCallback(p2) + '</del>';
});
return text;
}
Expand All @@ -381,7 +379,7 @@ var Parser = (function () {
}, {
key: 'parseBlock',
value: function parseBlock(text, lines) {
var _this5 = this;
var _this6 = this;

this.blocks = [];
this.current = 'normal';
Expand Down Expand Up @@ -496,14 +494,14 @@ var Parser = (function () {
var tableMatches = /^((?:(?:(?:[ :]*\-[ :]*)+(?:\||\+))|(?:(?:\||\+)(?:[ :]*\-[ :]*)+)|(?:(?:[ :]*\-[ :]*)+(?:\||\+)(?:[ :]*\-[ :]*)+))+)$/g.exec(line);
if (this.isBlock('normal')) {
(function () {
var block = _this5.getBlock();
var block = _this6.getBlock();
var head = false;

if (block.length === 0 || block[0] !== 'normal' || /^\s*$/.test(lines[block[2]])) {
_this5.startBlock('table', key);
_this6.startBlock('table', key);
} else {
head = true;
_this5.backBlock(1, 'table');
_this6.backBlock(1, 'table');
}

if (tableMatches[1][0] == '|') {
Expand Down Expand Up @@ -532,7 +530,7 @@ var Parser = (function () {
aligns.push(align);
});

_this5.setBlock(key, [head, aligns]);
_this6.setBlock(key, [head, aligns]);
})();
}
break;
Expand Down Expand Up @@ -671,9 +669,9 @@ var Parser = (function () {
var types = ['list', 'quote'];

if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) {
if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0] !== -1)) {
if (prevBlock[0] == nextBlock[0] && types.indexOf(prevBlock[0]) !== -1) {
// combine 3 blocks
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], NULL];
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], null];
blocks.splice(key, 2);
}
}
Expand Down Expand Up @@ -724,10 +722,10 @@ var Parser = (function () {
}, {
key: 'parsePre',
value: function parsePre(lines) {
var _this6 = this;
var _this7 = this;

lines.forEach(function (line, ind) {
lines[ind] = _this6.htmlspecialchars(line.substr(4));
lines[ind] = _this7.htmlspecialchars(line.substr(4));
});
var str = lines.join('\n');

Expand Down Expand Up @@ -799,7 +797,7 @@ var Parser = (function () {
}, {
key: 'parseList',
value: function parseList(lines) {
var _this7 = this;
var _this8 = this;

var html = '';
var minSpace = 99999;
Expand Down Expand Up @@ -846,7 +844,7 @@ var Parser = (function () {
leftLines.push(line.replace(pattern, ''));
} else {
if (leftLines.length) {
html += "<li>" + _this7.parse(leftLines.join("\n")) + "</li>";
html += "<li>" + _this8.parse(leftLines.join("\n")) + "</li>";
}
if (lastType !== type) {
if (lastType.length) {
Expand Down Expand Up @@ -880,7 +878,7 @@ var Parser = (function () {
}, {
key: 'parseTable',
value: function parseTable(lines, value) {
var _this8 = this;
var _this9 = this;

var _value = _slicedToArray(value, 2);

Expand Down Expand Up @@ -959,7 +957,7 @@ var Parser = (function () {
html += ' align="' + aligns[key] + '"';
}

html += '>' + _this8.parseInline(text) + ('</' + tag + '>');
html += '>' + _this9.parseInline(text) + ('</' + tag + '>');
});

html += '</tr>';
Expand All @@ -972,9 +970,9 @@ var Parser = (function () {
};

for (var key in lines) {
var _ret2 = _loop(key);
var _ret3 = _loop(key);

if (_ret2 === 'continue') continue;
if (_ret3 === 'continue') continue;
}

if (body !== null) {
Expand Down Expand Up @@ -1005,10 +1003,10 @@ var Parser = (function () {
}, {
key: 'parseNormal',
value: function parseNormal(lines) {
var _this9 = this;
var _this10 = this;

lines = lines.map(function (line) {
return _this9.parseInline(line);
return _this10.parseInline(line);
});

var str = lines.join("\n").trim();
Expand Down Expand Up @@ -1066,10 +1064,10 @@ var Parser = (function () {
}, {
key: 'parseHtml',
value: function parseHtml(lines, type) {
var _this10 = this;
var _this11 = this;

lines.forEach(function (line) {
line = _this10.parseInline(line, _this10.specialWhiteList[type] ? _this10.specialWhiteList[type] : '');
line = _this11.parseInline(line, _this11.specialWhiteList[type] ? _this11.specialWhiteList[type] : '');
});

return lines.join("\n");
Expand Down Expand Up @@ -1252,7 +1250,4 @@ var Parser = (function () {
})();

exports['default'] = Parser;

var parser = new Parser();
console.log(parser.makeHtml('[Genymotion](https://www.genymotion.com/)是。[Genymotion](https://www.genymotion.com/) [VirtualBox](https://www.virtualbox.org/)富的[付费版](https://shop.genymotion.com/index.php?controller=order-opc)'));
module.exports = exports['default'];
Loading

0 comments on commit 3870345

Please sign in to comment.