Skip to content

Commit

Permalink
Merge pull request #677 from typelevel/fix/remove-excluded-earlier
Browse files Browse the repository at this point in the history
remove documents excluded from output earlier
  • Loading branch information
jenshalm authored Jan 23, 2025
2 parents 3988537 + 07dc736 commit b98d23a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
22 changes: 18 additions & 4 deletions io/src/main/scala/laika/io/internal/runtime/ParserRuntime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import laika.ast.Path.Root
import laika.ast.*
import laika.api.config.Config.IncludeMap
import laika.api.config.{ ConfigBuilder, ConfigParser }
import laika.config.LinkValidation
import laika.config.{ LinkValidation, TargetFormats }
import laika.io.api.TreeParser
import laika.io.internal.config.IncludeHandler
import laika.io.internal.config.IncludeHandler.RequestedInclude
Expand Down Expand Up @@ -122,17 +122,31 @@ private[io] object ParserRuntime {
}.combineAll.toEither.leftMap(es => ParserErrors(es.toList.toSet))

def rewriteTree(root: DocumentTreeRoot): Either[InvalidDocuments, ParsedTree[F]] = {
val parsedTree = ParsedTree(root)

def filterExcluded(tree: DocumentTree): DocumentTree = {
val newContent = tree.content.flatMap {
case tree: DocumentTree => Some(filterExcluded(tree))
case doc: Document if doc.config.get[TargetFormats].exists(_ == TargetFormats.None) =>
None
case doc: Document => Some(doc)
}
tree.replaceContent(newContent)
}

val parsedTree = ParsedTree(root)
.addStaticDocuments(inputs.binaryInputs)
.modifyRoot(_.addStaticDocuments(inputs.providedPaths))
val finalTree = for {
val finalTree = for {
phase1 <- parsedTree.root.rewrite(
op.config.rewriteRulesFor(parsedTree.root, RewritePhase.Build)
)
result <- phase1.rewrite(op.config.rewriteRulesFor(phase1, RewritePhase.Resolve))
} yield result
val filteredTree = finalTree.map { root =>
root.modifyTree(filterExcluded)
}
InvalidDocuments
.from(finalTree, op.config.messageFilters.failOn)
.from(filteredTree, op.config.messageFilters.failOn)
.map(tree => parsedTree.modifyRoot(_ => tree))
}

Expand Down
20 changes: 13 additions & 7 deletions io/src/test/scala/laika/directive/std/IncludeDirectiveSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,42 @@ class IncludeDirectiveSpec extends CatsEffectSuite with InputBuilder {
.withTheme(Theme.empty)
.build

private val excludeFromOutputHOCON =
"""|{%
|laika.targetFormats = []
|%}
|""".stripMargin

def inputs(docUnderTest: String): Seq[(Path, String)] = {
Seq(
Root / "dir1" / "doc-1.md" -> "# Ref",
Root / "dir1" / "doc-2.md" -> "# Ref",
Root / "dir2" / "doc-3.md" -> "# Ref",
Root / "dir2" / "doc-4.md" -> docUnderTest,
Root / "inc" / "inc-1.md" -> "aaa (${?_.key}) bbb",
Root / "inc" / "inc-2.md" ->
Root / "inc" / "inc-2.md" -> (excludeFromOutputHOCON +
"""aaa (${?_.key}) bbb
|
|${?_.embeddedBody}
|${_.embeddedBody}
|
|ccc
""".stripMargin,
""".stripMargin),
Root / "inc" / "header.md" ->
"""# Header
|
|aaa (${?_.key}) bbb
|""".stripMargin,
Root / "inc" / "header-embed.md" ->
Root / "inc" / "header-embed.md" -> (excludeFromOutputHOCON +
"""# Header
|
|aaa (${?_.key}) bbb
|
|${?_.embeddedBody}
|${_.embeddedBody}
|
|ccc
|""".stripMargin,
|""".stripMargin),
Root / "inc" / "inc-1.template.html" -> "aaa (${?_.key}) bbb",
Root / "inc" / "inc-2.template.html" -> """aaa (${?_.key}) bbb <${?_.embeddedBody}> ccc"""
Root / "inc" / "inc-2.template.html" -> """aaa (${?_.key}) bbb <${_.embeddedBody}> ccc"""
)
}

Expand Down

0 comments on commit b98d23a

Please sign in to comment.