Skip to content

Commit

Permalink
drop laika, generic string=>string transform
Browse files Browse the repository at this point in the history
  • Loading branch information
yurique committed Feb 22, 2023
1 parent 99c0ad1 commit ef28b48
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.4.0

Replace Laika with a generic `String => String` transformations.

## 0.3.1

Removed a bad println.
Expand Down
22 changes: 4 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,12 @@ object EmbeddedFilesIndex {
}
```

## Laika transform (markdown)
## Transforming text

It is possible to configure a basic transformation for text files using [Laika](https://planet42.github.io/Laika).
It is possible to configure a basic transformation for text files using.


```scala
import laika.api.Transformer
import laika.ast.Path.Root
import laika.format.{AST, HTML, Markdown, ReStructuredText, XSLFO}
import laika.markdown.bundle.VerbatimHTML
import laika.markdown.github.GitHubFlavor
import laika.parse.code.SyntaxHighlighting
import laika.rewrite.link.LinkConfig

project
// ...
.settings(
Expand All @@ -193,20 +185,14 @@ project
embedTransform := Seq(
TransformConfig(
when = _.getFileName.toString.endsWith(".md"),
transformer =
Transformer
.from(Markdown)
.to(HTML)
.using(GitHubFlavor, VerbatimHTML, SyntaxHighlighting)
.withConfigValue(LinkConfig(excludeFromValidation = Seq(Root)))
.build
transform = _.toUpperCase
)
),
(Compile / sourceGenerators) += embedFiles
)
```

For each text file, the `.transformer` of the first of the `TransformConfig`-s to match the path (`.when` returns `true`)
For each text file, the `.transform` function of the first of the `TransformConfig`-s to match the path (`.when` returns `true`)
will be applied to the contents of the file.

If none of the configuration matches - the file contents will be used as is.
Expand Down
4 changes: 0 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,4 @@ console / initialCommands := """import com.yurique.embedded.sbt._"""
enablePlugins(ScriptedPlugin)
scriptedLaunchOpts ++=
Seq("-Xmx1024M", "-Dplugin.version=" + version.value)
libraryDependencies ++= Seq(
"org.planet42" %% "laika-core" % "0.19.0", // % Provided,
"org.planet42" %% "laika-core" % "0.19.0" % Test
)
// test := scripted.value
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import sbt.Keys._
import java.nio.file.Files
import java.nio.file.Path
import sbt.nio.Keys._
import laika.api.Transformer

object EmbeddedFilesPlugin extends AutoPlugin {

Expand All @@ -16,7 +15,7 @@ object EmbeddedFilesPlugin extends AutoPlugin {

case class TransformConfig(
when: Path => Boolean,
transformer: Transformer
transform: String => String
)

val embedFiles = taskKey[Seq[File]]("Creates an ExternalFile object with a content field for each file.")
Expand Down Expand Up @@ -209,9 +208,7 @@ object EmbeddedFilesPlugin extends AutoPlugin {
): Unit = {

def doTransform(s: String): String =
transform.find(_.when(input)).fold(s) { transform =>
transform.transformer.transform(s).fold(throw _, identity)
}
transform.find(_.when(input)).fold(s)(_.transform(s))

val fileContent =
doTransform(IO.read(input.toFile))
Expand Down
13 changes: 2 additions & 11 deletions src/sbt-test/sbt-embedded-files/without-index-transform/build.sbt
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import laika.api.Transformer
import laika.ast.Path.Root
import laika.format.{AST, HTML, Markdown, ReStructuredText, XSLFO}
import laika.markdown.bundle.VerbatimHTML
import laika.markdown.github.GitHubFlavor
import laika.parse.code.SyntaxHighlighting
import laika.rewrite.link.LinkConfig


version := "0.1"
scalaVersion := "2.13.6"

Expand All @@ -19,8 +10,8 @@ val root = project
embedDirectories ++= (Compile / unmanagedSourceDirectories).value,
embedTransform := Seq(
TransformConfig(
when = _.getFileName.toString.endsWith(".md"),
transformer = Transformer.from(Markdown).to(HTML).using(GitHubFlavor, VerbatimHTML, SyntaxHighlighting).build
when = _.getFileName.toString.endsWith("test-resource.txt"),
transform = _.toUpperCase
)
),
(Compile / sourceGenerators) += embedFiles
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object test_resource_txt extends __embedded_files.EmbeddedTextFile {

val path: String = """test-resource.txt"""

val content: String = """A file from resources.
val content: String = """A FILE FROM RESOURCES.
"""

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
> compile
$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala expected/__embedded_files/com/yurique/embedded/sbt/txt/test_file_1_txt.scala
$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/test_resource_txt.scala expected/__embedded_files/test_resource_txt.scala
$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/test_resource_md.scala expected/__embedded_files/test_resource_md.scala
$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala expected/__embedded_files/com/yurique/embedded/sbt/binary/test_bin_file_1_bin.scala
$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/test/test_bin_resource_bin.scala expected/__embedded_files/test/test_bin_resource_bin.scala
$ must-mirror target/scala-2.13/src_managed/main/scala/__embedded_files/EmbeddedTextFile.scala expected/__embedded_files/EmbeddedTextFile.scala
Expand Down

0 comments on commit ef28b48

Please sign in to comment.