penrose 0.2.46 → 0.3.0

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.
@@ -217,54 +217,4 @@ describe('Penrose', () => {
217
217
  await fs.remove(path.join(dir, 'data/files/styles'));
218
218
  });
219
219
  });
220
-
221
- describe('createMathFile', () => {
222
- test('Should create math files', async () => {
223
- await fs.remove(path.join(dir, 'data/files/math'));
224
-
225
- const math = [
226
- {
227
- data: 'E = mc^2',
228
- format: 'tex'
229
- }
230
- ];
231
-
232
- const tasks = [];
233
- const expected = [];
234
-
235
- _.forEach(math, function (value) {
236
- const outputFormat = 'svg';
237
- const task = {
238
- input: value.data,
239
- inputFormat: value.format,
240
- outputFormat: outputFormat
241
- };
242
- const filename = penrose.getMathFilename(task);
243
- task.output = penrose.getMathPath(outputFormat, filename);
244
-
245
- tasks.push(task);
246
-
247
- expected.push(
248
- path.join(config.schemes.public.path, 'math/svg', filename)
249
- );
250
- });
251
-
252
- await Promise.mapSeries(tasks, function (task) {
253
- return penrose.createMathFile(task);
254
- });
255
-
256
- const actual = await multiGlob(
257
- _.map(config.schemes, function (scheme) {
258
- return scheme.path + 'math/**/*';
259
- }),
260
- {
261
- nodir: true
262
- }
263
- );
264
-
265
- expect(actual).toEqual(expected);
266
-
267
- await fs.remove(path.join(dir, 'data/files/math'));
268
- });
269
- });
270
220
  });
package/lib/penrose.js CHANGED
@@ -1,15 +1,10 @@
1
1
  const _ = require('lodash');
2
2
  const chalk = require('chalk');
3
- const cheerio = require('cheerio');
4
- const crypto = require('crypto');
5
3
  const fs = require('fs-extra');
6
- const mathjax = require('mathjax-node/lib/mj-single.js');
7
4
  const path = require('path');
8
- const pkg = require('../package.json');
9
5
  const Promise = require('bluebird');
10
6
  const replaceExt = require('replace-ext');
11
7
  const sharp = require('sharp');
12
- const stringify = require('fast-json-stable-stringify');
13
8
 
14
9
  /**
15
10
  * Constants
@@ -20,10 +15,6 @@ const JPEG = 'jpeg';
20
15
  const PNG = 'png';
21
16
  const SVG = 'svg';
22
17
  const WEBP = 'webp';
23
- const TEX = 'tex';
24
- const INLINE_TEX = 'inline-tex';
25
- const ASCIIMATH = 'asciimath';
26
- const MATHML = 'mathml';
27
18
  const RESIZE = 'resize';
28
19
 
29
20
  const EXTNAME_FORMAT_MAP = {
@@ -39,13 +30,6 @@ const FORMAT_EXTNAME_MAP = {
39
30
  [WEBP]: 'webp'
40
31
  };
41
32
 
42
- const MATHJAX_FORMAT_MAP = {
43
- [TEX]: 'TeX',
44
- [INLINE_TEX]: 'inline-TeX',
45
- [ASCIIMATH]: 'AsciiMath',
46
- [MATHML]: 'MathML'
47
- };
48
-
49
33
  const JPEG_OPTIONS = [
50
34
  'quality',
51
35
  'progressive',
@@ -89,57 +73,6 @@ const RESIZE_OPTIONS = [
89
73
 
90
74
  Promise.promisifyAll(fs);
91
75
 
92
- /**
93
- *
94
- * @param {string} value
95
- * @param {number} ex
96
- * @returns {number}
97
- */
98
- function exToPx(value, ex) {
99
- const match = value.match(/^(.+)ex$/i);
100
-
101
- if (match) {
102
- return Number(match[1]) * ex;
103
- }
104
-
105
- return Number(value);
106
- }
107
-
108
- /**
109
- *
110
- * @param {Object} args
111
- * @returns {Promise}
112
- */
113
- function typesetMath(args) {
114
- const format = MATHJAX_FORMAT_MAP[args.inputFormat];
115
-
116
- if (_.isUndefined(format)) {
117
- throw new Error('Input format `' + args.inputFormat + '` is not supported');
118
- }
119
-
120
- if (!~[SVG].indexOf(args.outputFormat)) {
121
- throw new Error(
122
- 'Output format `' + args.outputFormat + '` is not supported'
123
- );
124
- }
125
-
126
- return new Promise((resolve, reject) => {
127
- const config = _.assign(_.pick(args, ['ex', 'width']), {
128
- math: args.input,
129
- format: format,
130
- svg: true
131
- });
132
-
133
- mathjax.typeset(config, (result) => {
134
- if (result.errors) {
135
- reject(result.errors);
136
- } else {
137
- resolve(result);
138
- }
139
- });
140
- });
141
- }
142
-
143
76
  /**
144
77
  *
145
78
  * @param {Object} config
@@ -147,30 +80,8 @@ function typesetMath(args) {
147
80
  */
148
81
  function Penrose(config) {
149
82
  this.config = {
150
- ...config,
151
- math: {
152
- displayErrors: false,
153
- displayMessages: false,
154
- undefinedCharError: false,
155
- MathJax: {
156
- SVG: {
157
- font: 'TeX'
158
- }
159
- },
160
- ex: 6, // ex-size in pixels
161
- width: 100, // Width of container (in ex) for linebreaking and tags
162
- ...config.math
163
- }
83
+ ...config
164
84
  };
165
-
166
- mathjax.config(
167
- _.pick(this.config.math, [
168
- 'displayErrors',
169
- 'displayMessages',
170
- 'undefinedCharError',
171
- 'MathJax'
172
- ])
173
- );
174
85
  }
175
86
 
176
87
  Penrose.prototype = {
@@ -234,140 +145,7 @@ Penrose.prototype = {
234
145
  return transformer.toFile(distPath).then(() => distPath);
235
146
  });
236
147
  },
237
- /**
238
- *
239
- * @param {Object} args
240
- * @returns {Promise}
241
- */
242
- createMath: function (args) {
243
- console.log('Typesetting math', chalk.cyan(args.input));
244
-
245
- const config = _.assign(
246
- _.pick(this.config.math, ['ex', 'width']),
247
- _.pick(args, ['input', 'inputFormat', 'outputFormat', 'ex', 'width'])
248
- );
249
-
250
- if (SVG === config.outputFormat) {
251
- return typesetMath(config).then((result) => {
252
- const $svg = cheerio.load(result.svg)('svg');
253
- const width = Math.ceil(exToPx($svg.attr('width'), config.ex));
254
- const height = Math.ceil(exToPx($svg.attr('height'), config.ex));
255
-
256
- return Promise.resolve({
257
- data: result.svg,
258
- width: width,
259
- height: height
260
- });
261
- });
262
- }
263
-
264
- throw new Error(
265
- 'Output format `' + config.outputFormat + '` is not supported'
266
- );
267
- },
268
- /**
269
- *
270
- * @param {Object} args
271
- * @returns {Promise}
272
- */
273
- createMathDataURIBase64: function (args) {
274
- console.log('Typesetting math', chalk.cyan(args.input));
275
-
276
- const config = _.assign(
277
- _.pick(this.config.math, ['ex', 'width']),
278
- _.pick(args, ['input', 'inputFormat', 'outputFormat', 'ex', 'width'])
279
- );
280
-
281
- if (SVG === config.outputFormat) {
282
- return typesetMath(config).then((result) => {
283
- const buffer = Buffer.from(result.svg, 'utf-8');
284
148
 
285
- return Promise.resolve(
286
- 'data:image/svg+xml;base64,' + buffer.toString('base64')
287
- );
288
- });
289
- }
290
-
291
- if (PNG === config.outputFormat) {
292
- config.outputFormat = SVG;
293
-
294
- return typesetMath(config)
295
- .then((result) => {
296
- const buffer = Buffer.from(result.svg, 'utf-8');
297
- const $svg = cheerio.load(result.svg)('svg');
298
- const width = Math.ceil(exToPx($svg.attr('width'), config.ex));
299
- const height = Math.ceil(exToPx($svg.attr('height'), config.ex));
300
- const resize = {
301
- width: width,
302
- height: height
303
- };
304
-
305
- return sharp(buffer).resize(resize).toBuffer();
306
- })
307
- .then((buffer) =>
308
- Promise.resolve('data:image/png;base64,' + buffer.toString('base64'))
309
- );
310
- }
311
-
312
- throw new Error(
313
- 'Output format `' + config.outputFormat + '` is not supported'
314
- );
315
- },
316
- /**
317
- *
318
- * @param {Object} args
319
- * @returns {Promise}
320
- */
321
- createMathFile: function (args) {
322
- console.log('Typesetting math', chalk.cyan(args.input));
323
-
324
- const config = _.assign(
325
- _.pick(this.config.math, ['ex', 'width']),
326
- _.pick(args, [
327
- 'input',
328
- 'inputFormat',
329
- 'output',
330
- 'outputFormat',
331
- 'ex',
332
- 'width'
333
- ])
334
- );
335
- const outputResolved = this.resolvePath(config.output);
336
-
337
- if (SVG === config.outputFormat) {
338
- return fs
339
- .mkdirp(path.dirname(outputResolved))
340
- .then(() => typesetMath(config))
341
- .then((result) =>
342
- fs.writeFileAsync(outputResolved, result.svg, 'utf-8')
343
- );
344
- }
345
-
346
- if (PNG === config.outputFormat) {
347
- config.outputFormat = SVG;
348
-
349
- return fs
350
- .mkdirp(path.dirname(outputResolved))
351
- .then(() => typesetMath(config))
352
- .then((result) => {
353
- const buffer = Buffer.from(result.svg, 'utf-8');
354
- const $svg = cheerio.load(result.svg)('svg');
355
- const width = Math.ceil(exToPx($svg.attr('width'), config.ex));
356
- const height = Math.ceil(exToPx($svg.attr('height'), config.ex));
357
- const resize = {
358
- width: width,
359
- height: height
360
- };
361
-
362
- return sharp(buffer).resize(resize).toBuffer();
363
- })
364
- .then((buffer) => fs.writeFileAsync(outputResolved, buffer));
365
- }
366
-
367
- throw new Error(
368
- 'Output format `' + config.outputFormat + '` is not supported'
369
- );
370
- },
371
149
  /**
372
150
  *
373
151
  * @param {string} uri
@@ -382,6 +160,7 @@ Penrose.prototype = {
382
160
 
383
161
  return uri.substring(0, index);
384
162
  },
163
+
385
164
  /**
386
165
  *
387
166
  * @param {string} uri
@@ -391,6 +170,7 @@ Penrose.prototype = {
391
170
  setScheme: function (uri, scheme) {
392
171
  return scheme + '://' + this.getTarget(uri);
393
172
  },
173
+
394
174
  /**
395
175
  *
396
176
  * @param {string} uri
@@ -405,6 +185,7 @@ Penrose.prototype = {
405
185
 
406
186
  return uri.substring(index + 3);
407
187
  },
188
+
408
189
  /**
409
190
  * Returns absolute URL.
410
191
  *
@@ -420,6 +201,7 @@ Penrose.prototype = {
420
201
 
421
202
  return uri;
422
203
  },
204
+
423
205
  /**
424
206
  *
425
207
  * @param {string} uri
@@ -443,6 +225,7 @@ Penrose.prototype = {
443
225
 
444
226
  return schemePath + target;
445
227
  },
228
+
446
229
  /**
447
230
  *
448
231
  * @param {string} styleName
@@ -467,6 +250,7 @@ Penrose.prototype = {
467
250
 
468
251
  return scheme + '://styles/' + styleName + '/' + target;
469
252
  },
253
+
470
254
  /**
471
255
  * Returns absolute URL to derivative image.
472
256
  *
@@ -483,75 +267,6 @@ Penrose.prototype = {
483
267
  return '/' + this.resolvePath(uri);
484
268
  }
485
269
 
486
- throw new Error('Scheme `' + scheme + '` not supported');
487
- },
488
- /**
489
- *
490
- * @param {Object} args
491
- * @returns {string}
492
- */
493
- getMathDigest: function (args) {
494
- const config = _.assign(
495
- _.pick(this.config.math, ['ex', 'width']),
496
- _.pick(args, ['ex', 'width'])
497
- );
498
-
499
- const data =
500
- pkg.version + ';' + stringify(config) + ';' + args.input.trim();
501
-
502
- return crypto.createHash('md5').update(data).digest('hex');
503
- },
504
- /**
505
- *
506
- * @param {Object} args
507
- * @returns {string}
508
- */
509
- getMathFilename: function (args) {
510
- let ext;
511
-
512
- if (SVG === args.outputFormat) {
513
- ext = '.svg';
514
- } else if (PNG === args.outputFormat) {
515
- ext = '.png';
516
- } else {
517
- throw new Error('Format `' + args.outputFormat + '` is not supported');
518
- }
519
-
520
- return this.getMathDigest(args) + ext;
521
- },
522
- /**
523
- *
524
- * @param {string} outputFormat
525
- * @param {string} uri
526
- * @returns {string}
527
- */
528
- getMathPath: function (outputFormat, uri) {
529
- let scheme = this.getScheme(uri);
530
- let target;
531
-
532
- if (_.isUndefined(scheme)) {
533
- scheme = PUBLIC;
534
- target = uri;
535
- } else {
536
- target = this.getTarget(uri);
537
- }
538
-
539
- return scheme + '://math/' + outputFormat + '/' + target;
540
- },
541
- /**
542
- *
543
- * @param {string} outputFormat
544
- * @param {string} path
545
- * @returns {string}
546
- */
547
- getMathURL: function (outputFormat, path) {
548
- const uri = this.getMathPath(outputFormat, path);
549
- const scheme = this.getScheme(uri);
550
-
551
- if (PUBLIC === scheme || TEMPORARY === scheme) {
552
- return '/' + this.resolvePath(uri);
553
- }
554
-
555
270
  throw new Error('Scheme `' + scheme + '` not supported');
556
271
  }
557
272
  };
@@ -561,10 +276,6 @@ module.exports = {
561
276
  PNG,
562
277
  SVG,
563
278
  WEBP,
564
- TEX,
565
- INLINE_TEX,
566
- ASCIIMATH,
567
- MATHML,
568
279
  RESIZE,
569
280
  PUBLIC,
570
281
  TEMPORARY,
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "penrose",
3
- "version": "0.2.46",
3
+ "version": "0.3.0",
4
4
  "description": "Build derivative images",
5
5
  "main": "lib/penrose.js",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "git://github.com/netbek/penrose"
8
+ "url": "git://github.com/netbek/penrose.git"
9
9
  },
10
10
  "scripts": {
11
11
  "test": "vitest run"
@@ -27,14 +27,10 @@
27
27
  "babel-plugin-transform-class-properties": "6.24.1",
28
28
  "bluebird": "3.7.2",
29
29
  "chalk": "4.1.2",
30
- "cheerio": "1.2.0",
31
- "fast-json-stable-stringify": "2.1.0",
32
30
  "fs-extra": "11.3.5",
33
31
  "glob-promise": "6.0.7",
34
32
  "lodash": "4.18.1",
35
33
  "lodash-webpack-plugin": "0.11.6",
36
- "mathjax": "2.7.9",
37
- "mathjax-node": "0.5.2",
38
34
  "phantomjs-prebuilt": "2.1.16",
39
35
  "prettier": "3.8.3",
40
36
  "replace-ext": "2.0.0",