Skip to content

Commit 901fc2c

Browse files
cscheidjjallaire
authored andcommitted
Add mappedTrim. Use mappedTrim for ojs, mermaid, and dot. Closes #1648. Closes #1452.
1 parent 1e72e0a commit 901fc2c

File tree

5 files changed

+99
-12
lines changed

5 files changed

+99
-12
lines changed

src/core/handlers/base.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
mappedConcat,
2222
mappedLines,
2323
MappedString,
24+
mappedTrim,
2425
} from "../lib/mapped-text.ts";
2526
import {
2627
addLanguageComment,
@@ -585,7 +586,7 @@ export const baseHandler: LanguageHandler = {
585586
classes: cellInputClasses,
586587
attrs: cellInputAttrs,
587588
});
588-
cellInput.push(pandocRawStr(mappedConcat(inputLines)));
589+
cellInput.push(pandocRawStr(mappedTrim(mappedConcat(inputLines))));
589590
cellBlock.push(cellInput);
590591
break;
591592
}
@@ -602,7 +603,7 @@ export const baseHandler: LanguageHandler = {
602603
...frontMatterLines,
603604
...inputLines,
604605
]);
605-
cellFence.push(pandocRawStr(fencedInput));
606+
cellFence.push(pandocRawStr(mappedTrim(fencedInput)));
606607
cellInput.push(cellFence);
607608
cellBlock.push(cellInput);
608609
break;

src/core/lib/mapped-text.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,56 @@ export function breakOnDelimiter(
392392
}
393393
return substrings;
394394
}
395+
396+
function findSpaceStart(string: MappedString): number {
397+
const result = string.value.match(/^\s+/);
398+
if (result === null || result.length === 0) {
399+
return 0;
400+
}
401+
return result[0].length;
402+
}
403+
404+
function findSpaceEnd(string: MappedString): number {
405+
const result = string.value.match(/\s+$/);
406+
if (result === null || result.length === 0) {
407+
return 0;
408+
}
409+
return result[0].length;
410+
}
411+
412+
/**
413+
* mappedTrim(): MappedString version of String.trim()
414+
*/
415+
export function mappedTrim(string: MappedString): MappedString {
416+
const start = findSpaceStart(string);
417+
const end = findSpaceEnd(string);
418+
if (start === 0 && end === 0) {
419+
return string;
420+
}
421+
if (start > string.value.length - end) {
422+
return mappedSubstring(string, 0, 0);
423+
}
424+
return mappedSubstring(string, start, string.value.length - end);
425+
}
426+
427+
/**
428+
* mappedTrimStart(): MappedString version of String.trimStart()
429+
*/
430+
export function mappedTrimStart(string: MappedString): MappedString {
431+
const start = findSpaceStart(string);
432+
if (start === 0) {
433+
return string;
434+
}
435+
return mappedSubstring(string, start, string.value.length);
436+
}
437+
438+
/**
439+
* mappedTrimEnd(): MappedString version of String.trimEnd()
440+
*/
441+
export function mappedTrimEnd(string: MappedString): MappedString {
442+
const end = findSpaceEnd(string);
443+
if (end === 0) {
444+
return string;
445+
}
446+
return mappedSubstring(string, 0, string.value.length - end);
447+
}

src/execute/ojs/compile.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import {
7676
import {
7777
EitherString,
7878
join as mappedJoin,
79+
mappedTrim,
7980
} from "../../core/lib/mapped-text.ts";
8081
import { getDivAttributes } from "../../core/handlers/base.ts";
8182
import { pathWithForwardSlashes } from "../../core/path.ts";
@@ -276,7 +277,7 @@ export async function ojsCompile(
276277
`source-offset="${cell.sourceOffset}"`,
277278
],
278279
});
279-
preDiv.push(pandocRawStr(cell.sourceVerbatim.value));
280+
preDiv.push(pandocRawStr(cell.sourceVerbatim.value.trim()));
280281
div.push(preDiv);
281282
const errMsgDiv = pandocDiv({
282283
classes: ["cell-output", "cell-output-error"],
@@ -499,7 +500,7 @@ export async function ojsCompile(
499500
classes: ourClasses,
500501
attrs: ourAttrs,
501502
});
502-
srcDiv.push(pandocRawStr(cell.sourceVerbatim.value));
503+
srcDiv.push(pandocRawStr(cell.sourceVerbatim.value.trim()));
503504
div.push(srcDiv);
504505
}
505506

@@ -557,7 +558,7 @@ export async function ojsCompile(
557558
cellSrcStr.value.substring(
558559
innerInfo[0].start,
559560
innerInfo[innerInfo.length - 1].end,
560-
),
561+
).trim(),
561562
));
562563
div.push(srcDiv);
563564
}
@@ -629,7 +630,7 @@ export async function ojsCompile(
629630
classes: srcConfig.classes,
630631
});
631632
srcDiv.push(
632-
pandocRawStr(cellSrcStr),
633+
pandocRawStr(mappedTrim(cellSrcStr)),
633634
);
634635
div.push(srcDiv);
635636
}

src/resources/formats/html/ojs/quarto-ojs-runtime.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @quarto/quarto-ojs-runtime v0.0.10 Copyright 2022 undefined
1+
// @quarto/quarto-ojs-runtime v0.0.11 Copyright 2022 undefined
22
var EOL = {},
33
EOF = {},
44
QUOTE = 34,
@@ -1668,7 +1668,9 @@ class PandocCodeDecorator {
16681668
const startIndex = startEntry.index;
16691669
const endIndex = endEntry && endEntry.index || this._elementEntryPoints.length;
16701670
for (let i = startIndex; i < endIndex; ++i) {
1671-
yield this._elementEntryPoints[i];
1671+
if (this._elementEntryPoints[i] !== void 0) {
1672+
yield this._elementEntryPoints[i];
1673+
}
16721674
}
16731675
}
16741676
decorateSpan(start, end, classes) {
@@ -1705,11 +1707,11 @@ class PandocCodeDecorator {
17051707
this._elementEntryPoints.sort((a, b) => a.offset - b.offset);
17061708
};
17071709
const startEntry = this.locateEntry(start);
1708-
if (startEntry !== void 0 && startEntry.entry.offset != start) {
1710+
if (startEntry !== void 0 && startEntry.entry !== void 0 && startEntry.entry.offset != start) {
17091711
splitEntry(startEntry.entry, start);
17101712
}
17111713
const endEntry = this.locateEntry(end);
1712-
if (endEntry !== void 0 && endEntry.entry.offset !== end) {
1714+
if (endEntry !== void 0 && startEntry.entry !== void 0 && endEntry.entry.offset !== end) {
17131715
splitEntry(endEntry.entry, end);
17141716
}
17151717
}

tests/unit/mapped-strings/mapped-text.test.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
*
66
*/
77
import { unitTest } from "../../test.ts";
8-
import { assert } from "testing/asserts.ts";
8+
import { assert, assertEquals } from "testing/asserts.ts";
99
import {
1010
asMappedString,
1111
mappedDiff,
1212
mappedString,
1313
} from "../../../src/core/mapped-text.ts";
14-
import { mappedSubstring } from "../../../src/core/lib/mapped-text.ts";
14+
import {
15+
mappedSubstring,
16+
mappedTrim,
17+
mappedTrimEnd,
18+
mappedTrimStart,
19+
} from "../../../src/core/lib/mapped-text.ts";
1520

1621
// deno-lint-ignore require-await
1722
unitTest("mapped-text - mappedString()", async () => {
@@ -188,3 +193,28 @@ viewof x = Inputs.range([0, 100], label = "hello!", value = 20)
188193

189194
mappedDiff(asMappedString(text1), text2);
190195
});
196+
197+
// deno-lint-ignore require-await
198+
unitTest("mapped-text - mappedTrim{,Start,End}()", async () => {
199+
const whitespace = "\u000A\u000D\u2028\u2029\u0009\u000B\u000C\uFEFF \t";
200+
const content = "a \n";
201+
for (let i = 0; i < 1000; ++i) {
202+
const startTrimLength = Math.random() * 10;
203+
const endTrimLength = Math.random() * 10;
204+
const contentLength = Math.random() * 10;
205+
const strContent = [];
206+
for (let j = 0; j < startTrimLength; ++j) {
207+
strContent.push(whitespace[~~(Math.random() * whitespace.length)]);
208+
}
209+
for (let j = 0; j < contentLength; ++j) {
210+
strContent.push(content[~~(Math.random() * content.length)]);
211+
}
212+
for (let j = 0; j < endTrimLength; ++j) {
213+
strContent.push(whitespace[~~(Math.random() * whitespace.length)]);
214+
}
215+
const mappedStr = asMappedString(strContent.join(""));
216+
assertEquals(mappedTrim(mappedStr).value, mappedStr.value.trim());
217+
assertEquals(mappedTrimStart(mappedStr).value, mappedStr.value.trimStart());
218+
assertEquals(mappedTrimEnd(mappedStr).value, mappedStr.value.trimEnd());
219+
}
220+
});

0 commit comments

Comments
 (0)