Skip to content

Commit

Permalink
Add macros for printer configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Toliak committed Mar 29, 2024
1 parent 5a8ae8d commit 5082c65
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/macro/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ import {
} from '../diagnostic';

export const enum ArgInfoType {
/**
* returns data in `Node[]`
*/
NodeArray = 'NodeArray',

/**
* returns `string`
*/
Text = 'Text',

/**
* return `TextNode`
*/
TextNode = 'TextNode',
}

Expand All @@ -24,7 +35,12 @@ export interface ArgInfo {
aliases: string[];
type: ArgInfoType;
optional: boolean;
onlySpans: boolean; // No block nodes

/**
* Allows only Span nodes
* See [SPAN_NODE_TYPES]
*/
onlySpans: boolean;
}

export type ParsedMacrosArguments = Record<
Expand Down
1 change: 1 addition & 0 deletions src/macro/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { default as tableKey } from './tableKey';
export { default as tableAmount } from './tableAmount';
export { default as pictureAmount } from './pictureAmount';
export { default as formulaKey } from './formulaKey';
export { default as printerCmd } from './printerCmd';
74 changes: 74 additions & 0 deletions src/macro/command/printerCmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { CommandInfo, CommandInfoCallback } from '../struct';
import {
CodeApplicationNode,
PictureApplicationNode,
PrinterCmdNode,
ProcessedNodeType,
RawApplicationNode,
} from '../node';
import { Node, TextNode } from '../../ast/node';
import { ArgInfoType } from '../args';
import {
DiagnoseErrorType,
DiagnoseSeverity,
nodeToDiagnose,
} from '../../diagnostic';
import { unpackerParagraphOnce } from '../unpack';

interface PrinterCmdArgsType {
command?: string;
data?: Node[];
}

const callbackPrinterCmd: CommandInfoCallback<PrinterCmdArgsType, TextNode> =
function (ctx, data, args) {
if (!args.args.command) {
ctx.c.diagnostic.push(
nodeToDiagnose(
data.node.n,
DiagnoseSeverity.Fatal,
DiagnoseErrorType.MacrosError,
'PrinterCmd macros name argument is undefined ' +
'(internal error)',
),
);

return [];
}

const node: PrinterCmdNode = {
type: ProcessedNodeType.PrinterCmd,
pos: { ...data.node.n.pos },
parent: data.node.n.parent,
command: args.args.command,
data: args.args.data ?? [],
};

return [node];
};

export default [
{
args: [
{
name: 'command',
aliases: ['c'],
type: ArgInfoType.Text,
onlySpans: true,
optional: false,
},
{
name: 'data',
// Data or Arguments
aliases: ['d', 'a'],
type: ArgInfoType.NodeArray,
onlySpans: false,
optional: true,
},
],
name: 'SWEARIHATEIT',
callback: callbackPrinterCmd,
unpacker: unpackerParagraphOnce,
labelOptional: true,
},
] as CommandInfo[];
2 changes: 2 additions & 0 deletions src/macro/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const ALL_COMMAND_LIST: Readonly<CommandInfo[]> = [
command.listAllReferences,

command.formulaKey,

...command.printerCmd,
];

export function getCommandFromOpCodeNode(
Expand Down
9 changes: 8 additions & 1 deletion src/macro/node/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const enum ProcessedNodeType {
ReferenceKey = 'ReferenceKey',
FormulaKey = 'FormulaKey',

// LatexSpecific = 'LatexSpecific', // TODO
PrinterCmd = 'PrinterCmd',

AllApplications = 'AllApplications',
AllReferences = 'AllReferences',
Expand Down Expand Up @@ -192,3 +192,10 @@ export interface TableAmountNode extends NodeProcessed {
// TODO: get rid of lambdas
numberLazy: () => number;
}

export interface PrinterCmdNode extends NodeProcessed {
type: ProcessedNodeType.PrinterCmd;

command: string;
data: Node[];
}
2 changes: 1 addition & 1 deletion src/macro/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ProcessedNodesByTypeMap = {
[ProcessedNodeType.ApplicationKey]: {} as nodes.ApplicationKeyNode,
[ProcessedNodeType.ReferenceKey]: {} as nodes.ReferenceKeyNode,
[ProcessedNodeType.FormulaKey]: {} as nodes.FormulaKeyNode,
// [ProcessedNodeType.LatexSpecific]: {} as nodes.LatexSpecificNode,
[ProcessedNodeType.PrinterCmd]: {} as nodes.PrinterCmdNode,
[ProcessedNodeType.AllApplications]: {} as nodes.AllApplicationsNode,
[ProcessedNodeType.AllReferences]: {} as nodes.AllReferencesNode,
[ProcessedNodeType.RawApplication]: {} as nodes.RawApplicationNode,
Expand Down
174 changes: 174 additions & 0 deletions test/macro/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,180 @@ Look at the pic. !PK[image-label-2] and !PK[image-label].
]
`;

exports[`printer specific command 1`] = `
Array [
Object {
"command": "testcommand",
"data": Array [
Object {
"children": Array [
Object {
"bullet": Object {
"parent": [Circular],
"pos": Object {
"end": 40,
"start": 38,
},
"text": "- ",
"type": "Text",
},
"children": Array [
Object {
"children": Array [],
"parent": [Circular],
"pos": Object {
"end": 41,
"start": 40,
},
"text": "1",
"type": "Text",
},
],
"loose": false,
"parent": [Circular],
"pos": Object {
"end": 41,
"start": 34,
},
"task": false,
"type": "ListItem",
},
Object {
"bullet": Object {
"parent": [Circular],
"pos": Object {
"end": 48,
"start": 46,
},
"text": "- ",
"type": "Text",
},
"children": Array [
Object {
"children": Array [],
"parent": [Circular],
"pos": Object {
"end": 49,
"start": 48,
},
"text": "2",
"type": "Text",
},
],
"loose": false,
"parent": [Circular],
"pos": Object {
"end": 50,
"start": 42,
},
"task": false,
"type": "ListItem",
},
],
"loose": false,
"ordered": false,
"parent": Object {
"keyArgs": Object {
"d": [Circular],
},
"keys": Object {
"d": Object {
"parent": [Circular],
"pos": Object {
"end": 32,
"start": 31,
},
"text": "d",
"type": "Text",
},
},
"label": Object {
"parent": [Circular],
"pos": Object {
"end": 15,
"start": 15,
},
"text": "",
"type": "Text",
},
"opcode": Object {
"parent": [Circular],
"pos": Object {
"end": 14,
"start": 2,
},
"text": "SWEARIHATEIT",
"type": "Text",
},
"parent": Object {
"children": [Circular],
"parent": null,
"path": "test.md",
"pos": Object {
"end": 52,
"start": 0,
},
"raw": "
!SWEARIHATEIT[](testcommand)(@d
- 1
- 2
)
",
"type": "File",
},
"pos": Object {
"end": 51,
"start": 1,
},
"posArgs": Array [
Array [
Object {
"children": Array [],
"parent": [Circular],
"pos": Object {
"end": 28,
"start": 17,
},
"text": "testcommand",
"type": "Text",
},
],
],
"type": "OpCode",
},
"pos": Object {
"end": 50,
"start": 34,
},
"start": 0,
"type": "List",
},
],
"parent": Object {
"children": [Circular],
"parent": null,
"path": "test.md",
"pos": Object {
"end": 52,
"start": 0,
},
"raw": "
!SWEARIHATEIT[](testcommand)(@d
- 1
- 2
)
",
"type": "File",
},
"pos": Object {
"end": 51,
"start": 1,
},
"type": "PrinterCmd",
},
]
`;

exports[`references multiple references 1`] = `
Array [
Object {
Expand Down
17 changes: 17 additions & 0 deletions test/macro/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,20 @@ for Image Denoising described in [!RK[ref-3], !RK[ref-1]].

// TODO: !LAR / !LAA into the table cell for example
});

describe('printer specific', () => {
test('command', () => {
const rawNode = rawNodeTemplate(`
!SWEARIHATEIT[](testcommand)(@d
- 1
- 2
)
`);
const { nodes, diagnostic: diagnostic1 } = applyVisitors([rawNode]);
(rawNode.parent as FileNode).children = nodes;

const diagnostic = applyMacrosFull(rawNode.parent as FileNode);
expect(diagnostic).toHaveLength(0);
expect(nodes).toMatchSnapshot();
});
});

0 comments on commit 5082c65

Please sign in to comment.