« get me outta code hell

replacer.js « util « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util/replacer.js
blob: 4bd9998854e0d3f3ef6b2e222a4596414956df0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// Regex-based forward parser for wiki content, breaking up text input into
// text and (possibly nested) tag nodes.
//
// The behavior here is quite tied into the `transformContent` content
// function, which converts nodes parsed here into actual HTML, links, etc
// for embedding in a wiki webpage.

import * as marked from 'marked';

import * as html from '#html';
import {escapeRegex, typeAppearance} from '#sugar';

export const replacerSpec = {
  'album': {
    find: 'album',
    link: 'linkAlbum',
  },

  'album-commentary': {
    find: 'album',
    link: 'linkAlbumCommentary',
  },

  'album-gallery': {
    find: 'album',
    link: 'linkAlbumGallery',
  },

  'artist': {
    find: 'artist',
    link: 'linkArtist',
  },

  'artist-gallery': {
    find: 'artist',
    link: 'linkArtistGallery',
  },

  'commentary-index': {
    find: null,
    link: 'linkCommentaryIndex',
  },

  'date': {
    find: null,
    value: (ref) => new Date(ref),
    html: (date, {html, language}) =>
      html.tag('time',
        {datetime: date.toUTCString()},
        language.formatDate(date)),
  },

  'flash-index': {
    find: null,
    link: 'linkFlashIndex',
  },

  'flash': {
    find: 'flash',
    link: 'linkFlash',
    transformName(name, node, input) {
      const nextCharacter = input[node.iEnd];
      const lastCharacter = name[name.length - 1];
      if (![' ', '\n', '<'].includes(nextCharacter) && lastCharacter === '.') {
        return name.slice(0, -1);
      } else {
        return name;
      }
    },
  },

  'flash-act': {
    find: 'flashAct',
    link: 'linkFlashAct',
  },

  'group': {
    find: 'group',
    link: 'linkGroup',
  },

  'group-gallery': {
    find: 'group',
    link: 'linkGroupGallery',
  },

  'home': {
    find: null,
    link: 'linkWikiHome',
  },

  'listing-index': {
    find: null,
    link: 'linkListingIndex',
  },

  'listing': {
    find: 'listing',
    link: 'linkListing',
  },

  'media': {
    find: null,
    link: 'linkPathFromMedia',
  },

  'news-index': {
    find: null,
    link: 'linkNewsIndex',
  },

  'news-entry': {
    find: 'newsEntry',
    link: 'linkNewsEntry',
  },

  'root': {
    find: null,
    link: 'linkPathFromRoot',
  },

  'site': {
    find: null,
    link: 'linkPathFromSite',
  },

  'static': {
    find: 'staticPage',
    link: 'linkStaticPage',
  },

  'string': {
    find: null,
    value: (ref) => ref,
    html: (ref, {language, args}) => language.$(ref, args),
  },

  'tag': {
    find: 'artTag',
    link: 'linkArtTag',
  },

  'track': {
    find: 'track',
    link: 'linkTrackDynamically',
  },
};

// Syntax literals.
const tagBeginning = '[[';
const tagEnding = ']]';
const tagReplacerValue = ':';
const tagHash = '#';
const tagArgument = '*';
const tagArgumentValue = '=';
const tagLabel = '|';

const noPrecedingWhitespace = '(?<!\\s)';

const R_tagBeginning = escapeRegex(tagBeginning);

const R_tagEnding = escapeRegex(tagEnding);

const R_tagReplacerValue =
  noPrecedingWhitespace + escapeRegex(tagReplacerValue);

const R_tagHash = noPrecedingWhitespace + escapeRegex(tagHash);

const R_tagArgument = escapeRegex(tagArgument);

const R_tagArgumentValue = escapeRegex(tagArgumentValue);

const R_tagLabel = escapeRegex(tagLabel);

const regexpCache = {};

const makeError = (i, message) => ({i, type: 'error', data: {message}});
const endOfInput = (i, comment) =>
  makeError(i, `Unexpected end of input (${comment}).`);

// These are 8asically stored on the glo8al scope, which might seem odd
// for a recursive function, 8ut the values are only ever used immediately
// after they're set.
let stopped, stop_iParse, stop_literal;

function parseOneTextNode(input, i, stopAt) {
  return parseNodes(input, i, stopAt, true)[0];
}

function parseNodes(input, i, stopAt, textOnly) {
  let nodes = [];
  let string = '';
  let iString = 0;

  stopped = false;

  const pushTextNode = (isLast) => {
    string = input.slice(iString, i);

    // If this is the last text node 8efore stopping (at a stopAt match
    // or the end of the input), trim off whitespace at the end.
    if (isLast) {
      string = string.trimEnd();
    }

    string = cleanRawText(string);

    if (string.length) {
      nodes.push({i: iString, iEnd: i, type: 'text', data: string});
      string = '';
    }
  };

  const literalsToMatch = stopAt
    ? stopAt.concat([R_tagBeginning])
    : [R_tagBeginning];

  // The 8ackslash stuff here is to only match an even (or zero) num8er
  // of sequential 'slashes. Even amounts always cancel out! Odd amounts
  // don't, which would mean the following literal is 8eing escaped and
  // should 8e counted only as part of the current string/text.
  //
  // Inspired 8y this: https://stackoverflow.com/a/41470813
  const regexpSource = `(?<!\\\\)(?:\\\\{2})*(${literalsToMatch.join('|')})`;

  // There are 8asically only a few regular expressions we'll ever use,
  // 8ut it's a pain to hard-code them all, so we dynamically gener8te
  // and cache them for reuse instead.
  let regexp;
  if (Object.hasOwn(regexpCache, regexpSource)) {
    regexp = regexpCache[regexpSource];
  } else {
    regexp = new RegExp(regexpSource);
    regexpCache[regexpSource] = regexp;
  }

  // Skip whitespace at the start of parsing. This is run every time
  // parseNodes is called (and thus parseOneTextNode too), so spaces
  // at the start of syntax elements will always 8e skipped. We don't
  // skip whitespace that shows up inside content (i.e. once we start
  // parsing below), though!
  const whitespaceOffset = input.slice(i).search(/[^\s]/);

  // If the string is all whitespace, that's just zero content, so
  // return the empty nodes array.
  if (whitespaceOffset === -1) {
    return nodes;
  }

  i += whitespaceOffset;

  while (i < input.length) {
    const match = input.slice(i).match(regexp);

    if (!match) {
      iString = i;
      i = input.length;
      pushTextNode(true);
      break;
    }

    const closestMatch = match[0];
    const closestMatchIndex = i + match.index;

    if (textOnly && closestMatch === tagBeginning)
      throw makeError(i, `Unexpected [[tag]] - expected only text here.`);

    const stopHere = closestMatch !== tagBeginning;

    iString = i;
    i = closestMatchIndex;
    pushTextNode(stopHere);

    i += closestMatch.length;

    if (stopHere) {
      stopped = true;
      stop_iParse = i;
      stop_literal = closestMatch;
      break;
    }

    if (closestMatch === tagBeginning) {
      const iTag = closestMatchIndex;

      let N;

      // Replacer key (or value)

      N = parseOneTextNode(input, i, [
        R_tagReplacerValue,
        R_tagHash,
        R_tagArgument,
        R_tagLabel,
        R_tagEnding,
      ]);

      if (!stopped) throw endOfInput(i, `reading replacer key`);

      if (!N) {
        switch (stop_literal) {
          case tagReplacerValue:
          case tagArgument:
            throw makeError(i, `Expected text (replacer key).`);
          case tagLabel:
          case tagHash:
          case tagEnding:
            throw makeError(i, `Expected text (replacer key/value).`);
        }
      }

      const replacerFirst = N;
      i = stop_iParse;

      // Replacer value (if explicit)

      let replacerSecond;

      if (stop_literal === tagReplacerValue) {
        N = parseNodes(input, i, [
          R_tagHash,
          R_tagArgument,
          R_tagLabel,
          R_tagEnding,
        ]);

        if (!stopped) throw endOfInput(i, `reading replacer value`);
        if (!N.length) throw makeError(i, `Expected content (replacer value).`);

        replacerSecond = N;
        i = stop_iParse;
      }

      // Assign first & second to replacer key/value

      let replacerKey, replacerValue;

      // Value is an array of nodes, 8ut key is just one (or null).
      // So if we use replacerFirst as the value, we need to stick
      // it in an array (on its own).
      if (replacerSecond) {
        replacerKey = replacerFirst;
        replacerValue = replacerSecond;
      } else {
        replacerKey = null;
        replacerValue = [replacerFirst];
      }

      // Hash

      let hash;

      if (stop_literal === tagHash) {
        N = parseOneTextNode(input, i, [R_tagArgument, R_tagLabel, R_tagEnding]);

        if (!stopped) throw endOfInput(i, `reading hash`);
        if (!N) throw makeError(i, `Expected text (hash).`);

        hash = N;
        i = stop_iParse;
      }

      // Arguments

      const args = [];

      while (stop_literal === tagArgument) {
        N = parseOneTextNode(input, i, [
          R_tagArgumentValue,
          R_tagArgument,
          R_tagLabel,
          R_tagEnding,
        ]);

        if (!stopped) throw endOfInput(i, `reading argument key`);

        if (stop_literal !== tagArgumentValue)
          throw makeError(
            i,
            `Expected ${tagArgumentValue.literal} (tag argument).`
          );

        if (!N) throw makeError(i, `Expected text (argument key).`);

        const key = N;
        i = stop_iParse;

        N = parseNodes(input, i, [R_tagArgument, R_tagLabel, R_tagEnding]);

        if (!stopped) throw endOfInput(i, `reading argument value`);
        if (!N.length) throw makeError(i, `Expected content (argument value).`);

        const value = N;
        i = stop_iParse;

        args.push({key, value});
      }

      let label;

      if (stop_literal === tagLabel) {
        N = parseOneTextNode(input, i, [R_tagEnding]);

        if (!stopped) throw endOfInput(i, `reading label`);
        if (!N) throw makeError(i, `Expected text (label).`);

        label = N;
        i = stop_iParse;
      }

      nodes.push({
        i: iTag,
        iEnd: i,
        type: 'tag',
        data: {replacerKey, replacerValue, hash, args, label},
      });

      continue;
    }
  }

  return nodes;
}

export function squashBackslashes(text) {
  // Squash backslashes which aren't themselves escaped into
  // the following character, unless that character is one of
  // a set of characters where the backslash carries meaning
  // into later formatting (i.e. markdown). Note that we do
  // NOT compress double backslashes into single backslashes.
  return text.replace(/([^\\](?:\\{2})*)\\(?![\\*_-])/g, '$1');
}

export function restoreRawHTMLTags(text) {
  // Replace stuff like <html:a> with <a>; these signal that
  // the tag shouldn't be processed by the replacer system,
  // and should just be embedded into the content as raw HTML.
  return text.replace(/<html:(.*?)(?=[ >])/g, '<$1');
}

export function cleanRawText(text) {
  text = squashBackslashes(text);
  text = restoreRawHTMLTags(text);
  return text;
}

export function postprocessImages(inputNodes) {
  const outputNodes = [];

  let atStartOfLine = true;

  const lastNode = inputNodes.at(-1);

  for (const node of inputNodes) {
    if (node.type === 'tag') {
      atStartOfLine = false;
    }

    if (node.type === 'text') {
      const imageRegexp = /<img (.*?)>/g;

      let match = null, parseFrom = 0;
      while (match = imageRegexp.exec(node.data)) {
        const previousText = node.data.slice(parseFrom, match.index);

        outputNodes.push({
          type: 'text',
          data: previousText,
          i: node.i + parseFrom,
          iEnd: node.i + parseFrom + match.index,
        });

        parseFrom = match.index + match[0].length;

        const imageNode = {type: 'image'};
        const attributes = html.parseAttributes(match[1]);

        imageNode.src = attributes.get('src');

        if (previousText.endsWith('\n')) {
          atStartOfLine = true;
        } else if (previousText.length) {
          atStartOfLine = false;
        }

        imageNode.inline = (() => {
          // Images can force themselves to be rendered inline using a custom
          // attribute - this style just works better for certain embeds,
          // usually jokes or small images.
          if (attributes.get('inline')) return true;

          // If we've already determined we're in the middle of a line,
          // we're inline. (Of course!)
          if (!atStartOfLine) {
            return true;
          }

          // If there's more text to go in this text node, and what's
          // remaining doesn't start with a line break, we're inline.
          if (
            parseFrom !== node.data.length &&
            node.data[parseFrom] !== '\n'
          ) {
            return true;
          }

          // If we're at the end of this text node, but this text node
          // isn't the last node overall, we're inline.
          if (
            parseFrom === node.data.length &&
            node !== lastNode
          ) {
            return true;
          }

          // If no other condition matches, this image is on its own line.
          return false;
        })();

        if (attributes.get('link')) imageNode.link = attributes.get('link');
        if (attributes.get('style')) imageNode.style = attributes.get('style');
        if (attributes.get('width')) imageNode.width = parseInt(attributes.get('width'));
        if (attributes.get('height')) imageNode.height = parseInt(attributes.get('height'));
        if (attributes.get('align')) imageNode.align = attributes.get('align');
        if (attributes.get('pixelate')) imageNode.pixelate = true;

        if (attributes.get('warning')) {
          imageNode.warnings =
            attributes.get('warning').split(', ');
        }

        outputNodes.push(imageNode);

        // No longer at the start of a line after an image - there will at
        // least be a text node with only '\n' before the next image that's
        // on its own line.
        atStartOfLine = false;
      }

      if (parseFrom !== node.data.length) {
        outputNodes.push({
          type: 'text',
          data: node.data.slice(parseFrom),
          i: node.i + parseFrom,
          iEnd: node.iEnd,
        });
      }

      continue;
    }

    outputNodes.push(node);
  }

  return outputNodes;
}

export function postprocessHeadings(inputNodes) {
  const outputNodes = [];

  for (const node of inputNodes) {
    if (node.type !== 'text') {
      outputNodes.push(node);
      continue;
    }

    const headingRegexp = /<h2 (.*?)>/g;

    let textContent = '';

    let match = null, parseFrom = 0;
    while (match = headingRegexp.exec(node.data)) {
      textContent += node.data.slice(parseFrom, match.index);
      parseFrom = match.index + match[0].length;

      const attributes = html.parseAttributes(match[1]);
      attributes.push('class', 'content-heading');

      // We're only modifying the opening tag here. The remaining content,
      // including the closing tag, will be pushed as-is.
      textContent += `<h2 ${attributes}>`;
    }

    if (parseFrom !== node.data.length) {
      textContent += node.data.slice(parseFrom);
    }

    outputNodes.push({
      type: 'text',
      data: textContent,
      i: node.i,
      iEnd: node.iEnd,
    });
  }

  return outputNodes;
}

export function postprocessExternalLinks(inputNodes) {
  const outputNodes = [];

  for (const node of inputNodes) {
    if (node.type !== 'text') {
      outputNodes.push(node);
      continue;
    }

    const plausibleLinkRegexp = /\[.*?\)/g;

    let textContent = '';

    let plausibleMatch = null, parseFrom = 0;
    while (plausibleMatch = plausibleLinkRegexp.exec(node.data)) {
      textContent += node.data.slice(parseFrom, plausibleMatch.index);

      const definiteMatch =
        marked.Lexer.rules.inline.link.exec(
          node.data.slice(plausibleMatch.index));

      if (definiteMatch) {
        const {1: label, 2: href} = definiteMatch;

        // Split the containing text node into two - the second of these will
        // be added after iterating over matches, or by the next match.
        if (textContent.length) {
          outputNodes.push({type: 'text', data: textContent});
          textContent = '';
        }

        const offset = plausibleMatch.index + definiteMatch.index;
        const length = definiteMatch[0].length;

        outputNodes.push({
          i: node.i + offset,
          iEnd: node.i + offset + length,
          type: 'external-link',
          data: {label, href},
        });

        parseFrom = offset + length;
      } else {
        parseFrom = plausibleMatch.index;
      }
    }

    if (parseFrom !== node.data.length) {
      textContent += node.data.slice(parseFrom);
    }

    if (textContent.length) {
      outputNodes.push({type: 'text', data: textContent});
    }
  }

  return outputNodes;
}

export function parseInput(input) {
  if (typeof input !== 'string') {
    throw new TypeError(`Expected input to be string, got ${typeAppearance(input)}`);
  }

  try {
    let output = parseNodes(input, 0);
    output = postprocessImages(output);
    output = postprocessHeadings(output);
    output = postprocessExternalLinks(output);
    return output;
  } catch (errorNode) {
    if (errorNode.type !== 'error') {
      throw errorNode;
    }

    const {
      i,
      data: {message},
    } = errorNode;

    let lineStart = input.slice(0, i).lastIndexOf('\n');
    if (lineStart >= 0) {
      lineStart += 1;
    } else {
      lineStart = 0;
    }

    let lineEnd = input.slice(i).indexOf('\n');
    if (lineEnd >= 0) {
      lineEnd += i;
    } else {
      lineEnd = input.length;
    }

    const line = input.slice(lineStart, lineEnd);

    const cursor = i - lineStart;

    throw new SyntaxError([
      `Parse error (at pos ${i}): ${message}`,
      line,
      '-'.repeat(cursor) + '^',
    ].join('\n'));
  }
}