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

Fix the skipped test cases in the PreTeXt package #103

Merged
merged 72 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
3a85c98
added math mode macro report function and test file
renee-k May 25, 2024
0c1971a
updated report unsupported katex and expand command files
renee-k May 26, 2024
a140ea3
added more tests to be implemented
renee-k May 26, 2024
575141d
implemented expand macros tests and function
renee-k May 28, 2024
2711765
formatted code
renee-k May 29, 2024
a450ac1
addressed some katex related comments
renee-k May 30, 2024
bd2f4d5
added more tests
renee-k May 31, 2024
76352c5
removed uneeded variable
renee-k Jun 4, 2024
6f6221a
revised tests
renee-k Jun 5, 2024
b55e08c
revised some test descriptions
renee-k Jun 5, 2024
49e25bc
fixed expand userdefined macros
renee-k Jun 5, 2024
577d1be
addressed more PR comments
renee-k Jun 8, 2024
7648bc7
moved files to pre-conversion-subs
renee-k Jun 9, 2024
80fce28
edited docstrings
renee-k Jun 12, 2024
34214da
Add pretext to tested branches
siefkenj Jun 12, 2024
8383767
Merge branch 'main' into pretext
renee-k Jun 12, 2024
c5cea44
Merge branch 'siefkenj:pretext' into pretext
renee-k Jun 26, 2024
a0439b6
modified some test cases
renee-k Jul 3, 2024
537c71b
Merge branch 'pretext' of https://github.com/renee-k/unified-latex in…
renee-k Jul 3, 2024
97f3b2b
fixed a failed test case
renee-k Jul 4, 2024
9d0c2da
added boolean to the plugins
renee-k Jul 6, 2024
24008c6
updated a few more test cases
renee-k Jul 6, 2024
78a9463
modified plugins to include new boolean
renee-k Jul 9, 2024
0809260
added another tabular test
renee-k Jul 9, 2024
7c9e262
looked into all skipped test cases
renee-k Jul 10, 2024
02b525a
added comments for cases to fix later
renee-k Jul 11, 2024
03966d2
unskipped some test cases
renee-k Jul 12, 2024
a522c4f
changed tags for displaymath and tabular
renee-k Jul 16, 2024
fd21bae
fixed a couple more test cases
renee-k Jul 22, 2024
09c711e
fixed xml like test cases
renee-k Jul 24, 2024
dae8baf
began fixing enumerate
renee-k Jul 24, 2024
b6cce1e
Merge branch 'siefkenj:pretext' into pretext
renee-k Jul 24, 2024
37c55b6
fixed all list related test cases
renee-k Jul 26, 2024
46a4001
implemented support for tabular borders
renee-k Jul 27, 2024
7803f0f
corrected most subs
renee-k Jul 28, 2024
034d58d
added new functions into the xml plugin
renee-k Jul 28, 2024
8a8fbd5
fixed a list related test case
renee-k Jul 28, 2024
bd41f2e
Merge branch 'pretext' into pretext-xml-plugin
renee-k Jul 28, 2024
1a39d75
started adding in mandatory pretext tags
renee-k Jul 30, 2024
adef778
removed unneeded imports
renee-k Jul 30, 2024
7565f7b
Merge branch 'pretext' into pretext-xml-plugin
renee-k Jul 30, 2024
414dcda
added new plugin option to every test file
renee-k Jul 30, 2024
f1ca69c
Merge branch 'pretext' into pretext-xml-plugin
renee-k Jul 30, 2024
e5229d0
added option to one more test file
renee-k Jul 30, 2024
d3b0b6b
Merge branch 'pretext' into pretext-xml-plugin
renee-k Jul 30, 2024
31726e5
fixed unskipped test case to be proper pretext output
renee-k Jul 30, 2024
3759e35
Merge branch 'pretext' into pretext-xml-plugin
renee-k Jul 30, 2024
4d1594f
changed order of function calls in xml plugin
renee-k Jul 31, 2024
18821c3
fixed tabular code
renee-k Jul 31, 2024
eae1fc8
added vfile to xmllike plugin
renee-k Jul 31, 2024
155b07f
added vfile import
renee-k Jul 31, 2024
62b4511
made functions to create a valid document
renee-k Aug 2, 2024
2be12ad
changed return of report unsupported macros by katex to VMessages
renee-k Aug 3, 2024
d2da32f
Merge branch 'pretext' into pretext-xml-plugin
renee-k Aug 3, 2024
491f409
added function that creates an empty string node
renee-k Aug 4, 2024
295a6db
added function warnings to VFile
renee-k Aug 4, 2024
d7f9277
added warnings to VFile for no equivalent pretext tags
renee-k Aug 4, 2024
83fd352
fixed warning message positions being null
renee-k Aug 5, 2024
c64b1f8
added title tag to valid pretext doc
renee-k Aug 5, 2024
603539e
cleaned up code
renee-k Aug 7, 2024
6ec2e43
fixed one test case
renee-k Aug 7, 2024
9ae7ee3
addressed some PR comments
renee-k Aug 9, 2024
14a005c
moved some functions into utils file
renee-k Aug 10, 2024
b5cfa21
added new file for creating tabular
renee-k Aug 11, 2024
11a5182
fixed bug in to pretext file
renee-k Aug 11, 2024
5c59985
changed structure of to pretext plugin
renee-k Aug 12, 2024
1fe0c37
fixed comments using html to using pretext
renee-k Aug 12, 2024
3ae132c
cleaned up code
renee-k Aug 13, 2024
6737ed3
fixed last test case and removed only center env
renee-k Aug 14, 2024
a09daa9
changed div and span fallbacks
renee-k Aug 14, 2024
d5b50f9
cleaned up code again
renee-k Aug 15, 2024
c359495
fixed all new PR comments
renee-k Aug 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import {
} from "@unified-latex/unified-latex-util-split";
import { visit } from "@unified-latex/unified-latex-util-visit";
import { VFileMessage } from "vfile-message";
import { makeWarningMessage } from "./utils";

/**
* All the divisions, where each item is {division macro, mapped environment}.
* Note that this is ordered from the "largest" division to the "smallest" division.
*/
const divisions: { division: string; mappedEnviron: string }[] = [
export const divisions: { division: string; mappedEnviron: string }[] = [
{ division: "part", mappedEnviron: "_part" },
{ division: "chapter", mappedEnviron: "_chapter" },
{ division: "section", mappedEnviron: "_section" },
Expand All @@ -34,7 +35,7 @@ const isDivisionMacro = match.createMacroMatcher(
);

// check if an environment is a newly created environment
const isMappedEnviron = match.createEnvironmentMatcher(
export const isMappedEnviron = match.createEnvironmentMatcher(
divisions.map((x) => x.mappedEnviron)
);

Expand All @@ -54,29 +55,15 @@ export function breakOnBoundaries(ast: Ast.Ast): { messages: VFileMessage[] } {
return anyMacro(child) && isDivisionMacro(child);
})
) {
const message = new VFileMessage(
"Warning: hoisted out of a group, which might break the LaTeX code."
// add a warning message
messagesLst.messages.push(
makeWarningMessage(
node,
"Warning: hoisted out of a group, which might break the LaTeX code.",
"break-on-boundaries"
)
);

// add the position of the group 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 = "LatexConversion";
messagesLst.messages.push(message);

return node.content;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import * as Ast from "@unified-latex/unified-latex-types";
import { htmlLike } from "@unified-latex/unified-latex-util-html-like";
import {
parseTabularSpec,
TabularColumn,
} from "@unified-latex/unified-latex-ctan/package/tabularx";
import { parseAlignEnvironment } from "@unified-latex/unified-latex-util-align";
import { getArgsContent } from "@unified-latex/unified-latex-util-arguments";
import { trim } from "@unified-latex/unified-latex-util-trim";

type Attributes = Record<string, string | Record<string, string>>;

/**
* Convert env into a tabular in PreTeXt.
*/
export function createTableFromTabular(env: Ast.Environment) {
const tabularBody = parseAlignEnvironment(env.content);
const args = getArgsContent(env);
let columnSpecs: TabularColumn[] = [];
try {
columnSpecs = parseTabularSpec(args[1] || []);
} catch (e) {}

// for the tabular tag
const attributes: Attributes = {};

// we only need the col tags if one of the columns aren't left aligned/have a border
let notLeftAligned: boolean = false;

// stores which columns have borders to the right
// number is the column's index in columnSpecs
const columnRightBorder: Record<number, boolean> = {};

const tableBody = tabularBody.map((row) => {
const content = row.cells.map((cell, i) => {
const columnSpec = columnSpecs[i];

if (columnSpec) {
const { alignment } = columnSpec;

// this will need to be in the tabular tag
if (
columnSpec.pre_dividers.some(
(div) => div.type === "vert_divider"
)
) {
attributes["left"] = "minor";
}

// check if the column has a right border
if (
columnSpec.post_dividers.some(
(div) => div.type === "vert_divider"
)
) {
columnRightBorder[i] = true;
}

// check if the default alignment isn't used
if (alignment.alignment !== "left") {
notLeftAligned = true;
}
}

// trim whitespace off cell
trim(cell);

return htmlLike({
tag: "cell",
content: cell,
});
});
return htmlLike({ tag: "row", content });
});

// add col tags if needed
if (notLeftAligned || Object.values(columnRightBorder).some((b) => b)) {
// go backwards since adding col tags to the front of the tableBody list
// otherwise, col tags will be in the reversed order
for (let i = columnSpecs.length; i >= 0; i--) {
const columnSpec = columnSpecs[i];

if (!columnSpec) {
continue;
}

const colAttributes: Attributes = {};
const { alignment } = columnSpec;

// add h-align attribute if not default
if (alignment.alignment !== "left") {
colAttributes["halign"] = alignment.alignment;
}

// if there is a right border add it
if (columnRightBorder[i] === true) {
colAttributes["right"] = "minor";
}

tableBody.unshift(
htmlLike({ tag: "col", attributes: colAttributes })
);
}
}

return htmlLike({
tag: "tabular",
content: tableBody,
attributes: attributes,
});
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import cssesc from "cssesc";
import {
parseTabularSpec,
TabularColumn,
} from "@unified-latex/unified-latex-ctan/package/tabularx";
import { htmlLike } from "@unified-latex/unified-latex-util-html-like";
import * as Ast from "@unified-latex/unified-latex-types";
import { parseAlignEnvironment } from "@unified-latex/unified-latex-util-align";
import {
getArgsContent,
getNamedArgsContent,
} from "@unified-latex/unified-latex-util-arguments";
import { getNamedArgsContent } from "@unified-latex/unified-latex-util-arguments";
import { match } from "@unified-latex/unified-latex-util-match";
import { printRaw } from "@unified-latex/unified-latex-util-print-raw";
import { wrapPars } from "../wrap-pars";
import { VisitInfo } from "@unified-latex/unified-latex-util-visit";
import { VFile } from "vfile";
import { makeWarningMessage } from "./utils";
import { createTableFromTabular } from "./create-table-from-tabular";

const ITEM_ARG_NAMES_REG = ["label"] as const;
const ITEM_ARG_NAMES_BEAMER = [null, "label", null] as const;
Expand Down Expand Up @@ -48,139 +41,92 @@ function getItemArgs(node: Ast.Macro): ItemArgs {
return ret as ItemArgs;
}

function enumerateFactory(parentTag = "ol", className = "enumerate") {
function enumerateFactory(parentTag = "ol") {
return function enumerateToHtml(env: Ast.Environment) {
// The body of an enumerate has already been processed and all relevant parts have
// been attached to \item macros as arguments.
const items = env.content.filter((node) => match.macro(node, "item"));

// Figure out if there any manually-specified item labels. If there are,
// we need to add a title tag
let isDescriptionList = false;

const content = items.flatMap((node) => {
if (!match.macro(node) || !node.args) {
return [];
}

const attributes: Record<string, string | Record<string, string>> =
{};
// Figure out if there any manually-specified item labels. If there are,
// we need to specify a custom list-style-type.
// We test the open mark to see if an optional argument was actually supplied.
const namedArgs = getItemArgs(node);

// if there are custom markers, don't want the title tag to be wrapped in pars
// so we wrap the body first
namedArgs.body = wrapPars(namedArgs.body);

// check if a custom marker is used
if (namedArgs.label != null) {
const formattedLabel = cssesc(printRaw(namedArgs.label || []));
attributes.style = {
// Note the space after `formattedLabel`. That is on purpose!
"list-style-type": formattedLabel
? `'${formattedLabel} '`
: "none",
};
isDescriptionList = true;

// add title tag containing custom marker
namedArgs.body.unshift(
htmlLike({
tag: "title",
content: namedArgs.label,
})
);
}

const body = namedArgs.body;

return htmlLike({
tag: "li",
content: wrapPars(body),
attributes,
content: body,
});
});

return htmlLike({
tag: parentTag,
attributes: { className },
tag: isDescriptionList ? "dl" : parentTag,
content,
});
};
}

function createCenteredElement(env: Ast.Environment) {
return htmlLike({
tag: "center",
attributes: { className: "center" },
content: env.content,
});
}

function createTableFromTabular(env: Ast.Environment) {
const tabularBody = parseAlignEnvironment(env.content);
const args = getArgsContent(env);
let columnSpecs: TabularColumn[] = [];
try {
columnSpecs = parseTabularSpec(args[1] || []);
} catch (e) {}

const tableBody = tabularBody.map((row) => {
const content = row.cells.map((cell, i) => {
const columnSpec = columnSpecs[i];
const styles: Record<string, string> = {};
if (columnSpec) {
const { alignment } = columnSpec;
if (alignment.alignment === "center") {
styles["text-align"] = "center";
}
if (alignment.alignment === "right") {
styles["text-align"] = "right";
}
if (
columnSpec.pre_dividers.some(
(div) => div.type === "vert_divider"
)
) {
styles["border-left"] = "1px solid";
}
if (
columnSpec.post_dividers.some(
(div) => div.type === "vert_divider"
)
) {
styles["border-right"] = "1px solid";
}
}
return htmlLike(
Object.keys(styles).length > 0
? {
tag: "td",
content: cell,
attributes: { style: styles },
}
: {
tag: "td",
content: cell,
}
);
});
return htmlLike({ tag: "tr", content });
});
/**
* Remove the env environment by returning the content in env only.
*/
function removeEnv(env: Ast.Environment, info: VisitInfo, file?: VFile) {
// add warning
file?.message(
makeWarningMessage(
env,
`Warning: There is no equivalent tag for \"${env.env}\", so the ${env.env} environment was removed.`,
"environment-subs"
)
);

return htmlLike({
tag: "table",
content: [
htmlLike({
tag: "tbody",
content: tableBody,
}),
],
attributes: { className: "tabular" },
});
return env.content;
}

/**
* Rules for replacing a macro with an html-like macro
* that will render has html when printed.
* that will render has pretext when printed.
*/
export const environmentReplacements: Record<
string,
(
node: Ast.Environment,
info: VisitInfo
) => Ast.Macro | Ast.String | Ast.Environment
info: VisitInfo,
file?: VFile
) => Ast.Macro | Ast.String | Ast.Environment | Ast.Node[]
> = {
enumerate: enumerateFactory("ol"),
itemize: enumerateFactory("ul", "itemize"),
center: createCenteredElement,
itemize: enumerateFactory("ul"),
center: removeEnv,
tabular: createTableFromTabular,
quote: (env) => {
return htmlLike({
tag: "blockquote",
content: env.content,
attributes: { className: "environment quote" },
});
},
};
Loading