From d3a6ab658a8ffd960589a43d21962f4058c40d02 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 26 Jun 2022 12:32:22 +0300 Subject: [PATCH] Add ref resolution to basic page features --- data/home/content/consulting/index.md | 10 ++++----- .../space/kscience/snark/SnarkPlugin.kt | 2 +- .../kscience/snark/TextTransformation.kt | 22 ++++++++++++++++--- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/data/home/content/consulting/index.md b/data/home/content/consulting/index.md index 3ffef2c..447c5f2 100644 --- a/data/home/content/consulting/index.md +++ b/data/home/content/consulting/index.md @@ -1,7 +1,7 @@ --- type: page title: Consulting -transformation: snark.replaceLinks +transformation: snark.basic language: en --- @@ -21,7 +21,7 @@ Our research, which primarily includes open source projects, has already been ap
-

Research

+

Research

Analytics, prototyping and research for fundamental science and industry.

@@ -30,7 +30,7 @@ Our research, which primarily includes open source projects, has already been ap
-

Expert activity

+

Expert activity

Analysis and improvement of existing scientific software and projects.

@@ -39,7 +39,7 @@ Our research, which primarily includes open source projects, has already been ap
-

Architecture

+

Architecture

Design the architecture for scientific and technological applications.

@@ -48,7 +48,7 @@ Our research, which primarily includes open source projects, has already been ap
-

Development

+

Development

The development and support of open-source and in-house solutions( libraries, frameworks and end-user application).

diff --git a/src/main/kotlin/space/kscience/snark/SnarkPlugin.kt b/src/main/kotlin/space/kscience/snark/SnarkPlugin.kt index 42d8f57..547b2ad 100644 --- a/src/main/kotlin/space/kscience/snark/SnarkPlugin.kt +++ b/src/main/kotlin/space/kscience/snark/SnarkPlugin.kt @@ -126,7 +126,7 @@ class SnarkPlugin : AbstractPlugin() { "gif".asName() to SnarkParser(ImageIOReader, "gif"), ) TextTransformation.TYPE -> mapOf( - "replaceLinks".asName() to TextTransformation.replaceLinks + "basic".asName() to BasicTextTransformation ) else -> super.content(target) } diff --git a/src/main/kotlin/space/kscience/snark/TextTransformation.kt b/src/main/kotlin/space/kscience/snark/TextTransformation.kt index 2438d18..09f453c 100644 --- a/src/main/kotlin/space/kscience/snark/TextTransformation.kt +++ b/src/main/kotlin/space/kscience/snark/TextTransformation.kt @@ -10,10 +10,26 @@ fun interface TextTransformation { companion object { const val TYPE = "snark.textTransformation" val TEXT_TRANSFORMATION_KEY = NameToken("transformation") + } +} - val replaceLinks: TextTransformation = object : TextTransformation { - context(PageBuilder) override fun transform(text: String): String { - return text.replace("\${homeRef}", homeRef) +object BasicTextTransformation : TextTransformation { + + private val regex = "\\\$\\{(\\w*)(?>\\(\"(.*)\"\\))?\\}".toRegex() + + context(PageBuilder) override fun transform(text: String): String { + return text.replace(regex) { match -> + when (match.groups[1]!!.value) { + "homeRef" -> homeRef + "resolveRef" -> { + val refString = match.groups[2]?.value ?: error("resolveRef requires a string (quoted) argument") + resolveRef(refString) + } + "resolvePageRef" -> { + val refString = match.groups[2]?.value ?: error("resolvePageRef requires a string (quoted) argument") + resolvePageRef(refString) + } + else -> match.value } } }