@kubohiroya/turbowarp-text-lines 0.1.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.
- package/.github/workflows/ci.yml +17 -0
- package/README.md +66 -0
- package/package.json +21 -0
- package/pnpm-workspace.yaml +4 -0
- package/scripts/generate-readme.mjs +57 -0
- package/src/block-definitions.json +34 -0
- package/src/extension.ts +72 -0
- package/src/globals.d.ts +16 -0
- package/src/index.ts +7 -0
- package/tests/extension.test.ts +48 -0
- package/tsconfig.json +14 -0
- package/vite.config.ts +15 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
check:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
- uses: actions/setup-node@v4
|
|
14
|
+
with:
|
|
15
|
+
node-version: 22
|
|
16
|
+
- run: npm install
|
|
17
|
+
- run: npm run check
|
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# TurboWarp Text Lines
|
|
2
|
+
|
|
3
|
+
A TurboWarp extension for counting, reading, and splitting text by lines.
|
|
4
|
+
|
|
5
|
+
## Blocks
|
|
6
|
+
|
|
7
|
+
<!-- BEGIN GENERATED BLOCKS -->
|
|
8
|
+
|
|
9
|
+
### `number of lines in [TEXT]`
|
|
10
|
+
|
|
11
|
+
Returns the number of lines in the supplied text.
|
|
12
|
+
|
|
13
|
+
| Property | Value |
|
|
14
|
+
|---|---|
|
|
15
|
+
| Type | Reporter |
|
|
16
|
+
| Opcode | `lineCount` |
|
|
17
|
+
| `TEXT` | String, default: `first line\nsecond line` |
|
|
18
|
+
|
|
19
|
+
### `line [LINE] of [TEXT]`
|
|
20
|
+
|
|
21
|
+
Returns one line using a one-based line number. Invalid line numbers return an empty string.
|
|
22
|
+
|
|
23
|
+
| Property | Value |
|
|
24
|
+
|---|---|
|
|
25
|
+
| Type | Reporter |
|
|
26
|
+
| Opcode | `lineAt` |
|
|
27
|
+
| `TEXT` | String, default: `first line\nsecond line` |
|
|
28
|
+
| `LINE` | Number, default: `1` |
|
|
29
|
+
|
|
30
|
+
### `put lines of [TEXT] into list [LIST]`
|
|
31
|
+
|
|
32
|
+
Replaces the contents of the named Scratch list with the lines of the supplied text.
|
|
33
|
+
|
|
34
|
+
| Property | Value |
|
|
35
|
+
|---|---|
|
|
36
|
+
| Type | Command |
|
|
37
|
+
| Opcode | `writeLinesToList` |
|
|
38
|
+
| `TEXT` | String, default: `first line\nsecond line` |
|
|
39
|
+
| `LIST` | String, default: `lines` |
|
|
40
|
+
|
|
41
|
+
<!-- END GENERATED BLOCKS -->
|
|
42
|
+
|
|
43
|
+
Line numbers are one-based. An invalid line number returns an empty string. The list block replaces the contents of the named Scratch list.
|
|
44
|
+
|
|
45
|
+
The extension accepts LF, CRLF, and CR line endings.
|
|
46
|
+
|
|
47
|
+
## Development
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm install
|
|
51
|
+
npm run check
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Regenerate the block documentation after changing `src/block-definitions.json`:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npm run docs
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The build produces `dist/text-lines.js`.
|
|
61
|
+
|
|
62
|
+
Because the extension writes directly to Scratch list variables, load it as an unsandboxed custom extension.
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
MPL-2.0
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kubohiroya/turbowarp-text-lines",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite build --watch",
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"build": "vite build",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"docs": "node scripts/generate-readme.mjs",
|
|
12
|
+
"docs:check": "pnpm run docs && git diff --exit-code -- README.md",
|
|
13
|
+
"check": "pnpm run typecheck && pnpm run test && pnpm run build && pnpm run docs:check"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@kubohiroya/vite-plugin-turbowarp-extension": "0.1.0",
|
|
17
|
+
"typescript": "^5.9.3",
|
|
18
|
+
"vite": "^7.3.6",
|
|
19
|
+
"vitest": "^4.1.10"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {readFile, writeFile} from 'node:fs/promises';
|
|
2
|
+
|
|
3
|
+
const START = '<!-- BEGIN GENERATED BLOCKS -->';
|
|
4
|
+
const END = '<!-- END GENERATED BLOCKS -->';
|
|
5
|
+
|
|
6
|
+
const definitions = JSON.parse(
|
|
7
|
+
await readFile(new URL('../src/block-definitions.json', import.meta.url), 'utf8')
|
|
8
|
+
);
|
|
9
|
+
const readmeUrl = new URL('../README.md', import.meta.url);
|
|
10
|
+
const readme = await readFile(readmeUrl, 'utf8');
|
|
11
|
+
|
|
12
|
+
const generated = definitions.blocks.map(renderBlock).join('\n\n');
|
|
13
|
+
const replacement = `${START}\n\n${generated}\n\n${END}`;
|
|
14
|
+
|
|
15
|
+
if (!readme.includes(START) || !readme.includes(END)) {
|
|
16
|
+
throw new Error('README.md does not contain the generated block markers.');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const next = readme.replace(
|
|
20
|
+
new RegExp(`${escapeRegExp(START)}[\\s\\S]*?${escapeRegExp(END)}`),
|
|
21
|
+
replacement
|
|
22
|
+
);
|
|
23
|
+
await writeFile(readmeUrl, next);
|
|
24
|
+
|
|
25
|
+
function renderBlock(block) {
|
|
26
|
+
const rows = [
|
|
27
|
+
['Type', titleCase(block.blockType)],
|
|
28
|
+
['Opcode', `\`${block.opcode}\``]
|
|
29
|
+
];
|
|
30
|
+
for (const [name, argument] of Object.entries(block.arguments ?? {})) {
|
|
31
|
+
rows.push([
|
|
32
|
+
`\`${name}\``,
|
|
33
|
+
`${titleCase(argument.type)}, default: \`${formatDefault(argument.defaultValue)}\``
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
36
|
+
return [
|
|
37
|
+
`### \`${block.text}\``,
|
|
38
|
+
'',
|
|
39
|
+
block.description,
|
|
40
|
+
'',
|
|
41
|
+
'| Property | Value |',
|
|
42
|
+
'|---|---|',
|
|
43
|
+
...rows.map(([name, value]) => `| ${name} | ${value} |`)
|
|
44
|
+
].join('\n');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function titleCase(value) {
|
|
48
|
+
return value.charAt(0) + value.slice(1).toLowerCase();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function formatDefault(value) {
|
|
52
|
+
return String(value).replaceAll('\\', '\\\\').replaceAll('\n', '\\n').replaceAll('`', '\\`');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function escapeRegExp(value) {
|
|
56
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
57
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extensionName": "Text Lines",
|
|
3
|
+
"blocks": [
|
|
4
|
+
{
|
|
5
|
+
"opcode": "lineCount",
|
|
6
|
+
"blockType": "REPORTER",
|
|
7
|
+
"text": "number of lines in [TEXT]",
|
|
8
|
+
"description": "Returns the number of lines in the supplied text.",
|
|
9
|
+
"arguments": {
|
|
10
|
+
"TEXT": {"type": "STRING", "defaultValue": "first line\nsecond line"}
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"opcode": "lineAt",
|
|
15
|
+
"blockType": "REPORTER",
|
|
16
|
+
"text": "line [LINE] of [TEXT]",
|
|
17
|
+
"description": "Returns one line using a one-based line number. Invalid line numbers return an empty string.",
|
|
18
|
+
"arguments": {
|
|
19
|
+
"TEXT": {"type": "STRING", "defaultValue": "first line\nsecond line"},
|
|
20
|
+
"LINE": {"type": "NUMBER", "defaultValue": 1}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"opcode": "writeLinesToList",
|
|
25
|
+
"blockType": "COMMAND",
|
|
26
|
+
"text": "put lines of [TEXT] into list [LIST]",
|
|
27
|
+
"description": "Replaces the contents of the named Scratch list with the lines of the supplied text.",
|
|
28
|
+
"arguments": {
|
|
29
|
+
"TEXT": {"type": "STRING", "defaultValue": "first line\nsecond line"},
|
|
30
|
+
"LIST": {"type": "STRING", "defaultValue": "lines"}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
package/src/extension.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import definitions from './block-definitions.json' with {type: 'json'};
|
|
2
|
+
|
|
3
|
+
type BlockTypeName = 'COMMAND' | 'REPORTER';
|
|
4
|
+
type ArgumentTypeName = 'STRING' | 'NUMBER';
|
|
5
|
+
|
|
6
|
+
interface DefinitionArgument {
|
|
7
|
+
type: ArgumentTypeName;
|
|
8
|
+
defaultValue: string | number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface BlockDefinition {
|
|
12
|
+
opcode: string;
|
|
13
|
+
blockType: BlockTypeName;
|
|
14
|
+
text: string;
|
|
15
|
+
description: string;
|
|
16
|
+
arguments: Record<string, DefinitionArgument>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const blockDefinitions = definitions.blocks as readonly BlockDefinition[];
|
|
20
|
+
|
|
21
|
+
export class TextLinesExtension {
|
|
22
|
+
getInfo() {
|
|
23
|
+
const translate = Scratch.translate;
|
|
24
|
+
return {
|
|
25
|
+
id: 'kubohiroyatextlines',
|
|
26
|
+
name: translate(definitions.extensionName),
|
|
27
|
+
color1: '#5B80A5',
|
|
28
|
+
blocks: blockDefinitions.map((block) => this.toScratchBlock(block))
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
lineCount(args: {TEXT: unknown}): number {
|
|
33
|
+
return splitLines(Scratch.Cast.toString(args.TEXT)).length;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
lineAt(args: {TEXT: unknown; LINE: unknown}): string {
|
|
37
|
+
const lines = splitLines(Scratch.Cast.toString(args.TEXT));
|
|
38
|
+
const line = Math.trunc(Scratch.Cast.toNumber(args.LINE));
|
|
39
|
+
if (!Number.isFinite(line) || line < 1 || line > lines.length) return '';
|
|
40
|
+
return lines[line - 1] ?? '';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
writeLinesToList(args: {TEXT: unknown; LIST: unknown}, util: ScratchBlockUtility): void {
|
|
44
|
+
const name = Scratch.Cast.toString(args.LIST);
|
|
45
|
+
const variable =
|
|
46
|
+
util.target.lookupVariableByNameAndType(name, 'list') ??
|
|
47
|
+
Scratch.vm.runtime.getTargetForStage().lookupVariableByNameAndType(name, 'list');
|
|
48
|
+
if (!variable) throw new Error(`List not found: ${name}`);
|
|
49
|
+
variable.value = splitLines(Scratch.Cast.toString(args.TEXT));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private toScratchBlock(block: BlockDefinition): Record<string, unknown> {
|
|
53
|
+
return {
|
|
54
|
+
opcode: block.opcode,
|
|
55
|
+
blockType: Scratch.BlockType[block.blockType],
|
|
56
|
+
text: Scratch.translate(block.text),
|
|
57
|
+
arguments: Object.fromEntries(
|
|
58
|
+
Object.entries(block.arguments).map(([name, argument]) => [
|
|
59
|
+
name,
|
|
60
|
+
{
|
|
61
|
+
type: Scratch.ArgumentType[argument.type],
|
|
62
|
+
defaultValue: argument.defaultValue
|
|
63
|
+
}
|
|
64
|
+
])
|
|
65
|
+
)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function splitLines(text: string): string[] {
|
|
71
|
+
return text.split(/\r\n|\n|\r/);
|
|
72
|
+
}
|
package/src/globals.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface ScratchVariable { value: unknown[]; type: string; name: string; }
|
|
2
|
+
interface ScratchTarget { lookupVariableByNameAndType(name: string, type: string): ScratchVariable | null; }
|
|
3
|
+
interface ScratchBlockUtility { target: ScratchTarget; }
|
|
4
|
+
interface ScratchTranslate {
|
|
5
|
+
(text: string): string;
|
|
6
|
+
(message: {default: string; description?: string}, placeholders?: Record<string, string | number>): string;
|
|
7
|
+
}
|
|
8
|
+
interface ScratchApi {
|
|
9
|
+
extensions: { unsandboxed: boolean; register(extension: unknown): void };
|
|
10
|
+
BlockType: Record<'COMMAND' | 'REPORTER', string>;
|
|
11
|
+
ArgumentType: Record<'STRING' | 'NUMBER', string>;
|
|
12
|
+
Cast: { toString(value: unknown): string; toNumber(value: unknown): number };
|
|
13
|
+
translate: ScratchTranslate;
|
|
14
|
+
vm: { runtime: { getTargetForStage(): ScratchTarget } };
|
|
15
|
+
}
|
|
16
|
+
declare const Scratch: ScratchApi;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
|
2
|
+
import {TextLinesExtension, splitLines} from '../src/extension.js';
|
|
3
|
+
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
vi.stubGlobal('Scratch', {
|
|
6
|
+
Cast: {
|
|
7
|
+
toString: (value: unknown) => String(value ?? ''),
|
|
8
|
+
toNumber: (value: unknown) => Number(value)
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('splitLines', () => {
|
|
14
|
+
it('supports LF, CRLF, and CR', () => {
|
|
15
|
+
expect(splitLines('a\nb\r\nc\rd')).toEqual(['a', 'b', 'c', 'd']);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('TextLinesExtension', () => {
|
|
20
|
+
it('counts lines', () => {
|
|
21
|
+
const extension = new TextLinesExtension();
|
|
22
|
+
expect(extension.lineCount({TEXT: 'a\nb'})).toBe(2);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('returns a one-based line', () => {
|
|
26
|
+
const extension = new TextLinesExtension();
|
|
27
|
+
expect(extension.lineAt({TEXT: 'a\nb', LINE: 2})).toBe('b');
|
|
28
|
+
expect(extension.lineAt({TEXT: 'a\nb', LINE: 3})).toBe('');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('replaces a Scratch list with lines', () => {
|
|
32
|
+
const extension = new TextLinesExtension();
|
|
33
|
+
const list = {name: 'lines', type: 'list', value: [] as unknown[]};
|
|
34
|
+
const target = {
|
|
35
|
+
lookupVariableByNameAndType: (name: string, type: string) =>
|
|
36
|
+
name === 'lines' && type === 'list' ? list : null
|
|
37
|
+
};
|
|
38
|
+
vi.stubGlobal('Scratch', {
|
|
39
|
+
Cast: {
|
|
40
|
+
toString: (value: unknown) => String(value ?? ''),
|
|
41
|
+
toNumber: (value: unknown) => Number(value)
|
|
42
|
+
},
|
|
43
|
+
vm: {runtime: {getTargetForStage: () => target}}
|
|
44
|
+
});
|
|
45
|
+
extension.writeLinesToList({TEXT: 'a\nb', LIST: 'lines'}, {target});
|
|
46
|
+
expect(list.value).toEqual(['a', 'b']);
|
|
47
|
+
});
|
|
48
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"resolveJsonModule": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noUncheckedIndexedAccess": true,
|
|
9
|
+
"exactOptionalPropertyTypes": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"types": ["vitest/globals"]
|
|
12
|
+
},
|
|
13
|
+
"include": ["src", "tests", "vite.config.ts"]
|
|
14
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {defineConfig} from 'vite';
|
|
2
|
+
import {turboWarpExtension} from '@kubohiroya/vite-plugin-turbowarp-extension';
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
plugins: [
|
|
6
|
+
turboWarpExtension({
|
|
7
|
+
id: 'kubohiroyatextlines',
|
|
8
|
+
name: 'Text Lines',
|
|
9
|
+
description: 'Count, read, and split text by lines in TurboWarp.',
|
|
10
|
+
author: 'Hiroya Kubo',
|
|
11
|
+
license: 'MPL-2.0',
|
|
12
|
+
fileName: 'text-lines.js'
|
|
13
|
+
})
|
|
14
|
+
]
|
|
15
|
+
});
|