Skip to content

Commit

Permalink
Implement parsing file management sigils for external paths
Browse files Browse the repository at this point in the history
This is the easier option with the path outside of the code block.
  • Loading branch information
TomasHubelbauer committed Oct 28, 2024
1 parent d558c35 commit 5399deb
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 30 deletions.
1 change: 1 addition & 0 deletions Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export type Block = {
meta: string;
code: string;
path?: string;
mode?: 'create' | 'append' | 'match';
};
138 changes: 138 additions & 0 deletions extractBlocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,141 @@ test(
}
])
);

test.only(
'path: external, tag: no, sigil: none',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```\ntest\n```\n')).toEqual([
{
tag: '',
meta: '',
code: 'test\n',
path: 'file-name.ext'
}
])
);

test.only(
'path: external, tag: no, sigil: append',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```!\ntest\n```\n')).toEqual([
{
tag: '',
meta: '',
code: 'test\n',
path: 'file-name.ext',
mode: 'append'
}
])
);

test.only(
'path: external, tag: no, sigil: match',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```?\ntest\n```\n')).toEqual([
{
tag: '',
meta: '',
code: 'test\n',
path: 'file-name.ext',
mode: 'match'
}
])
);

test.only(
'path: external, tag: yes, sigil: none, meta: no',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```txt\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: '',
code: 'test\n',
path: 'file-name.ext'
}
])
);

test.only(
'path: external, tag: yes, sigil: append, meta: no',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```txt !\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: '',
code: 'test\n',
path: 'file-name.ext',
mode: 'append'
}
])
);

test.only(
'path: external, tag: yes, sigil: match, meta: no',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```txt ?\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: '',
code: 'test\n',
path: 'file-name.ext',
mode: 'match'
}
])
);

test.only(
'path: external, tag: yes, sigil: none, meta: yes',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```txt test\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: 'test',
code: 'test\n',
path: 'file-name.ext'
}
])
);

test.only(
'path: external, tag: yes, sigil: none, meta: !yes',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```txt !test\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: '!test',
code: 'test\n',
path: 'file-name.ext'
}
])
);

test.only(
'path: external, tag: yes, sigil: none, meta: ?yes',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```txt ?test\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: '?test',
code: 'test\n',
path: 'file-name.ext'
}
])
);

test.only(
'path: external, tag: yes, sigil: append, meta: yes',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```txt ! test\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: 'test',
code: 'test\n',
path: 'file-name.ext',
mode: 'append'
}
])
);

test.only(
'path: external, tag: yes, sigil: match, meta: yes',
() => expect(extractBlocks('test\n`file-name.ext`:\n\n```txt ? test\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: 'test',
code: 'test\n',
path: 'file-name.ext',
mode: 'match'
}
])
);
90 changes: 90 additions & 0 deletions extractBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default function extractBlocks(text: string) {
| 'path'
| 'path-end'
| 'block-tag'
| 'block-meta-or-mode'
| 'block-meta-after-mode'
| 'block-meta'
| 'code-line-start'
| 'code-line'
Expand All @@ -31,6 +33,7 @@ export default function extractBlocks(text: string) {
let meta = '';
let code = '';
let path = '';
let mode: undefined | 'create' | 'append' | 'match';
let verbatim = false;
const blocks: Block[] = [];
for (let index = 0; index < text.length; index++) {
Expand Down Expand Up @@ -123,6 +126,16 @@ export default function extractBlocks(text: string) {
state = 'block-tag';
break;
}
case '!': {
mode = 'append';
state = 'block-meta';
break;
}
case '?': {
mode = 'match';
state = 'block-meta';
break;
}
case '\n': {
state = 'code-line-start';
break;
Expand Down Expand Up @@ -176,13 +189,69 @@ export default function extractBlocks(text: string) {
break;
}
case ' ': {
state = 'block-meta-or-mode';
break;
}
case '\n': {
state = 'code-line-start';
break;
}
}

break;
}
case 'block-meta-or-mode': {
switch (character) {
case '\n': {
state = 'code-line-start';
break;
}
case '!': {
mode = 'append';
state = 'block-meta-after-mode';
break;
}
case '?': {
mode = 'match';
state = 'block-meta-after-mode';
break;
}
default: {
meta += character;
state = 'block-meta';
break;
}
}

break;
}
case 'block-meta-after-mode': {
switch (character) {
case '\n': {
state = 'code-line-start';
break;
}
case ' ': {
state = 'block-meta';
break;
}
default: {
switch (mode) {
case 'append': {
meta += '!';
break;
}
case 'match': {
meta += '?';
break;
}
}

meta += character;
mode = undefined;
state = 'block-meta';
break;
}
}

break;
Expand Down Expand Up @@ -270,17 +339,23 @@ export default function extractBlocks(text: string) {
case '\n': {
if (!verbatim) {
const block: Block = { tag, meta, code };

if (path) {
block.path = path;
}

if (mode) {
block.mode = mode;
}

blocks.push(block);
}

tag = '';
meta = '';
code = '';
path = '';
mode = undefined;
state = 'line-start';
break;
}
Expand Down Expand Up @@ -393,10 +468,15 @@ export default function extractBlocks(text: string) {
case 'block-end-tick-3': {
if (!verbatim) {
const block: Block = { tag, meta, code };

if (path) {
block.path = path;
}

if (mode) {
block.mode = mode;
}

blocks.push(block);
}

Expand All @@ -406,10 +486,15 @@ export default function extractBlocks(text: string) {
code += '`';
if (!verbatim) {
const block: Block = { tag, meta, code };

if (path) {
block.path = path;
}

if (mode) {
block.mode = mode;
}

blocks.push(block);
}

Expand All @@ -419,10 +504,15 @@ export default function extractBlocks(text: string) {
code += '``';
if (!verbatim) {
const block: Block = { tag, meta, code };

if (path) {
block.path = path;
}

if (mode) {
block.mode = mode;
}

blocks.push(block);
}

Expand Down
33 changes: 3 additions & 30 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,37 +174,10 @@ Other sigils supported instead of the trailing `.` are `!` to mark as append to
the file and `?` to mark as a check for the file's contents match with the code
block content.

### Recognize append and match sigils with externalized related file name
### Respect the file management sigils on `Block` (`append` and `match`)

Once we implement the above, the related file name and create, append and match
sigils will be clear, but when the related file name is external like this:

~~~
`test`:
```
test
```
~~~

There should be a way to specify the sigils still without affecting the language
tag and the meta argument array.

Probably like this:

- `!` means append to the related file
- `?` means match with the related file
- `.` is not needed in this case, the lack of `!` and `?` implies it, but maybe
still support it for explicitness?
- `tag !` allows language tag for append
- `tag ?` allows language tag for match
- `tag .` allows language tag for create which is redundant but maybe support
anyway for explicitness?
- `tag ! meta` to support meta arguments for the handler
- `tag ? meta` to support meta arguments for the handler
- `tag . meta` to support meta arguments for the handler

Deleting files is left to the `sh` handler.
We're parsing these now but we have yet to start honoring them in the file
management handling code.

### Consider how to implement the `patch` and `diff` language tags

Expand Down

0 comments on commit 5399deb

Please sign in to comment.