Skip to content

Commit 245c654

Browse files
committed
Remove global cache for templates to allow changes
1 parent c60550a commit 245c654

File tree

17 files changed

+50
-28
lines changed

17 files changed

+50
-28
lines changed

core/src/main/scala/com/lightbend/paradox/ParadoxProcessor.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.lightbend.paradox
1818

1919
import com.lightbend.paradox.markdown.{ Breadcrumbs, Page, Path, Reader, TableOfContents, Writer, Frontin, PropertyUrl, Url }
20-
import com.lightbend.paradox.template.{ CachedTemplates, PageTemplate }
20+
import com.lightbend.paradox.template.PageTemplate
2121
import com.lightbend.paradox.tree.Tree.{ Forest, Location }
2222
import java.io.File
2323
import org.pegdown.ast.{ ActiveLinkNode, ExpLinkNode, RootNode }
@@ -40,7 +40,7 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
4040
targetSuffix: String,
4141
properties: Map[String, String],
4242
navigationDepth: Int,
43-
themeDir: File,
43+
pageTemplate: PageTemplate,
4444
errorListener: STErrorListener): Seq[(File, String)] = {
4545
val pages = parsePages(mappings, Path.replaceSuffix(sourceSuffix, targetSuffix))
4646
val paths = Page.allPaths(pages).toSet
@@ -57,9 +57,8 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
5757
val headerToc = new TableOfContents(pages = false, headers = true, ordered = false, maxDepth = navigationDepth)
5858
val pageContext = PageContents(leadingBreadcrumbs, loc, writer, writerContext, pageToc, headerToc)
5959
val outputFile = new File(outputDirectory, page.path)
60-
val template = CachedTemplates(themeDir, page.properties(Page.Properties.DefaultLayoutMdIndicator, PageTemplate.DefaultName))
6160
outputFile.getParentFile.mkdirs
62-
template.write(pageContext, outputFile, errorListener)
61+
pageTemplate.write(page.properties(Page.Properties.DefaultLayoutMdIndicator, pageTemplate.defaultName), pageContext, outputFile, errorListener)
6362
render(loc.next, rendered :+ (outputFile, page.path))
6463
case None => rendered
6564
}

core/src/main/scala/com/lightbend/paradox/template/PageTemplateSystem.scala renamed to core/src/main/scala/com/lightbend/paradox/template/PageTemplate.scala

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,16 @@ import org.stringtemplate.v4.misc.STMessage
2222
import org.stringtemplate.v4.{ STErrorListener, STRawGroupDir, ST, NoIndentWriter }
2323
import collection.concurrent.TrieMap
2424

25-
object CachedTemplates {
26-
def apply(dir: File, templateName: String = PageTemplate.DefaultName): PageTemplate = {
27-
cache.get((dir, templateName)) match {
28-
case Some(t) => t
29-
case _ =>
30-
val newTemplate = new PageTemplate(dir, templateName)
31-
cache((dir, templateName)) = newTemplate
32-
newTemplate
33-
}
34-
}
35-
36-
val cache: TrieMap[(File, String), PageTemplate] = TrieMap()
37-
}
38-
3925
/**
4026
* Page template writer.
4127
*/
42-
class PageTemplate(directory: File, name: String = PageTemplate.DefaultName, startDelimiter: Char = '$', stopDelimiter: Char = '$') {
28+
class PageTemplate(directory: File, val defaultName: String = "page", startDelimiter: Char = '$', stopDelimiter: Char = '$') {
4329
private val templates = new STRawGroupDir(directory.getAbsolutePath, startDelimiter, stopDelimiter)
4430

4531
/**
4632
* Write a templated page to the target file.
4733
*/
48-
def write(contents: PageTemplate.Contents, target: File, errorListener: STErrorListener): File = {
34+
def write(name: String, contents: PageTemplate.Contents, target: File, errorListener: STErrorListener): File = {
4935
import scala.collection.JavaConverters._
5036

5137
val template = Option(templates.getInstanceOf(name)) match {
@@ -64,8 +50,6 @@ class PageTemplate(directory: File, name: String = PageTemplate.DefaultName, sta
6450
}
6551

6652
object PageTemplate {
67-
val DefaultName = "page"
68-
6953
/**
7054
* All page information to give to the template.
7155
*/

core/src/test/scala/com/lightbend/paradox/markdown/MarkdownBaseSpec.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.lightbend.paradox.markdown
1919
import com.lightbend.paradox.tree.Tree.{ Forest, Location }
2020
import java.io.{ File, PrintWriter }
2121
import org.scalatest.{ FlatSpec, Matchers }
22-
import com.lightbend.paradox.template.{ CachedTemplates, PageTemplate }
22+
import com.lightbend.paradox.template.PageTemplate
2323
import java.nio.file._
2424

2525
abstract class MarkdownBaseSpec extends FlatSpec with Matchers {
@@ -51,8 +51,8 @@ abstract class MarkdownBaseSpec extends FlatSpec with Matchers {
5151
val html = normalize(markdownWriter.write(page.markdown, context(loc)))
5252
val outputFile = new File(page.path)
5353
val emptyPageContext = PartialPageContent(page.properties.get, html)
54-
val template = CachedTemplates(new File(templateDirectory.toString), page.properties(Page.Properties.DefaultLayoutMdIndicator, PageTemplate.DefaultName))
55-
template.write(emptyPageContext, outputFile, new PageTemplate.ErrorLogger(s => println("[error] " + s)))
54+
val template = new PageTemplate(new File(templateDirectory.toString))
55+
template.write(page.properties(Page.Properties.DefaultLayoutMdIndicator, template.defaultName), emptyPageContext, outputFile, new PageTemplate.ErrorLogger(s => println("[error] " + s)))
5656
val fileContent = fileToContent(outputFile)
5757
outputFile.delete
5858
render(loc.next, rendered :+ (page.path, normalize(fileContent)))

plugin/src/main/scala/com/lightbend/paradox/sbt/ParadoxKeys.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ trait ParadoxKeys {
3333
val paradoxTheme = settingKey[Option[ModuleID]]("Web module name of the paradox theme, otherwise local template.")
3434
val paradoxThemeDirectory = taskKey[File]("Sync combined theme and local template to a directory.")
3535
val paradoxOverlayDirectories = settingKey[Seq[File]]("Directory containing common source files for configuration.")
36+
val paradoxDefaultTemplateName = settingKey[String]("Name of default template for generating pages.")
3637
val paradoxTemplate = taskKey[PageTemplate]("PageTemplate to use when generating HTML pages.")
3738
val paradoxVersion = settingKey[String]("Paradox plugin version.")
3839
}

plugin/src/main/scala/com/lightbend/paradox/sbt/ParadoxPlugin.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import sbt._
2020
import sbt.Keys._
2121

2222
import com.lightbend.paradox.ParadoxProcessor
23-
import com.lightbend.paradox.template.{ PageTemplate, CachedTemplates }
23+
import com.lightbend.paradox.template.PageTemplate
2424
import com.typesafe.sbt.web.Import.{ Assets, WebKeys }
2525
import com.typesafe.sbt.web.SbtWeb
2626

@@ -45,6 +45,7 @@ object ParadoxPlugin extends AutoPlugin {
4545
paradoxNavigationDepth := 2,
4646
paradoxProperties := Map.empty,
4747
paradoxTheme := Some(builtinParadoxTheme("generic")),
48+
paradoxDefaultTemplateName := "page",
4849
paradoxLeadingBreadcrumbs := Nil,
4950
libraryDependencies ++= paradoxTheme.value.toSeq
5051
)
@@ -105,7 +106,7 @@ object ParadoxPlugin extends AutoPlugin {
105106
if (!dir.exists) {
106107
IO.createDirectory(dir)
107108
}
108-
CachedTemplates(dir)
109+
new PageTemplate(dir, paradoxDefaultTemplateName.value)
109110
},
110111

111112
sourceDirectory in paradoxTemplate := (target in paradoxTheme).value, // result of combining published theme and local theme template
@@ -127,7 +128,7 @@ object ParadoxPlugin extends AutoPlugin {
127128
paradoxTargetSuffix.value,
128129
paradoxProperties.value,
129130
paradoxNavigationDepth.value,
130-
paradoxThemeDirectory.value,
131+
paradoxTemplate.value,
131132
new PageTemplate.ErrorLogger(s => streams.value.log.error(s))
132133
)
133134
},
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
lazy val templateTest = project
2+
.in(file("."))
3+
.enablePlugins(ParadoxPlugin)
4+
.settings(
5+
// set the generic theme to test overriding page.st
6+
paradoxTheme := Some(builtinParadoxTheme("generic"))
7+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Modified alternative template
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Modified template
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Override generic template
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Modified template
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Alternative template
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Modified alternative template
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % sys.props("project.version"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Alternative template
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Override generic template
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Test page
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# check that the page.st from generic theme has been overridden
2+
> paradox
3+
$ must-mirror target/paradox/site/main/page.html expected/page1.html
4+
5+
# make a change to page.st and check that the template has been updated
6+
$ copy-file changes/page2.st src/main/paradox/_template/page.st
7+
$ sleep 1000
8+
> paradox
9+
$ must-mirror target/paradox/site/main/page.html expected/page2.html
10+
11+
# switch to a different default template
12+
> 'set paradoxDefaultTemplateName := "alt"'
13+
> paradox
14+
$ must-mirror target/paradox/site/main/page.html expected/page3.html
15+
16+
# make a change to alt.st and check that the template has been updated
17+
$ copy-file changes/alt2.st src/main/paradox/_template/alt.st
18+
$ sleep 1000
19+
> paradox
20+
$ must-mirror target/paradox/site/main/page.html expected/page4.html

0 commit comments

Comments
 (0)