Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite logic as patterns and transforms #42

Merged
merged 4 commits into from
Aug 7, 2017
Merged

rewrite logic as patterns and transforms #42

merged 4 commits into from
Aug 7, 2017

Conversation

arve0
Copy link
Owner

@arve0 arve0 commented Aug 7, 2017

Fixes issue #32

The new logic works like this:

  1. Try matching pattern to token stream.
  2. If pattern matches, run pattern.transform.
  3. pattern.transform finds attributes and alters token stream accordingly.

A patterns consist of several tests. Each test must contain either
shift or position. For each token, tests of all patterns are rerun, such
that you can match which tokens follow each other in the token stream.

Example:

{
  /**
   * | h1 |
   * | -- |
   * | c1 |
   * {.c}
   */
  name: 'tables',
  tests: [
    {
      shift: 0,
      type: 'table_close'
    }, {
      shift: 1,
      type: 'paragraph_open'
    }, {
      shift: 2,
      type: 'inline',
      content: utils.hasCurly('only')
    }
  ],...

The pattern above will match the token stream if

  1. current token is of type table_close
  2. the next token is a paragraph_open
  3. the token after paragraph_open is of type inline
  4. and it has content with curly

One may also specify patterns of children:

    /**
     * - item
     * {.a}
     */
    name: 'list softbreak',
    tests: [
      {
        shift: -2,
        type: 'list_item_open'
      }, {
        shift: 0,
        type: 'inline',
        children: [
          {
            position: -2,
            type: 'softbreak'
          }, {
            position: -1,
            content: utils.hasCurly('only')
          }
        ]
      }
    ],...

The children pattern is similar as previous example, but uses position
here instead of shift. This makes it easy to specify last token (position: -1).

A transform is usually something like this:

  1. get attributes
  2. find correct token where attributes should be set
  3. set attributes
  4. remove curly from output

Example:

transform: (tokens, i) => {
  let token = tokens[i];
  let start = token.info.lastIndexOf('{');
  let attrs = utils.getAttrs(token.info, start);
  utils.addAttrs(attrs, token);
  token.info = utils.removeCurly(token.info);
}

Note that keys in tests may be:

  1. boolean | number | string: strict compare with token
  2. function: function is called with token[key], should return true or false
  3. array of functions: same as above, but all functions are called
  4. array of tests: only for the key children, same as a token test,
    but will loop childrens of token, note that token should then be of type 'inline'

arve0 added 4 commits August 5, 2017 08:59
The logic works like this:

1. Try matching pattern to token stream.
2. If pattern matches, run `pattern.transform`.
3. `pattern.transform` finds attributes and alters token stream accordingly.

A patterns consist of several tests. Each test must contain either
`shift` or `position`. For each token, tests of all patterns are rerun, such
that you can match which tokens follow each other in the token stream.

Example:

```js
{
  /**
   * | h1 |
   * | -- |
   * | c1 |
   * {.c}
   */
  name: 'tables',
  tests: [
    {
      shift: 0,
      type: 'table_close'
    }, {
      shift: 1,
      type: 'paragraph_open'
    }, {
      shift: 2,
      type: 'inline',
      content: utils.hasCurly('only')
    }
  ],...
```

The pattern above will match the token stream if
1. current token is of type table_close
2. the next token is a paragraph_open
3. the token after paragraph_open is of type inline
4. and it has content with curly

One may also specify patterns of children:
```js
    /**
     * - item
     * {.a}
     */
    name: 'list softbreak',
    tests: [
      {
        shift: -2,
        type: 'list_item_open'
      }, {
        shift: 0,
        type: 'inline',
        children: [
          {
            position: -2,
            type: 'softbreak'
          }, {
            position: -1,
            content: utils.hasCurly('only')
          }
        ]
      }
    ],...
```

The children pattern is similar as previous example, but uses `position`
here instead of `shift`. This makes it easy to specify last token (position: -1).

A transform is usually something like this:
1. get attributes
2. find correct token where attributes should be set
3. set attributes
4. remove curly from output

Example:
```js
transform: (tokens, i) => {
  let token = tokens[i];
  let start = token.info.lastIndexOf('{');
  let attrs = utils.getAttrs(token.info, start);
  utils.addAttrs(attrs, token);
  token.info = utils.removeCurly(token.info);
}
```

Note that keys in tests may be:

1. boolean | number | string: strict compare with token
2. function: function is called with token[key], should return true or false
3. array of functions: same as above, but all functions are called
4. array of tests: only for the key `children`, same as a token test,
   but will loop childrens of token, note that token should then be of type 'inline'
@arve0 arve0 merged commit f5c6e81 into master Aug 7, 2017
@arve0 arve0 deleted the nested_lists branch August 7, 2017 22:29
@arve0 arve0 restored the nested_lists branch November 12, 2023 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant