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