mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-23 20:40:34 +03:00
refactor(*): move helper fucntions from core and preset-html5 to separate package — plugin-helper
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
dist
|
dist
|
||||||
test
|
test
|
||||||
|
rollup.config.js
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ const {
|
|||||||
OPEN_BRAKET,
|
OPEN_BRAKET,
|
||||||
CLOSE_BRAKET,
|
CLOSE_BRAKET,
|
||||||
SLASH,
|
SLASH,
|
||||||
} = require('./char');
|
} = require('@bbob/plugin-helper/lib/char');
|
||||||
|
|
||||||
// type, value, line, row,
|
// type, value, line, row,
|
||||||
const TOKEN_TYPE_ID = 'type'; // 0;
|
const TOKEN_TYPE_ID = 'type'; // 0;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const {
|
|||||||
PLACEHOLDER_SPACE, PLACEHOLDER_SPACE_TAB,
|
PLACEHOLDER_SPACE, PLACEHOLDER_SPACE_TAB,
|
||||||
SLASH,
|
SLASH,
|
||||||
BACKSLASH,
|
BACKSLASH,
|
||||||
} = require('./char');
|
} = require('@bbob/plugin-helper/lib/char');
|
||||||
const Token = require('./Token');
|
const Token = require('./Token');
|
||||||
|
|
||||||
const createTokenOfType = (type, value, line, row) => new Token(type, value, line, row);
|
const createTokenOfType = (type, value, line, row) => new Token(type, value, line, row);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
const Tokenizer = require('./Tokenizer');
|
const Tokenizer = require('./Tokenizer');
|
||||||
const TagNode = require('./TagNode');
|
const TagNode = require('@bbob/plugin-helper/lib/TagNode');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
@@ -28,14 +28,6 @@ let tokenizer = null;
|
|||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
let tokens = null;
|
let tokens = null;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param tag
|
|
||||||
* @param attrs
|
|
||||||
* @param content
|
|
||||||
*/
|
|
||||||
const newTagNode = (tag, attrs = {}, content = []) => new TagNode(tag, attrs, content);
|
|
||||||
|
|
||||||
const createTokenizer = (input, onToken) => new Tokenizer(input, { onToken });
|
const createTokenizer = (input, onToken) => new Tokenizer(input, { onToken });
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,7 +48,7 @@ const getTagNode = () => (tagNodes.length ? tagNodes[tagNodes.length - 1] : null
|
|||||||
* @param {Token} token
|
* @param {Token} token
|
||||||
* @return {Array}
|
* @return {Array}
|
||||||
*/
|
*/
|
||||||
const createTagNode = token => tagNodes.push(newTagNode(token.getValue()));
|
const createTagNode = token => tagNodes.push(TagNode.create(token.getValue()));
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @param {Token} token
|
* @param {Token} token
|
||||||
|
|||||||
@@ -19,6 +19,9 @@
|
|||||||
"html"
|
"html"
|
||||||
],
|
],
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
|
"dependencies": {
|
||||||
|
"@bbob/plugin-helper": "^1.0.0"
|
||||||
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git://github.com/JiLiZART/bbob.git"
|
"url": "git://github.com/JiLiZART/bbob.git"
|
||||||
|
|||||||
@@ -1,25 +1,7 @@
|
|||||||
const {
|
const {
|
||||||
getChar, N, CLOSE_BRAKET, OPEN_BRAKET, SLASH,
|
getChar, OPEN_BRAKET, CLOSE_BRAKET, SLASH,
|
||||||
} = require('./char');
|
} = require('./char');
|
||||||
|
const { getNodeLength, appendToNode } = require('./index');
|
||||||
const EOL = getChar(N);
|
|
||||||
const isNode = el => typeof el === 'object' && el.tag;
|
|
||||||
const isStringNode = el => typeof el === 'string';
|
|
||||||
const isEOL = el => el === EOL;
|
|
||||||
|
|
||||||
const getNodeLength = (node) => {
|
|
||||||
if (isNode(node)) {
|
|
||||||
return node.content.reduce((count, contentNode) => count + getNodeLength(contentNode), 0);
|
|
||||||
} else if (isStringNode(node)) {
|
|
||||||
return node.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
const appendToNode = (node, value) => {
|
|
||||||
node.content.push(value);
|
|
||||||
};
|
|
||||||
|
|
||||||
class TagNode {
|
class TagNode {
|
||||||
constructor(tag, attrs, content) {
|
constructor(tag, attrs, content) {
|
||||||
@@ -53,7 +35,5 @@ class TagNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = TagNode;
|
module.exports = TagNode;
|
||||||
module.exports.isNode = isNode;
|
module.exports.create = (tag, attrs = {}, content = []) => new TagNode(tag, attrs, content);
|
||||||
module.exports.isStringNode = isStringNode;
|
module.exports.isOf = (node, type) => (node.tag === type);
|
||||||
module.exports.isEOL = isEOL;
|
|
||||||
module.exports.appendToNode = appendToNode;
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
const { getChar, N } = require('./char');
|
||||||
|
|
||||||
|
const isTagNode = el => typeof el === 'object' && el.tag;
|
||||||
|
const isStringNode = el => typeof el === 'string';
|
||||||
|
|
||||||
|
const EOL = getChar(N);
|
||||||
|
const isEOL = el => el === EOL;
|
||||||
|
|
||||||
|
const getNodeLength = (node) => {
|
||||||
|
if (isTagNode(node)) {
|
||||||
|
return node.content.reduce((count, contentNode) => count + getNodeLength(contentNode), 0);
|
||||||
|
} else if (isStringNode(node)) {
|
||||||
|
return node.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
const appendToNode = (node, value) => {
|
||||||
|
node.content.push(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
appendToNode,
|
||||||
|
getNodeLength,
|
||||||
|
isTagNode,
|
||||||
|
isStringNode,
|
||||||
|
isEOL,
|
||||||
|
};
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"name": "@bbob/plugin-helper",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Set of utils to help write plugins",
|
||||||
|
"main": "lib/index.js",
|
||||||
|
"directories": {
|
||||||
|
"lib": "lib"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/JiLiZART/bbob.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"bbob",
|
||||||
|
"plugin",
|
||||||
|
"helper"
|
||||||
|
],
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/JiLiZART/bbob/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/JiLiZART/bbob#readme",
|
||||||
|
"scripts": {
|
||||||
|
"test": "../../node_modules/.bin/jest --",
|
||||||
|
"cover": "../../node_modules/.bin/jest --coverage",
|
||||||
|
"lint": "../../node_modules/.bin/eslint ."
|
||||||
|
},
|
||||||
|
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
|
||||||
|
"license": "MIT",
|
||||||
|
"publishConfig": {
|
||||||
|
"registry": "https://registry.npmjs.org/"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
describe('@bbob/plugin-helper', () => {
|
||||||
|
test('one', () => {
|
||||||
|
expect(1 + 2).toBe(3)
|
||||||
|
})
|
||||||
|
});
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
/* eslint-disable no-plusplus,no-lonely-if */
|
||||||
|
const { isStringNode, isTagNode } = require('@bbob/plugin-helper');
|
||||||
|
const TagNode = require('@bbob/plugin-helper/lib/TagNode');
|
||||||
|
|
||||||
|
const isStartsWith = (node, type) => (node[0] === type);
|
||||||
|
|
||||||
|
const styleMap = {
|
||||||
|
color: val => `color:${val};`,
|
||||||
|
size: val => `font-size:${val};`,
|
||||||
|
};
|
||||||
|
|
||||||
|
const getStyleFromAttrs = attrs =>
|
||||||
|
Object
|
||||||
|
.keys(attrs)
|
||||||
|
.reduce((acc, key) => (styleMap[key] ? acc.concat(styleMap[key](attrs[key])) : acc), [])
|
||||||
|
.join(' ');
|
||||||
|
|
||||||
|
const asListItems = (content) => {
|
||||||
|
let listIdx = 0;
|
||||||
|
const listItems = [];
|
||||||
|
|
||||||
|
const createItemNode = () => TagNode.create('li');
|
||||||
|
const ensureListItem = (val) => {
|
||||||
|
listItems[listIdx] = listItems[listIdx] || val;
|
||||||
|
};
|
||||||
|
const addItem = (val) => {
|
||||||
|
if (listItems[listIdx] && listItems[listIdx].content) {
|
||||||
|
listItems[listIdx].content = listItems[listIdx].content.concat(val);
|
||||||
|
} else {
|
||||||
|
listItems[listIdx] = listItems[listIdx].concat(val);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
content.forEach((el) => {
|
||||||
|
if (isStringNode(el) && isStartsWith(el, '*')) {
|
||||||
|
if (listItems[listIdx]) {
|
||||||
|
listIdx++;
|
||||||
|
}
|
||||||
|
ensureListItem(createItemNode());
|
||||||
|
addItem(el.substr(1));
|
||||||
|
} else if (isTagNode(el) && TagNode.isOf(el, '*')) {
|
||||||
|
if (listItems[listIdx]) {
|
||||||
|
listIdx++;
|
||||||
|
}
|
||||||
|
ensureListItem(createItemNode());
|
||||||
|
} else if (!isTagNode(listItems[listIdx])) {
|
||||||
|
listIdx++;
|
||||||
|
ensureListItem(el);
|
||||||
|
} else if (listItems[listIdx]) {
|
||||||
|
addItem(el);
|
||||||
|
} else {
|
||||||
|
ensureListItem(el);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return [].concat(listItems);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
b: node => ({
|
||||||
|
tag: 'span',
|
||||||
|
attrs: {
|
||||||
|
style: 'font-weight: bold;',
|
||||||
|
},
|
||||||
|
content: node.content,
|
||||||
|
}),
|
||||||
|
i: node => ({
|
||||||
|
tag: 'span',
|
||||||
|
attrs: {
|
||||||
|
style: 'font-style: italic;',
|
||||||
|
},
|
||||||
|
content: node.content,
|
||||||
|
}),
|
||||||
|
u: node => ({
|
||||||
|
tag: 'span',
|
||||||
|
attrs: {
|
||||||
|
style: 'text-decoration: underline;',
|
||||||
|
},
|
||||||
|
content: node.content,
|
||||||
|
}),
|
||||||
|
s: node => ({
|
||||||
|
tag: 'span',
|
||||||
|
attrs: {
|
||||||
|
style: 'text-decoration: line-through;',
|
||||||
|
},
|
||||||
|
content: node.content,
|
||||||
|
}),
|
||||||
|
url: (node, { render }) => ({
|
||||||
|
tag: 'a',
|
||||||
|
attrs: {
|
||||||
|
href: node.attrs.url ? node.attrs.url : render(node.content),
|
||||||
|
},
|
||||||
|
content: node.content,
|
||||||
|
}),
|
||||||
|
img: (node, { render }) => ({
|
||||||
|
tag: 'img',
|
||||||
|
attrs: {
|
||||||
|
src: render(node.content),
|
||||||
|
},
|
||||||
|
content: null,
|
||||||
|
}),
|
||||||
|
quote: node => ({
|
||||||
|
tag: 'blockquote',
|
||||||
|
attrs: {},
|
||||||
|
content: [{
|
||||||
|
tag: 'p',
|
||||||
|
attrs: {},
|
||||||
|
content: node.content,
|
||||||
|
}],
|
||||||
|
}),
|
||||||
|
code: node => ({
|
||||||
|
tag: 'pre',
|
||||||
|
attrs: {},
|
||||||
|
content: node.content,
|
||||||
|
}),
|
||||||
|
style: node => ({
|
||||||
|
tag: 'span',
|
||||||
|
attrs: {
|
||||||
|
style: getStyleFromAttrs(node.attrs),
|
||||||
|
},
|
||||||
|
content: node.content,
|
||||||
|
}),
|
||||||
|
list: node => ({
|
||||||
|
tag: 'ul',
|
||||||
|
attrs: {},
|
||||||
|
content: asListItems(node.content),
|
||||||
|
}),
|
||||||
|
};
|
||||||
@@ -1,133 +1,5 @@
|
|||||||
/* eslint-disable no-plusplus,no-lonely-if */
|
const { isTagNode } = require('@bbob/plugin-helper');
|
||||||
const isStringNode = node => (typeof node === 'string');
|
const defaultProcessors = require('./default');
|
||||||
const isStartsWith = (node, type) => (node[0] === type);
|
|
||||||
|
|
||||||
const isTagNode = node => (typeof node === 'object' && Boolean(node.tag));
|
|
||||||
const isTagOf = (node, type) => (node.tag === type);
|
|
||||||
const createTagNode = tag => ({ tag, attrs: {}, content: [] });
|
|
||||||
|
|
||||||
const styleMap = {
|
|
||||||
color: val => `color:${val};`,
|
|
||||||
size: val => `font-size:${val};`,
|
|
||||||
};
|
|
||||||
|
|
||||||
const getStyleFromAttrs = attrs =>
|
|
||||||
Object
|
|
||||||
.keys(attrs)
|
|
||||||
.reduce((acc, key) => (styleMap[key] ? acc.concat(styleMap[key](attrs[key])) : acc), [])
|
|
||||||
.join(' ');
|
|
||||||
|
|
||||||
const asListItems = (content) => {
|
|
||||||
const listItems = [];
|
|
||||||
let listIdx = 0;
|
|
||||||
const createItemNode = () => createTagNode('li');
|
|
||||||
const ensureListItem = (val) => {
|
|
||||||
listItems[listIdx] = listItems[listIdx] || val;
|
|
||||||
};
|
|
||||||
const addItem = (val) => {
|
|
||||||
if (listItems[listIdx] && listItems[listIdx].content) {
|
|
||||||
listItems[listIdx].content = listItems[listIdx].content.concat(val);
|
|
||||||
} else {
|
|
||||||
listItems[listIdx] = listItems[listIdx].concat(val);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
content.forEach((el) => {
|
|
||||||
if (isStringNode(el) && isStartsWith(el, '*')) {
|
|
||||||
if (listItems[listIdx]) {
|
|
||||||
listIdx++;
|
|
||||||
}
|
|
||||||
ensureListItem(createItemNode());
|
|
||||||
addItem(el.substr(1));
|
|
||||||
} else if (isTagNode(el) && isTagOf(el, '*')) {
|
|
||||||
if (listItems[listIdx]) {
|
|
||||||
listIdx++;
|
|
||||||
}
|
|
||||||
ensureListItem(createItemNode());
|
|
||||||
} else {
|
|
||||||
if (!isTagNode(listItems[listIdx])) {
|
|
||||||
listIdx++;
|
|
||||||
ensureListItem(el);
|
|
||||||
} else if (listItems[listIdx]) {
|
|
||||||
addItem(el);
|
|
||||||
} else {
|
|
||||||
ensureListItem(el);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return [].concat(listItems);
|
|
||||||
};
|
|
||||||
|
|
||||||
const defaultProcessors = {
|
|
||||||
b: node => ({
|
|
||||||
tag: 'span',
|
|
||||||
attrs: {
|
|
||||||
style: 'font-weight: bold;',
|
|
||||||
},
|
|
||||||
content: node.content,
|
|
||||||
}),
|
|
||||||
i: node => ({
|
|
||||||
tag: 'span',
|
|
||||||
attrs: {
|
|
||||||
style: 'font-style: italic;',
|
|
||||||
},
|
|
||||||
content: node.content,
|
|
||||||
}),
|
|
||||||
u: node => ({
|
|
||||||
tag: 'span',
|
|
||||||
attrs: {
|
|
||||||
style: 'text-decoration: underline;',
|
|
||||||
},
|
|
||||||
content: node.content,
|
|
||||||
}),
|
|
||||||
s: node => ({
|
|
||||||
tag: 'span',
|
|
||||||
attrs: {
|
|
||||||
style: 'text-decoration: line-through;',
|
|
||||||
},
|
|
||||||
content: node.content,
|
|
||||||
}),
|
|
||||||
url: (node, { render }) => ({
|
|
||||||
tag: 'a',
|
|
||||||
attrs: {
|
|
||||||
href: node.attrs.url ? node.attrs.url : render(node.content),
|
|
||||||
},
|
|
||||||
content: node.content,
|
|
||||||
}),
|
|
||||||
img: (node, { render }) => ({
|
|
||||||
tag: 'img',
|
|
||||||
attrs: {
|
|
||||||
src: render(node.content),
|
|
||||||
},
|
|
||||||
content: null,
|
|
||||||
}),
|
|
||||||
quote: node => ({
|
|
||||||
tag: 'blockquote',
|
|
||||||
attrs: {},
|
|
||||||
content: [{
|
|
||||||
tag: 'p',
|
|
||||||
attrs: {},
|
|
||||||
content: node.content,
|
|
||||||
}],
|
|
||||||
}),
|
|
||||||
code: node => ({
|
|
||||||
tag: 'pre',
|
|
||||||
attrs: {},
|
|
||||||
content: node.content,
|
|
||||||
}),
|
|
||||||
style: node => ({
|
|
||||||
tag: 'span',
|
|
||||||
attrs: {
|
|
||||||
style: getStyleFromAttrs(node.attrs),
|
|
||||||
},
|
|
||||||
content: node.content,
|
|
||||||
}),
|
|
||||||
list: node => ({
|
|
||||||
tag: 'ul',
|
|
||||||
attrs: {},
|
|
||||||
content: asListItems(node.content),
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = function html5Preset(opts = {}) {
|
module.exports = function html5Preset(opts = {}) {
|
||||||
const processors = Object.assign({}, defaultProcessors, opts.processors || {});
|
const processors = Object.assign({}, defaultProcessors, opts.processors || {});
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
"directories": {
|
"directories": {
|
||||||
"lib": "lib"
|
"lib": "lib"
|
||||||
},
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@bbob/plugin-helper": "^1.0.0"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@bbob/core": "^1.0.6"
|
"@bbob/core": "^1.0.6"
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user