Skip to content

Commit

Permalink
Remove .pagecounter, use standalone .currentpage and .totalpages
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Sep 4, 2024
1 parent a52475f commit 330d686
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package eu.iamgio.quarkdown.ast.quarkdown.inline

import eu.iamgio.quarkdown.ast.Node
import eu.iamgio.quarkdown.visitor.node.NodeVisitor

/**
* A counter for the current or total page number.
* In case the current document type does not support page counting (e.g. plain document),
* a placeholder is used at rendering time.
* @param target whether the counter should display the current or total page number
*/
data class PageCounter(val target: Target) : Node {
enum class Target {
/**
* The current page number.
*/
CURRENT,

/**
* The total amount of pages.
*/
TOTAL,
}

override fun <T> accept(visitor: NodeVisitor<T>): T = visitor.visit(this)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import eu.iamgio.quarkdown.ast.quarkdown.block.SlidesFragment
import eu.iamgio.quarkdown.ast.quarkdown.block.Stacked
import eu.iamgio.quarkdown.ast.quarkdown.block.TableOfContentsView
import eu.iamgio.quarkdown.ast.quarkdown.inline.MathSpan
import eu.iamgio.quarkdown.ast.quarkdown.inline.PageCounter
import eu.iamgio.quarkdown.ast.quarkdown.inline.TextTransform
import eu.iamgio.quarkdown.ast.quarkdown.inline.Whitespace
import eu.iamgio.quarkdown.ast.quarkdown.invisible.PageCounterInitializer
import eu.iamgio.quarkdown.ast.quarkdown.invisible.PageMarginContentInitializer
import eu.iamgio.quarkdown.ast.quarkdown.invisible.SlidesConfigurationInitializer
import eu.iamgio.quarkdown.context.Context
Expand Down Expand Up @@ -261,7 +261,7 @@ open class BaseHtmlNodeRenderer(context: Context) : TagNodeRenderer<HtmlTagBuild

override fun visit(node: PageMarginContentInitializer): CharSequence = throw UnsupportedRenderException(node)

override fun visit(node: PageCounterInitializer): CharSequence = throw UnsupportedRenderException(node)
override fun visit(node: PageCounter): CharSequence = throw UnsupportedRenderException(node)

override fun visit(node: SlidesConfigurationInitializer): CharSequence = throw UnsupportedRenderException(node)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import eu.iamgio.quarkdown.ast.quarkdown.block.SlidesFragment
import eu.iamgio.quarkdown.ast.quarkdown.block.Stacked
import eu.iamgio.quarkdown.ast.quarkdown.block.TableOfContentsView
import eu.iamgio.quarkdown.ast.quarkdown.inline.MathSpan
import eu.iamgio.quarkdown.ast.quarkdown.inline.PageCounter
import eu.iamgio.quarkdown.ast.quarkdown.inline.TextTransform
import eu.iamgio.quarkdown.ast.quarkdown.inline.Whitespace
import eu.iamgio.quarkdown.ast.quarkdown.invisible.PageCounterInitializer
import eu.iamgio.quarkdown.ast.quarkdown.invisible.PageMarginContentInitializer
import eu.iamgio.quarkdown.ast.quarkdown.invisible.SlidesConfigurationInitializer
import eu.iamgio.quarkdown.context.Context
Expand Down Expand Up @@ -220,23 +220,19 @@ class QuarkdownHtmlNodeRenderer(context: Context) : BaseHtmlNodeRenderer(context
// In slides and paged documents, these elements are copied to each page through the slides.js or paged.js script.
div("page-margin-content page-margin-${node.position.asCSS}", node.children)

override fun visit(node: PageCounterInitializer) =
visit(
PageMarginContentInitializer(
children =
node.content(
// The current page number.
tagBuilder("span")
.`class`("current-page-number")
.build(),
// The total amount of pages.
tagBuilder("span")
.`class`("total-page-number")
.build(),
),
position = node.position,
),
)
override fun visit(node: PageCounter) =
// The current or total page number.
// The actual number is filled by a script at runtime
// (either slides.js or paged.js, depending on the document type).
buildTag("span") {
+"-" // The default placeholder in case it is not filled by a script (e.g. plain documents).
`class`(
when (node.target) {
PageCounter.Target.CURRENT -> "current-page-number"
PageCounter.Target.TOTAL -> "total-page-number"
},
)
}

override fun visit(node: SlidesConfigurationInitializer): CharSequence =
buildTag("script") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import eu.iamgio.quarkdown.ast.quarkdown.block.SlidesFragment
import eu.iamgio.quarkdown.ast.quarkdown.block.Stacked
import eu.iamgio.quarkdown.ast.quarkdown.block.TableOfContentsView
import eu.iamgio.quarkdown.ast.quarkdown.inline.MathSpan
import eu.iamgio.quarkdown.ast.quarkdown.inline.PageCounter
import eu.iamgio.quarkdown.ast.quarkdown.inline.TextTransform
import eu.iamgio.quarkdown.ast.quarkdown.inline.Whitespace
import eu.iamgio.quarkdown.ast.quarkdown.invisible.PageCounterInitializer
import eu.iamgio.quarkdown.ast.quarkdown.invisible.PageMarginContentInitializer
import eu.iamgio.quarkdown.ast.quarkdown.invisible.SlidesConfigurationInitializer

Expand Down Expand Up @@ -140,13 +140,13 @@ interface NodeVisitor<T> {

fun visit(node: TextTransform): T

fun visit(node: PageCounter): T

fun visit(node: SlidesFragment): T

// Quarkdown invisible nodes

fun visit(node: PageMarginContentInitializer): T

fun visit(node: PageCounterInitializer): T

fun visit(node: SlidesConfigurationInitializer): T
}
47 changes: 17 additions & 30 deletions stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Document.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package eu.iamgio.quarkdown.stdlib
import eu.iamgio.quarkdown.ast.InlineMarkdownContent
import eu.iamgio.quarkdown.ast.MarkdownContent
import eu.iamgio.quarkdown.ast.base.block.Heading
import eu.iamgio.quarkdown.ast.base.inline.Text
import eu.iamgio.quarkdown.ast.quarkdown.block.TableOfContentsView
import eu.iamgio.quarkdown.ast.quarkdown.invisible.PageCounterInitializer
import eu.iamgio.quarkdown.ast.quarkdown.inline.PageCounter
import eu.iamgio.quarkdown.ast.quarkdown.invisible.PageMarginContentInitializer
import eu.iamgio.quarkdown.context.Context
import eu.iamgio.quarkdown.context.MutableContext
Expand All @@ -25,7 +24,6 @@ import eu.iamgio.quarkdown.function.value.NodeValue
import eu.iamgio.quarkdown.function.value.OutputValue
import eu.iamgio.quarkdown.function.value.StringValue
import eu.iamgio.quarkdown.function.value.VoidValue
import eu.iamgio.quarkdown.function.value.data.Lambda
import eu.iamgio.quarkdown.function.value.wrappedAsValue
import eu.iamgio.quarkdown.pipeline.error.IOPipelineException

Expand All @@ -44,7 +42,8 @@ val Document: Module =
::pageFormat,
::pageMarginContent,
::footer,
::pageCounter,
::currentPage,
::totalPages,
::autoPageBreak,
::disableAutoPageBreak,
::marker,
Expand Down Expand Up @@ -242,34 +241,22 @@ fun footer(content: MarkdownContent): NodeValue =
)

/**
* Sets the global page counter for a paged document.
* @param position position of the counter within the page
* @param text action that returns the text of the counter.
* Accepts two arguments: index of the current page and total amount of pages.
* Markdown content is not supported.
* @return a wrapped [PageCounterInitializer] node
* Displays the index (beginning from 1) of the page this element lies in.
* In case the current document type does not support page counting (e.g. plain document),
* a placeholder is used.
* @return a [PageCounter] node
*/
@Name("pagecounter")
fun pageCounter(
@Injected context: Context,
position: PageMarginPosition = PageMarginPosition.BOTTOM_CENTER,
text: Lambda =
Lambda(context) { (current, total), _ ->
"$current / $total".wrappedAsValue()
},
): NodeValue =
PageCounterInitializer(
content = { current, total ->
val textValue =
text.invoke<String, StringValue>(
StringValue(current),
StringValue(total),
).unwrappedValue
@Name("currentpage")
fun currentPage() = PageCounter(PageCounter.Target.CURRENT).wrappedAsValue()

listOf(Text(textValue))
},
position,
).wrappedAsValue()
/**
* Displays the total amount of pages in the document.
* In case the current document type does not support page counting (e.g. plain document),
* a placeholder is used.
* @return a [PageCounter] node
*/
@Name("totalpages")
fun totalPages() = PageCounter(PageCounter.Target.TOTAL).wrappedAsValue()

/**
* Sets a new automatic page break threshold when a heading is found:
Expand Down

0 comments on commit 330d686

Please sign in to comment.