@@ -30,37 +30,45 @@ abstract class MarkdownBaseSpec extends FlatSpec with Matchers {
30
30
val markdownReader = new Reader
31
31
val markdownWriter = new Writer
32
32
33
- def markdown (text : String )(implicit context : Location [Page ] => Writer .Context = writerContext): String = {
33
+ def markdown (text : String )(implicit context : Location [Page ] => Writer .Context = writerContext): String =
34
34
markdownPages(" test.md" -> text).getOrElse(" test.html" , " " )
35
- }
36
35
37
- def markdownPages (mappings : (String , String )* )(implicit context : Location [Page ] => Writer .Context = writerContext): Map [String , String ] = {
38
- def render (location : Option [Location [Page ]], rendered : Seq [(String , String )] = Seq .empty): Seq [(String , String )] = location match {
39
- case Some (loc) =>
40
- val page = loc.tree.label
41
- val html = normalize(markdownWriter.write(page.markdown, context(loc)))
42
- render(loc.next, rendered :+ (page.path, html))
43
- case None => rendered
44
- }
36
+ def markdownPages (mappings : (String , String )* )(
37
+ implicit context : Location [Page ] => Writer .Context = writerContext): Map [String , String ] = {
38
+ def render (location : Option [Location [Page ]], rendered : Seq [(String , String )] = Seq .empty): Seq [(String , String )] =
39
+ location match {
40
+ case Some (loc) =>
41
+ val page = loc.tree.label
42
+ val html = normalize(markdownWriter.write(page.markdown, context(loc)))
43
+ render(loc.next, rendered :+ (page.path, html))
44
+ case None => rendered
45
+ }
45
46
render(Location .forest(pages(mappings : _* ))).toMap
46
47
}
47
48
48
- def layoutPages (mappings : (String , String )* )(templates : (String , String )* )(implicit context : Location [Page ] => Writer .Context = writerContext): Map [String , String ] = {
49
+ def layoutPages (mappings : (String , String )* )(templates : (String , String )* )(
50
+ implicit context : Location [Page ] => Writer .Context = writerContext): Map [String , String ] = {
49
51
val templateDirectory = Files .createTempDirectory(" templates" )
50
52
createFileTemplates(templateDirectory, templates)
51
- def render (location : Option [Location [Page ]], rendered : Seq [(String , String )] = Seq .empty): Seq [(String , String )] = location match {
52
- case Some (loc) =>
53
- val page = loc.tree.label
54
- val html = normalize(markdownWriter.write(page.markdown, context(loc)))
55
- val outputFile = new File (page.path)
56
- val emptyPageContext = PartialPageContent (page.properties.get, html)
57
- val template = new PageTemplate (new File (templateDirectory.toString))
58
- template.write(page.properties(Page .Properties .DefaultLayoutMdIndicator , template.defaultName), emptyPageContext, outputFile, new PageTemplate .ErrorLogger (s => println(" [error] " + s)))
59
- val fileContent = fileToContent(outputFile)
60
- outputFile.delete
61
- render(loc.next, rendered :+ (page.path, normalize(fileContent)))
62
- case None => rendered
63
- }
53
+ def render (location : Option [Location [Page ]], rendered : Seq [(String , String )] = Seq .empty): Seq [(String , String )] =
54
+ location match {
55
+ case Some (loc) =>
56
+ val page = loc.tree.label
57
+ val html = normalize(markdownWriter.write(page.markdown, context(loc)))
58
+ val outputFile = new File (page.path)
59
+ val emptyPageContext = PartialPageContent (page.properties.get, html)
60
+ val template = new PageTemplate (new File (templateDirectory.toString))
61
+ template.write(
62
+ page.properties(Page .Properties .DefaultLayoutMdIndicator , template.defaultName),
63
+ emptyPageContext,
64
+ outputFile,
65
+ new PageTemplate .ErrorLogger (s => println(" [error] " + s))
66
+ )
67
+ val fileContent = fileToContent(outputFile)
68
+ outputFile.delete
69
+ render(loc.next, rendered :+ (page.path, normalize(fileContent)))
70
+ case None => rendered
71
+ }
64
72
render(Location .forest(pages(mappings : _* ))).toMap
65
73
}
66
74
@@ -83,40 +91,41 @@ abstract class MarkdownBaseSpec extends FlatSpec with Matchers {
83
91
writerContext(location).copy(properties = properties.toMap)
84
92
}
85
93
86
- def writerContext (location : Location [Page ]): Writer .Context = {
94
+ def writerContext (location : Location [Page ]): Writer .Context =
87
95
Writer .Context (
88
96
location,
89
97
Page .allPaths(List (location.root.tree)).toSet,
98
+ reader = new Reader ,
99
+ writer = new Writer ,
90
100
groups = Map (" Language" -> Seq (" Scala" , " Java" ))
91
101
)
92
- }
93
102
94
- def pages (mappings : (String , String )* ): Forest [Page ] = {
103
+ def pages (mappings : (String , String )* ): Forest [Page ] =
104
+ pagesWithProperties(Map .empty[String , String ], mappings : _* )
105
+
106
+ def pagesWithProperties (properties : Map [String , String ], mappings : (String , String )* ): Forest [Page ] = {
95
107
import com .lightbend .paradox .markdown .Path
96
108
val parsed = mappings map {
97
109
case (path, text) =>
98
110
val frontin = Frontin (prepare(text))
99
111
(new File (path), path, markdownReader.read(frontin.body), frontin.header)
100
112
}
101
- Page .forest(parsed, Path .replaceSuffix(Writer .DefaultSourceSuffix , Writer .DefaultTargetSuffix ))
113
+ Page .forest(parsed, Path .replaceSuffix(Writer .DefaultSourceSuffix , Writer .DefaultTargetSuffix ), properties )
102
114
}
103
115
104
- def html (text : String ): String = {
116
+ def html (text : String ): String =
105
117
normalize(prepare(text))
106
- }
107
118
108
- def htmlPages (mappings : (String , String )* ): Map [String , String ] = {
119
+ def htmlPages (mappings : (String , String )* ): Map [String , String ] =
109
120
(mappings map { case (path, text) => (path, html(text)) }).toMap
110
- }
111
121
112
- def prepare (text : String ): String = {
122
+ def prepare (text : String ): String =
113
123
text.stripMargin.trim
114
- }
115
124
116
125
def normalize (html : String ) = {
117
126
val reader = new java.io.StringReader (html)
118
127
val writer = new java.io.StringWriter
119
- val tidy = new org.w3c.tidy.Tidy
128
+ val tidy = new org.w3c.tidy.Tidy
120
129
tidy.setTabsize(2 )
121
130
tidy.setPrintBodyOnly(true )
122
131
tidy.setTrimEmptyElements(false )
@@ -129,26 +138,26 @@ abstract class MarkdownBaseSpec extends FlatSpec with Matchers {
129
138
case class PartialPageContent (properties : Map [String , String ], content : String ) extends PageTemplate .Contents {
130
139
import scala .collection .JavaConverters ._
131
140
132
- val getTitle = " "
141
+ val getTitle = " "
133
142
val getContent = content
134
143
135
- lazy val getBase = " "
136
- lazy val getHome = new EmptyLink ()
137
- lazy val getPrev = new EmptyLink ()
138
- lazy val getNext = new EmptyLink ()
144
+ lazy val getBase = " "
145
+ lazy val getHome = new EmptyLink ()
146
+ lazy val getPrev = new EmptyLink ()
147
+ lazy val getNext = new EmptyLink ()
139
148
lazy val getBreadcrumbs = " "
140
- lazy val getNavigation = " "
141
- lazy val hasSubheaders = false
142
- lazy val getToc = " "
143
- lazy val getSource_url = " "
149
+ lazy val getNavigation = " "
150
+ lazy val hasSubheaders = false
151
+ lazy val getToc = " "
152
+ lazy val getSource_url = " "
144
153
145
154
lazy val getProperties = properties.asJava
146
155
}
147
156
148
157
case class EmptyLink () extends PageTemplate .Link {
149
- lazy val getHref : String = " "
150
- lazy val getHtml : String = " "
151
- lazy val getTitle : String = " "
158
+ lazy val getHref : String = " "
159
+ lazy val getHtml : String = " "
160
+ lazy val getTitle : String = " "
152
161
lazy val isActive : Boolean = false
153
162
}
154
163
0 commit comments