Skip to content

Solves #183 for packages/orga #184

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions packages/orga/src/parse/block.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()) },
],
}
}
Expand Down
29 changes: 14 additions & 15 deletions packages/orga/src/parse/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -20,30 +19,30 @@ 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'
},
},
{
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) => {
Expand Down
29 changes: 13 additions & 16 deletions packages/orga/src/parse/latex.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -49,28 +46,28 @@ 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({
start: contentStart,
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'
Expand Down
2 changes: 1 addition & 1 deletion packages/orga/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.base.json",
"extends": "../../tsconfig.esm.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist"
Expand Down
8 changes: 7 additions & 1 deletion tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@
"allowJs": true,
"lib": ["ES2020"]
},
"exclude": ["node_modules", "**/*.spec.ts", "**/__tests__/**", "tests/**"]
"exclude": [
"node_modules",
"**/*.spec.ts",
"**/*.test.ts",
"**/__tests__/**",
"tests/**"
]
}