Skip to content

Commit

Permalink
began fixing enumerate
Browse files Browse the repository at this point in the history
  • Loading branch information
renee-k committed Jul 24, 2024
1 parent 09c711e commit dae8baf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cssesc from "cssesc";
// import cssesc from "cssesc";
import {
parseTabularSpec,
TabularColumn,
Expand All @@ -14,6 +14,7 @@ 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 { trim } from "@unified-latex/unified-latex-util-trim";

const ITEM_ARG_NAMES_REG = ["label"] as const;
const ITEM_ARG_NAMES_BEAMER = [null, "label", null] as const;
Expand Down Expand Up @@ -48,7 +49,7 @@ 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.
Expand All @@ -58,27 +59,45 @@ function enumerateFactory(parentTag = "ol", className = "enumerate") {
return [];
}

const attributes: Record<string, string | Record<string, string>> =
{};
// const attributes: Record<string, string | Record<string, string>> =
// {};

const customMarker: Record<string, string | Ast.Macro> = {};


// 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 (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",
};
// parentTag = "dl" // can't do it here
const formattedLabel = printRaw(namedArgs.label || []) // cssesc(printRaw(namedArgs.label || []));
// attributes.style = {
// // Note the space after `formattedLabel`. That is on purpose!
// "list-style-type": formattedLabel
// ? `'${formattedLabel} '`
// : "none",
// };
console.log(formattedLabel)
// customMarker.marker = htmlLike({
// tag: "title",
// content: namedArgs.label,
// // attributes,
// })
// namedArgs.body = htmlLike({
// tag: "title",
// content: namedArgs.label,
// // attributes,
// })
}

const body = namedArgs.body;
console.log(namedArgs.body)
return htmlLike({
tag: "li",
content: wrapPars(body),
attributes,
// attributes,
});
});

Expand Down Expand Up @@ -133,15 +152,18 @@ function createTableFromTabular(env: Ast.Environment) {
styles["border-right"] = "1px solid";
}
}
// trim whitespace off cell
trim(cell)

return htmlLike(
Object.keys(styles).length > 0
? {
tag: "cell", // cell -> td,
tag: "cell",
content: cell,
attributes: { style: styles },
}
: {
tag: "cell", // cell -> td,
tag: "cell",
content: cell,
}
);
Expand Down Expand Up @@ -177,7 +199,7 @@ export const environmentReplacements: Record<
) => Ast.Macro | Ast.String | Ast.Environment
> = {
enumerate: enumerateFactory("ol"),
itemize: enumerateFactory("ul", "itemize"),
itemize: enumerateFactory("ul"),
center: createCenteredElement,
tabular: createTableFromTabular,
quote: (env) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/unified-latex-to-pretext/libs/split-for-pars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export function splitForPars(
continue;
}
// Display-math should always break pars
if (node.type === "displaymath") {
pushBody();
ret.push({ content: [node], wrapInPar: false });
continue;
}
// if (node.type === "displaymath") {
// pushBody();
// ret.push({ content: [node], wrapInPar: true });
// continue;
// }
if (match.parbreak(node) || match.macro(node, "par")) {
pushBody();
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe("unified-latex-to-pretext:unified-latex-to-pretext", () => {
);
});

it.skip("Converts enumerate environments", () => {
it("Converts enumerate environments", () => {
html = process(`\\begin{enumerate}\\item a\\item b\\end{enumerate}`);
expect(normalizeHtml(html)).toEqual(
normalizeHtml(`<ol><li><p>a</p></li><li><p>b</p></li></ol>`)
Expand Down Expand Up @@ -183,42 +183,21 @@ describe("unified-latex-to-pretext:unified-latex-to-pretext", () => {
`<dl>
<li><title>x)</title><p>a</p></li>
<li><title></title><p>b</p></li>
</dl>`
</dl>` // list is centered though, @margins or @width in tabular could help tho
// width for how much space in marker so not helpful
// margins doesn't work for lists only tabulars
)
);
});

// \n in some tags for some reason, seems to come from normalizeHTML
it.skip("Converts tabular environment", () => {
// the spaces before or after each letter stay
it("Converts tabular environment", () => {
html = process(`\\begin{tabular}{l l}a & b\\\\c & d\\end{tabular}`);

expect(normalizeHtml(html)).toEqual(
// normalizeHtml(
// `<table class="tabular">
// <tbody>
// <tr>
// <td>a</td>
// <td>b</td>
// </tr>
// <tr>
// <td>c</td>
// <td>d</td>
// </tr>
// </tbody>
// </table>`
// )
// centered tho
// can fix with margins="0%"
normalizeHtml(
`<tabular>
<row>
<cell>a</cell>
<cell>b</cell>
</row>
<row>
<cell>c</cell>
<cell>d</cell>
</row>
</tabular>`
`<tabular><row><cell>a</cell><cell>b</cell></row><row><cell>c</cell><cell>d</cell></row></tabular>`
)
);
});
Expand All @@ -227,7 +206,7 @@ describe("unified-latex-to-pretext:unified-latex-to-pretext", () => {
html = process(`\\begin{tabular}{r l}a & b\\\\c & d\\end{tabular}`);
expect(normalizeHtml(html)).toEqual(
// note: even though only one col is right aligned, need all cols
normalizeHtml(
// put all in single line once implemented
`<tabular>
<col halign="right"/>
<col/>
Expand All @@ -240,7 +219,6 @@ describe("unified-latex-to-pretext:unified-latex-to-pretext", () => {
<cell>d</cell>
</row>
</tabular>`
)
);
});

Expand Down Expand Up @@ -295,14 +273,12 @@ describe("unified-latex-to-pretext:unified-latex-to-pretext", () => {
);
});

it.skip("Pars are broken at display math", () => {
it("Pars are broken at display math", () => {
let ast;

ast = process(`x\n\ny\\[a\\\\b\\]z`);
expect(normalizeHtml(ast)).toEqual(
// <me> block not wrapped by <p></> but should be
// likely needs to be added as an option in split-for-pars since type = displaymath isn't a macro or env
normalizeHtml(`<p>x</p><p>y</p><p><me>a\\\\b</me></p><p>z</p>`)
normalizeHtml(`<p>x</p><p>y<me>a\\\\b</me>z</p>`)
);
});
it("replaces command inside argument", () => {
Expand Down Expand Up @@ -330,6 +306,7 @@ describe("unified-latex-to-pretext:unified-latex-to-pretext", () => {

ast = process(`\\paragraph{Important.} Paragraph`);
expect(normalizeHtml(ast)).toEqual(
// should there be a <paragraphs> or <p> tag?
normalizeHtml(`
<title>Important.</title> Paragraph
`)
Expand Down

0 comments on commit dae8baf

Please sign in to comment.