mirror of
https://github.com/tenrok/BBob.git
synced 2026-05-15 11:59:37 +03:00
add html5 preset, some files structure changes
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
# bbob-cli
|
||||
|
||||
Convert BBCode to HTML
|
||||
|
||||
@@ -6,15 +6,6 @@
|
||||
"bin": {
|
||||
"bbob": "bbob.js"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "../../node_modules/.bin/jest --",
|
||||
"cover": "../../node_modules/.bin/jest --coverage"
|
||||
},
|
||||
"author": "Nikolay Kostyurin <jilizart@gmail.com>",
|
||||
"license": "MIT",
|
||||
"publishConfig": {
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"directories": {
|
||||
"lib": "lib"
|
||||
},
|
||||
@@ -22,8 +13,28 @@
|
||||
"@bbob/parser": "^1.0.3",
|
||||
"commander": "^2.15.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "^23.1.0",
|
||||
"xbbcode-parser": "^0.1.2"
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/JiLiZART/bbob.git"
|
||||
},
|
||||
"keywords": [
|
||||
"bbob",
|
||||
"cli",
|
||||
"bbcode",
|
||||
"pipe"
|
||||
],
|
||||
"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,11 @@
|
||||
# bbob
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const bbob = require('@bbob/core');
|
||||
const presetHTML5 = require('@bbob/preset-html5');
|
||||
const code = `[i]Text[/i]`;
|
||||
|
||||
const processor = bbob([presetHTML5]).process(code, {sync: true})
|
||||
```
|
||||
@@ -0,0 +1,20 @@
|
||||
class BBob {
|
||||
constructor(plugins) {
|
||||
}
|
||||
|
||||
parse() {
|
||||
|
||||
}
|
||||
|
||||
stringify() {
|
||||
|
||||
}
|
||||
|
||||
process(input) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function bbob(...plugins) {
|
||||
return new BBob(plugins);
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "@bbob/core",
|
||||
"version": "1.0.0",
|
||||
"description": "BBob is a tool to transform BBCode markup useing presets",
|
||||
"main": "lib/index.js",
|
||||
"directories": {
|
||||
"lib": "lib"
|
||||
},
|
||||
"dependencies": {
|
||||
"@bbob/parser": "*",
|
||||
"@bbob/html": "*",
|
||||
"@bbob/preset-html5": "*"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/JiLiZART/bbob.git"
|
||||
},
|
||||
"keywords": [
|
||||
"bbcode",
|
||||
"parser",
|
||||
"html",
|
||||
"transform"
|
||||
],
|
||||
"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/"
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
function attrs(obj) {
|
||||
let attr = '';
|
||||
|
||||
Object.keys(obj).forEach((key) => {
|
||||
if (typeof obj[key] === 'boolean' && obj[key]) {
|
||||
attr += ` ${key}`;
|
||||
} else if (typeof obj[key] === 'number') {
|
||||
attr += ` ${key}="${obj[key]}"`;
|
||||
} else if (typeof obj[key] === 'string') {
|
||||
attr += ` ${key}="${obj[key].replace(/"/g, '"')}"`;
|
||||
}
|
||||
});
|
||||
|
||||
return attr;
|
||||
}
|
||||
|
||||
function traverse(tree, cb) {
|
||||
if (Array.isArray(tree)) {
|
||||
tree.forEach((_, i) => {
|
||||
traverse(cb(tree[i]), cb);
|
||||
});
|
||||
} else if (typeof tree === 'object' && tree.content) {
|
||||
traverse(tree.content, cb);
|
||||
}
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
function render(ast) {
|
||||
function html(tree) {
|
||||
let result = '';
|
||||
|
||||
traverse([].concat(tree), (node) => {
|
||||
if (!node) return;
|
||||
|
||||
if (typeof node === 'string' || typeof node === 'number') {
|
||||
result += node;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof node === 'object') {
|
||||
result += `<${node.tag} ${attrs(node.attrs)}></${node.tag}>`;
|
||||
}
|
||||
|
||||
// treat as new root tree if node is an array
|
||||
if (Array.isArray(node)) {
|
||||
result += html(node);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return html(ast);
|
||||
}
|
||||
|
||||
module.exports = render;
|
||||
@@ -1,6 +0,0 @@
|
||||
|
||||
describe('bbob-html', () => {
|
||||
test('render proper markup', () => {
|
||||
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,61 @@
|
||||
function escapeQuote(value) {
|
||||
return value.replace(/"/g, '"');
|
||||
}
|
||||
|
||||
function attrValue(name, value) {
|
||||
if (typeof value === 'boolean' && value) {
|
||||
return `${name}`;
|
||||
} else if (typeof value === 'number') {
|
||||
return `${name}="${value}"`;
|
||||
} else if (typeof value === 'string') {
|
||||
return `${name}="${escapeQuote(value)}"`;
|
||||
} else if (typeof value === 'object') {
|
||||
return `${name}="${escapeQuote(JSON.stringify(value))}"`;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms attrs to html params string
|
||||
* @param values
|
||||
*/
|
||||
const attrs = values =>
|
||||
Object.keys(values)
|
||||
.reduce((arr, key) => [...arr, attrValue(key, values[key])], [''])
|
||||
.join(' ');
|
||||
|
||||
function traverse(tree, cb) {
|
||||
if (Array.isArray(tree)) {
|
||||
tree.forEach((_, i) => {
|
||||
traverse(cb(tree[i]), cb);
|
||||
});
|
||||
} else if (typeof tree === 'object' && tree.content) {
|
||||
traverse(tree.content, cb);
|
||||
}
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
function renderNodes(nodes) {
|
||||
return [].concat(nodes).reduce((r, node) => r + renderNode(node), '');
|
||||
}
|
||||
|
||||
function renderNode(node) {
|
||||
if (!node) return;
|
||||
|
||||
if (typeof node === 'string' || typeof node === 'number') {
|
||||
return node;
|
||||
}
|
||||
|
||||
if (typeof node === 'object') {
|
||||
return `<${node.tag}${attrs(node.attrs)}>${renderNodes(node.content)}</${node.tag}>`;
|
||||
}
|
||||
|
||||
if (Array.isArray(node)) {
|
||||
return renderNodes(node);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
module.exports = renderNodes;
|
||||
@@ -2,7 +2,10 @@
|
||||
"name": "@bbob/html",
|
||||
"version": "1.0.1",
|
||||
"description": "HTML renderer for BBCode pareser BBob",
|
||||
"main": "index.js",
|
||||
"main": "lib/index.js",
|
||||
"devDependencies": {
|
||||
"@bbob/parser": "*"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "../../node_modules/.bin/jest --",
|
||||
"cover": "../../node_modules/.bin/jest --coverage",
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
const render = require('../lib/index');
|
||||
const parse = require('@bbob/parser');
|
||||
|
||||
const process = input => render(parse(input));
|
||||
|
||||
describe('@bbob/html', () => {
|
||||
test('render bbcode tag with single param as html tag', () => {
|
||||
const input = '[url=https://ru.wikipedia.org]Text[/url]';
|
||||
const result = '<url url="https://ru.wikipedia.org">Text</url>';
|
||||
|
||||
expect(process(input)).toBe(result);
|
||||
});
|
||||
|
||||
test('render bbcode tag with multiple params as html tag', () => {
|
||||
const input = '[url href=https://ru.wikipedia.org target=_blank text="Foo Bar"]Text[/url]';
|
||||
const result = '<url url="https://ru.wikipedia.org">Text</url>';
|
||||
|
||||
expect(process(input)).toBe(result);
|
||||
});
|
||||
|
||||
test('render bbcode tag without params as html tag', () => {
|
||||
const input = '[url]https://ru.wikipedia.org[/url]';
|
||||
const result = '<url>https://ru.wikipedia.org</url>';
|
||||
|
||||
expect(process(input)).toBe(result);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
package-lock.json
|
||||
coverage
|
||||
dist
|
||||
@@ -0,0 +1,6 @@
|
||||
package-lock.json
|
||||
coverage
|
||||
src
|
||||
dist
|
||||
!lib
|
||||
*.test.js
|
||||
@@ -0,0 +1 @@
|
||||
Preset to render BBCode to HTML tags
|
||||
@@ -0,0 +1,72 @@
|
||||
// [b]bolded text[/b] => <span style="font-weight: bold;">bolded text</span>
|
||||
// [i]italicized text[/i] => <span style="font-style: italic;">italicized text</span>
|
||||
// [u]underlined text[/u] => <span style="text-decoration: underline;">underlined text</span>
|
||||
// [s]strikethrough text[/s] => <span style="text-decoration: line-through;">strikethrough text</span>
|
||||
// [url]https://en.wikipedia.org[/url] => <a href="https://en.wikipedia.org">https://en.wikipedia.org</a>
|
||||
// [url=http://step.pgc.edu/]ECAT[/url] => <a href="http://step.pgc.edu/">ECAT</a>
|
||||
// [img]https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Go-home-2.svg/100px-Go-home-2.svg.png[/img] => <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Go-home-2.svg/100px-Go-home-2.svg.png" alt="" />
|
||||
// [quote="author"]quoted text[/quote] => <blockquote><p>quoted text</p></blockquote>
|
||||
// [code]monospaced text[/code] => <pre>monospaced text</pre>
|
||||
// [style size="15px"]Large Text[/style] => <span style="font-size:15px">Large Text</span>
|
||||
// [style color="red"]Red Text[/style] => <span style="color:red;">Red Text</span>
|
||||
|
||||
/**
|
||||
[list]
|
||||
[*]Entry 1
|
||||
[*]Entry 2
|
||||
[/list]
|
||||
|
||||
[list]
|
||||
*Entry 1
|
||||
*Entry 2
|
||||
[/list]
|
||||
|
||||
=> <ul><li>Entry 1</li><li>Entry 2</li></ul>
|
||||
|
||||
*/
|
||||
|
||||
/**
|
||||
[table]
|
||||
[tr]
|
||||
[td]table 1[/td]
|
||||
[td]table 2[/td]
|
||||
[/tr]
|
||||
[tr]
|
||||
[td]table 3[/td]
|
||||
[td]table 4[/td]
|
||||
[/tr]
|
||||
[/table]
|
||||
=> <table><tr><td>table 1</td><td>table 2</td></tr><tr><td>table 3</td><td>table 4</td></tr></table>
|
||||
*/
|
||||
|
||||
// [b]bolded text[/b] => <span style="font-weight: bold;">bolded text</span>
|
||||
|
||||
|
||||
const processors = {
|
||||
b: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: 'font-weight: bold;',
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
i: node => ({
|
||||
tag: 'span',
|
||||
attrs: {
|
||||
style: 'font-style: italic;',
|
||||
},
|
||||
content: node.content,
|
||||
}),
|
||||
};
|
||||
|
||||
module.exports = function html5Preset(opts = {}) {
|
||||
return function process(tree) {
|
||||
tree.walk((node) => {
|
||||
if (node.tag && processors[node.tag]) {
|
||||
return processors[node.tag](node, opts);
|
||||
}
|
||||
|
||||
return node;
|
||||
});
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "@bbob/preset-html5",
|
||||
"version": "1.0.0",
|
||||
"description": "HTML5 Preset to transform BBCode to HTML",
|
||||
"main": "lib/index.js",
|
||||
"directories": {
|
||||
"lib": "lib"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@bbob/core": "*"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/JiLiZART/bbob.git"
|
||||
},
|
||||
"keywords": [
|
||||
"bbcode",
|
||||
"preset",
|
||||
"html5",
|
||||
"bbode",
|
||||
"transform"
|
||||
],
|
||||
"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,15 @@
|
||||
const preset = require('../lib/index');
|
||||
const bbob = require('@bbob/core');
|
||||
|
||||
const processor = bbob([
|
||||
preset(),
|
||||
]);
|
||||
|
||||
describe('bbob-preset-html5', () => {
|
||||
test('render [url]', () => {
|
||||
const input = '[url=https://ru.wikipedia.org]Text[/url]';
|
||||
const result = '<a href="https://ru.wikipedia.org">Text</a>';
|
||||
|
||||
expect(processor.process(input, { sync: true })).toBe(result);
|
||||
});
|
||||
});
|
||||
@@ -1,3 +0,0 @@
|
||||
function BBob() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user