Skip to content

Commit

Permalink
Merge pull request #26 from hildjj/quotes
Browse files Browse the repository at this point in the history
Make literal quoting configurable.
  • Loading branch information
hildjj authored Feb 29, 2024
2 parents c68f7ae + 82c22a0 commit 016e93c
Show file tree
Hide file tree
Showing 10 changed files with 3,190 additions and 3,185 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Usage: peggy-tracks [options] [input_file]
Options:
-a,--action Wrap actions in a box
-c,--css [file name] With no file name, outputs the default CSS. With a
filename, substitutes the CSS in that file.
-d,--depth <number> Maximum depth to expand references (set to Infinity
with --expand).
-e,--expand Expand rule references
filename, substitutes the CSS in that file
-d,--depth <number> Maximum depth to expand references
-e,--expand Expand rule references without depth limit
-o,--output <file name> File in which to save output
-q,--quote <string> Quote character for literals (default: "\"")
-s,--start <rule name> Rule to start with
-h, --help display help for command
```
Expand Down
1 change: 1 addition & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default class CliCommand extends Command {
.addOption(new Option("-d,--depth <number>", "Maximum depth to expand references").conflicts("expand"))
.option("-e,--expand", "Expand rule references without depth limit")
.option("-o,--output <file name>", "File in which to save output")
.option("-q,--quote <string>", "Quote character for literals", '"')
.option("-s,--start <rule name>", "Rule to start with");
}

Expand Down
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ const visit = peggy.compiler.visitor.build({
{ cls: "class" }
);
},
literal(node) {
literal(node, context) {
const q = context.opts.quote ?? '"';
return new Terminal(
`"${stringEscape(node.value)}"${node.ignoreCase ? "i" : ""}`,
`${q}${stringEscape(node.value)}${q}${node.ignoreCase ? "i" : ""}`,
{ cls: "literal" }
);
},
Expand Down
4 changes: 3 additions & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,12 @@ test("more examples", async t => {
test("depth", async t => {
const inp = url.fileURLToPath(new URL("test.peggy", import.meta.url));

let q = "";
for (let i = 0; i < 5; i++) {
const fullURL = url.fileURLToPath(new URL(`output/depth${i}.svg`, import.meta.url));
await exec(t, ["-d", String(i), "-o", fullURL, inp]);
await exec(t, ["-q", q, "-d", String(i), "-o", fullURL, inp]);
t.snapshot(await readFile(fullURL, "utf8"));
q += "Q";
}
});

Expand Down
6 changes: 3 additions & 3 deletions test/output/depth1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 016e93c

Please sign in to comment.