Skip to content

Commit

Permalink
Rename .autopagebreak's depth parameter to maxdepth
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Oct 1, 2024
1 parent c4922e5 commit ac196d1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ContextOptions : MediaStorageOptions {
/**
* When a [Heading] node has a depth equals or less than this value, a page break is forced.
*/
val autoPageBreakHeadingDepth: Int
val autoPageBreakHeadingMaxDepth: Int

/**
* Whether automatic identifiers should be generated for elements
Expand All @@ -24,15 +24,15 @@ interface ContextOptions : MediaStorageOptions {

/**
* @return whether the [heading] node should force a page break
* @see ContextOptions.autoPageBreakHeadingDepth
* @see ContextOptions.autoPageBreakHeadingMaxDepth
*/
fun Context.shouldAutoPageBreak(heading: Heading) = !heading.isMarker && heading.depth <= this.options.autoPageBreakHeadingDepth
fun Context.shouldAutoPageBreak(heading: Heading) = !heading.isMarker && heading.depth <= this.options.autoPageBreakHeadingMaxDepth

/**
* Mutable [ContextOptions] implementation.
*/
data class MutableContextOptions(
override var autoPageBreakHeadingDepth: Int = 1,
override var autoPageBreakHeadingMaxDepth: Int = 1,
override var enableAutomaticIdentifiers: Boolean = true,
override var enableRemoteMediaStorage: Boolean = false,
override var enableLocalMediaStorage: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ class HtmlNodeRendererTest {
val noIdNoPageBreak =
MutableContext(
QuarkdownFlavor,
options = MutableContextOptions(autoPageBreakHeadingDepth = 0, enableAutomaticIdentifiers = false),
options = MutableContextOptions(autoPageBreakHeadingMaxDepth = 0, enableAutomaticIdentifiers = false),
)

assertEquals(out.next(), Heading(1, listOf(Text("Foo bar"))).render(noIdNoPageBreak))
Expand All @@ -421,7 +421,7 @@ class HtmlNodeRendererTest {
val idNoPageBreak =
MutableContext(
QuarkdownFlavor,
options = MutableContextOptions(autoPageBreakHeadingDepth = 0),
options = MutableContextOptions(autoPageBreakHeadingMaxDepth = 0),
)

assertEquals(out.next(), Heading(1, listOf(Text("Foo bar"))).render(idNoPageBreak))
Expand All @@ -430,7 +430,7 @@ class HtmlNodeRendererTest {

// Automatic ID, force page break on depth <= 2
val autoPageBreak =
MutableContext(QuarkdownFlavor, options = MutableContextOptions(autoPageBreakHeadingDepth = 2))
MutableContext(QuarkdownFlavor, options = MutableContextOptions(autoPageBreakHeadingMaxDepth = 2))

assertEquals(out.next(), Heading(1, listOf(Text("Foo bar"))).render(autoPageBreak))
assertEquals(out.next(), Heading(2, listOf(Text("Foo bar"))).render(autoPageBreak))
Expand Down
2 changes: 1 addition & 1 deletion demo/demo.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.doctype {slides}
.doclang {English}
.theme {darko} layout:{minimal}
.autopagebreak depth:{2}
.autopagebreak maxdepth:{2}

.footer
.docauthor
Expand Down
12 changes: 6 additions & 6 deletions stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Document.kt
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,22 @@ fun totalPages() = PageCounter(PageCounter.Target.TOTAL).wrappedAsValue()

/**
* Sets a new automatic page break threshold when a heading is found:
* if a heading's depth value (the amount of leading `#`s) is equals or less than [depth],
* if a heading's depth value (the amount of leading `#`s) is equals or less than [maxDepth],
* a page break is forced before the heading.
* @param depth heading depth to force page breaks for (positive only).
* @throws IllegalArgumentException if [depth] is a negative value
* @param maxDepth heading depth to force page breaks for (positive only).
* @throws IllegalArgumentException if [maxDepth] is a negative value
* @see disableAutoPageBreak
*/
@Name("autopagebreak")
fun autoPageBreak(
@Injected context: MutableContext,
depth: Int,
@Name("maxdepth") maxDepth: Int,
): VoidValue {
if (depth < 0) {
if (maxDepth < 0) {
throw IllegalArgumentException("Heading depth cannot be negative.")
}

context.options.autoPageBreakHeadingDepth = depth
context.options.autoPageBreakHeadingMaxDepth = maxDepth
return VoidValue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class FullPipelineTest {
.theme {darko} layout:{minimal}
.pageformat {A3} orientation:{landscape} margin:{3cm 2px}
.slides transition:{zoom} speed:{fast}
.autopagebreak depth:{3}
.autopagebreak maxdepth:{3}
""".trimIndent(),
) {
assertEquals("My Quarkdown document", documentInfo.name)
Expand Down Expand Up @@ -212,6 +212,52 @@ class FullPipelineTest {
}
}

@Test
fun headings() {
execute("# Title") {
assertEquals("<div class=\"page-break\" data-hidden=\"\"></div><h1>Title</h1>", it)
}

execute("## Ti*tl*e") {
assertEquals("<h2>Ti<em>tl</em>e</h2>", it)
}

execute("#### .sum {3} {2}") {
assertEquals("<h4>5</h4>", it)
}

execute("###### .text size:{tiny} content:{Hello, **world**}") {
assertEquals("<h6><span class=\"size-tiny\">Hello, <strong>world</strong></span></h6>", it)
}

execute(
"""
.autopagebreak maxdepth:{4}
## A
### B
##### C
""".trimIndent(),
) {
assertEquals(
"<div class=\"page-break\" data-hidden=\"\"></div>" +
"<h2>A</h2>" +
"<div class=\"page-break\" data-hidden=\"\"></div>" +
"<h3>B</h3>" +
"<h5>C</h5>",
it,
)
}

execute(
"""
.noautopagebreak
# A
""".trimIndent(),
) {
assertEquals("<h1>A</h1>", it)
}
}

@Test
fun links() {
execute("This is a link: [link](https://example.com 'title')") {
Expand Down

0 comments on commit ac196d1

Please sign in to comment.