Skip to content

Commit

Permalink
self-documented divisions
Browse files Browse the repository at this point in the history
  • Loading branch information
renee-k committed Jun 26, 2024
1 parent 35627d8 commit 7fba2d7
Showing 1 changed file with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import { visit } from "@unified-latex/unified-latex-util-visit";
* All the divisions, where each item is [division macro, environment]
* Note that this is ordered from the "largest" division to the "smallest" division.
*/
const divisions: [string, string][] = [
["part", "_part"],
["chapter", "_chapter"],
["section", "_section"],
["subsection", "_subsection"],
["subsubsection", "_subsubsection"],
["paragraph", "_paragraph"],
["subparagraph", "_subparagraph"],
const divisions: { division: string; mappedEnviron: string }[] = [
{ division: "part", mappedEnviron: "_part" },
{ division: "chapter", mappedEnviron: "_chapter" },
{ division: "section", mappedEnviron: "_section" },
{ division: "subsection", mappedEnviron: "_subsection" },
{ division: "subsubsection", mappedEnviron: "_subsubsection" },
{ division: "paragraph", mappedEnviron: "_paragraph" },
{ division: "subparagraph", mappedEnviron: "_subparagraph" },
];

/**
Expand All @@ -37,7 +37,9 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } {
const messagesLst: { messages: string[] } = { messages: [] };

// check if a macro is a division macro
const isDivision = match.createMacroMatcher(divisions.map((x) => x[0]));
const isDivision = match.createMacroMatcher(
divisions.map((x) => x.division)
);

visit(ast, (node, info) => {
// needs to be an environment, root, or group node
Expand All @@ -55,7 +57,7 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } {
// if it's an environment, make sure it isn't a newly created one
else if (
anyEnvironment(node) &&
divisions.map((x) => x[1]).includes(node.env)
divisions.map((x) => x.mappedEnviron).includes(node.env)
) {
return;
}
Expand All @@ -70,7 +72,7 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } {
}

// now break up the divisions, starting at part
node.content = breakUp(node.content, divisions, 0);
node.content = breakUp(node.content, 0);
});

replaceNode(ast, (node) => {
Expand All @@ -92,24 +94,20 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: string[] } {
/**
* Recursively breaks up the AST at the division macros.
*/
function breakUp(
content: Ast.Node[],
divisions: [string, string][],
depth: number
): Ast.Node[] {
function breakUp(content: Ast.Node[], depth: number): Ast.Node[] {
// broke up all divisions
if (depth > 6) {
return content;
}

const splits = splitOnMacro(content, divisions[depth][0]);
const splits = splitOnMacro(content, divisions[depth].division);

// go through each segment to recursively break
for (let i = 0; i < splits.segments.length; i++) {
splits.segments[i] = breakUp(splits.segments[i], divisions, depth + 1);
splits.segments[i] = breakUp(splits.segments[i], depth + 1);
}

createEnvironments(splits, divisions[depth][1]);
createEnvironments(splits, divisions[depth].mappedEnviron);

// rebuild this part of the ast
return unsplitOnMacro(splits);
Expand Down

0 comments on commit 7fba2d7

Please sign in to comment.