Skip to content

Commit

Permalink
added new file for creating tabular
Browse files Browse the repository at this point in the history
  • Loading branch information
renee-k committed Aug 11, 2024
1 parent 14a005c commit b5cfa21
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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}.
Expand Down Expand Up @@ -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 = "latex-to-pretext:warning";
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; // supports all alignments but stuff like p{'width'} (closest is @colspan in cell)
}

// 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,20 +1,13 @@
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 { wrapPars } from "../wrap-pars";
import { VisitInfo } from "@unified-latex/unified-latex-util-visit";
import { trim } from "@unified-latex/unified-latex-util-trim";
import { VFile } from "vfile";
import { emptyStringWithWarning } from "./utils";
import { emptyStringWithWarning, makeWarningMessage } from "./utils";
import { createTableFromTabular } from "./create-table-from-tabular";
import { fileURLToPath } from "url";

const ITEM_ARG_NAMES_REG = ["label"] as const;
const ITEM_ARG_NAMES_BEAMER = [null, "label", null] as const;
Expand Down Expand Up @@ -99,104 +92,22 @@ function enumerateFactory(parentTag = "ol") {
};
}

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: Record<string, string | Record<string, string>> = {};

// 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: Record<
string,
string | Record<string, string>
> = {};
const { alignment } = columnSpec;

// add h-align attribute if not default
if (alignment.alignment !== "left") {
colAttributes["halign"] = alignment.alignment; // supports all alignments but stuff like p{'width'} (closest is @colspan in cell)
}

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

tableBody.unshift(
htmlLike({ tag: "col", attributes: colAttributes })
);
}
}
function removeCenterEnv(
center: Ast.Environment,
info: VisitInfo,
file?: VFile
) {
// add warning
file?.message(
makeWarningMessage(
center,
`Warning: There is no equivalent tag for \"center\", so the center environment was removed.`,
"environment-subs"
)
);

return htmlLike({
tag: "tabular",
content: tableBody,
attributes: attributes,
});
// can't return Node[] tho
return center.content; // or should it convert the stuff in here? or is that done already in plugin?
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getArgsContent } from "@unified-latex/unified-latex-util-arguments";
import { printRaw } from "@unified-latex/unified-latex-util-print-raw";
import { VisitInfo } from "@unified-latex/unified-latex-util-visit";
import { VFile } from "unified-lint-rule/lib";
import { createMessage, emptyStringWithWarning } from "./utils";
import { makeWarningMessage, emptyStringWithWarning } from "./utils";

/**
* Factory function that generates html-like macros that wrap their contents.
Expand All @@ -27,7 +27,7 @@ function factory(

// add a warning message to the file if needed
if (warningMessage && file) {
const message = createMessage(
const message = makeWarningMessage(
macro,
`Warning: There is no equivalent tag for \"${macro.content}\", \"${tag}\" was used as a replacement.`,
"macro-subs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { anyMacro, match } from "@unified-latex/unified-latex-util-match";
import { visit } from "@unified-latex/unified-latex-util-visit";
import { KATEX_SUPPORT } from "./katex-subs";
import { VFileMessage } from "vfile-message";
import { makeWarningMessage } from "./utils";

/**
* Return a list of macros used in ast that are unsupported by KaTeX
Expand All @@ -21,32 +22,16 @@ export function reportMacrosUnsupportedByKatex(ast: Ast.Ast): {
if (anyMacro(node) && info.context.hasMathModeAncestor) {
// check if not supported by katex
if (!isSupported(node)) {
// get rid of type never
node = node as Ast.Macro;

// create a message specifing the unsupported macro
const message = new VFileMessage(
`Warning: \"${node.content}\" is unsupported by Katex.`
// add a warning message
unsupported.messages.push(
makeWarningMessage(
node,
`Warning: \"${
(node as Ast.Macro).content
}\" is unsupported by Katex.`,
"report-unsupported-macro-katex"
)
);

// add the position of the node 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";
unsupported.messages.push(message);
}
}
});
Expand Down
Loading

0 comments on commit b5cfa21

Please sign in to comment.