mahabhuta 0.7.8 → 0.7.9

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 (2) hide show
  1. package/maha/partial.js +31 -41
  2. package/package.json +6 -6
package/maha/partial.js CHANGED
@@ -2,8 +2,7 @@
2
2
  const mahabhuta = require('../index');
3
3
  const ejs = require('ejs');
4
4
  const nunjucks = require('nunjucks');
5
- const Liquid = require('liquid');
6
- const engine = new Liquid.Engine();
5
+ const { Liquid } = require('liquidjs');
7
6
  const Handlebars = require("handlebars");
8
7
  const path = require('path');
9
8
  const util = require('util');
@@ -11,19 +10,17 @@ const fs = require('fs/promises');
11
10
 
12
11
  const pluginName = "mahabhuta partials built-in";
13
12
 
14
-
15
-
16
13
  class Partial extends mahabhuta.CustomElement {
17
14
  get elementName() { return "partial"; }
18
15
  async process($element, metadata, dirty) {
19
- var data = $element.data();
20
- var fname = $element.attr("file-name");
21
- var body = $element.html();
22
-
23
- var d = {};
24
- for (var mprop in metadata) { d[mprop] = metadata[mprop]; }
25
- var data = $element.data();
26
- for (var dprop in data) { d[dprop] = data[dprop]; }
16
+ const data = $element.data();
17
+ const fname = $element.attr("file-name");
18
+ const body = $element.html();
19
+
20
+ const d = {};
21
+ for (let mprop in metadata) { d[mprop] = metadata[mprop]; }
22
+ // const data = $element.data();
23
+ for (let dprop in data) { d[dprop] = data[dprop]; }
27
24
  d["partialBody"] = body;
28
25
 
29
26
  // console.log(`mahabhuta Partial partialBody=${d["partialBody"]}`);
@@ -31,22 +28,23 @@ class Partial extends mahabhuta.CustomElement {
31
28
  if ($element.attr("dirty")) dirty();
32
29
 
33
30
  let array = this.array;
34
- // console.log(`Partial this.array ${util.inspect(array)}`);
35
31
  // console.log(`Partial array.options ${util.inspect(array.options)}`);
36
32
  return array.options.renderPartial
37
- ? array.options.renderPartial(fname, d, this.options)
38
- : module.exports.renderPartial(fname, d, this.options);
33
+ ? array.options.renderPartial(fname, d, array.options)
34
+ : module.exports.renderPartial(fname, d, array.options);
39
35
  }
40
36
  }
41
37
 
42
38
  async function lookForPartial(partialDirs, partialfn) {
39
+ // console.log(`lookForPartial checking in ${partialDirs} for ${partialfn}`);
43
40
  for (let dir of partialDirs) {
41
+ // console.log(`lookForPartial check ${dir} ${partialfn}`);
44
42
  let fn2check = path.join(dir, partialfn);
45
43
  let stats;
46
44
  try {
47
45
  stats = await fs.stat(fn2check);
48
- } catch (err) { stats = undefined; }
49
- if (stats.isFile()) {
46
+ } catch (err) { console.error(err); stats = undefined; }
47
+ if (stats && stats.isFile()) {
50
48
  return {
51
49
  basedir: dir,
52
50
  path: partialfn,
@@ -64,7 +62,14 @@ module.exports.renderPartial = async function (fname, attrs, options) {
64
62
  if (typeof options.partialDirs === 'undefined'
65
63
  || !options.partialDirs
66
64
  || options.partialDirs.length <= 0) {
67
- partialDirs = [ __dirname ];
65
+ if (typeof exports.configuration.partialDirs !== 'undefined'
66
+ && exports.configuration.partialDirs
67
+ && Array.isArray(exports.configuration.partialDirs)
68
+ && exports.configuration.partialDirs.length > 0) {
69
+ partialDirs = exports.configuration.partialDirs;
70
+ } else {
71
+ partialDirs = [ __dirname ];
72
+ }
68
73
  } else {
69
74
  partialDirs = options.partialDirs;
70
75
  }
@@ -88,10 +93,12 @@ module.exports.renderPartial = async function (fname, attrs, options) {
88
93
  }
89
94
  } else if (/\.liquid$/i.test(partialFound.fullpath)) {
90
95
  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;
96
+ let partialText = await fs.readFile(partialFound.fullpath, 'utf8');
97
+ const engine = new Liquid({
98
+ partials: partialDirs,
99
+ extname: '.liquid'
100
+ });
101
+ return await engine.parseAndRender(partialText, attrs);
95
102
  } catch (e) {
96
103
  throw new Error(`Liquid rendering of ${fname} failed because of ${e}`);
97
104
  }
@@ -111,14 +118,7 @@ module.exports.renderPartial = async function (fname, attrs, options) {
111
118
  } catch (e) {
112
119
  throw new Error(`Handlebars rendering of ${fname} failed because of ${e}`);
113
120
  }
114
- } /* else if (/\.literal$/i.test(partialFname)) {
115
- try {
116
- const t = literal(partialText);
117
- return t(attrs);
118
- } catch (e) {
119
- throw new Error(`Literal rendering of ${fname} failed because of ${e}`);
120
- }
121
- } */ else if (partialFound.fullpath.toLowerCase().endsWith('.html')
121
+ } else if (partialFound.fullpath.toLowerCase().endsWith('.html')
122
122
  || partialFound.fullpath.toLowerCase().endsWith('.xhtml')) {
123
123
  // NOTE: The partialBody gets lost in this case
124
124
  let partialText = await fs.readFile(partialFound.fullpath, 'utf8');
@@ -160,14 +160,7 @@ module.exports.configuration = {
160
160
  try { return ejs.render(partialText, attrs); } catch (e) {
161
161
  throw new Error(`EJS rendering of ${fname} failed because of ${e}`);
162
162
  }
163
- } /* else if (/\.literal$/i.test(partialFname)) {
164
- try {
165
- const t = literal(partialText);
166
- return t(attrs);
167
- } catch (e) {
168
- throw new Error(`Literal rendering of ${fname} failed because of ${e}`);
169
- }
170
- } */ else if (/\.html$/i.test(partialFname)) {
163
+ } else if (/\.html$/i.test(partialFname)) {
171
164
  // NOTE: The partialBody gets lost in this case
172
165
  return partialText;
173
166
  } else {
@@ -183,8 +176,5 @@ module.exports.mahabhutaArray = function(options) {
183
176
  };
184
177
 
185
178
 
186
- // Moot?
187
179
  exports.mahabhuta = module.exports.mahabhutaArray({});
188
180
 
189
-
190
- module.exports.mahabhuta.addMahafunc(new Partial());
package/package.json CHANGED
@@ -21,15 +21,15 @@
21
21
  "type": "git",
22
22
  "url": "https://github.com/akashacms/mahabhuta.git"
23
23
  },
24
- "version": "0.7.8",
24
+ "version": "0.7.9",
25
25
  "engines": {
26
26
  "node": ">=12.4"
27
27
  },
28
28
  "dependencies": {
29
- "cheerio": "0.22.x",
30
- "ejs": "*",
31
- "handlebars": "^4.7.6",
32
- "liquid": "^4.1.0",
33
- "nunjucks": "^3.2.2"
29
+ "cheerio": "^1.0.0-rc.10",
30
+ "ejs": "^3.1.x",
31
+ "handlebars": "^4.7.x",
32
+ "liquidjs": "^9.28.x",
33
+ "nunjucks": "^3.2.x"
34
34
  }
35
35
  }