-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #598 from typelevel/plugin/tree-processor
sbt plugin: add new laikaTreeProcessors setting
- Loading branch information
Showing
13 changed files
with
423 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package laika.sbt | ||
|
||
import cats.data.Kleisli | ||
import cats.effect.IO | ||
import laika.ast.OutputContext | ||
import laika.io.model.ParsedTree | ||
import laika.theme.Theme.TreeProcessor | ||
|
||
/** API shortcuts for adding tree processors to the the `laikaTreeProcessors` setting. | ||
* | ||
* Tree processors are effect-ful functions processing the document AST between parsing and rendering. | ||
* | ||
* One example would be generating a table of content based on inspecting the content of the tree and | ||
* insert it as the first document. You can also modify or delete content. | ||
* | ||
* Processors are usually specific per output format, therefore the API allows to specify the format | ||
* each processor should be applied to. | ||
* | ||
* @author Jens Halm | ||
*/ | ||
trait TreeProcessors { | ||
|
||
class LaikaTreeProcessor(val delegate: OutputContext => TreeProcessor[IO]) | ||
|
||
object LaikaTreeProcessor { | ||
|
||
/** Wraps a function that returns a tree processor depending on the output context passed. | ||
* | ||
* The returned instance can be added to the `laikaTreeProcessors` setting. | ||
*/ | ||
def apply(f: OutputContext => TreeProcessor[IO]): LaikaTreeProcessor = | ||
new LaikaTreeProcessor(f) | ||
|
||
/** Adds a function that processes the document tree between parsing and rendering, | ||
* to be executed only for the specified output format. | ||
* | ||
* The returned instance can be added to the `laikaTreeProcessors` setting. | ||
*/ | ||
def apply(f: TreeProcessor[IO], context: OutputContext): LaikaTreeProcessor = | ||
apply(rendererContext => | ||
if (rendererContext == context) f else Kleisli.ask[IO, ParsedTree[IO]] | ||
) | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import cats.effect.IO | ||
import laika.ast.Path.Root | ||
import laika.ast._ | ||
import laika.format.{ HTML, PDF } | ||
import laika.theme.TreeProcessorBuilder | ||
|
||
name := "site-sourceDirectories" | ||
|
||
version := "0.1" | ||
|
||
scalaVersion := "2.12.6" | ||
|
||
enablePlugins(LaikaPlugin) | ||
|
||
val addedAST = RootElement( | ||
Title("Title 3").withId("title").withStyle("title"), | ||
Paragraph( | ||
Text("Hello "), | ||
Emphasized("World 3"), | ||
Text(".") | ||
) | ||
) | ||
|
||
val removeDoc1 = TreeProcessorBuilder[IO].mapTree { tree => | ||
tree.modifyTree(_.removeContent(_ == Root / "hello1.md")) | ||
} | ||
|
||
val removeDoc2 = TreeProcessorBuilder[IO].mapTree { tree => | ||
tree.modifyTree(_.removeContent(_ == Root / "hello2.md")) | ||
} | ||
|
||
val addDoc3 = TreeProcessorBuilder[IO].mapTree { tree => | ||
tree.modifyTree(_.prependContent(Document(Root / "hello3.md", addedAST))) | ||
} | ||
|
||
laikaTreeProcessors ++= Seq( | ||
LaikaTreeProcessor(removeDoc1, OutputContext(HTML)), | ||
LaikaTreeProcessor(removeDoc2, OutputContext(PDF)), | ||
LaikaTreeProcessor(addDoc3, OutputContext(HTML)) | ||
) |
Oops, something went wrong.