@canopycanopycanopy/b-ber-grammar-attributes 3.0.0 → 3.0.3

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