From 2d8dde991f1f259442588acc889cdddbaed367d2 Mon Sep 17 00:00:00 2001 From: Bo-Y-G Date: Wed, 7 Aug 2024 20:28:20 +0000 Subject: [PATCH] Modified code and test --- .../libs/author-info.ts | 9 ++-- .../tests/author-info.test.ts | 54 ++++++++++++------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/packages/unified-latex-to-pretext/libs/author-info.ts b/packages/unified-latex-to-pretext/libs/author-info.ts index f880c1e2..be79e2f8 100644 --- a/packages/unified-latex-to-pretext/libs/author-info.ts +++ b/packages/unified-latex-to-pretext/libs/author-info.ts @@ -3,13 +3,14 @@ import { visit } from "@unified-latex/unified-latex-util-visit"; import { match } from "@unified-latex/unified-latex-util-match"; import { htmlLike } from "@unified-latex/unified-latex-util-html-like"; import { VFileMessage } from "vfile-message"; +import { VFile } from "vfile"; export type AuthorInfo = Record; /** * Visits all the matching nodes and gathers author information, then send them to render and output pretext. */ -export function gatherAuthorInfo(ast: Ast.Ast): AuthorInfo[] | VFileMessage { +export function gatherAuthorInfo(ast: Ast.Ast, file: VFile): AuthorInfo[] { const authorList: AuthorInfo[] = []; visit(ast, (node) => { @@ -29,8 +30,8 @@ export function gatherAuthorInfo(ast: Ast.Ast): AuthorInfo[] | VFileMessage { ); authorList.push(authorEmail); } else if (match.macro(node, "affil")) { - MacroReport(node); - throw new Error('Macro "${node.content}" is not supported'); + createVFileMessage(node); + //file.message } }); return authorList; @@ -57,7 +58,7 @@ export function renderCollectedAuthorInfo(authorList: AuthorInfo[]): Ast.Macro { return renderedAuthorList; } -function MacroReport(node: Ast.Macro): VFileMessage { +function createVFileMessage(node: Ast.Macro): VFileMessage { const message = new VFileMessage( `Macro \"${node.content}\" is not supported` ); diff --git a/packages/unified-latex-to-pretext/tests/author-info.test.ts b/packages/unified-latex-to-pretext/tests/author-info.test.ts index 17ec9d19..bf16c0cd 100644 --- a/packages/unified-latex-to-pretext/tests/author-info.test.ts +++ b/packages/unified-latex-to-pretext/tests/author-info.test.ts @@ -3,11 +3,14 @@ import Prettier from "prettier"; import util from "util"; import { getParser } from "@unified-latex/unified-latex-util-parse"; import { toXml } from "xast-util-to-xml"; +import { xmlCompilePlugin } from "../libs/convert-to-pretext"; import { unified } from "unified"; import { gatherAuthorInfo, renderCollectedAuthorInfo, } from "../libs/author-info"; +import { VFile } from "vfile"; +import { toPretextWithLoggerFactory } from "../libs/pretext-subs/to-pretext"; function normalizeHtml(str: string) { try { @@ -28,28 +31,28 @@ console.log = (...args) => { describe("unified-latex-to-pretext:author-info", () => { let sample: string; const parser = getParser(); + let file: VFile; it("collects author name, address, institution, and email information", () => { + file = new VFile(); sample = "\\author{First Middle LastName} \n \\address{Department, Address}"; let input = " First Middle LastName"; let input1 = " \n Department, Address"; - expect(gatherAuthorInfo(parser.parse(sample))).toEqual([ + expect(gatherAuthorInfo(parser.parse(sample), file)).toEqual([ { personname: parser.parse(input).content }, { address: parser.parse(input1).content }, ]); sample = "\\address{Affiliation}"; input = " Affiliation"; - expect(gatherAuthorInfo(parser.parse(sample))).toEqual([ + expect(gatherAuthorInfo(parser.parse(sample), file)).toEqual([ { address: parser.parse(input).content }, ]); sample = "\\affil{Affiliation}"; - expect(gatherAuthorInfo(parser.parse(sample))).toEqual([ - "error" - ]); + expect(gatherAuthorInfo(parser.parse(sample), file)).toEqual([]); sample = "\\author{First Author} \\email{example@example.com} \\author{Second Author}"; @@ -57,7 +60,7 @@ describe("unified-latex-to-pretext:author-info", () => { input1 = " example@example.com"; let input2 = " Second Author"; - expect(gatherAuthorInfo(parser.parse(sample))).toEqual([ + expect(gatherAuthorInfo(parser.parse(sample), file)).toEqual([ { personname: parser.parse(input).content }, { email: parser.parse(input1).content }, { personname: parser.parse(input2).content }, @@ -67,30 +70,41 @@ describe("unified-latex-to-pretext:author-info", () => { it("parses author name, address, and email information", () => { sample = "\\author{First Middle LastName} \n \\address{Department, Address}"; - expect( - renderCollectedAuthorInfo(gatherAuthorInfo(parser.parse(sample))) - ).toEqual( + let rendered = renderCollectedAuthorInfo( + gatherAuthorInfo(parser.parse(sample), file) + ); + const toXast = toPretextWithLoggerFactory(file.message.bind(file)); + const xxx = unified() + .use(xmlCompilePlugin) + .runSync({ type: "root", children: [toXast(rendered)].flat() }); + expect(normalizeHtml(toXml(xxx))).toEqual( normalizeHtml( - " First Middle LastName Department, Address " + "First Middle LastName
Department, Address
" ) ); sample = "\\address{Affiliation}"; - expect( - renderCollectedAuthorInfo(gatherAuthorInfo(parser.parse(sample))) - ).toEqual( - normalizeHtml( - " Affiliation " - ) + rendered = renderCollectedAuthorInfo( + gatherAuthorInfo(parser.parse(sample), file) + ); + const xxx1 = unified() + .use(xmlCompilePlugin) + .runSync({ type: "root", children: [toXast(rendered)].flat() }); + expect(normalizeHtml(toXml(xxx1))).toEqual( + normalizeHtml("
Affiliation
") ); sample = "\\author{First Author} \\email{example@example.com} \\author{Second Author}"; - expect( - renderCollectedAuthorInfo(gatherAuthorInfo(parser.parse(sample))) - ).toEqual( + rendered = renderCollectedAuthorInfo( + gatherAuthorInfo(parser.parse(sample), file) + ); + const xxx2 = unified() + .use(xmlCompilePlugin) + .runSync({ type: "root", children: [toXast(rendered)].flat() }); + expect(normalizeHtml(toXml(xxx2))).toEqual( normalizeHtml( - " First Author example@example.com Second Author " + "First Authorexample@example.comSecond Author" ) ); });