Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run resolvers for substitution references in all phases #674

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/shared/src/main/scala/laika/ast/resolvers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ case class MarkupContextReference(

def withOptions(options: Options): MarkupContextReference = copy(options = options)

def runsIn(phase: RewritePhase): Boolean =
phase.isInstanceOf[RewritePhase.Render] // TODO - test earlier phases
def runsIn(phase: RewritePhase): Boolean = true

lazy val unresolvedMessage: String =
s"Unresolved markup context reference with key '${ref.toString}'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,22 @@ private[laika] object RecursiveResolverRules {
phase: RewritePhase
): RewriteRules = {

val removeScopes = phase.isInstanceOf[RewritePhase.Render]

def rulesForScope(scope: ElementScope[_]): RewriteRules =
applyTo(cursor.withReferenceContext(scope.context), baseRules, phase)

lazy val rules: RewriteRules = RewriteRules.forBlocks {
case ph: BlockResolver if ph.runsIn(phase) => Replace(rules.rewriteBlock(ph.resolve(cursor)))
case scope: BlockScope if removeScopes =>
Replace(rulesForScope(scope).rewriteBlock(scope.content))
case scope: BlockScope =>
Replace(scope.copy(content = rulesForScope(scope).rewriteBlock(scope.content)))
Replace(rulesForScope(scope).rewriteBlock(scope.content))
} ++ RewriteRules.forSpans {
case ph: SpanResolver if ph.runsIn(phase) => Replace(rules.rewriteSpan(ph.resolve(cursor)))
case scope: SpanScope if removeScopes =>
Replace(rulesForScope(scope).rewriteSpan(scope.content))
case scope: SpanScope =>
Replace(scope.copy(content = rulesForScope(scope).rewriteSpan(scope.content)))
Replace(rulesForScope(scope).rewriteSpan(scope.content))
} ++ RewriteRules.forTemplates {
case ph: SpanResolver if ph.runsIn(phase) =>
Replace(rules.rewriteTemplateSpan(asTemplateSpan(ph.resolve(cursor))))
case scope: TemplateScope if removeScopes =>
Replace(rulesForScope(scope).rewriteTemplateSpan(scope.content))
case scope: TemplateScope =>
Replace(scope.copy(content = rulesForScope(scope).rewriteTemplateSpan(scope.content)))
Replace(rulesForScope(scope).rewriteTemplateSpan(scope.content))
} ++ baseRules

rules
Expand Down
14 changes: 8 additions & 6 deletions io/src/test/scala/laika/directive/std/IncludeDirectiveSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import laika.format.{ HTML, Markdown }
import laika.io.api.TreeParser
import laika.io.helper.InputBuilder
import laika.io.internal.errors.ConfigException
import laika.io.syntax._
import laika.io.syntax.*
import laika.theme.Theme
import munit.CatsEffectSuite

Expand Down Expand Up @@ -84,7 +84,7 @@ class IncludeDirectiveSpec extends CatsEffectSuite with InputBuilder {
),
Paragraph("ccc")
)
Seq(BlockSequence(composedBlocks))
composedBlocks
}

def parseAndExtract(input: String, template: Option[String] = None): IO[Seq[Block]] = {
Expand Down Expand Up @@ -112,19 +112,19 @@ class IncludeDirectiveSpec extends CatsEffectSuite with InputBuilder {

test("block include without attributes") {
val markup = "@:include(../inc/inc-1.md)"
parseAndExtract(markup).assertEquals(Seq(BlockSequence(Paragraph("aaa () bbb"))))
parseAndExtract(markup).assertEquals(Seq(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")))
Seq(Title("Header").withId("header").withStyle("title"), 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"))))
parseAndExtract(markup).assertEquals(Seq(Paragraph("aaa (foo) bbb")))
}

test("block embed without attributes") {
Expand All @@ -151,7 +151,9 @@ class IncludeDirectiveSpec extends CatsEffectSuite with InputBuilder {
|@:@
""".stripMargin
parseAndExtract(markup).assertEquals(
embedResult(Seq(Header(1, "Header").withId("header"), Paragraph("aaa () bbb")))
embedResult(
Seq(Title("Header").withId("header").withStyle("title"), Paragraph("aaa () bbb"))
)
)
}

Expand Down
Loading