@podlite/schema 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +1 -1
- package/lib/ast-helpers.d.ts +1 -0
- package/lib/ast-helpers.js +3 -2
- package/lib/ast-inerator.js +2 -2
- package/lib/exportAny.d.ts +19 -0
- package/lib/exportAny.js +64 -0
- package/lib/exportHtml.d.ts +15 -0
- package/lib/exportHtml.js +277 -0
- package/lib/grammar.d.ts +14 -0
- package/lib/grammar.js +6395 -0
- package/lib/grammarfc.d.ts +14 -0
- package/lib/grammarfc.js +3362 -0
- package/lib/helpers/config.d.ts +12 -0
- package/lib/helpers/config.js +51 -0
- package/lib/helpers/handlers.d.ts +47 -0
- package/lib/helpers/handlers.js +119 -0
- package/lib/helpers/makeInterator.d.ts +5 -0
- package/lib/helpers/makeInterator.js +51 -0
- package/lib/helpers/makeQuery.d.ts +47 -0
- package/lib/helpers/makeQuery.js +103 -0
- package/lib/helpers/makeQuery.test.d.ts +1 -0
- package/lib/helpers/makeQuery.test.js +41 -0
- package/lib/helpers/makeTransformer.d.ts +7 -0
- package/lib/helpers/makeTransformer.js +70 -0
- package/lib/index.d.ts +61 -0
- package/lib/index.js +71 -1
- package/lib/plugin-clean-location.d.ts +2 -0
- package/lib/plugin-clean-location.js +23 -0
- package/lib/plugin-defn-fill-term.d.ts +2 -0
- package/lib/plugin-defn-fill-term.js +52 -0
- package/lib/plugin-formatting-codes.d.ts +3 -0
- package/lib/plugin-formatting-codes.js +48 -0
- package/lib/plugin-group-defn.d.ts +2 -0
- package/lib/plugin-group-defn.js +73 -0
- package/lib/plugin-group-items.d.ts +2 -0
- package/lib/plugin-group-items.js +82 -0
- package/lib/plugin-heading.d.ts +2 -0
- package/lib/plugin-heading.js +32 -0
- package/lib/plugin-items.d.ts +2 -0
- package/lib/plugin-items.js +40 -0
- package/lib/plugin-tables.d.ts +5 -0
- package/lib/plugin-tables.js +131 -0
- package/lib/plugin-vmargin.d.ts +3 -0
- package/lib/plugin-vmargin.js +29 -0
- package/lib/query-helpers.d.ts +2 -1
- package/lib/query-helpers.js +2 -5
- package/lib/writer.d.ts +30 -0
- package/lib/writer.js +91 -0
- package/lib/writerHtml.d.ts +11 -0
- package/lib/writerHtml.js +33 -0
- package/package.json +13 -4
- package/lib/helpers.d.ts +0 -1
- package/lib/helpers.js +0 -5
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare type Context = {
|
|
2
|
+
config?: any;
|
|
3
|
+
};
|
|
4
|
+
export interface Attr {
|
|
5
|
+
getAllValues: (name: any) => any;
|
|
6
|
+
getFirstValue: (name: any) => any;
|
|
7
|
+
asHash: () => {};
|
|
8
|
+
(): {};
|
|
9
|
+
exists(name: string): boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare const makeAttrs: (node: any, ctx?: Context) => Attr;
|
|
12
|
+
export default makeAttrs;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeAttrs = void 0;
|
|
4
|
+
const makeAttrs = (node, ctx = {}) => {
|
|
5
|
+
const config = node.config instanceof Array ? node.config : [];
|
|
6
|
+
// add config's from ctx
|
|
7
|
+
let configured = [];
|
|
8
|
+
if (ctx.config && ctx.config.hasOwnProperty(node.name)) {
|
|
9
|
+
configured = ctx.config[node.name];
|
|
10
|
+
}
|
|
11
|
+
let result = {};
|
|
12
|
+
[...config, ...configured].map(a => {
|
|
13
|
+
if (!result.hasOwnProperty(a.name)) {
|
|
14
|
+
result[a.name] = [];
|
|
15
|
+
}
|
|
16
|
+
if (a.type === 'array') {
|
|
17
|
+
result[a.name].push(...a.value);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
result[a.name].push(a.value);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
let resfn = function () { };
|
|
24
|
+
/**
|
|
25
|
+
* check if prop exists
|
|
26
|
+
*
|
|
27
|
+
* for example: attrs.exists('caption')
|
|
28
|
+
*/
|
|
29
|
+
resfn.exists = (name) => result.hasOwnProperty(name);
|
|
30
|
+
/**
|
|
31
|
+
* return array for prop
|
|
32
|
+
*
|
|
33
|
+
* for example: attrs.getAllValues('caption')
|
|
34
|
+
*/
|
|
35
|
+
resfn.getAllValues = (name) => { return resfn.exists(name) ? result[name] : []; };
|
|
36
|
+
/**
|
|
37
|
+
* return first value or undefined if prop don't exists
|
|
38
|
+
*
|
|
39
|
+
* for example: attrs.exists('caption')
|
|
40
|
+
*/
|
|
41
|
+
resfn.getFirstValue = (name) => { return resfn.exists(name) ? resfn.getAllValues(name)[0] : undefined; };
|
|
42
|
+
/**
|
|
43
|
+
* return key: val
|
|
44
|
+
*
|
|
45
|
+
* for example: attrs.asHash()
|
|
46
|
+
*/
|
|
47
|
+
resfn.asHash = () => result;
|
|
48
|
+
return resfn;
|
|
49
|
+
};
|
|
50
|
+
exports.makeAttrs = makeAttrs;
|
|
51
|
+
exports.default = exports.makeAttrs;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { RuleObject, RuleHandler } from './makeQuery';
|
|
2
|
+
/**
|
|
3
|
+
* wrap content by open and closed tags
|
|
4
|
+
*/
|
|
5
|
+
export declare const wrapContent: (pre: any, post: any) => RuleHandler;
|
|
6
|
+
/**
|
|
7
|
+
* emptyContent - skip any child node
|
|
8
|
+
*/
|
|
9
|
+
export declare const emptyContent: () => RuleHandler;
|
|
10
|
+
/**
|
|
11
|
+
* content - process childs as regular content
|
|
12
|
+
*/
|
|
13
|
+
export declare const content: RuleHandler;
|
|
14
|
+
/**
|
|
15
|
+
|
|
16
|
+
Set hander after call with node
|
|
17
|
+
|
|
18
|
+
':para':setFn((node,ctx) => (ctx.parents || [] ).includes('head') ? content : wrapContent('<p>','</p>'),
|
|
19
|
+
|
|
20
|
+
*/
|
|
21
|
+
export declare const setFn: (check: any) => RuleHandler;
|
|
22
|
+
/**
|
|
23
|
+
|
|
24
|
+
Set new context for handler
|
|
25
|
+
|
|
26
|
+
const parents = (ctx.parents || [])
|
|
27
|
+
parents.push('head')
|
|
28
|
+
const {level} = node
|
|
29
|
+
return setContext( { ...ctx, parents }, wrapContent(`<h${level}>`,`</h${level}>`))
|
|
30
|
+
|
|
31
|
+
*/
|
|
32
|
+
export declare const setContext: (ctx: any, fn: any) => RuleHandler;
|
|
33
|
+
/**
|
|
34
|
+
* Make subset of rules for processing
|
|
35
|
+
*
|
|
36
|
+
* @param {*} rules
|
|
37
|
+
* @param {*} processNode
|
|
38
|
+
*/
|
|
39
|
+
export declare const subUse: (rules: RuleObject | Array<RuleObject>, processNode: any) => RuleHandler;
|
|
40
|
+
/**
|
|
41
|
+
* Add support nesting
|
|
42
|
+
*
|
|
43
|
+
* @param {*} rules
|
|
44
|
+
* @param {*} implicitLevel - default level of nesting
|
|
45
|
+
*
|
|
46
|
+
*/
|
|
47
|
+
export declare const handleNested: (defaultHandler: RuleHandler, implicitLevel?: number) => RuleHandler;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.handleNested = exports.subUse = exports.setContext = exports.setFn = exports.content = exports.emptyContent = exports.wrapContent = void 0;
|
|
7
|
+
const makeQuery_1 = require("./makeQuery");
|
|
8
|
+
const makeInterator_1 = __importDefault(require("./makeInterator"));
|
|
9
|
+
const config_1 = __importDefault(require("./config"));
|
|
10
|
+
/**
|
|
11
|
+
* wrap content by open and closed tags
|
|
12
|
+
*/
|
|
13
|
+
const wrapContent = (pre, post) => (writer, processor) => (node, ctx, interator) => {
|
|
14
|
+
writer.writeRaw(pre);
|
|
15
|
+
if (node.content)
|
|
16
|
+
interator(node.content, ctx);
|
|
17
|
+
writer.writeRaw(post);
|
|
18
|
+
};
|
|
19
|
+
exports.wrapContent = wrapContent;
|
|
20
|
+
/**
|
|
21
|
+
* emptyContent - skip any child node
|
|
22
|
+
*/
|
|
23
|
+
const emptyContent = () => () => () => { };
|
|
24
|
+
exports.emptyContent = emptyContent;
|
|
25
|
+
/**
|
|
26
|
+
* content - process childs as regular content
|
|
27
|
+
*/
|
|
28
|
+
const content = (writer, processor) => (node, ctx, interator) => {
|
|
29
|
+
if (node.content) {
|
|
30
|
+
return interator(node.content, ctx);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
exports.content = content;
|
|
34
|
+
/**
|
|
35
|
+
|
|
36
|
+
Set hander after call with node
|
|
37
|
+
|
|
38
|
+
':para':setFn((node,ctx) => (ctx.parents || [] ).includes('head') ? content : wrapContent('<p>','</p>'),
|
|
39
|
+
|
|
40
|
+
*/
|
|
41
|
+
const setFn = (check) => (writer, processor) => {
|
|
42
|
+
return (node, ctx, interator) => {
|
|
43
|
+
return check(node, ctx)(writer, processor)(node, ctx, interator);
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
exports.setFn = setFn;
|
|
47
|
+
/**
|
|
48
|
+
|
|
49
|
+
Set new context for handler
|
|
50
|
+
|
|
51
|
+
const parents = (ctx.parents || [])
|
|
52
|
+
parents.push('head')
|
|
53
|
+
const {level} = node
|
|
54
|
+
return setContext( { ...ctx, parents }, wrapContent(`<h${level}>`,`</h${level}>`))
|
|
55
|
+
|
|
56
|
+
*/
|
|
57
|
+
const setContext = (ctx, fn) => (writer, processor) => {
|
|
58
|
+
return (node, _, interator) => {
|
|
59
|
+
return fn(writer, processor)(node, ctx, interator);
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
exports.setContext = setContext;
|
|
63
|
+
/**
|
|
64
|
+
* Select handler from already inited wrappers
|
|
65
|
+
*
|
|
66
|
+
* @param {*} check
|
|
67
|
+
* @param {...any} fns
|
|
68
|
+
*/
|
|
69
|
+
const IfNode = (check, ...fns) => (writer, processor) => {
|
|
70
|
+
return (node, ctx, interator) => {
|
|
71
|
+
check(node, ctx, ...fns.map(i => i(writer, processor)))(node, ctx, interator);
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Make subset of rules for processing
|
|
76
|
+
*
|
|
77
|
+
* @param {*} rules
|
|
78
|
+
* @param {*} processNode
|
|
79
|
+
*/
|
|
80
|
+
const subUse = (rules, processNode) => {
|
|
81
|
+
const newFns = (0, makeQuery_1.makeRulesArray)(rules).reverse();
|
|
82
|
+
return (writer, processor) => {
|
|
83
|
+
// init new rules
|
|
84
|
+
const inited = newFns.map(item => (0, makeQuery_1.makeRule)(item.rule, item.fn(writer, processor))).reverse();
|
|
85
|
+
const processNodeInited = processNode(writer, processor);
|
|
86
|
+
let subInterator;
|
|
87
|
+
return (node, ctx, interator) => {
|
|
88
|
+
if (!subInterator)
|
|
89
|
+
subInterator = (0, makeInterator_1.default)([...interator.rules, ...inited]);
|
|
90
|
+
return processNodeInited(node, ctx, subInterator);
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
exports.subUse = subUse;
|
|
95
|
+
/**
|
|
96
|
+
* Add support nesting
|
|
97
|
+
*
|
|
98
|
+
* @param {*} rules
|
|
99
|
+
* @param {*} implicitLevel - default level of nesting
|
|
100
|
+
*
|
|
101
|
+
*/
|
|
102
|
+
const handleNested = (defaultHandler, implicitLevel) => {
|
|
103
|
+
return (writer, processor) => {
|
|
104
|
+
const defaultHandlerInited = defaultHandler(writer, processor);
|
|
105
|
+
return (node, ctx, interator) => {
|
|
106
|
+
const nesting = (0, config_1.default)(node, ctx).getFirstValue('nested') || implicitLevel;
|
|
107
|
+
// add nesting level
|
|
108
|
+
if (nesting !== undefined) {
|
|
109
|
+
writer.addLevel(nesting);
|
|
110
|
+
}
|
|
111
|
+
defaultHandlerInited(node, ctx, interator);
|
|
112
|
+
// remove nesting level
|
|
113
|
+
if (nesting !== undefined) {
|
|
114
|
+
writer.removeLevel(nesting);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
exports.handleNested = handleNested;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function flattenDeep(arr) {
|
|
4
|
+
if (!Array.isArray(arr)) {
|
|
5
|
+
return arr;
|
|
6
|
+
}
|
|
7
|
+
return arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
|
|
8
|
+
}
|
|
9
|
+
function thisFunc(rules) {
|
|
10
|
+
function interator(node, context) {
|
|
11
|
+
if (node instanceof Array) {
|
|
12
|
+
// filter null and undefined nodes
|
|
13
|
+
return flattenDeep(node.map(item => interator(item, context))).filter(Boolean);
|
|
14
|
+
}
|
|
15
|
+
if ('string' === typeof node) {
|
|
16
|
+
// convert string to lex node with type
|
|
17
|
+
return interator({ type: 'text', value: node }, context);
|
|
18
|
+
}
|
|
19
|
+
// get first rule for this node
|
|
20
|
+
const reversed = rules.slice();
|
|
21
|
+
reversed.reverse();
|
|
22
|
+
const ruleIndex = reversed.findIndex(rule => rule.isFor(node));
|
|
23
|
+
if (ruleIndex !== -1) {
|
|
24
|
+
// try to find next rule
|
|
25
|
+
const nextRuleSet = reversed.slice(ruleIndex + 1);
|
|
26
|
+
const nextRuleIndex = nextRuleSet.findIndex(rule => rule.isFor(node));
|
|
27
|
+
const defaultFn = (n = node, ctx = context, localInterator = interator) => {
|
|
28
|
+
if (nextRuleIndex !== -1) {
|
|
29
|
+
nextRuleSet[nextRuleIndex].fn(n, ctx, localInterator, () => { });
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
if (typeof reversed[ruleIndex].fn !== 'function') {
|
|
36
|
+
console.warn('[pod6] bad fn for ' + JSON.stringify(node, null, 2));
|
|
37
|
+
}
|
|
38
|
+
return reversed[ruleIndex].fn(node, context, interator, defaultFn);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// not found rule
|
|
42
|
+
const newNode = { ...node };
|
|
43
|
+
if (newNode.hasOwnProperty('content')) {
|
|
44
|
+
return interator(newNode.content, context);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
interator.rules = rules;
|
|
49
|
+
return interator;
|
|
50
|
+
}
|
|
51
|
+
exports.default = thisFunc;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Writer from '../writer';
|
|
2
|
+
/**
|
|
3
|
+
sample | type | name
|
|
4
|
+
----------------------------------
|
|
5
|
+
'C<>',
|
|
6
|
+
'C:fcode' fcode C
|
|
7
|
+
'<>' fcode any name, or /.+/
|
|
8
|
+
'name' block 'name'
|
|
9
|
+
':blankline' blankline any name, or /.+/
|
|
10
|
+
'C:fcode' fcode C
|
|
11
|
+
'*:*', '*' any type any name
|
|
12
|
+
|
|
13
|
+
*/
|
|
14
|
+
export interface RuleHandler<T = any> {
|
|
15
|
+
(writer: Writer, processor: any): (node: T, ctx: any, interator: any) => void;
|
|
16
|
+
}
|
|
17
|
+
export declare type RuleObject = {
|
|
18
|
+
[name: string]: RuleHandler;
|
|
19
|
+
};
|
|
20
|
+
export declare type Filter = {
|
|
21
|
+
name: string;
|
|
22
|
+
type?: string;
|
|
23
|
+
} | {
|
|
24
|
+
name?: any;
|
|
25
|
+
type: 'block' | any;
|
|
26
|
+
} | {};
|
|
27
|
+
export interface GetQuery {
|
|
28
|
+
(k: string): Filter;
|
|
29
|
+
}
|
|
30
|
+
export declare const makePlug: (k: string) => {};
|
|
31
|
+
export declare function is(query: any, node: any): boolean;
|
|
32
|
+
export interface Rule {
|
|
33
|
+
(): {};
|
|
34
|
+
isFor(): void;
|
|
35
|
+
fn(): void;
|
|
36
|
+
}
|
|
37
|
+
export declare const makeRule: (query: any, fn: any) => {
|
|
38
|
+
(): void;
|
|
39
|
+
rule: any;
|
|
40
|
+
isFor: (node: any) => boolean;
|
|
41
|
+
fn: any;
|
|
42
|
+
};
|
|
43
|
+
interface MakeRulesArray {
|
|
44
|
+
(key: RuleObject | Array<RuleObject>, fn?: Function): Array<any>;
|
|
45
|
+
}
|
|
46
|
+
export declare const makeRulesArray: MakeRulesArray;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeRulesArray = exports.makeRule = exports.is = exports.makePlug = void 0;
|
|
4
|
+
const getQuery = function (k) {
|
|
5
|
+
if (k === '*:*' || k === '*') {
|
|
6
|
+
return {};
|
|
7
|
+
}
|
|
8
|
+
if (k === '<>') {
|
|
9
|
+
return { type: 'fcode' };
|
|
10
|
+
}
|
|
11
|
+
// try to split 'name:type'
|
|
12
|
+
let [name, type] = k.split(':');
|
|
13
|
+
if (name && type) {
|
|
14
|
+
return { name, type };
|
|
15
|
+
}
|
|
16
|
+
if (!name) {
|
|
17
|
+
return { type };
|
|
18
|
+
}
|
|
19
|
+
if (!type) {
|
|
20
|
+
// check C<>
|
|
21
|
+
const re = name.match(/(.+)\<\>/);
|
|
22
|
+
if (re) {
|
|
23
|
+
return { type: 'fcode', name: re[1] };
|
|
24
|
+
}
|
|
25
|
+
return { name, type: 'block' };
|
|
26
|
+
}
|
|
27
|
+
return { name, type };
|
|
28
|
+
};
|
|
29
|
+
const makePlug = (k) => {
|
|
30
|
+
const res = getQuery(k);
|
|
31
|
+
// @ts-ignores
|
|
32
|
+
let { name, type } = res;
|
|
33
|
+
if (type && type === '*') {
|
|
34
|
+
return { name };
|
|
35
|
+
}
|
|
36
|
+
if (name && name === '*') {
|
|
37
|
+
return { type };
|
|
38
|
+
}
|
|
39
|
+
return res;
|
|
40
|
+
};
|
|
41
|
+
exports.makePlug = makePlug;
|
|
42
|
+
// is - check if node have handler
|
|
43
|
+
function is(query, node) {
|
|
44
|
+
function isEmpty(obj) {
|
|
45
|
+
for (var prop in obj) {
|
|
46
|
+
if (obj.hasOwnProperty(prop))
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
// check if query empty
|
|
52
|
+
if (isEmpty(query))
|
|
53
|
+
return true;
|
|
54
|
+
for (var prop in query) {
|
|
55
|
+
if (query === undefined) {
|
|
56
|
+
console.warn('[pod6] undefined key!!!');
|
|
57
|
+
}
|
|
58
|
+
if (query.hasOwnProperty(prop)) {
|
|
59
|
+
if (node === undefined) {
|
|
60
|
+
console.warn('[pod6] undefined node!!!');
|
|
61
|
+
}
|
|
62
|
+
if (node && node.hasOwnProperty(prop)) {
|
|
63
|
+
if (query[prop] !== node[prop]) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
exports.is = is;
|
|
75
|
+
const makeRule = (query, fn) => {
|
|
76
|
+
rule.rule = query;
|
|
77
|
+
rule.isFor = isFor;
|
|
78
|
+
rule.fn = fn;
|
|
79
|
+
return rule;
|
|
80
|
+
function rule() { }
|
|
81
|
+
function isFor(node) {
|
|
82
|
+
if ('string' === typeof node)
|
|
83
|
+
return false;
|
|
84
|
+
return is(query, node);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
exports.makeRule = makeRule;
|
|
88
|
+
const makeRulesArray = (key, fn) => {
|
|
89
|
+
if (key instanceof Array) {
|
|
90
|
+
return key.reduce((acc, item) => { acc.push(...(0, exports.makeRulesArray)(item)); return acc; }, []);
|
|
91
|
+
}
|
|
92
|
+
if (key instanceof Object) {
|
|
93
|
+
let rules = [];
|
|
94
|
+
for (var prop in key) {
|
|
95
|
+
if (key.hasOwnProperty(prop)) {
|
|
96
|
+
rules.push((0, exports.makeRule)((0, exports.makePlug)(prop), key[prop]));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return rules;
|
|
100
|
+
}
|
|
101
|
+
return [(0, exports.makeRule)((0, exports.makePlug)(key), fn)];
|
|
102
|
+
};
|
|
103
|
+
exports.makeRulesArray = makeRulesArray;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const makePlug = require('./makeQuery').makePlug;
|
|
2
|
+
const fixtures = [
|
|
3
|
+
'name:block', {
|
|
4
|
+
"name": "name",
|
|
5
|
+
"type": "block"
|
|
6
|
+
},
|
|
7
|
+
'C<>', {
|
|
8
|
+
'name': 'C',
|
|
9
|
+
'type': 'fcode'
|
|
10
|
+
},
|
|
11
|
+
':blankline', {
|
|
12
|
+
'type': 'blankline'
|
|
13
|
+
},
|
|
14
|
+
'*:block', {
|
|
15
|
+
'type': 'block',
|
|
16
|
+
},
|
|
17
|
+
'*:*', {},
|
|
18
|
+
'*', {},
|
|
19
|
+
'<>', {
|
|
20
|
+
'type': 'fcode'
|
|
21
|
+
},
|
|
22
|
+
'name', {
|
|
23
|
+
'name': 'name',
|
|
24
|
+
'type': 'block'
|
|
25
|
+
},
|
|
26
|
+
'Special<>', {
|
|
27
|
+
'name': 'Special',
|
|
28
|
+
'type': 'fcode'
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
const pairs = fixtures.reduce(function (result, value, index, array) {
|
|
32
|
+
if (index % 2 === 0)
|
|
33
|
+
result.push(array.slice(index, index + 2));
|
|
34
|
+
return result;
|
|
35
|
+
}, []);
|
|
36
|
+
const log = (a) => console.log(JSON.stringify(a, null, 2));
|
|
37
|
+
describe("run make query helpers tests", () => {
|
|
38
|
+
pairs.map(([k, v], idx) => {
|
|
39
|
+
test(`test ${idx}`, () => expect(makePlug(k)).toEqual(v));
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeTransformer = exports.isSemanticBlock = exports.isNamedBlock = void 0;
|
|
4
|
+
const { makeRule, makePlug } = require('./makeQuery');
|
|
5
|
+
// the following names: MyBlock, myBlock are use for extending pod6
|
|
6
|
+
function isNamedBlock(name) {
|
|
7
|
+
return (name
|
|
8
|
+
&&
|
|
9
|
+
name !== name.toLowerCase()
|
|
10
|
+
&&
|
|
11
|
+
name !== name.toUpperCase());
|
|
12
|
+
}
|
|
13
|
+
exports.isNamedBlock = isNamedBlock;
|
|
14
|
+
// skip warnings for semantic blocks
|
|
15
|
+
function isSemanticBlock(node) {
|
|
16
|
+
const name = node.name || '';
|
|
17
|
+
const isTypeBlock = (node.type || '') === 'block';
|
|
18
|
+
return isTypeBlock && name === name.toUpperCase();
|
|
19
|
+
}
|
|
20
|
+
exports.isSemanticBlock = isSemanticBlock;
|
|
21
|
+
function flattenDeep(arr) {
|
|
22
|
+
return arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
|
|
23
|
+
}
|
|
24
|
+
const makeTransformer = (rule) => {
|
|
25
|
+
let rules = [];
|
|
26
|
+
function use(key, fn) {
|
|
27
|
+
if (key instanceof Array) {
|
|
28
|
+
console.warn(`[pod] Unsupported param for ${key}`);
|
|
29
|
+
}
|
|
30
|
+
if (key instanceof Object) {
|
|
31
|
+
for (var prop in key) {
|
|
32
|
+
if (key.hasOwnProperty(prop)) {
|
|
33
|
+
use(prop, key[prop]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
rules.push(makeRule(makePlug(key), fn));
|
|
39
|
+
}
|
|
40
|
+
function visiter(node, context = {}) {
|
|
41
|
+
if (node instanceof Array) {
|
|
42
|
+
return flattenDeep(node.map(item => visiter(item, context)));
|
|
43
|
+
}
|
|
44
|
+
if ('string' === typeof node) {
|
|
45
|
+
// convert string to lex node with type
|
|
46
|
+
//return visiter({type:'text', value:node}, context)
|
|
47
|
+
return node;
|
|
48
|
+
}
|
|
49
|
+
// get first rule for this node
|
|
50
|
+
const reversed = rules.slice();
|
|
51
|
+
reversed.reverse();
|
|
52
|
+
const ruleIndex = reversed.findIndex(rule => rule.isFor(node));
|
|
53
|
+
if (ruleIndex !== -1) {
|
|
54
|
+
return reversed[ruleIndex].fn(node, context, visiter);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
// not found rule
|
|
58
|
+
const newNode = { ...node };
|
|
59
|
+
if (newNode.hasOwnProperty('content')) {
|
|
60
|
+
newNode.content = visiter(newNode.content, context);
|
|
61
|
+
}
|
|
62
|
+
return newNode;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
use(rule);
|
|
66
|
+
visiter.rules = rules;
|
|
67
|
+
return visiter;
|
|
68
|
+
};
|
|
69
|
+
exports.makeTransformer = makeTransformer;
|
|
70
|
+
exports.default = exports.makeTransformer;
|
package/lib/index.d.ts
CHANGED
|
@@ -5,7 +5,12 @@ export * from './types';
|
|
|
5
5
|
export * from './blocks-helpers';
|
|
6
6
|
export * from './query-helpers';
|
|
7
7
|
export * from './ast-helpers';
|
|
8
|
+
export * from './helpers/handlers';
|
|
8
9
|
export { makeInterator } from './ast-inerator';
|
|
10
|
+
export { makeTransformer, isNamedBlock, isSemanticBlock } from './helpers/makeTransformer';
|
|
11
|
+
export { toAny } from './exportAny';
|
|
12
|
+
export { makeAttrs } from './helpers/config';
|
|
13
|
+
export { pluginCleanLocation } from './plugin-clean-location';
|
|
9
14
|
export declare function toAst(): {};
|
|
10
15
|
export declare type SchemaValidationError = ErrorObject<string, Record<string, any>>;
|
|
11
16
|
export declare function getTextContentFromNode(node: PodNode): string;
|
|
@@ -16,3 +21,59 @@ export declare function isValidateError(result: SchemaValidationError[], src: an
|
|
|
16
21
|
export interface Test {
|
|
17
22
|
ters: string;
|
|
18
23
|
}
|
|
24
|
+
export interface nBlock {
|
|
25
|
+
"type": "block";
|
|
26
|
+
name: "pod";
|
|
27
|
+
content: AST;
|
|
28
|
+
margin: string;
|
|
29
|
+
}
|
|
30
|
+
export interface nCode {
|
|
31
|
+
"type": "code";
|
|
32
|
+
"content": [];
|
|
33
|
+
"name": string;
|
|
34
|
+
"margin": string;
|
|
35
|
+
"config": [];
|
|
36
|
+
"text": string;
|
|
37
|
+
}
|
|
38
|
+
export interface nText {
|
|
39
|
+
"type": "text";
|
|
40
|
+
"content": [];
|
|
41
|
+
"name": string;
|
|
42
|
+
"margin": string;
|
|
43
|
+
"config": [];
|
|
44
|
+
'value': string;
|
|
45
|
+
}
|
|
46
|
+
export interface nPara {
|
|
47
|
+
"type": "para";
|
|
48
|
+
"content": [];
|
|
49
|
+
"name": string;
|
|
50
|
+
"margin": string;
|
|
51
|
+
"config": [];
|
|
52
|
+
}
|
|
53
|
+
export interface nVerbatim {
|
|
54
|
+
type: 'verbatim';
|
|
55
|
+
value: string;
|
|
56
|
+
"margin": string;
|
|
57
|
+
"text": string;
|
|
58
|
+
}
|
|
59
|
+
export declare type Node = nBlock | nCode | nText | nPara | nVerbatim;
|
|
60
|
+
export declare type AST = Array<Node>;
|
|
61
|
+
export declare type ParserPlugin = (opt: {
|
|
62
|
+
skipChain: number;
|
|
63
|
+
podMode: number;
|
|
64
|
+
}) => (param: AST) => AST;
|
|
65
|
+
declare function makeTree(): {
|
|
66
|
+
(): void;
|
|
67
|
+
use: (plugin: ParserPlugin) => any;
|
|
68
|
+
parse: (src: string, opt?: {
|
|
69
|
+
skipChain: number;
|
|
70
|
+
podMode: number;
|
|
71
|
+
}) => AST;
|
|
72
|
+
};
|
|
73
|
+
export { makeTree as toTree };
|
|
74
|
+
declare const parse: Function;
|
|
75
|
+
export { parse as parse };
|
|
76
|
+
export { default as toHtml } from './exportHtml';
|
|
77
|
+
export { default as Writer } from './writer';
|
|
78
|
+
declare const VERSION: any;
|
|
79
|
+
export { VERSION as version };
|