securemark 0.257.3 → 0.258.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.
Files changed (76) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/index.js +1216 -606
  3. package/markdown.d.ts +1 -12
  4. package/package.json +9 -9
  5. package/src/combinator/control/manipulation/convert.ts +8 -4
  6. package/src/combinator/control/manipulation/scope.ts +10 -2
  7. package/src/combinator/data/parser/context/delimiter.ts +70 -0
  8. package/src/combinator/data/parser/context/memo.ts +30 -0
  9. package/src/combinator/{control/manipulation → data/parser}/context.test.ts +9 -9
  10. package/src/combinator/data/parser/context.ts +161 -0
  11. package/src/combinator/data/parser/sequence.test.ts +1 -1
  12. package/src/combinator/data/parser/sequence.ts +1 -1
  13. package/src/combinator/data/parser/some.test.ts +1 -1
  14. package/src/combinator/data/parser/some.ts +14 -37
  15. package/src/combinator/data/parser/subsequence.test.ts +1 -1
  16. package/src/combinator/data/parser/union.test.ts +1 -1
  17. package/src/combinator/data/parser.ts +7 -47
  18. package/src/combinator.ts +1 -2
  19. package/src/parser/api/bind.ts +5 -5
  20. package/src/parser/api/parse.ts +3 -1
  21. package/src/parser/block/blockquote.ts +1 -1
  22. package/src/parser/block/dlist.ts +4 -10
  23. package/src/parser/block/extension/figure.ts +4 -3
  24. package/src/parser/block/extension/table.ts +2 -2
  25. package/src/parser/block/heading.ts +5 -13
  26. package/src/parser/block/ilist.ts +3 -2
  27. package/src/parser/block/olist.ts +10 -7
  28. package/src/parser/block/paragraph.ts +1 -1
  29. package/src/parser/block/reply/cite.ts +1 -1
  30. package/src/parser/block/reply/quote.ts +1 -1
  31. package/src/parser/block/reply.ts +1 -1
  32. package/src/parser/block/sidefence.ts +1 -1
  33. package/src/parser/block/table.ts +9 -9
  34. package/src/parser/block/ulist.ts +4 -3
  35. package/src/parser/block.ts +1 -1
  36. package/src/parser/context.ts +32 -0
  37. package/src/parser/header.ts +1 -1
  38. package/src/parser/inline/annotation.ts +9 -17
  39. package/src/parser/inline/autolink/email.ts +1 -1
  40. package/src/parser/inline/autolink/url.ts +1 -1
  41. package/src/parser/inline/autolink.ts +5 -3
  42. package/src/parser/inline/bracket.ts +16 -15
  43. package/src/parser/inline/code.ts +1 -1
  44. package/src/parser/inline/comment.ts +4 -3
  45. package/src/parser/inline/deletion.ts +5 -4
  46. package/src/parser/inline/emphasis.ts +5 -4
  47. package/src/parser/inline/emstrong.ts +5 -4
  48. package/src/parser/inline/extension/index.ts +7 -14
  49. package/src/parser/inline/extension/indexee.ts +8 -10
  50. package/src/parser/inline/extension/indexer.ts +4 -3
  51. package/src/parser/inline/extension/label.ts +3 -2
  52. package/src/parser/inline/extension/placeholder.ts +5 -4
  53. package/src/parser/inline/html.ts +5 -4
  54. package/src/parser/inline/htmlentity.ts +1 -1
  55. package/src/parser/inline/insertion.ts +5 -4
  56. package/src/parser/inline/link.ts +11 -20
  57. package/src/parser/inline/mark.ts +5 -4
  58. package/src/parser/inline/math.ts +1 -1
  59. package/src/parser/inline/media.ts +8 -7
  60. package/src/parser/inline/reference.ts +10 -16
  61. package/src/parser/inline/ruby.ts +4 -3
  62. package/src/parser/inline/shortmedia.ts +3 -2
  63. package/src/parser/inline/strong.ts +5 -4
  64. package/src/parser/inline/template.test.ts +1 -1
  65. package/src/parser/inline/template.ts +9 -6
  66. package/src/parser/locale.ts +6 -7
  67. package/src/parser/processor/footnote.ts +5 -3
  68. package/src/parser/source/text.ts +1 -1
  69. package/src/parser/util.ts +0 -220
  70. package/src/parser/visibility.ts +205 -0
  71. package/src/util/info.ts +4 -2
  72. package/src/util/quote.ts +12 -15
  73. package/src/util/toc.ts +14 -17
  74. package/webpack.config.js +1 -0
  75. package/src/combinator/control/manipulation/context.ts +0 -70
  76. package/src/combinator/control/manipulation/resource.ts +0 -54
@@ -1,11 +1,12 @@
1
1
  import { EmStrongParser, EmphasisParser, StrongParser } from '../inline';
2
2
  import { Result, IntermediateParser } from '../../combinator/data/parser';
3
- import { union, some, precedence, creator, surround, open, lazy, bind } from '../../combinator';
3
+ import { union, syntax, some, surround, open, lazy, bind } from '../../combinator';
4
4
  import { inline } from '../inline';
5
5
  import { strong } from './strong';
6
6
  import { emphasis } from './emphasis';
7
7
  import { str } from '../source';
8
- import { startTight, blankWith } from '../util';
8
+ import { Rule } from '../context';
9
+ import { startTight, blankWith } from '../visibility';
9
10
  import { html, defrag } from 'typed-dom/dom';
10
11
  import { unshift } from 'spica/array';
11
12
 
@@ -26,7 +27,7 @@ const subemphasis: IntermediateParser<EmphasisParser> = lazy(() => some(union([
26
27
  ])),
27
28
  ])));
28
29
 
29
- export const emstrong: EmStrongParser = lazy(() => creator(precedence(1, surround(
30
+ export const emstrong: EmStrongParser = lazy(() => syntax(Rule.none, 1, surround(
30
31
  str('***'),
31
32
  startTight(some(union([
32
33
  some(inline, blankWith('*')),
@@ -57,4 +58,4 @@ export const emstrong: EmStrongParser = lazy(() => creator(precedence(1, surroun
57
58
  }
58
59
  assert(false);
59
60
  },
60
- ([as, bs], rest) => [unshift(as, bs), rest]))));
61
+ ([as, bs], rest) => [unshift(as, bs), rest])));
@@ -1,27 +1,20 @@
1
1
  import { undefined } from 'spica/global';
2
2
  import { ExtensionParser } from '../../inline';
3
- import { union, some, validate, guard, context, precedence, creator, surround, open, lazy, fmap } from '../../../combinator';
3
+ import { union, some, syntax, creator, precedence, guard, state, validate, surround, open, lazy, fmap } from '../../../combinator';
4
4
  import { inline } from '../../inline';
5
5
  import { indexee, identity } from './indexee';
6
6
  import { txt, str, stropt } from '../../source';
7
- import { startTight, trimBlankEnd } from '../../util';
7
+ import { Rule, State } from '../../context';
8
+ import { startTight, trimBlankEnd } from '../../visibility';
8
9
  import { html, define, defrag } from 'typed-dom/dom';
9
10
 
10
11
  import IndexParser = ExtensionParser.IndexParser;
11
12
 
12
- export const index: IndexParser = lazy(() => validate('[#', creator(precedence(2, fmap(indexee(surround(
13
+ export const index: IndexParser = lazy(() => validate('[#', syntax(Rule.index, 2, fmap(indexee(surround(
13
14
  '[#',
14
- guard(context => context.syntax?.inline?.index ?? true,
15
+ guard(context => ~context.state! & State.index,
16
+ state(State.annotation | State.reference | State.index | State.label | State.link | State.media | State.autolink,
15
17
  startTight(
16
- context({ syntax: { inline: {
17
- annotation: false,
18
- reference: false,
19
- index: false,
20
- label: false,
21
- link: false,
22
- media: false,
23
- autolink: false,
24
- }}},
25
18
  open(stropt(/^\|?/), trimBlankEnd(some(union([
26
19
  signature,
27
20
  inline,
@@ -37,7 +30,7 @@ export const index: IndexParser = lazy(() => validate('[#', creator(precedence(2
37
30
  href: el.id ? `#${el.id}` : undefined,
38
31
  },
39
32
  el.childNodes),
40
- ])))));
33
+ ]))));
41
34
 
42
35
  const signature: IndexParser.SignatureParser = lazy(() => creator(fmap(open(
43
36
  '|#',
@@ -3,6 +3,7 @@ import { MarkdownParser } from '../../../../markdown';
3
3
  import { Parser } from '../../../combinator/data/parser';
4
4
  import { fmap } from '../../../combinator';
5
5
  import { define } from 'typed-dom/dom';
6
+ import { duffEach } from 'spica/duff';
6
7
 
7
8
  export function indexee<P extends Parser<unknown, MarkdownParser.Context>>(parser: P, optional?: boolean): P;
8
9
  export function indexee(parser: Parser<HTMLElement, MarkdownParser.Context>, optional?: boolean): Parser<HTMLElement> {
@@ -27,35 +28,32 @@ export function text(source: HTMLElement | DocumentFragment, optional = false):
27
28
  if (index) return index;
28
29
  assert(!source.querySelector('.annotation, br'));
29
30
  const target = source.cloneNode(true) as typeof source;
30
- for (
31
- let es = target.querySelectorAll('code[data-src], .math[data-src], .comment, rt, rp, .reference, .checkbox, ul, ol'),
32
- i = 0, len = es.length; i < len; ++i) {
33
- const el = es[i];
31
+ duffEach(target.querySelectorAll('code[data-src], .math[data-src], .comment, rt, rp, .reference, .checkbox, ul, ol'), el => {
34
32
  switch (el.tagName) {
35
33
  case 'CODE':
36
34
  define(el, el.getAttribute('data-src')!);
37
- continue;
35
+ return;
38
36
  case 'RT':
39
37
  case 'RP':
40
38
  case 'UL':
41
39
  case 'OL':
42
40
  el.remove();
43
- continue;
41
+ return;
44
42
  }
45
43
  switch (el.className) {
46
44
  case 'math':
47
45
  define(el, el.getAttribute('data-src')!);
48
- continue;
46
+ return;
49
47
  case 'comment':
50
48
  case 'checkbox':
51
49
  el.remove();
52
- continue;
50
+ return;
53
51
  case 'reference':
54
52
  assert(el.firstElementChild?.hasAttribute('hidden'));
55
53
  el.firstChild!.remove();
56
- continue;
54
+ return;
57
55
  }
58
- }
56
+ });
59
57
  // Better:
60
58
  //return target.innerText;
61
59
  return target.textContent!;
@@ -1,12 +1,13 @@
1
1
  import { ExtensionParser } from '../../inline';
2
- import { union, verify, focus, creator, surround, fmap } from '../../../combinator';
2
+ import { union, creator, state, verify, focus, surround, fmap } from '../../../combinator';
3
3
  import { index } from './index';
4
- import { clean } from '../../util';
4
+ import { State } from '../../context';
5
5
  import { html } from 'typed-dom/dom';
6
6
 
7
7
  export const indexer: ExtensionParser.IndexerParser = creator(fmap(verify(surround(
8
8
  /^\s+(?=\[#\S)/,
9
- clean(union([
9
+ state(State.index, false,
10
+ union([
10
11
  focus('[#]', () => [[html('a', { href: '#' })], '']),
11
12
  index,
12
13
  ])),
@@ -1,7 +1,8 @@
1
1
  import { Array } from 'spica/global';
2
2
  import { ExtensionParser } from '../../inline';
3
- import { union, validate, guard, creator, surround, clear, fmap } from '../../../combinator';
3
+ import { union, guard, creator, validate, surround, clear, fmap } from '../../../combinator';
4
4
  import { str } from '../../source';
5
+ import { State } from '../../context';
5
6
  import { html } from 'typed-dom/dom';
6
7
 
7
8
  const body = str(/^\$[A-Za-z]*(?:(?:-[A-Za-z][0-9A-Za-z]*)+|-(?:(?:0|[1-9][0-9]*)\.)*(?:0|[1-9][0-9]*)(?![0-9A-Za-z]))/);
@@ -12,7 +13,7 @@ export const segment: ExtensionParser.LabelParser.SegmentParser = clear(validate
12
13
  ])));
13
14
 
14
15
  export const label: ExtensionParser.LabelParser = validate(['[$', '$'], creator(fmap(
15
- guard(context => context.syntax?.inline?.label ?? true,
16
+ guard(context => ~context.state! & State.label,
16
17
  union([
17
18
  surround('[', body, ']'),
18
19
  body,
@@ -1,8 +1,9 @@
1
1
  import { ExtensionParser } from '../../inline';
2
- import { union, some, validate, precedence, creator, surround, lazy } from '../../../combinator';
2
+ import { union, some, syntax, validate, surround, lazy } from '../../../combinator';
3
3
  import { inline } from '../../inline';
4
4
  import { str } from '../../source';
5
- import { startTight } from '../../util';
5
+ import { Rule } from '../../context';
6
+ import { startTight } from '../../visibility';
6
7
  import { html, defrag } from 'typed-dom/dom';
7
8
  import { unshift } from 'spica/array';
8
9
 
@@ -10,7 +11,7 @@ import { unshift } from 'spica/array';
10
11
 
11
12
  // All syntax surrounded by square brackets shouldn't contain line breaks.
12
13
 
13
- export const placeholder: ExtensionParser.PlaceholderParser = lazy(() => validate(['[:', '[^'], creator(precedence(2, surround(
14
+ export const placeholder: ExtensionParser.PlaceholderParser = lazy(() => validate(['[:', '[^'], syntax(Rule.none, 2, surround(
14
15
  str(/^\[[:^]/),
15
16
  startTight(some(union([inline]), ']', [[/^\\?\n/, 9], [']', 2]])),
16
17
  str(']'), false,
@@ -22,4 +23,4 @@ export const placeholder: ExtensionParser.PlaceholderParser = lazy(() => validat
22
23
  'data-invalid-message': `Reserved start symbol "${as[0][1]}" cannot be used in "[]"`,
23
24
  }, defrag(bs)),
24
25
  ], rest],
25
- ([as, bs], rest) => [unshift(as, bs), rest])))));
26
+ ([as, bs], rest) => [unshift(as, bs), rest]))));
@@ -1,9 +1,10 @@
1
1
  import { undefined, Object } from 'spica/global';
2
2
  import { HTMLParser } from '../inline';
3
- import { union, subsequence, some, validate, focus, precedence, creator, surround, open, match, lazy } from '../../combinator';
3
+ import { union, subsequence, some, syntax, validate, focus, surround, open, match, lazy } from '../../combinator';
4
4
  import { inline } from '../inline';
5
5
  import { str } from '../source';
6
- import { isStartLooseNodes, blankWith } from '../util';
6
+ import { Rule } from '../context';
7
+ import { isStartLooseNodes, blankWith } from '../visibility';
7
8
  import { html as h, defrag } from 'typed-dom/dom';
8
9
  import { memoize } from 'spica/memoize';
9
10
  import { Cache } from 'spica/cache';
@@ -18,7 +19,7 @@ const attrspecs = {
18
19
  Object.setPrototypeOf(attrspecs, null);
19
20
  Object.values(attrspecs).forEach(o => Object.setPrototypeOf(o, null));
20
21
 
21
- export const html: HTMLParser = lazy(() => validate('<', validate(/^<[a-z]+(?=[^\S\n]|>)/, creator(precedence(5, union([
22
+ export const html: HTMLParser = lazy(() => validate('<', validate(/^<[a-z]+(?=[^\S\n]|>)/, syntax(Rule.none, 5, union([
22
23
  focus(
23
24
  '<wbr>',
24
25
  () => [[h('wbr')], '']),
@@ -59,7 +60,7 @@ export const html: HTMLParser = lazy(() => validate('<', validate(/^<[a-z]+(?=[^
59
60
  [[elem(tag, as, bs, [])], rest]),
60
61
  ([, tag]) => tag,
61
62
  new Cache(10000))),
62
- ]))))));
63
+ ])))));
63
64
 
64
65
  export const attribute: HTMLParser.TagParser.AttributeParser = union([
65
66
  str(/^[^\S\n]+[a-z]+(?:-[a-z]+)*(?:="(?:\\[^\n]|[^\\\n"])*")?(?=[^\S\n]|>)/),
@@ -1,6 +1,6 @@
1
1
  import { undefined } from 'spica/global';
2
2
  import { HTMLEntityParser, UnsafeHTMLEntityParser } from '../inline';
3
- import { union, validate, focus, creator, fmap } from '../../combinator';
3
+ import { union, creator, validate, focus, fmap } from '../../combinator';
4
4
  import { html } from 'typed-dom/dom';
5
5
  import { reduce } from 'spica/memoize';
6
6
 
@@ -1,12 +1,13 @@
1
1
  import { InsertionParser } from '../inline';
2
- import { union, some, precedence, creator, surround, open, lazy } from '../../combinator';
2
+ import { union, some, syntax, surround, open, lazy } from '../../combinator';
3
3
  import { inline } from '../inline';
4
4
  import { str } from '../source';
5
- import { blankWith } from '../util';
5
+ import { Rule } from '../context';
6
+ import { blankWith } from '../visibility';
6
7
  import { html, defrag } from 'typed-dom/dom';
7
8
  import { unshift } from 'spica/array';
8
9
 
9
- export const insertion: InsertionParser = lazy(() => creator(precedence(1, surround(
10
+ export const insertion: InsertionParser = lazy(() => syntax(Rule.none, 1, surround(
10
11
  str('++'),
11
12
  some(union([
12
13
  some(inline, blankWith('\n', '++')),
@@ -14,4 +15,4 @@ export const insertion: InsertionParser = lazy(() => creator(precedence(1, surro
14
15
  ])),
15
16
  str('++'), false,
16
17
  ([, bs], rest) => [[html('ins', defrag(bs))], rest],
17
- ([as, bs], rest) => [unshift(as, bs), rest]))));
18
+ ([as, bs], rest) => [unshift(as, bs), rest])));
@@ -1,12 +1,14 @@
1
1
  import { undefined, location, encodeURI, decodeURI, Location } from 'spica/global';
2
2
  import { LinkParser, TextLinkParser } from '../inline';
3
3
  import { Result, eval } from '../../combinator/data/parser';
4
- import { union, inits, tails, subsequence, some, validate, guard, context, precedence, creator, surround, open, dup, reverse, lazy, fmap, bind } from '../../combinator';
4
+ import { union, inits, tails, subsequence, some, guard, syntax, state, validate, surround, open, dup, reverse, lazy, fmap, bind } from '../../combinator';
5
5
  import { inline, media, shortmedia } from '../inline';
6
6
  import { attributes } from './html';
7
7
  import { autolink } from '../autolink';
8
8
  import { unescsource, str } from '../source';
9
- import { trimNode, stringify } from '../util';
9
+ import { Rule, State } from '../context';
10
+ import { trimNode } from '../visibility';
11
+ import { stringify } from '../util';
10
12
  import { html, define, defrag } from 'typed-dom/dom';
11
13
  import { ReadonlyURL } from 'spica/url';
12
14
 
@@ -15,27 +17,16 @@ const optspec = {
15
17
  } as const;
16
18
  Object.setPrototypeOf(optspec, null);
17
19
 
18
- export const link: LinkParser = lazy(() => validate(['[', '{'], creator(10, precedence(2, bind(
19
- guard(context => context.syntax?.inline?.link ?? true,
20
+ export const link: LinkParser = lazy(() => validate(['[', '{'], syntax(Rule.link, 2, 10, bind(
21
+ guard(context => ~context.state! & State.link,
20
22
  fmap(subsequence([
21
- context({ syntax: { inline: {
22
- link: false,
23
- }}},
23
+ state(State.link,
24
24
  dup(union([
25
25
  surround('[', media, ']'),
26
26
  surround('[', shortmedia, ']'),
27
27
  surround(
28
28
  '[',
29
- context({ syntax: { inline: {
30
- annotation: false,
31
- reference: false,
32
- index: false,
33
- label: false,
34
- // Redundant
35
- //link: false,
36
- media: false,
37
- autolink: false,
38
- }}},
29
+ state(State.annotation | State.reference | State.index | State.label | State.media | State.autolink,
39
30
  some(inline, ']', [[/^\\?\n/, 9], [']', 2]])),
40
31
  ']',
41
32
  true,
@@ -67,9 +58,9 @@ export const link: LinkParser = lazy(() => validate(['[', '{'], creator(10, prec
67
58
  if (el.classList.contains('invalid')) return [[el], rest];
68
59
  assert(el.classList.length === 0);
69
60
  return [[define(el, attributes('link', [], optspec, params))], rest];
70
- })))));
61
+ }))));
71
62
 
72
- export const textlink: TextLinkParser = lazy(() => validate(['[', '{'], creator(10, precedence(2, bind(
63
+ export const textlink: TextLinkParser = lazy(() => validate(['[', '{'], syntax(Rule.link, 2, 10, bind(
73
64
  reverse(tails([
74
65
  dup(surround('[', some(union([unescsource]), ']'), ']')),
75
66
  dup(surround(/^{(?![{}])/, inits([uri, some(option)]), /^[^\S\n]*}/)),
@@ -92,7 +83,7 @@ export const textlink: TextLinkParser = lazy(() => validate(['[', '{'], creator(
92
83
  assert(!el.classList.contains('invalid'));
93
84
  assert(el.classList.length === 0);
94
85
  return [[define(el, attributes('link', [], optspec, params))], rest];
95
- })))));
86
+ }))));
96
87
 
97
88
  export const uri: LinkParser.ParameterParser.UriParser = fmap(union([
98
89
  open(/^[^\S\n]+/, str(/^\S+/)),
@@ -1,12 +1,13 @@
1
1
  import { MarkParser } from '../inline';
2
- import { union, some, precedence, creator, surround, open, lazy } from '../../combinator';
2
+ import { union, some, syntax, surround, open, lazy } from '../../combinator';
3
3
  import { inline } from '../inline';
4
4
  import { str } from '../source';
5
- import { startTight, blankWith } from '../util';
5
+ import { startTight, blankWith } from '../visibility';
6
+ import { Rule } from '../context';
6
7
  import { html, defrag } from 'typed-dom/dom';
7
8
  import { unshift } from 'spica/array';
8
9
 
9
- export const mark: MarkParser = lazy(() => creator(precedence(1, surround(
10
+ export const mark: MarkParser = lazy(() => syntax(Rule.none, 1, surround(
10
11
  str('=='),
11
12
  startTight(some(union([
12
13
  some(inline, blankWith('==')),
@@ -14,4 +15,4 @@ export const mark: MarkParser = lazy(() => creator(precedence(1, surround(
14
15
  ]))),
15
16
  str('=='), false,
16
17
  ([, bs], rest) => [[html('mark', defrag(bs))], rest],
17
- ([as, bs], rest) => [unshift(as, bs), rest]))));
18
+ ([as, bs], rest) => [unshift(as, bs), rest])));
@@ -1,5 +1,5 @@
1
1
  import { MathParser } from '../inline';
2
- import { union, some, validate, focus, rewrite, precedence, creator, surround, lazy } from '../../combinator';
2
+ import { union, some, creator, precedence, validate, focus, rewrite, surround, lazy } from '../../combinator';
3
3
  import { escsource, unescsource } from '../source';
4
4
  import { html } from 'typed-dom/dom';
5
5
 
@@ -1,10 +1,11 @@
1
1
  import { undefined, location } from 'spica/global';
2
2
  import { MediaParser } from '../inline';
3
- import { union, inits, tails, some, validate, verify, guard, precedence, creator, surround, open, dup, lazy, fmap, bind } from '../../combinator';
3
+ import { union, inits, tails, some, creator, precedence, guard, syntax, validate, verify, surround, open, dup, lazy, fmap, bind } from '../../combinator';
4
4
  import { textlink, uri, option as linkoption, resolve } from './link';
5
5
  import { attributes } from './html';
6
6
  import { unsafehtmlentity } from './htmlentity';
7
7
  import { txt, str } from '../source';
8
+ import { Rule, State } from '../context';
8
9
  import { html, define } from 'typed-dom/dom';
9
10
  import { ReadonlyURL } from 'spica/url';
10
11
  import { unshift, shift, push } from 'spica/array';
@@ -17,9 +18,9 @@ const optspec = {
17
18
  } as const;
18
19
  Object.setPrototypeOf(optspec, null);
19
20
 
20
- export const media: MediaParser = lazy(() => validate(['![', '!{'], creator(10, precedence(2, bind(verify(fmap(open(
21
+ export const media: MediaParser = lazy(() => validate(['![', '!{'], syntax(Rule.media, 2, 10, bind(verify(fmap(open(
21
22
  '!',
22
- guard(context => context.syntax?.inline?.media ?? true,
23
+ guard(context => ~context.state! & State.media,
23
24
  tails([
24
25
  dup(surround(
25
26
  '[',
@@ -52,19 +53,19 @@ export const media: MediaParser = lazy(() => validate(['![', '!{'], creator(10,
52
53
  if (el.hasAttribute('aspect-ratio')) {
53
54
  el.style.aspectRatio = el.getAttribute('aspect-ratio')!;
54
55
  }
55
- if (context.syntax?.inline?.link === false || cache && cache.tagName !== 'IMG') return [[el], rest];
56
+ if (context.state! & State.link || cache && cache.tagName !== 'IMG') return [[el], rest];
56
57
  return fmap(
57
58
  textlink as MediaParser,
58
59
  ([link]) => [define(link, { target: '_blank' }, [el])])
59
60
  (`{ ${INSECURE_URI}${params.join('')} }${rest}`, context);
60
- })))));
61
+ }))));
61
62
 
62
- const bracket: MediaParser.TextParser.BracketParser = lazy(() => union([
63
+ const bracket: MediaParser.TextParser.BracketParser = lazy(() => creator(union([
63
64
  surround(str('('), some(union([unsafehtmlentity, bracket, txt]), ')'), str(')'), true, undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
64
65
  surround(str('['), some(union([unsafehtmlentity, bracket, txt]), ']'), str(']'), true, undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
65
66
  surround(str('{'), some(union([unsafehtmlentity, bracket, txt]), '}'), str('}'), true, undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
66
67
  surround(str('"'), precedence(8, some(union([unsafehtmlentity, txt]), '"')), str('"'), true),
67
- ]));
68
+ ])));
68
69
 
69
70
  const option: MediaParser.ParameterParser.OptionParser = union([
70
71
  fmap(str(/^[^\S\n]+[1-9][0-9]*x[1-9][0-9]*(?=[^\S\n]|})/), ([opt]) => [` width="${opt.slice(1).split('x')[0]}"`, ` height="${opt.slice(1).split('x')[1]}"`]),
@@ -1,35 +1,29 @@
1
1
  import { undefined } from 'spica/global';
2
2
  import { ReferenceParser } from '../inline';
3
- import { union, subsequence, some, validate, guard, context, precedence, creator, recursion, surround, open, lazy, bind } from '../../combinator';
3
+ import { union, subsequence, some, context, creator, guard, syntax, state, validate, surround, open, lazy, bind } from '../../combinator';
4
4
  import { inline } from '../inline';
5
5
  import { optimize } from './link';
6
6
  import { str, stropt } from '../source';
7
- import { regBlankStart, startLoose, trimNode, stringify } from '../util';
7
+ import { Rule, State } from '../context';
8
+ import { regBlankStart, startLoose, trimNode } from '../visibility';
9
+ import { stringify } from '../util';
8
10
  import { html, defrag } from 'typed-dom/dom';
9
11
 
10
- export const reference: ReferenceParser = lazy(() => validate('[[', creator(recursion(precedence(6, surround(
12
+ export const reference: ReferenceParser = lazy(() => validate('[[', syntax(Rule.reference, 6, surround(
11
13
  '[[',
12
- guard(context => context.syntax?.inline?.reference ?? true,
14
+ guard(context => ~context.state! & State.reference,
15
+ state(State.annotation | State.reference | State.media,
13
16
  startLoose(
14
- context({ syntax: { inline: {
15
- annotation: false,
16
- reference: false,
17
- media: false,
18
- // Redundant
19
- //index: true,
20
- //label: true,
21
- //link: true,
22
- //autolink: true,
23
- }}, delimiters: undefined },
17
+ context({ delimiters: undefined },
24
18
  subsequence([
25
19
  abbr,
26
20
  open(stropt(/^(?=\^)/), some(inline, ']', [[/^\\?\n/, 9], [']', 2], [']]', 6]])),
27
21
  some(inline, ']', [[/^\\?\n/, 9], [']', 2], [']]', 6]]),
28
- ])), ']')),
22
+ ])), ']'))),
29
23
  ']]',
30
24
  false,
31
25
  ([, ns], rest) => [[html('sup', attributes(ns), [html('span', trimNode(defrag(ns)))])], rest],
32
- ([, ns, rest], next) => next[0] === ']' ? undefined : optimize('[[', ns, rest)))))));
26
+ ([, ns, rest], next) => next[0] === ']' ? undefined : optimize('[[', ns, rest)))));
33
27
 
34
28
  const abbr: ReferenceParser.AbbrParser = creator(bind(surround(
35
29
  '^',
@@ -1,14 +1,15 @@
1
1
  import { undefined } from 'spica/global';
2
2
  import { RubyParser } from '../inline';
3
3
  import { eval, exec } from '../../combinator/data/parser';
4
- import { sequence, validate, verify, focus, creator, surround, lazy, fmap } from '../../combinator';
4
+ import { sequence, syntax, creator, validate, verify, focus, surround, lazy, fmap } from '../../combinator';
5
5
  import { unsafehtmlentity } from './htmlentity';
6
6
  import { text as txt } from '../source';
7
- import { isStartTightNodes } from '../util';
7
+ import { Rule } from '../context';
8
+ import { isStartTightNodes } from '../visibility';
8
9
  import { html, defrag } from 'typed-dom/dom';
9
10
  import { unshift, push } from 'spica/array';
10
11
 
11
- export const ruby: RubyParser = lazy(() => validate('[', creator(fmap(verify(
12
+ export const ruby: RubyParser = lazy(() => validate('[', syntax(Rule.none, 2, fmap(verify(
12
13
  sequence([
13
14
  surround('[', focus(/^(?:\\[^\n]|[^\\[\](){}"\n])+(?=]\()/, text), ']'),
14
15
  surround('(', focus(/^(?:\\[^\n]|[^\\[\](){}"\n])+(?=\))/, text), ')'),
@@ -1,10 +1,11 @@
1
1
  import { ShortmediaParser } from '../inline';
2
- import { union, rewrite, guard, open, convert } from '../../combinator';
2
+ import { union, guard, rewrite, open, convert } from '../../combinator';
3
3
  import { url } from './autolink/url';
4
4
  import { media } from './media';
5
+ import { State } from '../context';
5
6
 
6
7
  export const shortmedia: ShortmediaParser = rewrite(
7
- guard(context => context.syntax?.inline?.media ?? true,
8
+ guard(context => ~context.state! & State.media,
8
9
  open('!', url)),
9
10
  convert(
10
11
  source => `!{ ${source.slice(1)} }`,
@@ -1,13 +1,14 @@
1
1
  import { StrongParser } from '../inline';
2
- import { union, some, precedence, creator, surround, open, lazy } from '../../combinator';
2
+ import { union, some, syntax, surround, open, lazy } from '../../combinator';
3
3
  import { inline } from '../inline';
4
4
  import { emstrong } from './emstrong';
5
5
  import { str } from '../source';
6
- import { startTight, blankWith } from '../util';
6
+ import { Rule } from '../context';
7
+ import { startTight, blankWith } from '../visibility';
7
8
  import { html, defrag } from 'typed-dom/dom';
8
9
  import { unshift } from 'spica/array';
9
10
 
10
- export const strong: StrongParser = lazy(() => creator(precedence(1, surround(
11
+ export const strong: StrongParser = lazy(() => syntax(Rule.none, 1, surround(
11
12
  str('**'),
12
13
  startTight(some(union([
13
14
  some(inline, blankWith('**')),
@@ -18,4 +19,4 @@ export const strong: StrongParser = lazy(() => creator(precedence(1, surround(
18
19
  ])), '*'),
19
20
  str('**'), false,
20
21
  ([, bs], rest) => [[html('strong', defrag(bs))], rest],
21
- ([as, bs], rest) => [unshift(as, bs), rest]))));
22
+ ([as, bs], rest) => [unshift(as, bs), rest])));
@@ -10,7 +10,7 @@ describe('Unit: parser/inline/template', () => {
10
10
  assert.deepStrictEqual(inspect(parser('')), undefined);
11
11
  assert.deepStrictEqual(inspect(parser('{')), undefined);
12
12
  assert.deepStrictEqual(inspect(parser('{}')), undefined);
13
- assert.deepStrictEqual(inspect(parser('{{')), undefined);
13
+ assert.deepStrictEqual(inspect(parser('{{')), [['', '{{'], '']);
14
14
  assert.deepStrictEqual(inspect(parser('{{\\}}')), undefined);
15
15
  assert.deepStrictEqual(inspect(parser('{{a}b}')), undefined);
16
16
  assert.deepStrictEqual(inspect(parser('{{{a}}')), undefined);
@@ -1,17 +1,20 @@
1
1
  import { undefined } from 'spica/global';
2
2
  import { TemplateParser } from '../inline';
3
- import { union, some, rewrite, precedence, creator, surround, lazy } from '../../combinator';
3
+ import { union, some, syntax, creator, precedence, surround, lazy } from '../../combinator';
4
+ import { optimize } from './link';
4
5
  import { escsource, str } from '../source';
6
+ import { Rule } from '../context';
5
7
  import { html } from 'typed-dom/dom';
6
8
  import { unshift } from 'spica/array';
7
9
 
8
- export const template: TemplateParser = lazy(() => creator(precedence(2, rewrite(
9
- surround('{{', some(union([bracket, escsource]), '}'), '}}', true),
10
- source => [[html('span', { class: 'template' }, source.replace(/\x1B/g, ''))], '']))));
10
+ export const template: TemplateParser = lazy(() => syntax(Rule.none, 2, surround(
11
+ '{{', some(union([bracket, escsource]), '}'), '}}', true,
12
+ ([, ns = []], rest) => [[html('span', { class: 'template' }, `{{${ns.join('').replace(/\x1B/g, '')}}}`)], rest],
13
+ ([, ns = [], rest], next) => next[0] === '}' ? undefined : optimize('{{', ns, rest))));
11
14
 
12
- const bracket: TemplateParser.BracketParser = lazy(() => union([
15
+ const bracket: TemplateParser.BracketParser = lazy(() => creator(union([
13
16
  surround(str('('), some(union([bracket, escsource]), ')'), str(')'), true, undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
14
17
  surround(str('['), some(union([bracket, escsource]), ']'), str(']'), true, undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
15
18
  surround(str('{'), some(union([bracket, escsource]), '}'), str('}'), true, undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
16
19
  surround(str('"'), precedence(8, some(escsource, /^"|^\\?\n/)), str('"'), true),
17
- ]));
20
+ ])));
@@ -2,6 +2,7 @@ import { Parser } from '../combinator/data/parser';
2
2
  import { fmap } from '../combinator';
3
3
  import { japanese } from './locale/ja';
4
4
  import { html } from 'typed-dom/dom';
5
+ import { duffEach } from 'spica/duff';
5
6
 
6
7
  export function localize<P extends Parser<HTMLElement | string>>(parser: P): P;
7
8
  export function localize(parser: Parser<HTMLElement | string>): Parser<HTMLElement | string> {
@@ -10,13 +11,11 @@ export function localize(parser: Parser<HTMLElement | string>): Parser<HTMLEleme
10
11
  const el = ns.length === 1 && typeof ns[0] === 'object'
11
12
  ? ns[0]
12
13
  : html('div', ns);
13
- const es = el.querySelectorAll('.linebreak:not(:empty)');
14
- for (let i = 0, len = es.length; i < len; ++i) {
15
- const sb = es[i];
16
- assert(sb.firstChild!.textContent === ' ');
17
- if (!check(sb)) continue;
18
- sb.firstChild!.remove();
19
- }
14
+ duffEach(el.querySelectorAll('.linebreak:not(:empty)'), el => {
15
+ assert(el.firstChild!.textContent === ' ');
16
+ if (!check(el)) return;
17
+ el.firstChild!.remove();
18
+ });
20
19
  return ns;
21
20
  });
22
21
  }
@@ -2,6 +2,7 @@ import { undefined, Infinity, Map, Node } from 'spica/global';
2
2
  import { text } from '../inline/extension/indexee';
3
3
  import { frag, html, define } from 'typed-dom/dom';
4
4
  import { MultiMap } from 'spica/multimap';
5
+ import { duffEach, duffReduce } from 'spica/duff';
5
6
  import { push } from 'spica/array';
6
7
 
7
8
  export function* footnote(
@@ -12,7 +13,7 @@ export function* footnote(
12
13
  ): Generator<HTMLAnchorElement | HTMLLIElement | undefined, undefined, undefined> {
13
14
  // Bug: Firefox
14
15
  //target.querySelectorAll(`:scope > .annotations`).forEach(el => el.remove());
15
- target.querySelectorAll(`.annotations`).forEach(el => el.parentNode === target && el.remove());
16
+ duffEach(target.querySelectorAll(`.annotations`), el => el.parentNode === target && el.remove());
16
17
  yield* reference(target, footnotes?.references, opts, bottom);
17
18
  yield* annotation(target, footnotes?.annotations, opts, bottom);
18
19
  return;
@@ -40,8 +41,9 @@ function build(
40
41
  const titles = new Map<string, string>();
41
42
  // Bug: Firefox
42
43
  //const splitters = push([], target.querySelectorAll(`:scope > :is(${splitter ?? '_'})`));
43
- const splitters = push([], target.querySelectorAll(splitter ?? '_'))
44
- .filter(el => el.parentNode === target);
44
+ const splitters = duffReduce(target.querySelectorAll(splitter ?? '_'), (acc, el) =>
45
+ el.parentNode === target ? push(acc, [el]) : acc
46
+ , [] as Element[]);
45
47
  let count = 0;
46
48
  let total = 0;
47
49
  let style: 'count' | 'abbr';
@@ -1,6 +1,6 @@
1
1
  import { undefined } from 'spica/global';
2
2
  import { TextParser, TxtParser, LinebreakParser } from '../source';
3
- import { union, focus, creator } from '../../combinator';
3
+ import { union, creator, focus } from '../../combinator';
4
4
  import { str } from './str';
5
5
  import { html } from 'typed-dom/dom';
6
6