2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-06-08 17:22:26 +03:00

feat(parser): better handlinf of unclosed tags like '[My unclosed and [closed] tag'

This commit is contained in:
Nikolay Kostyurin
2018-09-24 00:33:27 +02:00
parent 505152bf4c
commit b49b7435da
5 changed files with 79 additions and 21 deletions
+2 -1
View File
@@ -1 +1,2 @@
export { parse, createTagNode } from './parse';
export { default, parse } from './parse';
export { TagNode } from '@bbob/plugin-helper/lib/TagNode';
+27 -4
View File
@@ -22,6 +22,7 @@ const createCharGrabber = (source) => {
idx += 1;
};
const hasNext = () => source.length > idx;
const getRest = () => source.substr(idx);
return {
skip,
@@ -39,6 +40,20 @@ const createCharGrabber = (source) => {
getNext: () => source[idx + 1],
getPrev: () => source[idx - 1],
getCurr: () => source[idx],
moveIdxTo: (val) => {
idx += val;
},
getRest,
substrUntilChar: (char) => {
const restStr = getRest();
const indexOfChar = restStr.indexOf(char);
if (indexOfChar >= 0) {
return restStr.substr(0, indexOfChar);
}
return '';
},
};
};
@@ -75,6 +90,7 @@ function createLexer(buffer, options = {}) {
const isWhiteSpace = char => (WHITESPACES.indexOf(char) >= 0);
const isCharToken = char => (NOT_CHAR_TOKENS.indexOf(char) === -1);
const isSpecialChar = char => (SPECIAL_CHARS.indexOf(char) >= 0);
const isNotValidCharInTag = char => ([openTag].indexOf(char) >= 0);
const emitToken = (token) => {
if (options.onToken) {
@@ -159,15 +175,22 @@ function createLexer(buffer, options = {}) {
emitToken(createToken(TYPE_SPACE, str, row, col));
} else if (char === openTag) {
const nextChar = bufferGrabber.getNext();
bufferGrabber.skip(); // skip [
bufferGrabber.skip(); // skip openTag
if (isCharReserved(nextChar)) {
// detect case where we have '[My word [tag][/tag]' or we have '[My last line word'
const substr = bufferGrabber.substrUntilChar(closeTag);
const hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0;
if (isCharReserved(nextChar) || hasInvalidChars || bufferGrabber.isLast()) {
emitToken(createToken(TYPE_WORD, char, row, col));
} else {
const str = bufferGrabber.grabWhile(val => val !== closeTag);
bufferGrabber.skip(); // skip ]
if (!(str.indexOf(EQ) > 0) || str[0] === SLASH) {
bufferGrabber.skip(); // skip closeTag
const isNoAttrsInTag = str.indexOf(EQ) === -1;
const isClosingTag = str[0] === SLASH;
if (isNoAttrsInTag || isClosingTag) {
emitToken(createToken(TYPE_TAG, str, row, col));
} else {
const parsed = parseAttrs(str);
+1 -1
View File
@@ -239,5 +239,5 @@ const parse = (input, opts = {}) => {
return nodes;
};
export { createTagNode, parse };
export { parse };
export default parse;