From b924c03d511c2c0eade68ad390d845bdbe96ceb1 Mon Sep 17 00:00:00 2001 From: Mauro Mandracchia Date: Wed, 26 Oct 2022 21:11:57 +0200 Subject: [PATCH] Solves #183 for `packages/orga` --- packages/orga/src/parse/block.ts | 34 ++++++++++++++----------------- packages/orga/src/parse/drawer.ts | 29 +++++++++++++------------- packages/orga/src/parse/latex.ts | 29 ++++++++++++-------------- packages/orga/tsconfig.json | 2 +- tsconfig.esm.json | 8 +++++++- 5 files changed, 50 insertions(+), 52 deletions(-) diff --git a/packages/orga/src/parse/block.ts b/packages/orga/src/parse/block.ts index 94524344..7ed48563 100644 --- a/packages/orga/src/parse/block.ts +++ b/packages/orga/src/parse/block.ts @@ -1,23 +1,20 @@ import { Action, Handler } from '.' import { BlockBegin, BlockEnd } from '../types' -const block: Action = ( - begin: BlockBegin, - { save, push, enter, lexer, attributes } -): Handler => { - save() +const block: Action = (begin: BlockBegin, context): Handler => { + context.save() const contentStart = begin.position.end const blockName = begin.name.toLowerCase() - const block = enter({ + const block = context.enter({ type: 'block', name: begin.name, params: begin.params, value: '', - attributes: { ...attributes }, + attributes: { ...context.attributes }, children: [], }) - push(lexer.eat()) + context.push(context.lexer.eat()) /* * find the indentation of the block and apply it to @@ -56,34 +53,33 @@ const block: Action = ( rules: [ { test: 'block.end', - action: (token: BlockEnd, { exit, push, lexer }) => { - const { eat } = lexer + action: (token: BlockEnd, context) => { if (token.name.toLowerCase() !== blockName) return 'next' block.value = align( - lexer.substring({ + context.lexer.substring({ start: contentStart, end: token.position.start, }) ) - push(eat()) - eat('newline') - exit('block') + context.push(context.lexer.eat()) + context.lexer.eat('newline') + context.exit('block') return 'break' }, }, { test: ['stars', 'EOF'], - action: (_, { restore, lexer }) => { - restore() - lexer.modify((t) => ({ + action: (_, context) => { + context.restore() + context.lexer.modify((t) => ({ type: 'text', - value: lexer.substring(t.position), + value: context.lexer.substring(t.position), position: t.position, })) return 'break' }, }, - { test: /./, action: (_, { push, lexer }) => push(lexer.eat()) }, + { test: /./, action: (_, context) => context.push(context.lexer.eat()) }, ], } } diff --git a/packages/orga/src/parse/drawer.ts b/packages/orga/src/parse/drawer.ts index df050821..4231bed2 100644 --- a/packages/orga/src/parse/drawer.ts +++ b/packages/orga/src/parse/drawer.ts @@ -2,16 +2,15 @@ import { Action } from '.' import { DrawerBegin, Section } from '../types' const drawer: Action = (begin: DrawerBegin, context) => { - const { save, enter, push, exit, lexer } = context - const { eat } = lexer - save() - const drawer = enter({ + const lexer = context.lexer + context.save() + const drawer = context.enter({ type: 'drawer', name: begin.name, value: '', children: [], }) - push(eat()) + context.push(lexer.eat()) const contentStart = begin.position.end @@ -20,11 +19,11 @@ const drawer: Action = (begin: DrawerBegin, context) => { rules: [ { test: ['stars', 'EOF'], - action: (_, { restore, lexer }) => { - restore() - lexer.modify((t) => ({ + action: (_, context) => { + context.restore() + context.lexer.modify((t) => ({ type: 'text', - value: lexer.substring(t.position), + value: context.lexer.substring(t.position), position: t.position, })) return 'break' @@ -32,18 +31,18 @@ const drawer: Action = (begin: DrawerBegin, context) => { }, { test: 'drawer.end', - action: (token, { lexer, push, getParent }) => { - push(lexer.eat()) - drawer.value = lexer.substring({ + action: (token, context) => { + context.push(context.lexer.eat()) + drawer.value = context.lexer.substring({ start: contentStart, end: token.position.start, }) - exit('drawer') - eat('newline') + context.exit('drawer') + lexer.eat('newline') if (drawer.name.toLowerCase() === 'properties') { - const section = getParent() as Section + const section = context.getParent() as Section section.properties = drawer.value .split('\n') .reduce((accu, current) => { diff --git a/packages/orga/src/parse/latex.ts b/packages/orga/src/parse/latex.ts index d2a0a4f8..0385a067 100644 --- a/packages/orga/src/parse/latex.ts +++ b/packages/orga/src/parse/latex.ts @@ -1,21 +1,18 @@ import { Action, Handler } from '.' import { LatexBegin, LatexEnd } from '../types' -const latex: Action = ( - begin: LatexBegin, - { save, push, enter, lexer } -): Handler => { - save() +const latex: Action = (begin: LatexBegin, context): Handler => { + context.save() const contentStart = begin.position.start const envName = begin.name.toLowerCase() - const latexBlock = enter({ + const latexBlock = context.enter({ type: 'latex', name: begin.name, value: '', children: [], }) - push(lexer.eat()) + context.push(context.lexer.eat()) /* * find the indentation of the block and apply it to @@ -49,8 +46,8 @@ const latex: Action = ( rules: [ { test: 'latex.end', - action: (token: LatexEnd, { exit, push, lexer }) => { - const { eat } = lexer + action: (token: LatexEnd, context) => { + const lexer = context.lexer if (token.name.toLowerCase() !== envName) return 'next' latexBlock.value = align( lexer.substring({ @@ -58,19 +55,19 @@ const latex: Action = ( end: token.position.end, }) ) - push(eat()) - eat('newline') - exit('latex') + context.push(lexer.eat()) + lexer.eat('newline') + context.exit('latex') return 'break' }, }, { test: ['stars', 'EOF'], - action: (_, { restore, lexer }) => { - restore() - lexer.modify((t) => ({ + action: (_, context) => { + context.restore() + context.lexer.modify((t) => ({ type: 'text', - value: lexer.substring(t.position), + value: context.lexer.substring(t.position), position: t.position, })) return 'break' diff --git a/packages/orga/tsconfig.json b/packages/orga/tsconfig.json index 02b1c765..059b4c85 100644 --- a/packages/orga/tsconfig.json +++ b/packages/orga/tsconfig.json @@ -1,5 +1,5 @@ { - "extends": "../../tsconfig.base.json", + "extends": "../../tsconfig.esm.json", "compilerOptions": { "rootDir": "./src", "outDir": "./dist" diff --git a/tsconfig.esm.json b/tsconfig.esm.json index 8e4ddcd8..9f3ed38a 100644 --- a/tsconfig.esm.json +++ b/tsconfig.esm.json @@ -11,5 +11,11 @@ "allowJs": true, "lib": ["ES2020"] }, - "exclude": ["node_modules", "**/*.spec.ts", "**/__tests__/**", "tests/**"] + "exclude": [ + "node_modules", + "**/*.spec.ts", + "**/*.test.ts", + "**/__tests__/**", + "tests/**" + ] }