mahabhuta 0.7.3 → 0.7.4

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/built-in.js CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
- const mahabhuta = require('./index');
4
- const globfs = require('globfs');
3
+ THIS APPEARS TO NOT BE USED
5
4
 
5
+ const mahabhuta = require('./index');
6
6
  exports.mahabhuta = new mahabhuta.MahafuncArray("mahabhuta built-in", {});
7
7
 
8
8
  class SiteVerification extends mahabhuta.CustomElement {
package/index.js CHANGED
@@ -27,10 +27,10 @@
27
27
 
28
28
  const cheerio = require('cheerio');
29
29
  const util = require('util');
30
- const fs = require('fs-extra');
31
30
 
32
31
  var configCheerio;
33
32
  var traceFlag = false;
33
+ var tracePerf = true;
34
34
 
35
35
  exports.config = function(_configCheerio) {
36
36
  configCheerio = _configCheerio;
@@ -85,11 +85,16 @@ exports.CustomElement = class CustomElement extends exports.Mahafunc {
85
85
  try {
86
86
  var elements = custom.findElements($);
87
87
  if (elements.length <= 0) return;
88
+ // Performance testing
89
+ let _start;
90
+ if (tracePerf) _start = new Date();
88
91
  for (var element of elements) {
89
92
  let replaceWith = await custom.process($(element), metadata, setDirty);
90
93
  // console.log(`CustomElement ${this.elementName} process returned ${replaceWith}`);
91
94
  $(element).replaceWith(replaceWith);
92
95
  }
96
+ // Performance testing
97
+ if (tracePerf) console.log(`CustomElement ${this.array.name} ${this.elementName} ${(new Date() - _start) / 1000} seconds`);
93
98
  } catch (e) {
94
99
  console.error(`CustomElement ${custom.elementName} Errored with ${util.inspect(e)}`);
95
100
  throw e;
@@ -112,9 +117,15 @@ exports.Munger = class Munger extends exports.Mahafunc {
112
117
  try {
113
118
  var elements = munger.findElements($);
114
119
  if (elements.length <= 0) return;
120
+ // Performance testing
121
+ let _start;
122
+ if (tracePerf) _start = new Date();
123
+ // console.log(`Munger ${this.array.name} ${this.elementName} found ${elements.length} elements`);
115
124
  for (let element of elements) {
116
125
  await munger.process($, $(element), metadata, setDirty);
117
126
  }
127
+ // Performance testing
128
+ if (tracePerf) console.log(`Munger ${this.array.name} ${this.elementName} ${(new Date() - _start) / 1000} seconds`);
118
129
  } catch (e) {
119
130
  console.error(`Munger ${munger.selector} Errored with ${util.inspect(e)}`);
120
131
  throw e;
@@ -199,24 +210,37 @@ exports.MahafuncArray = class MahafuncArray {
199
210
  if (traceFlag) console.log(`Mahabhuta FINISHED Munger ${this.name} ${mahafunc.selector}`);
200
211
  // loops.push(`... Munger ${mahafunc.selector} ${(new Date() - startProcessing) / 1000} seconds`);
201
212
  } else if (mahafunc instanceof exports.PageProcessor) {
213
+ // Performance testing
214
+ let _start;
215
+ if (tracePerf) _start = new Date();
202
216
  if (traceFlag) console.log(`Mahabhuta calling ${this.name} PageProcessor `);
203
217
  try {
204
218
  await mahafunc.process($, metadata, dirty);
205
219
  } catch (errPageProcessor) {
206
220
  throw new Error(`Mahabhuta ${this.name} caught error in PageProcessor: ${errPageProcessor.message}`);
207
221
  }
222
+ // Performance testing
223
+ if (tracePerf) console.log(`PageProcessor ${this.name} ${(new Date() - _start) / 1000} seconds`)
208
224
  // loops.push(`... PageProcessor ${(new Date() - startProcessing) / 1000} seconds`);
209
225
  } else if (mahafunc instanceof exports.MahafuncArray) {
226
+ // Performance testing
227
+ let _start;
228
+ if (tracePerf) _start = new Date();
210
229
  let results = [];
211
230
  try {
212
231
  results = await mahafunc.process($, metadata, dirty);
213
232
  } catch (errMahafuncArray) {
214
233
  throw new Error(`Mahabhuta ${this.name} caught error in MahafuncArray: ${errMahafuncArray.message}`);
215
234
  }
235
+ // Performance testing
236
+ if (tracePerf) console.log(`MahafuncArray ${this.name} ${mahafunc.name} ${(new Date() - _start) / 1000} seconds`)
216
237
 
217
238
  // results.forEach(result => { loops.push(` ... "${mahafunc.name} result" ${result} ${(new Date() - startProcessing) / 1000} seconds`); });
218
239
  // loops.push(`... MahafuncArray ${mahafunc.name} ${(new Date() - startProcessing) / 1000} seconds`);
219
240
  } else if (typeof mahafunc === 'function') {
241
+ // Performance testing
242
+ let _start;
243
+ if (tracePerf) _start = new Date();
220
244
  if (traceFlag) console.log(`Mahabhuta calling an ${this.name} "function" `);
221
245
  try {
222
246
  await new Promise((resolve, reject) => {
@@ -228,11 +252,18 @@ exports.MahafuncArray = class MahafuncArray {
228
252
  } catch (errFunction) {
229
253
  throw new Error(`Mahabhuta ${this.name} caught error in function: ${errFunction.message}`);
230
254
  }
255
+ // Performance testing
256
+ if (tracePerf) console.log(`function ${this.name} ${(new Date() - _start) / 1000} seconds`)
231
257
  // loops.push(`... MahafuncArray "function" ${(new Date() - startProcessing) / 1000} seconds`);
232
258
  } else if (Array.isArray(mahafunc)) {
259
+ // Performance testing
260
+ let _start;
261
+ if (tracePerf) _start = new Date();
233
262
  let mhObj = new exports.MahafuncArray("inline", this._config);
234
263
  mhObj.setMahafuncArray(mahafunc);
235
264
  let results = await mhObj.process($, metadata, dirty);
265
+ // Performance testing
266
+ if (tracePerf) console.log(`Array ${this.name} inline ${(new Date() - _start) / 1000} seconds`)
236
267
  // results.forEach(result => { loops.push(` ... "inline result" ${result} ${(new Date() - startProcessing) / 1000} seconds`); });
237
268
  // loops.push(`... MahafuncArray "inline array" ${(new Date() - startProcessing) / 1000} seconds`);
238
269
  } else {
@@ -263,8 +294,8 @@ exports.processAsync = async function(text, metadata, mahabhutaFuncs) {
263
294
  var $ = typeof text === 'function' ? text : exports.parse(text);
264
295
 
265
296
  const loops = [];
266
- const startProcessing = new Date();
267
297
  do {
298
+ let startProcessing = new Date();
268
299
  var mhObj;
269
300
  if (Array.isArray(mahabhutaFuncs)) {
270
301
  // console.log(`ARRAY substitution`);
@@ -279,7 +310,7 @@ exports.processAsync = async function(text, metadata, mahabhutaFuncs) {
279
310
  let results = await mhObj.process($, metadata, () => { cleanOrDirty = 'dirty'; });
280
311
 
281
312
  // results.forEach(result => { loops.push(mhObj.name +' '+ result); });
282
- // loops.push(`MAHABHUTA processAsync ${metadata.document.path} FINISH ${(new Date() - startProcessing) / 1000} seconds ${cleanOrDirty}`);
313
+ // console.log(`MAHABHUTA processAsync ${metadata.document.path} FINISH ${(new Date() - startProcessing) / 1000} seconds ${cleanOrDirty}`);
283
314
  } while (cleanOrDirty === 'dirty');
284
315
 
285
316
  // loops.forEach(l => { console.log(l); });
package/maha/metadata.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const url = require('url');
4
4
  const path = require('path');
5
+ // const util = require('util');
5
6
  const mahabhuta = require('../index');
6
7
 
7
8
  // TODO JavaScript script tags
@@ -102,10 +103,11 @@ class ExternalStylesheet extends mahabhuta.CustomElement {
102
103
 
103
104
  class RSSHeaderMeta extends mahabhuta.Munger {
104
105
  get selector() { return "rss-header-meta"; }
106
+ get elementName() { return "rss-header-meta"; }
105
107
 
106
- async process($, $link, metadata, dirty) {
108
+ async process($, $element, metadata, dirty) {
107
109
  if ($('html head').get(0)) {
108
- var href = $link.attr('href');
110
+ var href = $element.attr('href');
109
111
  if (!href) {
110
112
  throw new Error("No href in rss-header-meta tag");
111
113
  }
@@ -118,7 +120,7 @@ class RSSHeaderMeta extends mahabhuta.Munger {
118
120
  let $link = mahabhuta.parse('<link rel="alternate" type="application/rss+xml" href=""/>');
119
121
  $link('link').attr('href', href);
120
122
  $('head').append($link.html());
121
- $link.remove();
123
+ $element.remove();
122
124
  }
123
125
  }
124
126
  }
package/maha/partial.js CHANGED
@@ -1,10 +1,13 @@
1
1
 
2
2
  const mahabhuta = require('../index');
3
- const globfs = require('globfs');
4
3
  const ejs = require('ejs');
4
+ const nunjucks = require('nunjucks');
5
+ const Liquid = require('liquid');
6
+ const engine = new Liquid.Engine();
7
+ const Handlebars = require("handlebars");
5
8
  const path = require('path');
6
9
  const util = require('util');
7
- const fs = require('fs-extra');
10
+ const fs = require('fs/promises');
8
11
 
9
12
  const pluginName = "mahabhuta partials built-in";
10
13
 
@@ -35,6 +38,25 @@ class Partial extends mahabhuta.CustomElement {
35
38
  : module.exports.renderPartial(fname, d, this.options);
36
39
  }
37
40
  }
41
+
42
+ async function lookForPartial(partialDirs, partialfn) {
43
+ for (let dir of partialDirs) {
44
+ let fn2check = path.join(dir, partialfn);
45
+ let stats;
46
+ try {
47
+ stats = await fs.stat(fn2check);
48
+ } catch (err) { stats = undefined; }
49
+ if (stats.isFile()) {
50
+ return {
51
+ basedir: dir,
52
+ path: partialfn,
53
+ fullpath: fn2check
54
+ };
55
+ }
56
+ }
57
+ return undefined;
58
+ }
59
+
38
60
  module.exports.renderPartial = async function (fname, attrs, options) {
39
61
 
40
62
  let partialDirs;
@@ -48,11 +70,8 @@ module.exports.renderPartial = async function (fname, attrs, options) {
48
70
  }
49
71
 
50
72
  // console.log(`renderPartial looking for ${util.inspect(partialDirs)} ${fname}`);
51
- var partialFound = await globfs.findAsync(partialDirs, fname);
73
+ const partialFound = await lookForPartial(partialDirs, fname);
52
74
  if (!partialFound) throw new Error(`No partial found for ${fname} in ${util.inspect(partialDirs)}`);
53
- // Pick the first partial found
54
- // console.log(`renderPartial found `, partialFound);
55
- partialFound = partialFound[0];
56
75
  // console.log(`module.exports.configuration renderPartial ${partialFound}`);
57
76
  if (!partialFound) throw new Error(`No partial found for ${fname} in ${util.inspect(partialDirs)}`);
58
77
 
@@ -67,6 +86,31 @@ module.exports.renderPartial = async function (fname, attrs, options) {
67
86
  } catch (e) {
68
87
  throw new Error(`EJS rendering of ${fname} failed because of ${e}`);
69
88
  }
89
+ } else if (/\.liquid$/i.test(partialFound.fullpath)) {
90
+ try {
91
+ let partialText = await fs.readFile(partialFound.fullpath, 'utf8');
92
+ let template = await engine.parse(partialText);
93
+ let result = await template.render(attrs);
94
+ return result;
95
+ } catch (e) {
96
+ throw new Error(`Liquid rendering of ${fname} failed because of ${e}`);
97
+ }
98
+ } else if (/\.njk$/i.test(partialFound.fullpath)) {
99
+ try {
100
+ let partialText = await fs.readFile(partialFound.fullpath, 'utf8');
101
+ nunjucks.configure({ autoescape: false });
102
+ return nunjucks.renderString(partialText, attrs);
103
+ } catch (e) {
104
+ throw new Error(`Nunjucks rendering of ${fname} failed because of ${e}`);
105
+ }
106
+ } else if (/\.handlebars$/i.test(partialFound.fullpath)) {
107
+ try {
108
+ let partialText = await fs.readFile(partialFound.fullpath, 'utf8');
109
+ const template = Handlebars.compile(partialText);
110
+ return template(attrs);
111
+ } catch (e) {
112
+ throw new Error(`Handlebars rendering of ${fname} failed because of ${e}`);
113
+ }
70
114
  } /* else if (/\.literal$/i.test(partialFname)) {
71
115
  try {
72
116
  const t = literal(partialText);
@@ -101,10 +145,8 @@ module.exports.configuration = {
101
145
  partialDirs = module.exports.configuration.partialDirs;
102
146
  }
103
147
 
104
- var partialFound = await globfs.findAsync(partialDirs, fname);
148
+ const partialFound = await lookForPartial(partialDirs, fname);
105
149
  if (!partialFound) throw new Error(`No partial found for ${fname} in ${util.inspect(partialDirs)}`);
106
- // Pick the first partial found
107
- partialFound = partialFound[0];
108
150
  // console.log(`module.exports.configuration renderPartial ${partialFound}`);
109
151
  if (!partialFound) throw new Error(`No partial found for ${fname} in ${util.inspect(partialDirs)}`);
110
152
 
@@ -134,98 +176,6 @@ module.exports.configuration = {
134
176
  }
135
177
  };
136
178
 
137
- module.exports.doPartialAsync = async function (fname, attrs) {
138
-
139
- throw new Error("Deprecated");
140
-
141
- /*
142
- // find the partial
143
- // render the partial using the data provided
144
-
145
- // TBD configuration for partialDirs
146
- // console.log(`doPartialAsync ${util.inspect(fname)} ${util.inspect(module.exports.configuration.partialDirs)}`);
147
- var partialFound = await globfs.findAsync(module.exports.configuration.partialDirs, fname);
148
- // console.log(`doPartialAsync ${partialFound}`);
149
- if (!partialFound) throw new Error(`No partial directory found for ${fname}`);
150
- // Pick the first partial found
151
- partialFound = partialFound[0];
152
- if (!partialFound) throw new Error(`No partial directory found for ${fname}`);
153
-
154
- var partialFname = path.join(partialFound.basedir, partialFound.path);
155
- // console.log(`doPartialAsync before reading ${partialFname}`);
156
- var stats = await fs.stat(partialFname);
157
- if (!stats.isFile()) {
158
- throw new Error(`doPartialAsync non-file found for ${fname} - ${partialFname}`);
159
- }
160
- var partialText = await fs.readFile(partialFname, 'utf8');
161
- // console.log(`doPartialAsync after reading ${partialFname} text length=${partialText.length}`);
162
-
163
- // TODO based on file extension render through a template engine
164
- // TODO Need support for a broader spectrum of template engines
165
-
166
- // dirty();
167
- if (/\.ejs$/i.test(partialFname)) {
168
- try { return ejs.render(partialText, attrs); } catch (e) {
169
- throw new Error(`EJS rendering of ${fname} failed because of ${e}`);
170
- }
171
- } /* else if (/\.literal$/i.test(partialFname)) {
172
- try {
173
- const t = literal.compile(partialText);
174
- return t(attrs);
175
- } catch (e) {
176
- throw new Error(`Literal rendering of ${fname} failed because of ${e}`);
177
- }
178
- } * / else if (/\.html$/i.test(partialFname)) {
179
- // NOTE: The partialBody gets lost in this case
180
- return partialText;
181
- } else {
182
- throw new Error("No rendering support for ${fname}");
183
- } */
184
- };
185
-
186
-
187
- module.exports.doPartialSync = function(fname, attrs) {
188
- throw new Error("Deprecated");
189
-
190
- /* var partialFound = globfs.findSync(module.exports.configuration.partialDirs, fname);
191
- if (!partialFound) throw new Error(`No partial directory found for ${fname}`);
192
- // Pick the first partial found
193
- partialFound = partialFound[0];
194
-
195
- // console.log(`doPartialSync found ${util.inspect(partialFound)} for ${util.inspect(module.exports.configuration.partialDirs)} ${fname}`);
196
-
197
- var partialFname = path.join(partialFound.basedir, partialFound.path);
198
- // console.log(`doPartialSync before reading ${partialFname}`);
199
- var stats = fs.statSync(partialFname);
200
- if (!stats.isFile()) {
201
- throw new Error(`doPartialSync non-file found for ${fname} - ${partialFname}`);
202
- }
203
- var partialText = fs.readFileSync(partialFname, 'utf8');
204
-
205
- // TODO based on file extension render through a template engine
206
- // TODO Need support for a broader spectrum of template engines
207
-
208
- // dirty();
209
- if (/\.ejs$/i.test(partialFname)) {
210
- try { return ejs.render(partialText, attrs); } catch (e) {
211
- throw new Error(`EJS rendering of ${fname} failed because of ${e}`);
212
- }
213
- } /* else if (/\.literal$/i.test(partialFname)) {
214
- try {
215
- const t = literal.compile(partialText);
216
- return t(attrs);
217
- } catch (e) {
218
- throw new Error(`Literal rendering of ${fname} failed because of ${e}`);
219
- }
220
- } * / else if (/\.html$/i.test(partialFname)) {
221
- // NOTE: The partialBody gets lost in this case
222
- return partialText;
223
- } else {
224
- throw new Error("No rendering support for ${fname}");
225
- } */
226
- };
227
-
228
-
229
179
  module.exports.mahabhutaArray = function(options) {
230
180
  let ret = new mahabhuta.MahafuncArray(pluginName, options);
231
181
  ret.addMahafunc(new Partial());
package/package.json CHANGED
@@ -21,14 +21,15 @@
21
21
  "type": "git",
22
22
  "url": "https://github.com/akashacms/mahabhuta.git"
23
23
  },
24
- "version": "0.7.3",
24
+ "version": "0.7.4",
25
25
  "engines": {
26
26
  "node": ">=12.4"
27
27
  },
28
28
  "dependencies": {
29
- "cheerio": ">=0.22.x",
30
- "globfs": ">=0.3",
29
+ "cheerio": ">=1.0.0-rc.10",
31
30
  "ejs": "*",
32
- "fs-extra": ">=4.0.1"
31
+ "handlebars": "^4.7.6",
32
+ "liquid": "^4.1.0",
33
+ "nunjucks": "^3.2.2"
33
34
  }
34
35
  }