Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 34 additions & 3 deletions bundle/latex2html5.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4785,6 +4785,9 @@ var LaTeX2HTML5 = (() => {
});
return;
}
if (typeof data.text === "string") {
data.text = this.parseLabel(data.text);
}
}
plot[k].push({ data, env, match: m, fn: this.PSTricks.Functions[k] });
if (this.dialect === "pstricks") {
Expand Down Expand Up @@ -4906,13 +4909,41 @@ var LaTeX2HTML5 = (() => {
return contents;
}
parseText(line) {
var contents = this.parseTextTransforms(line);
Object.entries(this.Headers.Expressions).forEach(([k, exp]) => {
contents = this.parseHeadersExpression(line, exp, k, contents);
});
return contents;
}
parseLabel(line) {
var contents = "";
var textStart = 0;
var mathStart = -1;
for (var i = 0; i < line.length; i++) {
if (line[i] !== "$") {
continue;
}
if (mathStart === -1) {
contents += this.parseTextTransforms(line.slice(textStart, i));
mathStart = i;
} else {
contents += line.slice(mathStart, i + 1);
textStart = i + 1;
mathStart = -1;
}
}
if (mathStart === -1) {
contents += this.parseTextTransforms(line.slice(textStart));
} else {
contents += this.parseTextTransforms(line.slice(mathStart));
}
return contents;
}
parseTextTransforms(line) {
var contents = line;
Object.entries(this.Text.Expressions).forEach(([k, exp]) => {
contents = this.parseTextExpression(line, exp, k, contents);
});
Object.entries(this.Headers.Expressions).forEach(([k, exp]) => {
contents = this.parseHeadersExpression(line, exp, k, contents);
});
return contents;
}
// -------------------------------------------------------------------------
Expand Down
46 changes: 41 additions & 5 deletions packages/latex2js/src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,9 @@ class Parser {
});
return;
}
if (typeof data.text === 'string') {
data.text = this.parseLabel(data.text);
}
}

plot[k].push({ data: data, env: env, match: m, fn: this.PSTricks.Functions[k] });
Expand Down Expand Up @@ -1032,11 +1035,7 @@ class Parser {
}

parseText(line: string): string {
var contents = line;
// TEXT
Object.entries(this.Text.Expressions).forEach(([k, exp]: [string, any]) => {
contents = this.parseTextExpression(line, exp, k, contents);
});
var contents = this.parseTextTransforms(line);

// HEADERS
Object.entries(this.Headers.Expressions).forEach(([k, exp]: [string, any]) => {
Expand All @@ -1046,6 +1045,43 @@ class Parser {
return contents;
}

parseLabel(line: string): string {
var contents = '';
var textStart = 0;
var mathStart = -1;

for (var i = 0; i < line.length; i++) {
if (line[i] !== '$') {
continue;
}
if (mathStart === -1) {
contents += this.parseTextTransforms(line.slice(textStart, i));
mathStart = i;
} else {
contents += line.slice(mathStart, i + 1);
textStart = i + 1;
mathStart = -1;
}
}

if (mathStart === -1) {
contents += this.parseTextTransforms(line.slice(textStart));
} else {
contents += this.parseTextTransforms(line.slice(mathStart));
}

return contents;
}

parseTextTransforms(line: string): string {
var contents = line;
Object.entries(this.Text.Expressions).forEach(([k, exp]: [string, any]) => {
contents = this.parseTextExpression(line, exp, k, contents);
});

return contents;
}

// -------------------------------------------------------------------------
// Diagnostics
// -------------------------------------------------------------------------
Expand Down
25 changes: 25 additions & 0 deletions packages/latex2js/test/parser-semantics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,31 @@ describe('Peggy grammar parser (new)', () => {
expect(env.plot.rput[0].data.text).toBe('$a--b$');
});

it('applies text transforms to rput labels', () => {
const parsed = latex.parse(`
\\begin{pspicture}(0,0)(4,4)
\\rput(1.5,1){\\LaTeX}
\\rput(2,1){\\TeX}
\\rput(0.3,3.75){ $Im$ }
\\end{pspicture}
`);

const env = parsed.find((e: any) => e.type === 'pspicture');
const labels = env.plot.rput.map((rput: any) => rput.data.text);
expect(labels).toEqual(['$\\LaTeX$', '$\\TeX$', ' $Im$ ']);
});

it('transforms non-math parts of mixed rput labels', () => {
const parsed = latex.parse(`
\\begin{pspicture}(0,0)(4,4)
\\rput(1,1){{\\LaTeX\\ vs $a--b$}}
\\end{pspicture}
`);

const env = parsed.find((e: any) => e.type === 'pspicture');
expect(env.plot.rput[0].data.text).toBe('{$\\LaTeX$ vs $a--b$}');
});

it('collects diagnostics for unknown commands', () => {
const parsed = latex.parse(`
\\begin{pspicture}(0,0)(4,4)
Expand Down
Loading