nsp-server-pages 0.2.0 → 0.2.1

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/README.md CHANGED
@@ -11,7 +11,7 @@ NSP JavaScript Server Pages for Node.js
11
11
  - `${ f:h("tag") }` - custom taglib static function call
12
12
  - `<ns:tag attr="${ expression }"/>` - custom taglib action tag
13
13
  - `<%-- comments --%>` - comments in JSP just ignored
14
- - See TypeScript declaration [index.d.ts](https://github.com/kawanet/nsp-server-pages/blob/main/index.d.ts) for API detail.
14
+ - See [TypeScript declaration files](https://github.com/kawanet/nsp-server-pages/tree/main/types/) for API detail.
15
15
 
16
16
  ## SYNOPSIS
17
17
 
@@ -156,6 +156,7 @@ app.use("/", async (req, res, next) => {
156
156
  - https://github.com/kawanet/nsp-server-pages
157
157
  - https://github.com/kawanet/nsp-jstl-taglib
158
158
  - https://github.com/kawanet/nsp-struts1-taglib
159
+ - https://github.com/kawanet/nsp-seasar2-taglib
159
160
  - https://github.com/apache/tomcat
160
161
 
161
162
  ## LICENSE
@@ -53,9 +53,9 @@ class Attr {
53
53
  _toJS(option) {
54
54
  const { app } = this;
55
55
  const { indent } = app.options;
56
- const spaces = +indent ? " ".repeat(+indent) : (indent ?? "");
57
- const currentLF = option?.LF ?? "\n";
58
- const nextLF = currentLF + spaces;
56
+ const SP = option?.SP ?? (("string" === typeof indent) ? indent : (+indent ? " ".repeat(+indent) : ""));
57
+ const LF = option?.LF ?? "\n";
58
+ const nextLF = LF + SP;
59
59
  const keys = this.keys();
60
60
  const items = keys.map(key => {
61
61
  if (!isSafeKey(key)) {
@@ -69,7 +69,7 @@ class Attr {
69
69
  return 'null';
70
70
  const js = items.join(`,${nextLF}`);
71
71
  const trailingComma = (keys.length > 1) ? "," : "";
72
- return `{${nextLF}${js}${trailingComma}${currentLF}}`;
72
+ return `{${nextLF}${js}${trailingComma}${LF}}`;
73
73
  }
74
74
  }
75
75
  exports.Attr = Attr;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Tag = void 0;
4
- const text_js_1 = require("./text.js");
5
4
  const attr_js_1 = require("./attr.js");
5
+ const text_js_1 = require("./text.js");
6
6
  const emptyText = {
7
7
  '""': true,
8
8
  "''": true,
@@ -34,13 +34,14 @@ class Tag {
34
34
  getBodyJS(option) {
35
35
  const { app } = this;
36
36
  const { indent, trimSpaces, vName } = app.options;
37
- const spaces = +indent ? " ".repeat(+indent) : (indent ?? "");
38
- const currentLF = option?.LF ?? "\n";
39
- const nextLF = currentLF + spaces;
37
+ const SP = option?.SP ?? (("string" === typeof indent) ? indent : (+indent ? " ".repeat(+indent) : ""));
38
+ const LF = option?.LF ?? "\n";
39
+ const nextLF = LF + SP;
40
+ const nextOption = { SP, LF: nextLF };
40
41
  const { children } = this;
41
42
  const args = children.map(item => {
42
43
  if (isTranspiler(item)) {
43
- return item.toJS({ LF: nextLF });
44
+ return item.toJS(nextOption);
44
45
  }
45
46
  else if (!/\S/.test(item)) {
46
47
  // item with only whitespace
@@ -53,7 +54,7 @@ class Tag {
53
54
  item = item.replace(/^[ \t]+/s, " ");
54
55
  item = item.replace(/[ \t]+$/s, " ");
55
56
  }
56
- let js = new text_js_1.Text(app, item).toJS({ LF: nextLF });
57
+ let js = new text_js_1.Text(app, item).toJS(nextOption);
57
58
  if (/\(.+?\)|\$\{.+?}/s.test(js)) {
58
59
  js = `${vName} => ${js}`; // array function
59
60
  }
@@ -75,11 +76,11 @@ class Tag {
75
76
  args[idx] += ",";
76
77
  }
77
78
  else if (idx === last && isComment) {
78
- args[idx] += currentLF;
79
+ args[idx] += LF;
79
80
  }
80
81
  });
81
82
  const bodyL = /^`\n/s.test(args.at(0)) ? "" : nextLF;
82
- const bodyR = /(\n`|[)\s])$/s.test(args.at(-1)) ? "" : currentLF;
83
+ const bodyR = /(\n`|[)\s])$/s.test(args.at(-1)) ? "" : LF;
83
84
  return bodyL + args.join(nextLF) + bodyR;
84
85
  }
85
86
  /**
@@ -98,13 +99,13 @@ class Tag {
98
99
  const commentJS = this.getCommentJS(option);
99
100
  const attr = new attr_js_1.Attr(app, this.src);
100
101
  const body = this.getBodyJS(option);
101
- const spaces = +indent ? " ".repeat(+indent) : (indent ?? "");
102
- const currentLF = option?.LF ?? "\n";
103
- const nextLF = currentLF + spaces;
104
- const tagOption = { LF: (body ? nextLF : currentLF) };
102
+ const SP = option?.SP ?? (("string" === typeof indent) ? indent : (+indent ? " ".repeat(+indent) : ""));
103
+ const LF = option?.LF ?? "\n";
104
+ const nextLF = LF + SP;
105
+ const nextOption = { SP, LF: (body ? nextLF : LF) };
105
106
  const type = `parse.tag.${tagName}`;
106
- const def = { app, name: tagName, attr, body, LF: currentLF, nextLF };
107
- const tagJS = app.process(type, def) ?? this.getTagJS(def, tagOption);
107
+ const def = { app, name: tagName, attr, body, LF: LF, nextLF };
108
+ const tagJS = app.process(type, def) ?? this.getTagJS(def, nextOption);
108
109
  return commentJS ? commentJS + tagJS : tagJS;
109
110
  }
110
111
  getCommentJS(option) {
@@ -120,10 +121,13 @@ class Tag {
120
121
  const attrRaw = def.attr.toJS(option);
121
122
  // transpile attributes to array function if they include variables
122
123
  const hasVars = /\(.+?\)|\$\{.+?}/s.test(attrRaw);
123
- const attrJS = hasVars ? `${vName} => (${attrRaw})` : attrRaw;
124
+ const attrArg = hasVars ? `, ${vName} => (${attrRaw})` : `, ${attrRaw}`;
124
125
  const nameJS = JSON.stringify(tagName);
125
126
  const hasAttr = /:/.test(attrRaw);
126
- const restJS = def.body ? (`, ${attrJS}, ${def.body}`) : (hasAttr ? `, ${attrJS}` : "");
127
+ let bodyArg = def.body;
128
+ if (bodyArg)
129
+ bodyArg = ((/^\n/.test(bodyArg)) ? "," : ", ") + bodyArg;
130
+ const restJS = bodyArg ? (attrArg + bodyArg) : (hasAttr ? attrArg : "");
127
131
  return `${nspName}.tag(${nameJS}${restJS})`;
128
132
  }
129
133
  }
@@ -50,9 +50,9 @@ export class Attr {
50
50
  _toJS(option) {
51
51
  const { app } = this;
52
52
  const { indent } = app.options;
53
- const spaces = +indent ? " ".repeat(+indent) : (indent ?? "");
54
- const currentLF = option?.LF ?? "\n";
55
- const nextLF = currentLF + spaces;
53
+ const SP = option?.SP ?? (("string" === typeof indent) ? indent : (+indent ? " ".repeat(+indent) : ""));
54
+ const LF = option?.LF ?? "\n";
55
+ const nextLF = LF + SP;
56
56
  const keys = this.keys();
57
57
  const items = keys.map(key => {
58
58
  if (!isSafeKey(key)) {
@@ -66,7 +66,7 @@ export class Attr {
66
66
  return 'null';
67
67
  const js = items.join(`,${nextLF}`);
68
68
  const trailingComma = (keys.length > 1) ? "," : "";
69
- return `{${nextLF}${js}${trailingComma}${currentLF}}`;
69
+ return `{${nextLF}${js}${trailingComma}${LF}}`;
70
70
  }
71
71
  }
72
72
  const UNESCAPE = {
@@ -1,5 +1,5 @@
1
- import { Text } from "./text.js";
2
1
  import { Attr } from "./attr.js";
2
+ import { Text } from "./text.js";
3
3
  const emptyText = {
4
4
  '""': true,
5
5
  "''": true,
@@ -31,13 +31,14 @@ export class Tag {
31
31
  getBodyJS(option) {
32
32
  const { app } = this;
33
33
  const { indent, trimSpaces, vName } = app.options;
34
- const spaces = +indent ? " ".repeat(+indent) : (indent ?? "");
35
- const currentLF = option?.LF ?? "\n";
36
- const nextLF = currentLF + spaces;
34
+ const SP = option?.SP ?? (("string" === typeof indent) ? indent : (+indent ? " ".repeat(+indent) : ""));
35
+ const LF = option?.LF ?? "\n";
36
+ const nextLF = LF + SP;
37
+ const nextOption = { SP, LF: nextLF };
37
38
  const { children } = this;
38
39
  const args = children.map(item => {
39
40
  if (isTranspiler(item)) {
40
- return item.toJS({ LF: nextLF });
41
+ return item.toJS(nextOption);
41
42
  }
42
43
  else if (!/\S/.test(item)) {
43
44
  // item with only whitespace
@@ -50,7 +51,7 @@ export class Tag {
50
51
  item = item.replace(/^[ \t]+/s, " ");
51
52
  item = item.replace(/[ \t]+$/s, " ");
52
53
  }
53
- let js = new Text(app, item).toJS({ LF: nextLF });
54
+ let js = new Text(app, item).toJS(nextOption);
54
55
  if (/\(.+?\)|\$\{.+?}/s.test(js)) {
55
56
  js = `${vName} => ${js}`; // array function
56
57
  }
@@ -72,11 +73,11 @@ export class Tag {
72
73
  args[idx] += ",";
73
74
  }
74
75
  else if (idx === last && isComment) {
75
- args[idx] += currentLF;
76
+ args[idx] += LF;
76
77
  }
77
78
  });
78
79
  const bodyL = /^`\n/s.test(args.at(0)) ? "" : nextLF;
79
- const bodyR = /(\n`|[)\s])$/s.test(args.at(-1)) ? "" : currentLF;
80
+ const bodyR = /(\n`|[)\s])$/s.test(args.at(-1)) ? "" : LF;
80
81
  return bodyL + args.join(nextLF) + bodyR;
81
82
  }
82
83
  /**
@@ -95,13 +96,13 @@ export class Tag {
95
96
  const commentJS = this.getCommentJS(option);
96
97
  const attr = new Attr(app, this.src);
97
98
  const body = this.getBodyJS(option);
98
- const spaces = +indent ? " ".repeat(+indent) : (indent ?? "");
99
- const currentLF = option?.LF ?? "\n";
100
- const nextLF = currentLF + spaces;
101
- const tagOption = { LF: (body ? nextLF : currentLF) };
99
+ const SP = option?.SP ?? (("string" === typeof indent) ? indent : (+indent ? " ".repeat(+indent) : ""));
100
+ const LF = option?.LF ?? "\n";
101
+ const nextLF = LF + SP;
102
+ const nextOption = { SP, LF: (body ? nextLF : LF) };
102
103
  const type = `parse.tag.${tagName}`;
103
- const def = { app, name: tagName, attr, body, LF: currentLF, nextLF };
104
- const tagJS = app.process(type, def) ?? this.getTagJS(def, tagOption);
104
+ const def = { app, name: tagName, attr, body, LF: LF, nextLF };
105
+ const tagJS = app.process(type, def) ?? this.getTagJS(def, nextOption);
105
106
  return commentJS ? commentJS + tagJS : tagJS;
106
107
  }
107
108
  getCommentJS(option) {
@@ -117,10 +118,13 @@ export class Tag {
117
118
  const attrRaw = def.attr.toJS(option);
118
119
  // transpile attributes to array function if they include variables
119
120
  const hasVars = /\(.+?\)|\$\{.+?}/s.test(attrRaw);
120
- const attrJS = hasVars ? `${vName} => (${attrRaw})` : attrRaw;
121
+ const attrArg = hasVars ? `, ${vName} => (${attrRaw})` : `, ${attrRaw}`;
121
122
  const nameJS = JSON.stringify(tagName);
122
123
  const hasAttr = /:/.test(attrRaw);
123
- const restJS = def.body ? (`, ${attrJS}, ${def.body}`) : (hasAttr ? `, ${attrJS}` : "");
124
+ let bodyArg = def.body;
125
+ if (bodyArg)
126
+ bodyArg = ((/^\n/.test(bodyArg)) ? "," : ", ") + bodyArg;
127
+ const restJS = bodyArg ? (attrArg + bodyArg) : (hasAttr ? attrArg : "");
124
128
  return `${nspName}.tag(${nameJS}${restJS})`;
125
129
  }
126
130
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nsp-server-pages",
3
3
  "description": "NSP JavaScript Server Pages for Node.js",
4
- "version": "0.2.0",
4
+ "version": "0.2.1",
5
5
  "author": "@kawanet",
6
6
  "bugs": {
7
7
  "url": "https://github.com/kawanet/nsp-server-pages/issues"
@@ -13,7 +13,7 @@
13
13
  "devDependencies": {
14
14
  "@rollup/plugin-node-resolve": "^15.2.1",
15
15
  "@types/mocha": "^10.0.1",
16
- "@types/node": "^20.5.6",
16
+ "@types/node": "^20.5.7",
17
17
  "mocha": "^10.2.0",
18
18
  "typescript": "^5.2.2"
19
19
  },
package/types/index.d.ts CHANGED
@@ -198,6 +198,7 @@ export declare namespace NSP {
198
198
 
199
199
  interface ToJSOption {
200
200
  LF?: string;
201
+ SP?: string;
201
202
  }
202
203
 
203
204
  interface Transpiler {