Skip to content

Commit

Permalink
core: run @:include and @:embed directives in RewritePhase.Build
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshalm committed Dec 30, 2024
1 parent 4909d66 commit 80b3bf3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ lazy val core = crossProject(JSPlatform, JVMPlatform)
"org.typelevel" %%% "cats-core" % versions.catsCore
),
scalacOptions ++= inferOverrideFor2_13(scalaVersion.value),
Test / scalacOptions ~= disableMissingInterpolatorWarning
Test / scalacOptions ~= disableMissingInterpolatorWarning,
mimaBinaryIssueFilters ++= MimaFilters.internalDirectiveAPI
)
.jvmSettings(
libraryDependencies += jTidy
Expand Down
15 changes: 11 additions & 4 deletions core/shared/src/main/scala/laika/api/bundle/directives.scala
Original file line number Diff line number Diff line change
Expand Up @@ -570,16 +570,23 @@ trait DirectiveBuilderContext[E <: Element] {

/** Represents a directive, its name and its (combined) parts.
*/
class Directive private[bundle] (val name: String, part: DirectivePart[E]) {
class Directive private[bundle] (
val name: String,
part: DirectivePart[E],
allowsCursorInBuildPhase: Boolean = false
) {
private[bundle] def apply(context: DirectiveContext): Result[E] = part(context)
def hasBody: Boolean = part.hasBody
def separators: Set[String] = part.separators

def runsIn(phase: RewritePhase): Boolean = phase match {
case RewritePhase.Render(_) => true
case _ => !part.needsCursor
def runsIn(phase: RewritePhase): Boolean = {
phase match {
case RewritePhase.Render(_) => true
case _ => allowsCursorInBuildPhase || !part.needsCursor
}
}

def allowCursorInBuildPhase: Directive = new Directive(name, part, true)
}

/** Represents a separator directive, its name and its (combined) parts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private[laika] object IncludeDirectives {
val content = BlockSequence(doc.content.content)
BlockScope(content, context, source)
}
.toRight(s"Unresolved reference to template '${path.toString}'")
.toRight(s"Unresolved reference to document '${path.toString}'")
}

/** Implementation of the `include` directive for templates.
Expand Down Expand Up @@ -133,7 +133,7 @@ private[laika] object IncludeDirectives {
resolvePath(literalPath, pathKey, cursor.config)
.flatMap(resolveDocumentReference(_, attributes, cursor, source))
}
}
}.allowCursorInBuildPhase

/** Implementation of the `embed` directive for text markup documents.
*/
Expand All @@ -152,6 +152,6 @@ private[laika] object IncludeDirectives {
resolvePath(literalPath, pathKey, cursor.config)
.flatMap(resolveDocumentReference(_, attributes, cursor, source, Some(body)))
}
}
}.allowCursorInBuildPhase

}
46 changes: 42 additions & 4 deletions io/src/test/scala/laika/directive/std/IncludeDirectiveSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,29 @@ class IncludeDirectiveSpec extends CatsEffectSuite with InputBuilder {
|
|ccc
""".stripMargin,
Root / "inc" / "header.md" ->
"""# Header
|
|aaa (${?_.key}) bbb
|""".stripMargin,
Root / "inc" / "header-embed.md" ->
"""# Header
|
|aaa (${?_.key}) bbb
|
|${?_.embeddedBody}
|
|ccc
|""".stripMargin,
Root / "inc" / "inc-1.template.html" -> "aaa (${?_.key}) bbb",
Root / "inc" / "inc-2.template.html" -> """aaa (${?_.key}) bbb <${?_.embeddedBody}> ccc"""
)
}

def embedResult(firstPara: String): Seq[Block] = Seq(
BlockSequence(
Paragraph(firstPara),
def embedResult(firstPara: String): Seq[Block] = embedResult(Seq(Paragraph(firstPara)))

def embedResult(blocks: Seq[Block]): Seq[Block] = {
val composedBlocks = blocks ++ Seq(
Paragraph(
TemplateElement(
BlockSequence(
Expand All @@ -69,7 +84,8 @@ class IncludeDirectiveSpec extends CatsEffectSuite with InputBuilder {
),
Paragraph("ccc")
)
)
Seq(BlockSequence(composedBlocks))
}

def parseAndExtract(input: String, template: Option[String] = None): IO[Seq[Block]] = {
val inputTree = build(inputs(input) ++ template.map((DefaultTemplatePath.forHTML, _)).toSeq)
Expand Down Expand Up @@ -99,6 +115,13 @@ class IncludeDirectiveSpec extends CatsEffectSuite with InputBuilder {
parseAndExtract(markup).assertEquals(Seq(BlockSequence(Paragraph("aaa () bbb"))))
}

test("block include with header") {
val markup = "@:include(../inc/header.md)"
parseAndExtract(markup).assertEquals(
Seq(BlockSequence(Header(1, "Header").withId("header"), Paragraph("aaa () bbb")))
)
}

test("block include with attributes") {
val markup = "@:include(../inc/inc-1.md) { key = foo }"
parseAndExtract(markup).assertEquals(Seq(BlockSequence(Paragraph("aaa (foo) bbb"))))
Expand All @@ -117,6 +140,21 @@ class IncludeDirectiveSpec extends CatsEffectSuite with InputBuilder {
parseAndExtract(markup).assertEquals(embedResult("aaa () bbb"))
}

test("block embed with header") {
val markup =
"""@:embed(../inc/header-embed.md)
|
|Par 1
|
|Par 2
|
|@:@
""".stripMargin
parseAndExtract(markup).assertEquals(
embedResult(Seq(Header(1, "Header").withId("header"), Paragraph("aaa () bbb")))
)
}

test("block embed with attributes") {
val markup =
"""@:embed(../inc/inc-2.md) { key = foo }
Expand Down
13 changes: 12 additions & 1 deletion project/MimaFilters.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import com.typesafe.tools.mima.core.{ MissingClassProblem, ProblemFilter, ProblemFilters }
import com.typesafe.tools.mima.core.{
MissingClassProblem,
ProblemFilter,
ProblemFilters,
ReversedMissingMethodProblem
}

object MimaFilters {

Expand Down Expand Up @@ -27,4 +32,10 @@ object MimaFilters {
ProblemFilters.exclude[MissingClassProblem]("laika.helium.internal.config.InlineJS$")
)

val internalDirectiveAPI: Seq[ProblemFilter] = Seq(
ProblemFilters.exclude[ReversedMissingMethodProblem](
"laika.api.bundle.DirectiveBuilderContext.Directive"
)
)

}

0 comments on commit 80b3bf3

Please sign in to comment.