@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.
Files changed (54) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +1 -1
  3. package/lib/ast-helpers.d.ts +1 -0
  4. package/lib/ast-helpers.js +3 -2
  5. package/lib/ast-inerator.js +2 -2
  6. package/lib/exportAny.d.ts +19 -0
  7. package/lib/exportAny.js +64 -0
  8. package/lib/exportHtml.d.ts +15 -0
  9. package/lib/exportHtml.js +277 -0
  10. package/lib/grammar.d.ts +14 -0
  11. package/lib/grammar.js +6395 -0
  12. package/lib/grammarfc.d.ts +14 -0
  13. package/lib/grammarfc.js +3362 -0
  14. package/lib/helpers/config.d.ts +12 -0
  15. package/lib/helpers/config.js +51 -0
  16. package/lib/helpers/handlers.d.ts +47 -0
  17. package/lib/helpers/handlers.js +119 -0
  18. package/lib/helpers/makeInterator.d.ts +5 -0
  19. package/lib/helpers/makeInterator.js +51 -0
  20. package/lib/helpers/makeQuery.d.ts +47 -0
  21. package/lib/helpers/makeQuery.js +103 -0
  22. package/lib/helpers/makeQuery.test.d.ts +1 -0
  23. package/lib/helpers/makeQuery.test.js +41 -0
  24. package/lib/helpers/makeTransformer.d.ts +7 -0
  25. package/lib/helpers/makeTransformer.js +70 -0
  26. package/lib/index.d.ts +61 -0
  27. package/lib/index.js +71 -1
  28. package/lib/plugin-clean-location.d.ts +2 -0
  29. package/lib/plugin-clean-location.js +23 -0
  30. package/lib/plugin-defn-fill-term.d.ts +2 -0
  31. package/lib/plugin-defn-fill-term.js +52 -0
  32. package/lib/plugin-formatting-codes.d.ts +3 -0
  33. package/lib/plugin-formatting-codes.js +48 -0
  34. package/lib/plugin-group-defn.d.ts +2 -0
  35. package/lib/plugin-group-defn.js +73 -0
  36. package/lib/plugin-group-items.d.ts +2 -0
  37. package/lib/plugin-group-items.js +82 -0
  38. package/lib/plugin-heading.d.ts +2 -0
  39. package/lib/plugin-heading.js +32 -0
  40. package/lib/plugin-items.d.ts +2 -0
  41. package/lib/plugin-items.js +40 -0
  42. package/lib/plugin-tables.d.ts +5 -0
  43. package/lib/plugin-tables.js +131 -0
  44. package/lib/plugin-vmargin.d.ts +3 -0
  45. package/lib/plugin-vmargin.js +29 -0
  46. package/lib/query-helpers.d.ts +2 -1
  47. package/lib/query-helpers.js +2 -5
  48. package/lib/writer.d.ts +30 -0
  49. package/lib/writer.js +91 -0
  50. package/lib/writerHtml.d.ts +11 -0
  51. package/lib/writerHtml.js +33 -0
  52. package/package.json +13 -4
  53. package/lib/helpers.d.ts +0 -1
  54. package/lib/helpers.js +0 -5
package/lib/index.js CHANGED
@@ -25,7 +25,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
25
25
  return (mod && mod.__esModule) ? mod : { "default": mod };
26
26
  };
27
27
  Object.defineProperty(exports, "__esModule", { value: true });
28
- exports.isValidateError = exports.validateAst = exports.validateAstTree = exports.validatePodliteAst = exports.getTextContentFromNode = exports.toAst = exports.makeInterator = void 0;
28
+ exports.version = exports.Writer = exports.toHtml = exports.parse = exports.toTree = exports.isValidateError = exports.validateAst = exports.validateAstTree = exports.validatePodliteAst = exports.getTextContentFromNode = exports.toAst = exports.pluginCleanLocation = exports.makeAttrs = exports.toAny = exports.isSemanticBlock = exports.isNamedBlock = exports.makeTransformer = exports.makeInterator = void 0;
29
29
  const ajv_1 = __importDefault(require("ajv"));
30
30
  const pointer = __importStar(require("json-pointer"));
31
31
  const jsonShemes = __importStar(require("../schema"));
@@ -34,8 +34,29 @@ __exportStar(require("./types"), exports);
34
34
  __exportStar(require("./blocks-helpers"), exports);
35
35
  __exportStar(require("./query-helpers"), exports);
36
36
  __exportStar(require("./ast-helpers"), exports);
37
+ __exportStar(require("./helpers/handlers"), exports);
37
38
  var ast_inerator_2 = require("./ast-inerator");
38
39
  Object.defineProperty(exports, "makeInterator", { enumerable: true, get: function () { return ast_inerator_2.makeInterator; } });
40
+ var makeTransformer_1 = require("./helpers/makeTransformer");
41
+ Object.defineProperty(exports, "makeTransformer", { enumerable: true, get: function () { return makeTransformer_1.makeTransformer; } });
42
+ Object.defineProperty(exports, "isNamedBlock", { enumerable: true, get: function () { return makeTransformer_1.isNamedBlock; } });
43
+ Object.defineProperty(exports, "isSemanticBlock", { enumerable: true, get: function () { return makeTransformer_1.isSemanticBlock; } });
44
+ var exportAny_1 = require("./exportAny");
45
+ Object.defineProperty(exports, "toAny", { enumerable: true, get: function () { return exportAny_1.toAny; } });
46
+ var config_1 = require("./helpers/config");
47
+ Object.defineProperty(exports, "makeAttrs", { enumerable: true, get: function () { return config_1.makeAttrs; } });
48
+ var plugin_clean_location_1 = require("./plugin-clean-location");
49
+ Object.defineProperty(exports, "pluginCleanLocation", { enumerable: true, get: function () { return plugin_clean_location_1.pluginCleanLocation; } });
50
+ // @ts-ignore
51
+ const parser = __importStar(require("./grammar"));
52
+ const plugin_vmargin_1 = __importDefault(require("./plugin-vmargin"));
53
+ const plugin_formatting_codes_1 = __importDefault(require("./plugin-formatting-codes"));
54
+ const plugin_items_1 = __importDefault(require("./plugin-items"));
55
+ const plugin_heading_1 = __importDefault(require("./plugin-heading"));
56
+ const plugin_group_defn_1 = __importDefault(require("./plugin-group-defn"));
57
+ const plugin_group_items_1 = __importDefault(require("./plugin-group-items"));
58
+ const plugin_defn_fill_term_1 = __importDefault(require("./plugin-defn-fill-term"));
59
+ const plugin_tables_1 = __importDefault(require("./plugin-tables"));
39
60
  function toAst() {
40
61
  return {};
41
62
  }
@@ -87,3 +108,52 @@ function isValidateError(result, src) {
87
108
  return undefined;
88
109
  }
89
110
  exports.isValidateError = isValidateError;
111
+ function makeTree() {
112
+ var plugins = [];
113
+ chain.use = use;
114
+ chain.parse = parse;
115
+ chain.use(plugin_vmargin_1.default);
116
+ chain.use(plugin_items_1.default);
117
+ chain.use(plugin_heading_1.default);
118
+ chain.use(plugin_defn_fill_term_1.default);
119
+ chain.use(plugin_tables_1.default);
120
+ chain.use(plugin_formatting_codes_1.default);
121
+ // save order for the next two plugins
122
+ chain.use(plugin_group_items_1.default);
123
+ chain.use(plugin_group_defn_1.default);
124
+ return chain;
125
+ function chain() {
126
+ return;
127
+ }
128
+ function use(plugin) {
129
+ if (!["function"].includes(typeof plugin)) {
130
+ throw (plugin);
131
+ }
132
+ plugins.push(plugin);
133
+ return chain;
134
+ }
135
+ function parse(src, opt = { skipChain: 0, podMode: 1 }) {
136
+ let tree = parser.parse(src, { podMode: opt.podMode });
137
+ if (!opt.skipChain) {
138
+ for (let i = 0; i < plugins.length; i++) {
139
+ const plugin = plugins[i];
140
+ // init
141
+ const visitor = plugin(opt);
142
+ // process tree
143
+ tree = visitor(tree);
144
+ }
145
+ }
146
+ return tree;
147
+ }
148
+ }
149
+ exports.toTree = makeTree;
150
+ const parse = makeTree().parse;
151
+ exports.parse = parse;
152
+ var exportHtml_1 = require("./exportHtml");
153
+ Object.defineProperty(exports, "toHtml", { enumerable: true, get: function () { return __importDefault(exportHtml_1).default; } });
154
+ var writer_1 = require("./writer");
155
+ Object.defineProperty(exports, "Writer", { enumerable: true, get: function () { return __importDefault(writer_1).default; } });
156
+ // Cannot be `import` as it's not under TS root dir
157
+ // https://stackoverflow.com/questions/51070138/how-to-import-package-json-into-typescript-file-without-including-it-in-the-comp
158
+ const { version: VERSION } = require('../package.json');
159
+ exports.version = VERSION;
@@ -0,0 +1,2 @@
1
+ export declare const pluginCleanLocation: () => (tree: any) => any;
2
+ export default pluginCleanLocation;
@@ -0,0 +1,23 @@
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.pluginCleanLocation = void 0;
7
+ /**
8
+ * Delete location attribute
9
+ * use in test suite
10
+ */
11
+ const makeTransformer_1 = __importDefault(require("./helpers/makeTransformer"));
12
+ const pluginCleanLocation = () => (tree) => {
13
+ const transformer = (0, makeTransformer_1.default)({ '*': (node, ctx, visiter) => {
14
+ if (node.content) {
15
+ node.content = visiter(node.content, ctx, visiter);
16
+ }
17
+ delete node.location;
18
+ return node;
19
+ } });
20
+ return transformer(tree, {});
21
+ };
22
+ exports.pluginCleanLocation = pluginCleanLocation;
23
+ exports.default = exports.pluginCleanLocation;
@@ -0,0 +1,2 @@
1
+ declare const _default: () => (tree: any) => any;
2
+ export default _default;
@@ -0,0 +1,52 @@
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
+ /**
7
+ * Plugin for fill term's for defn. From S26:
8
+ * The first non-blank line of content is treated as a term being defined,
9
+ * and the remaining content is treated as the definition for the term.
10
+ */
11
+ const makeTransformer_1 = __importDefault(require("./helpers/makeTransformer"));
12
+ exports.default = () => (tree) => {
13
+ const transformer = (0, makeTransformer_1.default)({ 'defn': (node) => {
14
+ // get first non blank block
15
+ const content = node.content;
16
+ // skip all blank blocks
17
+ const count = content.length;
18
+ let newContent = [];
19
+ let isFirstParaProcessed = false;
20
+ for (let i = 0; i < count; i++) {
21
+ const item = content[i];
22
+ if (item.type === 'para' && !isFirstParaProcessed) {
23
+ // get text content
24
+ const textNode = item.content[0];
25
+ const text = textNode.value;
26
+ // split text by lines
27
+ // get first line
28
+ const splited = text.split('\n');
29
+ const term = splited.shift();
30
+ const newTermPara = {
31
+ type: 'para',
32
+ name: 'term',
33
+ text: term,
34
+ content: [{ type: 'text', value: term }]
35
+ };
36
+ newContent.push(newTermPara);
37
+ if (!(splited.length == 1 && splited[0] === '')) {
38
+ textNode.value = splited.join('\n');
39
+ item.text = textNode.value;
40
+ newContent.push(item);
41
+ }
42
+ isFirstParaProcessed = true;
43
+ }
44
+ else {
45
+ newContent.push(item);
46
+ }
47
+ }
48
+ node.content = newContent;
49
+ return node;
50
+ } });
51
+ return transformer(tree, {});
52
+ };
@@ -0,0 +1,3 @@
1
+ import { ParserPlugin } from './';
2
+ declare const middle: ParserPlugin;
3
+ export default middle;
@@ -0,0 +1,48 @@
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
+ var fcparser = require("./grammarfc");
7
+ const makeTransformer_1 = __importDefault(require("./helpers/makeTransformer"));
8
+ const makeTransformer_2 = require("./helpers/makeTransformer");
9
+ const config_1 = __importDefault(require("./helpers/config"));
10
+ const middle = () => (tree) => {
11
+ const transformerBlocks = (0, makeTransformer_1.default)({
12
+ ':para': (n, ctx, visiter) => {
13
+ return (0, makeTransformer_1.default)({
14
+ ':text': (n, ctx) => { return fcparser.parse(n.value); },
15
+ ':verbatim': (n, ctx) => { return fcparser.parse(n.value); },
16
+ })(n, { ...ctx });
17
+ return n;
18
+ },
19
+ ':block': (n, ctx, visiter) => {
20
+ // only =pod may have childs blocks
21
+ if ("name" in n && n.name === 'pod')
22
+ return {
23
+ ...n,
24
+ content: visiter(n.content, ctx, visiter)
25
+ };
26
+ const conf = (0, config_1.default)(n, ctx);
27
+ const isCodeBlock = "name" in n && n.name === 'code';
28
+ const isDataBlock = "name" in n && n.name === 'data';
29
+ const allowValues = conf.getAllValues('allow');
30
+ // for code block not parse content by default
31
+ if ((isCodeBlock || isDataBlock) && allowValues.length == 0)
32
+ return n;
33
+ const allowed = allowValues.sort();
34
+ const transformer = (0, makeTransformer_1.default)({
35
+ ':verbatim': (n, ctx) => { return fcparser.parse(n.value, { allowed }); },
36
+ ':text': (n, ctx) => { return fcparser.parse(n.value, { allowed }); },
37
+ ':block': (n, ctx) => {
38
+ if ((0, makeTransformer_2.isNamedBlock)(n.name))
39
+ return n;
40
+ return { ...n, content: transformer(n.content, { ...ctx }) };
41
+ }
42
+ });
43
+ return transformer(n, { ...ctx });
44
+ },
45
+ });
46
+ return transformerBlocks(tree, {});
47
+ };
48
+ exports.default = middle;
@@ -0,0 +1,2 @@
1
+ declare const _default: () => (tree: any) => any;
2
+ export default _default;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = () => (tree) => {
4
+ const isNodesEqual = (n1, n2) => (
5
+ // if nodes items
6
+ (n1.name === 'item' || n1.name === 'defn') &&
7
+ n2.name === n1.name &&
8
+ n1['level'] === n2.level &&
9
+ isNumbered(n1) === isNumbered(n2));
10
+ const isNumbered = (node) => Boolean(node.config && node.config.numbered);
11
+ const group = (a) => {
12
+ let lastItem = {};
13
+ let result = a.reduce((a, i) => {
14
+ if (i.name === 'defn') {
15
+ if (isNodesEqual(lastItem, i)) {
16
+ // group items into arrays
17
+ a[a.length - 1].push(i);
18
+ }
19
+ else {
20
+ // group items into arrays
21
+ a.push([i]);
22
+ }
23
+ }
24
+ else {
25
+ // save not 'item' elements as is
26
+ a.push(i);
27
+ }
28
+ lastItem = i;
29
+ return a;
30
+ }, []);
31
+ // convert grouped items into object 'itemlist'
32
+ /**
33
+ * { type = 'list',
34
+ * ordered = [true, false]
35
+ * content = [ .... ]
36
+ * list = ['ordered', 'variable', 'itemized']
37
+ */
38
+ return result.map(item => {
39
+ if (item instanceof Array) {
40
+ if (item[0].name !== 'defn') {
41
+ return item;
42
+ }
43
+ // get type of list
44
+ const list = item[0].name === 'defn' ? 'variable' :
45
+ isNumbered(item[0]) ? 'ordered' : 'itemized';
46
+ // create items container
47
+ return {
48
+ type: 'list',
49
+ list,
50
+ level: 1,
51
+ content: [...item]
52
+ };
53
+ }
54
+ if (item.content && item.type !== 'fcode') {
55
+ item.content = group(item.content);
56
+ }
57
+ return item;
58
+ });
59
+ };
60
+ const visit = (node) => {
61
+ if (Array.isArray(node)) {
62
+ return group(node);
63
+ }
64
+ else {
65
+ if (node.type === 'block' && node.name !== 'table') {
66
+ node.content = group(node.content);
67
+ }
68
+ }
69
+ return node;
70
+ };
71
+ tree = visit(tree);
72
+ return tree;
73
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: () => (tree: any) => any;
2
+ export default _default;
@@ -0,0 +1,82 @@
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
+ const config_1 = __importDefault(require("./helpers/config"));
7
+ exports.default = () => (tree) => {
8
+ const isNodesEqual = (n1, n2) => (
9
+ // if nodes items
10
+ (n1.name === 'item' || n1.name === 'defn') &&
11
+ n2.name === n1.name &&
12
+ n1['level'] === n2.level &&
13
+ isNumbered(n1) === isNumbered(n2));
14
+ const isNumbered = (node) => Boolean((0, config_1.default)(node, {}).exists('numbered'));
15
+ const group = (a, level = 1) => {
16
+ const isInListMode = (a) => { return a.length > 0 && a[a.length - 1].type === 'list'; };
17
+ const getList = (a) => { if (isInListMode(a))
18
+ return a[a.length - 1]; };
19
+ const isInsertableToList = (l, i) => (i.name && i.name === 'item' && i.type && i.type === 'block'
20
+ ||
21
+ i.type === 'blankline');
22
+ let lastItem = {};
23
+ let result = a.reduce((a, i) => {
24
+ // skip items out of level
25
+ if (i.name === 'item' && i.type === 'block' && i.level < level) {
26
+ return [...a, i];
27
+ }
28
+ if (i.name === 'item' && i.type === 'block' && (!isInListMode(a)
29
+ ||
30
+ // different type of list and item
31
+ // at the same level
32
+ (i.level === level
33
+ &&
34
+ getList(a).list !== (isNumbered(i) ? 'ordered' : 'itemized')))) {
35
+ // create empty list
36
+ // list = ['ordered', 'variable', 'itemized']
37
+ a.push({
38
+ type: 'list',
39
+ level: i.level,
40
+ content: [],
41
+ list: isNumbered(i) ? 'ordered' : 'itemized'
42
+ });
43
+ }
44
+ // main logic
45
+ if (isInListMode(a)
46
+ && isInsertableToList(getList(a), i)) { // getlist and push item to them
47
+ getList(a).content.push(i);
48
+ }
49
+ else {
50
+ a.push(i);
51
+ }
52
+ return a;
53
+ }, []);
54
+ // convert grouped items into object 'itemlist'
55
+ /**
56
+ * { type = 'list',
57
+ * ordered = [true, false]
58
+ * content = [ .... ]
59
+ * list = ['ordered', 'variable', 'itemized']
60
+ */
61
+ return result.map(item => {
62
+ // process any item with content ( except formatting codes)
63
+ if (item.content && item.type !== 'fcode') {
64
+ item.content = group(item.content, item.level + 1);
65
+ }
66
+ return item;
67
+ });
68
+ };
69
+ const visit = (node) => {
70
+ if (Array.isArray(node)) {
71
+ node = group(node);
72
+ }
73
+ else {
74
+ if (node.type === 'block' && node.name !== 'table') {
75
+ node.content = group(node.content);
76
+ }
77
+ }
78
+ return node;
79
+ };
80
+ tree = visit(tree);
81
+ return tree;
82
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: () => (tree: any) => any;
2
+ export default _default;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = () => (tree) => {
4
+ const visit = (node) => {
5
+ if (Array.isArray(node)) {
6
+ node.forEach(i => { visit(i); });
7
+ }
8
+ else {
9
+ if (node && node.type === 'block') {
10
+ const matchHead = /^head(\d+)?/.exec(node.name);
11
+ if (matchHead) {
12
+ node.name = 'head';
13
+ node.level = matchHead[1] || 1;
14
+ if (node.content[0]) {
15
+ const startText = node.content[0];
16
+ if (startText.text) {
17
+ let re = /(\s*#\s*)/;
18
+ const match = re.exec(startText.text);
19
+ if (match) {
20
+ startText.text = startText.text.replace(re, '');
21
+ node.config = { ...node.config, ...{ numbered: true } };
22
+ }
23
+ }
24
+ }
25
+ }
26
+ visit(node.content);
27
+ }
28
+ }
29
+ };
30
+ visit(tree);
31
+ return tree;
32
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: () => (tree: any) => any;
2
+ export default _default;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = () => (tree) => {
4
+ const visit = (node) => {
5
+ if (Array.isArray(node)) {
6
+ node.forEach(i => { visit(i); });
7
+ }
8
+ else {
9
+ if (node && node.type === 'block') {
10
+ const matchItem = /^item(\d+)?/.exec(node.name);
11
+ if (matchItem) {
12
+ node.name = 'item';
13
+ node.level = matchItem[1] || 1;
14
+ if (node.content[0]) {
15
+ const startText = node.content[0];
16
+ if (startText.text) {
17
+ let re = /^(\s*#\s*)/;
18
+ const match = re.exec(startText.text);
19
+ if (match) {
20
+ startText.text = startText.text.replace(re, '');
21
+ if (startText.type === 'para') {
22
+ startText.content = [startText.text];
23
+ }
24
+ node.config = node.config || [];
25
+ node.config.push({
26
+ "name": "numbered",
27
+ "value": true,
28
+ "type": "boolean"
29
+ });
30
+ }
31
+ }
32
+ }
33
+ }
34
+ visit(node.content);
35
+ }
36
+ }
37
+ };
38
+ visit(tree);
39
+ return tree;
40
+ };
@@ -0,0 +1,5 @@
1
+ declare const _default: () => (tree: any) => any;
2
+ /**
3
+ * Main transforms
4
+ */
5
+ export default _default;
@@ -0,0 +1,131 @@
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
+ /**
7
+ * Plugin for fill term's for defn. From S26:
8
+ * The first non-blank line of content is treated as a term being defined,
9
+ * and the remaining content is treated as the definition for the term.
10
+ */
11
+ const makeTransformer_1 = __importDefault(require("./helpers/makeTransformer"));
12
+ function flattenDeep(arr) {
13
+ return arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
14
+ }
15
+ /**
16
+ * Helpers section
17
+ */
18
+ // run cb in symbols pair
19
+ const strbin = (str1, str2, cb) => {
20
+ let res = [];
21
+ for (let i = 0; str1.length > i; i++) {
22
+ res.push(cb(parseInt(str1[i], 10), parseInt(str2[i], 10)));
23
+ }
24
+ return res.join('');
25
+ };
26
+ // Create mask for extract columns
27
+ const makeMask = (lines, separators) => {
28
+ // calculate template length
29
+ const tmplLength = Math.max(...[...lines, ...separators].map(s => s.length));
30
+ // make bin mask for each string
31
+ const masks = lines.map((str) => {
32
+ /** make mask for each line
33
+ ' The Shoveller | Eddie Stevens | King Arthur\'s singing shovel',
34
+ '0000000011111111111110001111111111111000001111111111111111111111111111' ]
35
+ then not(mask) ... then & masks
36
+ */
37
+ // enlarge string to tmplLength
38
+ let tstr = str + " ".repeat(tmplLength - str.length);
39
+ let mask = [];
40
+ const re = /\s+[+|\s]\s/g;
41
+ let match;
42
+ while ((match = re.exec(tstr)) != null) {
43
+ const tmpMask = '1'.repeat(match.index) +
44
+ '0'.repeat(match[0].length);
45
+ mask.push(tmpMask + '1'.repeat(tmplLength - tmpMask.length));
46
+ }
47
+ return mask.reduce((a, b) => { return strbin(a, b, (i1, i2) => i1 & i2); }, '1'.repeat(tmplLength));
48
+ });
49
+ // make result mask
50
+ const inverted = masks.map(m => strbin(m, '', (i1) => i1 == 0 ? 1 : 0));
51
+ const columnTemplate = inverted.reduce((a, b) => { return strbin(a, b, (i1, i2) => i1 & i2); }, '1'.repeat(tmplLength));
52
+ return columnTemplate;
53
+ };
54
+ const extractColumnsByTemplate = (text, template) => {
55
+ const lines = flattenDeep(text.split(/\n/) // split each row by eol
56
+ .filter((str) => str.length > 0) // filter empty strings ( after slit )
57
+ );
58
+ const cols = lines.map(line => {
59
+ const re = /((1+|0+))/g;
60
+ let columns = [];
61
+ let match;
62
+ while ((match = re.exec(template)) != null) {
63
+ if (match[0][0] == 1)
64
+ continue;
65
+ const s = line.substring(match.index, match.index + match[0].length);
66
+ columns.push(s);
67
+ }
68
+ return columns;
69
+ });
70
+ let result = [];
71
+ result = cols.reduce((a, b) => {
72
+ for (let i = 0; i < b.length; i++) {
73
+ a[i] = (a[i] === undefined ? '' : a[i]) + ' ' + b[i];
74
+ }
75
+ return a;
76
+ }, []);
77
+ return result;
78
+ };
79
+ /**
80
+ * Main transforms
81
+ */
82
+ exports.default = () => (tree) => {
83
+ const transformer = (0, makeTransformer_1.default)({ 'table': (node) => {
84
+ let rows = [];
85
+ const collectValues = (row) => { rows.push(row.value); };
86
+ // collect separators for calculate max length of rows
87
+ let seps = [];
88
+ (0, makeTransformer_1.default)({
89
+ 'row:text': collectValues,
90
+ 'head:text': collectValues,
91
+ ':separator': (sep) => { seps.push(sep.text); }
92
+ })(node);
93
+ // add table-caption
94
+ // if table empty return node as is
95
+ if (!rows.length)
96
+ return node;
97
+ const splitToLines = (row) => row.split(/\n/) // split each row by eol
98
+ .filter((str) => str.length > 0); // filter empty strings ( after slit )
99
+ // split each row into lines
100
+ const lines = flattenDeep(rows.map(splitToLines));
101
+ const separators = flattenDeep(seps.map(splitToLines));
102
+ // collect text rows
103
+ let textRows = [];
104
+ (0, makeTransformer_1.default)({
105
+ 'row:text': (row) => { textRows.push(row.value); }
106
+ })(node);
107
+ const columnTemplate = makeMask(lines, separators);
108
+ const makeBlock = (name, content, ...attr) => { return { ...attr, name, type: 'block', content: Array.isArray(content) ? content : [content] }; };
109
+ // make columns
110
+ const res = (0, makeTransformer_1.default)({
111
+ 'row:text': (row) => {
112
+ if (textRows.length == 1) {
113
+ // split each text row into lines
114
+ const textRowsLines = flattenDeep([row.value].map(splitToLines));
115
+ return textRowsLines.map((rowValue) => {
116
+ const res = extractColumnsByTemplate(rowValue, columnTemplate);
117
+ return makeBlock('table_row', res.map((col) => makeBlock('table_cell', { type: 'text', value: col })));
118
+ });
119
+ }
120
+ const res = extractColumnsByTemplate(row.value, columnTemplate);
121
+ return makeBlock('table_row', res.map((col) => makeBlock('table_cell', { type: 'text', value: col })));
122
+ },
123
+ 'head:text': (head) => {
124
+ const res = extractColumnsByTemplate(head.value, columnTemplate);
125
+ return makeBlock('table_head', res.map((col) => makeBlock('table_cell', { type: 'text', value: col })));
126
+ },
127
+ })(node);
128
+ return res;
129
+ } });
130
+ return transformer(tree, {});
131
+ };
@@ -0,0 +1,3 @@
1
+ import { ParserPlugin } from './';
2
+ declare const middle: ParserPlugin;
3
+ export default middle;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const middle = () => (tree) => {
4
+ const visit_node = (node, context = {}) => {
5
+ // if( (node.type || "").match(/(code|text)/) ) {
6
+ if (node.type == 'code' || node.type == 'text') {
7
+ if (node.margin && context.margin && context.margin.length > 0 && context.margin.length == node.margin.length) {
8
+ node.type = 'para';
9
+ }
10
+ if (node.type === 'code') {
11
+ // remove parent margin
12
+ var regex = new RegExp(`^[ \t]{0,${node.margin.length}}`, 'gm');
13
+ node.text = node.text.replace(regex, '');
14
+ }
15
+ }
16
+ if (node.type === 'block') {
17
+ context.margin = typeof node.margin !== 'string' ? '' : node.margin;
18
+ let newctx = { ...context };
19
+ newctx.nest = (newctx.nest || 0) + 1;
20
+ node.content.map((n) => {
21
+ visit_node(n, context);
22
+ });
23
+ }
24
+ };
25
+ var ctx = {};
26
+ tree.forEach((n) => visit_node(n, ctx));
27
+ return tree;
28
+ };
29
+ exports.default = middle;
@@ -1,3 +1,4 @@
1
+ import { PodNode } from './types';
1
2
  declare type Query = ((par: Object) => Boolean) | any;
2
3
  /**
3
4
  * compileQuery - compile a query to a function
@@ -23,5 +24,5 @@ export declare const compileQuery: (...obj: Query[]) => (any: any) => any;
23
24
  * @param queries
24
25
  * @returns array of matched nodes
25
26
  */
26
- export declare const getFromTree: (tree: any, ...queries: string[] | any) => any;
27
+ export declare const getFromTree: (tree: any, ...queries: string[] | any) => PodNode[];
27
28
  export {};