Skip to content

Commit b03b1ef

Browse files
committed
Fix handling interpolated properties
1 parent c3ac180 commit b03b1ef

File tree

5 files changed

+98
-58
lines changed

5 files changed

+98
-58
lines changed

docs/blog/index.html

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55
<main class="container">
66
<h1>{{ page.title }}</h1>
77

8-
<ul class="post-list">
9-
{% for post in site.posts %}
10-
<li>
11-
<h2>
12-
<a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a>
13-
</h2>
14-
<div class="byline">
15-
<time class="date">
16-
<i class="far fa-calendar-alt"></i>
17-
{{ post.date | date: "%Y-%m-%d" }}
18-
</time>
19-
{% if post.author %}
20-
<span class="author">
21-
<i class="fas fa-pen-nib"></i>
22-
{{ post.author }}
23-
</span>
24-
{% endif %}
25-
</div>
26-
<div class="excerpt">
27-
{{ post.excerpt }}
28-
</div>
29-
</li>
30-
{% endfor %}
31-
</ul>
8+
<ul class="post-list">
9+
{% for post in site.posts %}
10+
<li>
11+
<h2>
12+
<a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a>
13+
</h2>
14+
<div class="byline">
15+
<time class="date">
16+
<i class="far fa-calendar-alt"></i>
17+
{{ post.date | date: "%Y-%m-%d" }}
18+
</time>
19+
{% if post.author %}
20+
<span class="author">
21+
<i class="fas fa-pen-nib"></i>
22+
{{ post.author }}
23+
</span>
24+
{% endif %}
25+
</div>
26+
<div class="excerpt">
27+
{{ post.excerpt }}
28+
</div>
29+
</li>
30+
{% endfor %}
31+
</ul>
3232
</main>

scala3doc/scala3-docs/_layouts/blog-page.html

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@ <h1>{{ page.title }}</h1>
1414
{{ page.author }}
1515
</span>
1616
</div>
17-
{% if page.subTitle %}
18-
<div class="subtitle">
19-
{{ page.subTitle }}
20-
</div>
21-
{% endif %}
22-
</header>
23-
17+
<!-- This unusal HTML formatting is becuase of liquid java bugs -->
18+
{% if page.subTitle %}
19+
<div class="subtitle">
20+
{{ page.subTitle }}
21+
</div>
22+
{% endif %}</header>
23+
24+
2425
{{ content }}
2526

2627
{% if page.author and page.authorImg %}
27-
<hr/>
28-
<footer>
29-
<img id="author-img" src="{{ site.baseurl }}{{ page.authorImg }}">
30-
<span id="author-signature">
31-
{{ page.author }}
32-
</span>
33-
</footer>
28+
<hr/>
29+
<footer>
30+
<img id="author-img" src="{{ baseurl }}{{ page.authorImg }}">
31+
<span id="author-signature">
32+
{{ page.author }}
33+
</span>
34+
</footer>
3435
{% endif %}
3536
</main>

scala3doc/src/dotty/dokka/site/StaticSiteContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class StaticSiteContext(val root: File, sourceSets: Set[SourceSetWrapper]):
111111
val path = if isBlog then "blog" else url.stripSuffix(".html") + ".md"
112112
val file = root.toPath.resolve(path) // Add support for .html files!
113113
val LoadedTemplate(template, children, tFile) = loadTemplate(file.toFile, isBlog).get // Add proper logging if file does not exisits
114-
LoadedTemplate(template.copy(settings = template.settings + ("title" -> List(title))), children, tFile)
114+
LoadedTemplate(template.copy(settings = template.settings + ("title" -> title)), children, tFile)
115115
case Sidebar.Category(title, nested) =>
116116
// Add support for index.html/index.md files!
117117
val fakeFile = new File(root, title)

scala3doc/src/dotty/dokka/site/common.scala

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,30 @@ def loadTemplateFile(file: File): TemplateFile = {
6161
val yamlCollector = new AbstractYamlFrontMatterVisitor()
6262
yamlCollector.visit(configParsed)
6363

64+
extension (v: java.util.List[String]) private def getSettingValue: String | List[String] =
65+
if v.size == 1 then v.get(0) else v.asScala.toList
66+
67+
extension (sc: StringContext) private def r: util.matching.Regex =
68+
new util.matching.Regex(sc.parts.mkString, sc.parts.tail.map(_ => "x"): _*)
69+
70+
extension (map: Map[String, String | List[String]]) private def wrapInContext(fileName: String): Map[String, Element] =
71+
fileName match
72+
case r".*_layouts\/base\.html" => map
73+
case r".*_layouts\/blog-page\.html" => map
74+
case r".*_layouts\/doc-page\.html" => map
75+
case r".*_layouts\/index\.html" => map
76+
case r".*_layouts\/main\.html" => map
77+
case r".*_layouts\/search\.html" => map
78+
case r".*blog\/index\.html" => map
79+
case r".*blog\/_posts\/.*\.md" => Map("page" -> (map - "layout")) + ("layout" -> map("layout"))
80+
case r".*docs\/.*\.md" => Map("page" -> (map - "layout")) + ("layout" -> map("layout"))
81+
case _ => map
82+
6483
TemplateFile(
6584
file = file,
6685
file.getName.endsWith(".html"),
6786
rawCode = content.mkString(LineSeparator),
68-
settings = yamlCollector.getData.asScala.toMap.transform((_, v) => v.asScala.toList)
87+
settings = yamlCollector.getData.asScala.toMap.transform((_, v) => v.getSettingValue).wrapInContext(file.getAbsolutePath)
6988
)
7089
}
7190

scala3doc/src/dotty/dokka/site/templates.scala

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ import com.vladsch.flexmark.parser.{Parser, ParserEmulationProfile}
1515
import com.vladsch.flexmark.util.options.{DataHolder, MutableDataSet}
1616
import com.vladsch.flexmark.html.HtmlRenderer
1717
import liqp.Template
18-
1918
import scala.collection.JavaConverters._
2019
import scala.io.Source
2120

2221
case class RenderingContext(
23-
properties: Map[String, Object],
22+
properties: Map[String, Element],
2423
layouts: Map[String, TemplateFile] = Map(),
2524
resolving: Set[String] = Set(),
2625
resources: List[String] = Nil
@@ -33,8 +32,8 @@ case class RenderingContext(
3332
resources = this.resources ++ resources
3433
)
3534

36-
case class ResolvedPage(val code: String, val resources: List[String] = Nil)
37-
35+
case class ResolvedPage(code: String, resources: List[String] = Nil)
36+
type Element = String | List[String] | Map[String, String | List[String]]
3837
/**
3938
* case class for the template files.
4039
* Template file is a file `.md` or `.html` handling settings.
@@ -44,21 +43,27 @@ case class ResolvedPage(val code: String, val resources: List[String] = Nil)
4443
* @param settings The config defined in the begging of the file, between the pair of `---` (e.g. layout: basic).
4544
*/
4645
case class TemplateFile(
47-
val file: File,
48-
val isHtml: Boolean,
49-
val rawCode: String,
50-
val settings: Map[String, List[String]]
46+
file: File,
47+
isHtml: Boolean,
48+
rawCode: String,
49+
settings: Map[String, Element]
5150
):
5251

5352
private def stringSetting(name: String): Option[String] =
5453
settings.get(name) map {
55-
case List(single) => single.stripPrefix("\"").stripSuffix("\"")
54+
case string: String => string.stripPrefix("\"").stripSuffix("\"")
5655
case nonSingle =>
57-
throw new RuntimeException(s"Setting $name is a not a singlel-ement list but $nonSingle")
56+
throw new RuntimeException(s"Setting $name is a not a single-elment but $nonSingle")
5857
}
5958

6059

61-
private def listSetting(name: String): List[String] = settings.getOrElse(name, Nil)
60+
private def listSetting(name: String): Option[List[String]] = settings.get(name) map {
61+
case map: Map[_, _] =>
62+
throw new RuntimeException(s"Setting $name is a not a list but nested map")
63+
case nonSingle: List[String] => nonSingle
64+
case single =>
65+
throw new RuntimeException(s"Setting $name is a not a list but $single")
66+
}
6267

6368
def name(): String = stringSetting("name").getOrElse(file.getName.stripSuffix(if (isHtml) ".html" else ".md"))
6469

@@ -70,9 +75,7 @@ case class TemplateFile(
7075

7176
def isIndexPage() = file.isFile && (file.getName == "index.md" || file.getName == "index.html")
7277

73-
def resolveToHtml(ctx: StaticSiteContext): ResolvedPage =
74-
val props = Map("page" -> JMap("title" -> title()))
75-
resolveInner(RenderingContext(props, ctx.layouts))
78+
def resolveToHtml(ctx: StaticSiteContext): ResolvedPage = resolveInner(RenderingContext(settings, ctx.layouts))
7679

7780
private[site] def resolveInner(ctx: RenderingContext): ResolvedPage =
7881
if (ctx.resolving.contains(file.getAbsolutePath))
@@ -82,15 +85,32 @@ case class TemplateFile(
8285
ctx.layouts.getOrElse(name, throw new RuntimeException(s"No layouts named $name in ${ctx.layouts}")))
8386

8487
// Library requires mutable maps..
85-
val mutableProperties = new java.util.HashMap[String, Object](ctx.properties.asJava)
88+
val mutableProperties = new java.util.HashMap[String, Object](ctx.properties.map {
89+
case (k, v: Element) =>
90+
val newValue = v match
91+
case m: Map[String, String | List[String]] =>
92+
val newValue = m.map {
93+
case (k, v) =>
94+
val newValue = v match
95+
case l: List[String] => l.asJava
96+
case _: String => v
97+
(k, newValue)
98+
}
99+
new java.util.HashMap[String, Object](newValue.asJava)
100+
case l: List[String] => l.asJava
101+
case _ => v
102+
k -> newValue
103+
}.asJava)
104+
val debug = mutableProperties.clone.asInstanceOf[java.util.HashMap[String, Object]]
105+
debug.remove("content")
106+
println(debug.get("page"))
86107
val rendered = Template.parse(this.rawCode).render(mutableProperties)
87108
// We want to render markdown only if next template is html
88109
val code = if (isHtml || layoutTemplate.exists(!_.isHtml)) rendered else
89110
val parser: Parser = Parser.builder().build()
90111
HtmlRenderer.builder(defaultMarkdownOptions).build().render(parser.parse(rendered))
91112

92-
val resources = listSetting("extraCSS") ++ listSetting("extraJS")
93113
layoutTemplate match
94-
case None => ResolvedPage(code, resources ++ ctx.resources)
114+
case None => ResolvedPage(code, ctx.resources)
95115
case Some(layoutTemplate) =>
96-
layoutTemplate.resolveInner(ctx.nest(code, file, resources))
116+
layoutTemplate.resolveInner(ctx.nest(code, file, List.empty))

0 commit comments

Comments
 (0)