Skip to content

Commit

Permalink
plugin: add new laikaRenderers setting
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshalm committed Feb 25, 2024
1 parent e029d3b commit 7c6efe1
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 74 deletions.
12 changes: 11 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import laika.format.Markdown.GitHubFlavor
import laika.config.SyntaxHighlighting
import sbt.Keys.crossScalaVersions
import org.scalajs.linker.interface.ESVersion
import com.typesafe.tools.mima.core.{ ProblemFilters, MissingClassProblem }
import Dependencies._

inThisBuild(
Expand Down Expand Up @@ -179,5 +180,14 @@ lazy val plugin = project.in(file("sbt"))
io / publishLocal,
pdf / publishLocal,
preview / publishLocal
).evaluated
).evaluated,
mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[MissingClassProblem]("laika.sbt.Tasks$OutputFormat"),
ProblemFilters.exclude[MissingClassProblem]("laika.sbt.Tasks$OutputFormat$"),
ProblemFilters.exclude[MissingClassProblem]("laika.sbt.Tasks$OutputFormat$AST$"),
ProblemFilters.exclude[MissingClassProblem]("laika.sbt.Tasks$OutputFormat$EPUB$"),
ProblemFilters.exclude[MissingClassProblem]("laika.sbt.Tasks$OutputFormat$HTML$"),
ProblemFilters.exclude[MissingClassProblem]("laika.sbt.Tasks$OutputFormat$PDF$"),
ProblemFilters.exclude[MissingClassProblem]("laika.sbt.Tasks$OutputFormat$XSLFO$")
)
)
3 changes: 3 additions & 0 deletions sbt/src/main/scala/laika/sbt/LaikaPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ object LaikaPlugin extends AutoPlugin {

val laikaConfig = settingKey[LaikaConfig]("Configuration options for all transformations")

val laikaRenderers = settingKey[Seq[RendererConfig]]("Configurations of all included renderers")

val laikaInputs =
settingKey[InputTreeBuilder]("Freely composed input tree, overriding sourceDirectories")

Expand Down Expand Up @@ -154,6 +156,7 @@ object LaikaPlugin extends AutoPlugin {
laikaXSLFO / target := (Laika / target).value / "fo",
laikaAST / target := (Laika / target).value / "ast",
laikaExtensions := Nil,
laikaRenderers := Settings.rendererConfigs.value,
laikaConfig := LaikaConfig.defaults,
laikaPreviewConfig := LaikaPreviewConfig.defaults,
laikaTheme := Helium.defaults.build,
Expand Down
128 changes: 128 additions & 0 deletions sbt/src/main/scala/laika/sbt/RendererConfig.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package laika.sbt

import laika.api.format.{ BinaryPostProcessor, RenderFormat, TwoPhaseRenderFormat }

import java.io.File

/** Base trait for the configuration of renderers to be used with
* the `laikaGenerate` and `laikaSite` tasks.
*/
sealed abstract class RendererConfig private[sbt] {

/** The alias that triggers the execution of this renderer when
* passed to the `laikaGenerate` task in the format `laikaGenerate <alias>`.
*/
def alias: String

/** Indicates whether this renderer should be executed when the `laikaSite` task is run.
* When set to false, the renderer can still get executed by using `laikaGenerate <alias>`.
*/
def includeInSite: Boolean

/** The target directory the file(s) should be written into.
*/
def targetDirectory: File
}

/** Represents the configuration for a text renderer (of type `laika.api.format.RenderFormat`)
* to be used with the `laikaGenerate` and `laikaSite` tasks.
*/
sealed abstract class TextRendererConfig private extends RendererConfig {

/** The render format to be used with this renderer.
*/
def format: RenderFormat[?]
}

/** Represents the configuration for a binary renderer
* (of type `laika.api.format.TwoPhaseRenderFormat`)
* to be used with the `laikaGenerate` and `laikaSite` tasks.
*/
sealed abstract class BinaryRendererConfig private extends RendererConfig {

/** The binary render format to be used with this renderer.
*/
def format: TwoPhaseRenderFormat[?, BinaryPostProcessor.Builder]

/** The base name of the output file.
*
* The full path of the generated file will be
* `<targetDirectory>/<artifactBaseName><classifiers>.<fileSuffix>`
* where `classifiers` will be empty when `supportsSeparations` is `false`
* or the user did not use the `@:select` directive.
*/
def artifactBaseName: String

/** The file suffix (without dot) for the output file.
*/
def fileSuffix: String

/** Indicates whether multiple different versions of the output with different
* classifiers in their name should be written in case the user
* has used the `@:select` directive in the input sources.
*
* For details about this directive, see
* [[https://typelevel.org/Laika/latest/07-reference/01-standard-directives.html#select Select Directive]]
* in the manual.
*/
def supportsSeparations: Boolean
}

object TextRendererConfig {

private final case class Impl(
alias: String,
format: RenderFormat[?],
targetDirectory: File,
includeInSite: Boolean
) extends TextRendererConfig {
override def productPrefix = "TextRendererConfig"
}

def apply(
alias: String,
format: RenderFormat[?],
targetDirectory: File,
includeInSite: Boolean
): TextRendererConfig = Impl(
alias,
format,
targetDirectory,
includeInSite
)

}

object BinaryRendererConfig {

private final case class Impl(
alias: String,
format: TwoPhaseRenderFormat[?, BinaryPostProcessor.Builder],
targetDirectory: File,
artifactBaseName: String,
fileSuffix: String,
includeInSite: Boolean,
supportsSeparations: Boolean
) extends BinaryRendererConfig {
override def productPrefix = "BinaryRendererConfig"
}

def apply(
alias: String,
format: TwoPhaseRenderFormat[?, BinaryPostProcessor.Builder],
targetDirectory: File,
artifactBaseName: String,
fileSuffix: String,
includeInSite: Boolean,
supportsSeparations: Boolean
): BinaryRendererConfig = Impl(
alias,
format,
targetDirectory,
artifactBaseName,
fileSuffix,
includeInSite,
supportsSeparations
)

}
48 changes: 47 additions & 1 deletion sbt/src/main/scala/laika/sbt/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import laika.api.config.{ Config, ConfigBuilder }
import laika.api.format.MarkupFormat
import laika.api.config.Config.ConfigResult
import laika.config.LaikaKeys
import laika.format.{ Markdown, ReStructuredText }
import laika.format.{ AST, EPUB, HTML, Markdown, PDF, ReStructuredText, XSLFO }
import laika.io.api.TreeParser
import laika.io.internal.config.SiteConfig
import laika.io.syntax.*
Expand Down Expand Up @@ -103,6 +103,52 @@ object Settings {
validated(parserConfig.value.baseConfig.get[String](LaikaKeys.artifactBaseName, name.value))
}

val rendererConfigs: Initialize[Seq[RendererConfig]] = setting {

val baseConfig = parserConfig.value.baseConfig
val downloadPath =
(laikaSite / target).value / validated(SiteConfig.downloadPath(baseConfig)).relative.toString

Seq(
TextRendererConfig(
"html",
HTML,
(laikaSite / target).value,
includeInSite = true
),
TextRendererConfig(
"xsl-fo",
XSLFO,
(laikaXSLFO / target).value,
includeInSite = false
),
TextRendererConfig(
"ast",
AST,
(laikaAST / target).value,
includeInSite = false
),
BinaryRendererConfig(
"pdf",
PDF,
downloadPath,
artifactBaseName.value,
"pdf",
includeInSite = laikaIncludePDF.value,
supportsSeparations = true
),
BinaryRendererConfig(
"epub",
EPUB,
downloadPath,
artifactBaseName.value,
"epub",
includeInSite = laikaIncludeEPUB.value,
supportsSeparations = true
)
)
}

val apiTargetDirectory: Initialize[File] = setting {
(laikaSite / target).value / validated(
SiteConfig.apiPath(Settings.parserConfig.value.baseConfig)
Expand Down
Loading

0 comments on commit 7c6efe1

Please sign in to comment.