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
5 changes: 5 additions & 0 deletions .changeset/chubby-jokes-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pretextbook/format": minor
---

Improve verbatim; add option to break long lists of attributes
6 changes: 6 additions & 0 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@
"default": true,
"markdownDescription": "Whether to add a line break after each period in a paragraph."
},
"pretext-tools.formatter.breakLongAttributes": {
"order": 4,
"type": "boolean",
"default": false,
"markdownDescription": "Wrap long block start-tag attributes onto their own lines."
},
"pretext-tools.formatter.printWidth": {
"order": 5,
"type": "number",
Expand Down
2 changes: 2 additions & 0 deletions packages/format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ You can pass options to customize formatting:
const options = {
breakLines: "many",
breakSentences: true,
breakLongAttributes: true,
printWidth: 80,
insertSpaces: true,
tabSize: 2,
Expand Down Expand Up @@ -79,6 +80,7 @@ Options:
- `--stdin` read input from stdin
- `--break-lines <few|some|many>` choose line break density
- `--break-sentences` break plain-text sentences onto new lines
- `--break-long-attributes` wrap long block start-tag attributes onto their own lines
- `--tab-size <n>` set spaces per indent level
- `--use-tabs` indent with tabs instead of spaces
- `-h, --help` show help
Expand Down
7 changes: 6 additions & 1 deletion packages/format/cli.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Options:
--stdin Read input from stdin
--break-lines <mode> Line break mode: few | some | many
--break-sentences Break plain-text sentences onto new lines
--break-long-attributes Wrap long block start-tag attributes onto their own lines
--tab-size <n> Number of spaces per indent level
--use-tabs Indent with tabs instead of spaces
-h, --help Show this help
Expand All @@ -36,6 +37,7 @@ function parseCli() {
stdin: { type: "boolean", default: false },
"break-lines": { type: "string" },
"break-sentences": { type: "boolean", default: false },
"break-long-attributes": { type: "boolean", default: false },
"tab-size": { type: "string" },
"use-tabs": { type: "boolean", default: false },
help: { type: "boolean", short: "h", default: false },
Expand All @@ -50,7 +52,7 @@ function parseCli() {
}

function parseOptions(values) {
/** @type {{breakLines?: "few" | "some" | "many"; breakSentences?: boolean; insertSpaces?: boolean; tabSize?: number}} */
/** @type {{breakLines?: "few" | "some" | "many"; breakSentences?: boolean; breakLongAttributes?: boolean; insertSpaces?: boolean; tabSize?: number}} */
const formatOptions = {};
if (values["break-lines"] !== undefined) {
if (!["few", "some", "many"].includes(values["break-lines"])) {
Expand All @@ -61,6 +63,9 @@ function parseOptions(values) {
if (values["break-sentences"]) {
formatOptions.breakSentences = true;
}
if (values["break-long-attributes"]) {
formatOptions.breakLongAttributes = true;
}
if (values["use-tabs"]) {
formatOptions.insertSpaces = false;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/format/src/lib/__fixtures__/minimal-book.ptx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?><pretext><book xml:id="my-book"><title>My
Book</title><frontmatter><titlepage><author><personname>Jane
Doe</personname><institution>Some University</institution></author><date>2024</date></titlepage><abstract><p>This is a very short abstract for the book.</p></abstract></frontmatter><chapter xml:id="ch-intro"><title>Introduction</title><p>This is the first paragraph of the introduction chapter. It has some text that is reasonably long.</p><p>This is a second paragraph.</p><section xml:id="sec-first"><title>First Section</title><p>Some content here in the first section.</p></section><section xml:id="sec-second"><title>Second Section</title><p>Content in the second section.</p></section></chapter><backmatter><index/></backmatter></book></pretext>
Doe</personname><institution>Some University</institution></author><date>2024</date></titlepage><abstract><p>This is a very short abstract for the book.</p></abstract></frontmatter><chapter xml:id="ch-intro-has-a-really-long-id" label="a-really-long-label-for-this-chapter" custom="custom-attribute"><title>Introduction</title><p>This is the first paragraph of the introduction chapter. It has some text that is reasonably long.</p><p>This is a second paragraph.</p><section xml:id="sec-first"><title>First Section</title><p>Some content here in the first section.</p></section><section xml:id="sec-second"><title>Second Section</title><p>Content in the second section.</p></section></chapter><backmatter><index/></backmatter></book></pretext>
26 changes: 25 additions & 1 deletion packages/format/src/lib/__fixtures__/verbatim-blocks.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ f.factor()
(x + 1)*(x + 2)
</output></sage>
<p>Inline code like <c>print("hello")</c> should remain inline and not be broken across lines.</p>
<p>Inline code with surrounding spaces <c> x + 1 </c> should preserve those spaces.</p>
<p>A program with a trailing blank line:</p>
<program language="python">
def foo():
return 1

</program>
<p>
A console session:
</p>
Expand Down Expand Up @@ -49,8 +56,25 @@ f.factor()
edgePercentage: 0.35
});
anim.doDetectCycle();
</script>
</script>
<xi:include href="../../fragments/ds-graph-instructions.xml"/>
</interactive>

<listing>
<program>
<preamble>//line A
//line B
</preamble>
<code><![CDATA[


//line C

]]></code>
<postamble>

//line D</postamble>
</program>
</listing>

</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>

<pretext>
<book xml:id="my-book">
<title>My Book</title>

<frontmatter>
<titlepage>
<author>
<personname>Jane Doe</personname>
<institution>Some University</institution>
</author>
<date>2024</date>
</titlepage>

<abstract>
<p>
This is a very short abstract for the book.
</p>
</abstract>
</frontmatter>

<chapter xml:id="ch-intro-has-a-really-long-id"
label="a-really-long-label-for-this-chapter"
custom="custom-attribute">
<title>Introduction</title>

<p>
This is the first paragraph of the introduction chapter. It has some
text that is reasonably long.
</p>

<p>
This is a second paragraph.
</p>

<section xml:id="sec-first">
<title>First Section</title>

<p>
Some content here in the first section.
</p>
</section>

<section xml:id="sec-second">
<title>Second Section</title>

<p>
Content in the second section.
</p>
</section>
</chapter>

<backmatter>
<index/>
</backmatter>
</book>
</pretext>
2 changes: 1 addition & 1 deletion packages/format/src/lib/__snapshots__/minimal-book-few.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</p>
</abstract>
</frontmatter>
<chapter xml:id="ch-intro">
<chapter xml:id="ch-intro-has-a-really-long-id" label="a-really-long-label-for-this-chapter" custom="custom-attribute">
<title>Introduction</title>
<p>
This is the first paragraph of the introduction chapter. It has some
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

</frontmatter>

<chapter xml:id="ch-intro">
<chapter xml:id="ch-intro-has-a-really-long-id" label="a-really-long-label-for-this-chapter" custom="custom-attribute">

<title>Introduction</title>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</abstract>
</frontmatter>

<chapter xml:id="ch-intro">
<chapter xml:id="ch-intro-has-a-really-long-id" label="a-really-long-label-for-this-chapter" custom="custom-attribute">
<title>Introduction</title>

<p>
Expand Down
2 changes: 1 addition & 1 deletion packages/format/src/lib/__snapshots__/minimal-book.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</abstract>
</frontmatter>

<chapter xml:id="ch-intro">
<chapter xml:id="ch-intro-has-a-really-long-id" label="a-really-long-label-for-this-chapter" custom="custom-attribute">
<title>Introduction</title>

<p>
Expand Down
Loading