Skip to content
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

Title #112

Closed
wants to merge 5 commits into from
Closed

Title #112

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
71 changes: 71 additions & 0 deletions packages/unified-latex-to-pretext/libs/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as Ast from "@unified-latex/unified-latex-types";
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";

/**
* This function collects titles, even without \maketitle.
* It takes the last title before the ducument class and warn if there are multiple,
*/
export function gatherTitle(ast: Ast.Ast, file: VFile): Ast.Node[] {
const ti: Ast.Node[] = [];

visit(ast, (node) => {
if (match.macro(node, "title") && node.args) {
const titleContent = Object.fromEntries(
node.args.map((x) => ["title", x.content])
);
ti.push(titleContent.title[0]);
/*
*if ((ti.length = 2)) {
const message = createVFileMessage(node);
file.message(
message,
message.position,
"latex-to-pretext:warning"
)
}
*/
}
});
return ti.slice(-1);
}

/**
* This function wraps around the title collected and returns a htmllike macro.
*/
export function renderTitle(title: Ast.Node[]): Ast.Macro {
const renderedAuthorList = htmlLike({
tag: "title",
content: title,
});

return renderedAuthorList;
}

function createVFileMessage(node: Ast.Macro): VFileMessage {
const message = new VFileMessage(
`There are multiple titles, the last title was displayed.`
);

// add the position of the macro if available
if (node.position) {
message.line = node.position.start.line;
message.column = node.position.start.column;
message.position = {
start: {
line: node.position.start.line,
column: node.position.start.column,
},
end: {
line: node.position.end.line,
column: node.position.end.column,
},
};
}

message.source = "latex-to-pretext:warning";
return message;
}
58 changes: 58 additions & 0 deletions packages/unified-latex-to-pretext/tests/title.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, it, expect } from "vitest";
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 { gatherTitle, renderTitle } from "../libs/title";
import { VFile } from "vfile";
import { toPretextWithLoggerFactory } from "../libs/pretext-subs/to-pretext";

function normalizeHtml(str: string) {
try {
return Prettier.format(str, { parser: "html" });
} catch {
console.warn("Could not format HTML string", str);
return str;
}
}

// Make console.log pretty-print by default
const origLog = console.log;
console.log = (...args) => {
origLog(...args.map((x) => util.inspect(x, false, 10, true)));
};

describe("unified-latex-to-pretext:title", () => {
let sample: string;
let input: string;
let file: VFile;
const parser = getParser();

it("collects title content", () => {
sample = "\\title{Title}";
input = " Title";
expect(gatherTitle(parser.parse(sample), file)).toEqual(
parser.parse(input).content
);

sample = "\\title{Title1} \n \\title{Title2}";
input = " \n Title2";
expect(gatherTitle(parser.parse(sample), file)).toEqual(
parser.parse(input).content
);
});

it("parses title content", () => {
sample = "\\title{Title}";
let rendered = renderTitle(gatherTitle(parser.parse(sample), file));
const toXast = toPretextWithLoggerFactory(file.message.bind(file));

Check failure on line 50 in packages/unified-latex-to-pretext/tests/title.test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

packages/unified-latex-to-pretext/tests/title.test.ts > unified-latex-to-pretext:title > parses title content

TypeError: Cannot read properties of undefined (reading 'message') ❯ packages/unified-latex-to-pretext/tests/title.test.ts:50:56
const xxx = unified()
.use(xmlCompilePlugin)
.runSync({ type: "root", children: [toXast(rendered)].flat() });
expect(normalizeHtml(toXml(xxx))).toEqual(
normalizeHtml("<title>Title</title>")
);
});
});
Loading