@canopycanopycanopy/b-ber-grammar-attributes 2.0.0 → 3.0.2

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/dist/index.js +40 -114
  2. package/package.json +5 -5
package/dist/index.js CHANGED
@@ -1,55 +1,30 @@
1
1
  "use strict";
2
2
 
3
3
  var _Object$keys = require("@babel/runtime-corejs3/core-js-stable/object/keys");
4
-
5
4
  var _Object$getOwnPropertySymbols = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols");
6
-
7
5
  var _filterInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/filter");
8
-
9
6
  var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");
10
-
11
7
  var _forEachInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/for-each");
12
-
13
8
  var _Object$getOwnPropertyDescriptors = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors");
14
-
15
9
  var _Object$defineProperties = require("@babel/runtime-corejs3/core-js-stable/object/define-properties");
16
-
17
10
  var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
18
-
19
11
  var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
20
-
21
12
  _Object$defineProperty(exports, "__esModule", {
22
13
  value: true
23
14
  });
24
-
25
15
  exports.toAlias = exports.parseAttrs = exports.htmlId = exports.attributesString = exports.attributesQueryString = exports.attributesObject = exports.attributes = void 0;
26
-
27
16
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
28
-
29
17
  var _trim = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/trim"));
30
-
31
18
  var _reduce = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/reduce"));
32
-
33
19
  var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/entries"));
34
-
35
20
  var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
36
-
37
21
  var _path = _interopRequireDefault(require("path"));
38
-
39
22
  var _lodash = _interopRequireDefault(require("lodash.has"));
40
-
41
23
  var _bBerLib = require("@canopycanopycanopy/b-ber-lib");
42
-
43
- var _utils = require("@canopycanopycanopy/b-ber-lib/utils");
44
-
45
24
  var _bBerLogger = _interopRequireDefault(require("@canopycanopycanopy/b-ber-logger"));
46
-
47
25
  var _bBerShapesDirectives = require("@canopycanopycanopy/b-ber-shapes-directives");
48
-
49
26
  function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
50
-
51
27
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var _context3, _context4; var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? _forEachInstanceProperty(_context3 = ownKeys(Object(source), !0)).call(_context3, function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context4 = ownKeys(Object(source))).call(_context4, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
52
-
53
28
  //
54
29
  // Querying hierarchies gets confusing, so we're using biological taxonomic
55
30
  // rank as an analogue for classification. which is obvs less confusing...
@@ -57,57 +32,51 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
57
32
  // Class -> Order -> Family -> Genus
58
33
  // element -> block -> frontmatter -> preface
59
34
  //
60
- const _lookUpFamily = genus => _bBerShapesDirectives.FRONTMATTER_DIRECTIVES.has(genus) ? 'frontmatter' : _bBerShapesDirectives.BODYMATTER_DIRECTIVES.has(genus) ? 'bodymatter' : _bBerShapesDirectives.BACKMATTER_DIRECTIVES.has(genus) ? 'backmatter' : ''; // Determine the directive's classification and parent's type
61
35
 
36
+ const _lookUpFamily = genus => _bBerShapesDirectives.FRONTMATTER_DIRECTIVES.has(genus) ? 'frontmatter' : _bBerShapesDirectives.BODYMATTER_DIRECTIVES.has(genus) ? 'bodymatter' : _bBerShapesDirectives.BACKMATTER_DIRECTIVES.has(genus) ? 'backmatter' : '';
62
37
 
38
+ // Determine the directive's classification and parent's type
63
39
  const _directiveOrder = genus => _bBerShapesDirectives.BLOCK_DIRECTIVES.has(genus) ? 'block' : _bBerShapesDirectives.INLINE_DIRECTIVES.has(genus) ? 'inline' : _bBerShapesDirectives.MISC_DIRECTIVES.has(genus) ? 'misc' : null;
64
-
65
40
  const _requiresAltTag = genus => _bBerShapesDirectives.DIRECTIVES_REQUIRING_ALT_TAG.has(genus);
66
-
67
41
  const _isUnsupportedAttribute = (genus, attr) => {
68
42
  let key;
69
-
70
43
  if (_bBerShapesDirectives.BLOCK_DIRECTIVES.has(genus)) {
71
- // these are all containers which share the same attributes, so they're
44
+ // These are all containers which share the same attributes, so they're
72
45
  // grouped under a single property
73
46
  key = 'block';
74
47
  } else {
75
- // this will be the directive name, e.g., figure, video, etc
48
+ // This will be the directive name, e.g., figure, video, etc
76
49
  key = genus;
77
50
  }
78
-
79
51
  return !_bBerShapesDirectives.SUPPORTED_ATTRIBUTES[key] || _bBerShapesDirectives.SUPPORTED_ATTRIBUTES[key][attr] !== true;
80
52
  };
81
-
82
53
  const _applyTransforms = (k, v) => {
83
54
  switch (k) {
84
55
  case 'classes':
85
56
  return ` class="${v}"`;
86
-
87
57
  case 'epubTypes':
88
58
  return ` epub:type="${v}"`;
89
-
90
59
  case 'attrs':
91
60
  return '';
92
-
93
61
  case 'source':
94
62
  return ` src="${v}"`;
95
- // media controls enabled by default
96
63
 
64
+ // media controls enabled by default
97
65
  case 'controls':
98
66
  return v === 'no' ? '' : ` ${k}="${v}"`;
99
- // boolean attrs for audio/video elements
100
67
 
68
+ // boolean attrs for audio/video elements
101
69
  case 'autoplay':
102
70
  case 'muted':
103
71
  case 'autobuffer':
104
72
  case 'loop':
105
73
  return v === 'yes' ? ` ${k}="${k}"` : '';
106
-
107
74
  default:
108
75
  return ` ${k}="${v}"`;
109
76
  }
110
- }; // MarkdownIt returns a trimmed string of the directive's attributes, which
77
+ };
78
+
79
+ // MarkdownIt returns a trimmed string of the directive's attributes, which
111
80
  // are parsed and transformed into a string of HTML attributes
112
81
  //
113
82
  // ::: directive:id classes:"foo bar baz"
@@ -115,8 +84,6 @@ const _applyTransforms = (k, v) => {
115
84
  // -> {classes:"foo bar baz"}
116
85
  // -> class="foo bar baz"
117
86
  //
118
-
119
-
120
87
  const parseAttrs = s => {
121
88
  const out = {};
122
89
  let str = '';
@@ -125,37 +92,24 @@ const parseAttrs = s => {
125
92
  let char;
126
93
  let next;
127
94
  let key;
128
-
129
95
  for (let i = 0; i < s.length; i++) {
130
96
  char = s[i].charCodeAt(0);
131
97
  next = s[i + 1] ? s[i + 1].charCodeAt(0) : '';
132
-
133
- if (!open && char === 58
134
- /* : */
135
- ) {
98
+ if (!open && char === 58 /* : */) {
136
99
  // char is a token, we set `open` so that we don't misinterpret literals inside quotations
137
100
  open = true;
138
101
  key = str;
139
102
  str = '';
140
- delim = 32;
141
- /* */
142
-
143
- if (next === 34
144
- /* " */
145
- || next === 39
146
- /* ' */
147
- ) {
103
+ delim = 32; /* */
104
+ if (next === 34 /* " */ || next === 39 /* ' */) {
148
105
  i++;
149
106
  delim = next;
150
107
  }
151
-
152
108
  continue;
153
109
  }
154
-
155
110
  if (char === delim) {
156
111
  // token is ending delimiter since we've advanced our pointer
157
112
  key = (0, _trim.default)(key).call(key); // trim whitespace, allowing for multiple spaces
158
-
159
113
  if (key) out[key] = str;
160
114
  str = '';
161
115
  key = '';
@@ -163,9 +117,7 @@ const parseAttrs = s => {
163
117
  delim = null;
164
118
  continue;
165
119
  }
166
-
167
120
  str += s[i];
168
-
169
121
  if (i === s.length - 1) {
170
122
  // end of line
171
123
  if (key && key.length && str && str.length) {
@@ -174,42 +126,35 @@ const parseAttrs = s => {
174
126
  }
175
127
  }
176
128
  }
177
-
178
129
  return out;
179
- }; // -> prop="val"
180
- // Pass reference object in to only add keys/values that exist on reference
181
-
130
+ };
182
131
 
132
+ // -> prop="val"
133
+ // Pass reference object in to only add keys/values that exist on reference
183
134
  exports.parseAttrs = parseAttrs;
184
-
185
135
  const _buildAttrString = (obj, reference) => {
186
136
  var _context;
187
-
188
137
  return (0, _reduce.default)(_context = (0, _entries.default)(obj)).call(_context, (acc, [key, val]) => !reference || (0, _lodash.default)(reference, key) ? (0, _concat.default)(acc).call(acc, _applyTransforms(key, val)) : acc, '');
189
- }; // -> ?foo=bar&baz=bat
190
- // Pass reference object in to only add keys/values that exist on reference
191
-
138
+ };
192
139
 
140
+ // -> ?foo=bar&baz=bat
141
+ // Pass reference object in to only add keys/values that exist on reference
193
142
  const _buildAttrQueryString = (obj, reference) => {
194
143
  var _context2;
195
-
196
144
  return (0, _reduce.default)(_context2 = (0, _entries.default)(obj)).call(_context2, (acc, [key, val]) => {
197
145
  const prefix = acc.length ? '&amp;' : '?';
198
146
  const encodedVal = encodeURIComponent(_bBerLib.Url.ensureDecoded(val));
199
147
  return !reference || (0, _lodash.default)(reference, key) ? (0, _concat.default)(acc).call(acc, `${prefix}${key}=${encodedVal}`) : acc;
200
148
  }, '');
201
- }; // Ensure that attributes required for valid XHTML are present, and that system
202
- // defaults are merged into user settings
203
-
149
+ };
204
150
 
151
+ // Ensure that attributes required for valid XHTML are present, and that system
152
+ // defaults are merged into user settings
205
153
  const _extendWithDefaults = (obj, genus) => {
206
154
  const result = _objectSpread({}, obj);
207
-
208
155
  const order = _directiveOrder(genus);
209
-
210
156
  if (!order) throw new TypeError(`Invalid directive type: [${genus}]`);
211
157
  let taxonomy;
212
-
213
158
  switch (order) {
214
159
  case 'block':
215
160
  case 'misc':
@@ -218,34 +163,28 @@ const _extendWithDefaults = (obj, genus) => {
218
163
  if (order === 'block') {
219
164
  result.epubTypes = taxonomy;
220
165
  }
221
-
222
166
  if ((0, _lodash.default)(obj, 'classes')) {
223
167
  taxonomy = `${_lookUpFamily(result.classes)} ${genus}`;
224
-
225
168
  if (order === 'block') {
226
169
  result.epubTypes = taxonomy;
227
170
  }
228
-
229
171
  result.classes += ` ${taxonomy}`; // -> class="... bodymatter chapter"
230
172
  } else {
231
173
  result.classes = taxonomy; // -> class="bodymatter chapter"
232
174
  }
233
175
 
234
176
  return result;
235
-
236
177
  case 'inline':
237
178
  if (_requiresAltTag(genus) && !(0, _lodash.default)(obj, 'alt')) {
238
179
  result.alt = result.source;
239
180
  }
240
-
241
181
  return result;
242
-
243
182
  default:
244
183
  return result;
245
184
  }
246
- }; // Create an object from attributes in the given directive
247
-
185
+ };
248
186
 
187
+ // Create an object from attributes in the given directive
249
188
  const attributesObject = (attrs, origGenus, context = {}) => {
250
189
  const {
251
190
  fileName,
@@ -253,40 +192,37 @@ const attributesObject = (attrs, origGenus, context = {}) => {
253
192
  } = context;
254
193
  const attrsObject = {};
255
194
  let genus = origGenus;
256
-
257
195
  if (!genus || typeof genus !== 'string') {
258
196
  _bBerLogger.default.error(`No directive provided: ${fileName}:${lineNumber}`);
259
197
  }
260
-
261
198
  if (!_bBerShapesDirectives.ALL_DIRECTIVES.has(genus)) {
262
199
  _bBerLogger.default.error(`Invalid directive: [${genus}] at ${fileName}:${lineNumber}`);
263
200
  }
264
-
265
201
  if (_bBerShapesDirectives.DRAFT_DIRECTIVES.has(genus)) {
266
202
  _bBerLogger.default.warn(`render [epub:${genus}] is [draft]. substituting with [chapter]`);
267
-
268
203
  genus = 'chapter';
269
204
  }
270
-
271
205
  if (_bBerShapesDirectives.DEPRECATED_DIRECTIVES.has(genus)) {
272
206
  _bBerLogger.default.warn(`render [epub:${genus}] is [deprecated]. substituting with [chapter]`);
273
-
274
207
  genus = 'chapter';
275
208
  }
276
-
277
209
  if (attrs && typeof attrs === 'string') {
278
- (0, _utils.forOf)(parseAttrs((0, _trim.default)(attrs).call(attrs)), (k, v) => {
279
- if (_isUnsupportedAttribute(genus, k)) {
280
- return _bBerLogger.default.warn(`render omitting unsupported attribute [${k}] at [${fileName}:${lineNumber}]`);
210
+ const parsedAttrs = (0, _entries.default)(parseAttrs((0, _trim.default)(attrs).call(attrs)));
211
+
212
+ // eslint-disable-next-line no-unused-vars
213
+ for (const [key, val] of parsedAttrs) {
214
+ if (_isUnsupportedAttribute(genus, key)) {
215
+ _bBerLogger.default.warn(`render omitting unsupported attribute [${key}] at [${fileName}:${lineNumber}]`);
216
+ continue;
281
217
  }
218
+ attrsObject[key] = val;
219
+ }
220
+ }
282
221
 
283
- attrsObject[k] = v;
284
- });
285
- } // Add original `origGenus` as a class to the attrs object in case it's
222
+ // Add original `origGenus` as a class to the attrs object in case it's
286
223
  // different from the current `genus` (which might've changed due to it's
287
224
  // specification). do this to keep styling consistent
288
225
 
289
-
290
226
  if (genus !== origGenus) {
291
227
  if ((0, _lodash.default)(attrsObject, 'classes')) {
292
228
  attrsObject.classes += ` ${origGenus}`;
@@ -294,33 +230,23 @@ const attributesObject = (attrs, origGenus, context = {}) => {
294
230
  attrsObject.classes = origGenus;
295
231
  }
296
232
  }
297
-
298
233
  const mergedAttrs = _extendWithDefaults(attrsObject, genus);
299
-
300
234
  return mergedAttrs;
301
- }; // Create a string of attributes for an XHTML element
302
-
235
+ };
303
236
 
237
+ // Create a string of attributes for an XHTML element
304
238
  exports.attributesObject = attributesObject;
239
+ const attributesString = (obj, reference) => _buildAttrString(obj, reference);
305
240
 
306
- const attributesString = (obj, reference) => _buildAttrString(obj, reference); // Create a query string of attributes for an iframe src
307
-
308
-
241
+ // Create a query string of attributes for an iframe src
309
242
  exports.attributesString = attributesString;
243
+ const attributesQueryString = (obj, reference) => _buildAttrQueryString(obj, reference);
310
244
 
311
- const attributesQueryString = (obj, reference) => _buildAttrQueryString(obj, reference); // Convenience wrapper for creating attributes: String -> Object -> String
312
-
313
-
245
+ // Convenience wrapper for creating attributes: String -> Object -> String
314
246
  exports.attributesQueryString = attributesQueryString;
315
-
316
247
  const attributes = (str, type, context) => _buildAttrString(attributesObject(str, type, context));
317
-
318
248
  exports.attributes = attributes;
319
-
320
249
  const htmlId = s => s.replace(/[^0-9a-zA-Z_-]/g, '-');
321
-
322
250
  exports.htmlId = htmlId;
323
-
324
251
  const toAlias = fpath => _path.default.basename(_path.default.basename(fpath, _path.default.extname(fpath)));
325
-
326
252
  exports.toAlias = toAlias;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canopycanopycanopy/b-ber-grammar-attributes",
3
- "version": "2.0.0",
3
+ "version": "3.0.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -17,9 +17,9 @@
17
17
  "access": "public"
18
18
  },
19
19
  "dependencies": {
20
- "@canopycanopycanopy/b-ber-lib": "2.0.0",
21
- "@canopycanopycanopy/b-ber-logger": "2.0.0",
22
- "@canopycanopycanopy/b-ber-shapes-directives": "2.0.0",
20
+ "@canopycanopycanopy/b-ber-lib": "3.0.2",
21
+ "@canopycanopycanopy/b-ber-logger": "3.0.2",
22
+ "@canopycanopycanopy/b-ber-shapes-directives": "3.0.2",
23
23
  "lodash": "^4.17.21",
24
24
  "lodash.has": "latest",
25
25
  "tar": "^6.1.11"
@@ -49,5 +49,5 @@
49
49
  "url": "https://maxwellsimmer.com"
50
50
  }
51
51
  ],
52
- "gitHead": "dbeae192709705f5bc211195ff343cbd999f9caf"
52
+ "gitHead": "1ec8ab9a4030fb59839d347beb04ab7605e3d5dd"
53
53
  }