Skip to content

Commit

Permalink
imported VFileMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
renee-k committed Jul 12, 2024
1 parent 51ba97e commit 5e388ae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
anyMacro,
match,
} from "@unified-latex/unified-latex-util-match";
import { printRaw } from "@unified-latex/unified-latex-util-print-raw";
import { replaceNode } from "@unified-latex/unified-latex-util-replace";
import {
splitOnMacro,
unsplitOnMacro,
} from "@unified-latex/unified-latex-util-split";
import { visit } from "@unified-latex/unified-latex-util-visit";
import { VFileMessage } from "vfile-message";

/**
* All the divisions, where each item is {division macro, mapped environment}.
Expand All @@ -35,17 +35,16 @@ const isDivisionMacro = match.createMacroMatcher(

// check if an environment is a newly created environment
const isMappedEnviron = match.createEnvironmentMatcher(
// doesn't seem to work
divisions.map((x) => x.mappedEnviron)
);

/**
* Breaks up division macros into environments. Returns a list of warning messages
* for any groups that were removed.
*/
export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } {
export function breakOnBoundaries(ast: Ast.Ast): { messages: VFileMessage[] } {
// messages for any groups removed
const messagesLst: { messages: string[] } = { messages: [] };
const messagesLst: { messages: VFileMessage[] } = { messages: [] };

replaceNode(ast, (node) => {
if (match.group(node)) {
Expand All @@ -55,11 +54,28 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } {
return anyMacro(child) && isDivisionMacro(child);
})
) {
messagesLst.messages.push(
`Warning: hoisted out of a group, which might break the LaTeX code. { group: ${printRaw(
node
)} }`
const message = new VFileMessage(
"Warning: hoisted out of a group, which might break the LaTeX code.",
{
line: node.position?.start.line,
column: node.position?.start.column,
position: {
start: { line: node.position?.start.line, column: node.position?.start.column },
end: { line: node.position?.end.line, column: node.position?.end.column }
},
source: 'LatexConversion'
}
);
// line: null,
// column: null,
// position: {
// start: { line: null, column: null },
// end: { line: null, column: null }
// },
// source: null,
// ruleId: null

messagesLst.messages.push(message);

return node.content;
}
Expand Down Expand Up @@ -95,6 +111,8 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } {
}
});

console.log(messagesLst.messages);

return messagesLst;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => {
const parser = getParser();
const ast = parser.parse(value);

expect(breakOnBoundaries(ast)).toEqual({ messages: [] });
expect(breakOnBoundaries(ast).messages.length).toEqual(0);

expect(printRaw(ast)).toEqual(
String.raw`\begin{center}\begin{_part}[name]Hi, this is a part` +
Expand All @@ -84,12 +84,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => {
const parser = getParser();
const ast = parser.parse(value);

expect(breakOnBoundaries(ast)).toEqual({
messages: [
String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` +
String.raw`{ group: {\paragraph{Intro}Introduction.\begin{center}\subparagraph{Conclusion}Conclusion.\end{center}} }`,
],
});
expect(breakOnBoundaries(ast).messages.length).toEqual(1);

expect(printRaw(ast)).toEqual(
String.raw`\begin{document}\begin{_chapter}[Chap]\begin{_paragraph}[Intro]Introduction.` +
Expand All @@ -106,14 +101,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => {
const parser = getParser();
const ast = parser.parse(value);

expect(breakOnBoundaries(ast)).toEqual({
messages: [
String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` +
String.raw`{ group: {\subsection{Intro}description.\subsubsection{body}more text.{\subparagraph{Conclusion}Conclusion.}} }`,
String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` +
String.raw`{ group: {\subparagraph{Conclusion}Conclusion.} }`,
],
});
expect(breakOnBoundaries(ast).messages.length).toEqual(2);

expect(printRaw(ast)).toEqual(
String.raw`\begin{_part}[part1]\begin{_subsection}[Intro]description.` +
Expand All @@ -130,14 +118,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => {
const parser = getParser();
const ast = parser.parse(value);

expect(breakOnBoundaries(ast)).toEqual({
messages: [
String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` +
String.raw`{ group: {\subsection{Intro}\subsubsection{body}{$\mathbb{N}$\subparagraph{Conclusion}{no divisions 1}Conclusion.}} }`,
String.raw`Warning: hoisted out of a group, which might break the LaTeX code. ` +
String.raw`{ group: {$\mathbb{N}$\subparagraph{Conclusion}{no divisions 1}Conclusion.} }`,
],
});
expect(breakOnBoundaries(ast).messages.length).toEqual(2);

expect(printRaw(ast)).toEqual(
String.raw`\begin{_part}[part1]\begin{_subsection}[Intro]\begin{_subsubsection}[body]` +
Expand All @@ -152,7 +133,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => {
const parser = getParser();
const ast = parser.parse(value);

expect(breakOnBoundaries(ast)).toEqual({ messages: [] });
expect(breakOnBoundaries(ast).messages.length).toEqual(0);

expect(printRaw(ast)).toEqual(
String.raw`\begin{_chapter}[$x = \frac{1}{2}$]Chapter 1` +
Expand All @@ -167,7 +148,7 @@ describe("unified-latex-to-pretext:break-on-boundaries", () => {
const parser = getParser();
const ast = parser.parse(value);

expect(breakOnBoundaries(ast)).toEqual({ messages: [] });
expect(breakOnBoundaries(ast).messages.length).toEqual(0);

expect(printRaw(ast)).toEqual(
String.raw`\begin{_subsubsection}[first]subsection 1 ` +
Expand Down

0 comments on commit 5e388ae

Please sign in to comment.