@@ -15,12 +15,11 @@ import com.vladsch.flexmark.parser.{Parser, ParserEmulationProfile}
15
15
import com .vladsch .flexmark .util .options .{DataHolder , MutableDataSet }
16
16
import com .vladsch .flexmark .html .HtmlRenderer
17
17
import liqp .Template
18
-
19
18
import scala .collection .JavaConverters ._
20
19
import scala .io .Source
21
20
22
21
case class RenderingContext (
23
- properties : Map [String , Object ],
22
+ properties : Map [String , Element ],
24
23
layouts : Map [String , TemplateFile ] = Map (),
25
24
resolving : Set [String ] = Set (),
26
25
resources : List [String ] = Nil
@@ -33,8 +32,8 @@ case class RenderingContext(
33
32
resources = this .resources ++ resources
34
33
)
35
34
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 ]]
38
37
/**
39
38
* case class for the template files.
40
39
* 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)
44
43
* @param settings The config defined in the begging of the file, between the pair of `---` (e.g. layout: basic).
45
44
*/
46
45
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 ]
51
50
):
52
51
53
52
private def stringSetting (name : String ): Option [String ] =
54
53
settings.get(name) map {
55
- case List (single) => single .stripPrefix(" \" " ).stripSuffix(" \" " )
54
+ case string : String => string .stripPrefix(" \" " ).stripSuffix(" \" " )
56
55
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" )
58
57
}
59
58
60
59
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
+ }
62
67
63
68
def name (): String = stringSetting(" name" ).getOrElse(file.getName.stripSuffix(if (isHtml) " .html" else " .md" ))
64
69
@@ -70,9 +75,7 @@ case class TemplateFile(
70
75
71
76
def isIndexPage () = file.isFile && (file.getName == " index.md" || file.getName == " index.html" )
72
77
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))
76
79
77
80
private [site] def resolveInner (ctx : RenderingContext ): ResolvedPage =
78
81
if (ctx.resolving.contains(file.getAbsolutePath))
@@ -82,15 +85,32 @@ case class TemplateFile(
82
85
ctx.layouts.getOrElse(name, throw new RuntimeException (s " No layouts named $name in ${ctx.layouts}" )))
83
86
84
87
// 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" ))
86
107
val rendered = Template .parse(this .rawCode).render(mutableProperties)
87
108
// We want to render markdown only if next template is html
88
109
val code = if (isHtml || layoutTemplate.exists(! _.isHtml)) rendered else
89
110
val parser : Parser = Parser .builder().build()
90
111
HtmlRenderer .builder(defaultMarkdownOptions).build().render(parser.parse(rendered))
91
112
92
- val resources = listSetting(" extraCSS" ) ++ listSetting(" extraJS" )
93
113
layoutTemplate match
94
- case None => ResolvedPage (code, resources ++ ctx.resources)
114
+ case None => ResolvedPage (code, ctx.resources)
95
115
case Some (layoutTemplate) =>
96
- layoutTemplate.resolveInner(ctx.nest(code, file, resources ))
116
+ layoutTemplate.resolveInner(ctx.nest(code, file, List .empty ))
0 commit comments