-
Notifications
You must be signed in to change notification settings - Fork 103
/
SiteGenerator.scala
174 lines (144 loc) · 5.91 KB
/
SiteGenerator.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Copyright (C) 2009-2011 the original author or authors.
* See the notice.md file distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fusesource.scalate.support
import java.io.{ File, PrintWriter }
import java.{ util => ju }
import org.fusesource.scalate._
import org.fusesource.scalate.servlet.ServletTemplateEngine
import org.fusesource.scalate.util._
import scala.collection.JavaConverters._
object SiteGenerator extends Log
import org.fusesource.scalate.support.SiteGenerator._
import scala.language.reflectiveCalls
/**
* This class generates static HTML files for your website using the Scalate templates, filters and wiki markups
* you are using.
*
* @author <a href="http://macstrac.blogspot.com">James Strachan</a>
*/
class SiteGenerator {
var workingDirectory: File = _
var webappDirectory: File = _
var targetDirectory: File = _
var templateProperties: ju.Map[String, String] = _
var bootClassName: String = _
var info: { def apply(v1: String): Unit } = (value: String) => println(value)
def execute() = {
targetDirectory.mkdirs()
if (webappDirectory == null || !webappDirectory.exists) {
throw new IllegalArgumentException("The webappDirectory properly is not properly set")
}
info("Generating static website from Scalate Templates and wiki files...")
info("template properties: " + templateProperties)
val engine = new DummyTemplateEngine(webappDirectory)
engine.classLoader = Thread.currentThread.getContextClassLoader
if (bootClassName != null) {
engine.bootClassName = bootClassName
}
if (workingDirectory != null) {
engine.workingDirectory = workingDirectory
workingDirectory.mkdirs()
}
engine.boot
val attributes: Map[String, Any] = if (templateProperties != null) {
templateProperties.asScala.toMap
} else {
Map()
}
def processFile(file: File, baseuri: String): Unit = {
if (file.isDirectory()) {
if (file.getName != "WEB-INF" && !file.getName.startsWith("_")) {
val children = file.listFiles()
if (children != null) {
for (child <- children) {
if (child.isDirectory) {
processFile(child, baseuri + "/" + child.getName)
} else {
processFile(child, baseuri)
}
}
}
}
} else {
val parts = file.getName.split('.')
if (parts.size > 1 && !file.getName.startsWith("_")) {
val uri = baseuri + "/" + file.getName()
// uri = uri.replace(':', '_')
val ext = parts.last
if (engine.extensions.contains(ext)) {
try {
val source = TemplateSource.fromFile(file, uri)
val html = engine.layout(source, attributes)
val sourceFile = new File(targetDirectory, appendHtmlPostfix(uri.stripPrefix("/")))
info(" processing " + file + " with uri: " + uri + " => ")
sourceFile.getParentFile.mkdirs
//IOUtil.writeBinaryFile(sourceFile, transformHtml(html, uri, rootDir).getBytes("UTF-8"))
IOUtil.writeBinaryFile(sourceFile, html.getBytes("UTF-8"))
} catch {
case e: NoValueSetException => info(e.getMessage + ". Ignored template file due to missing attributes: " + file.getCanonicalPath)
case e: VirtualMachineError => throw e
case e: ThreadDeath => throw e
case e: Throwable => throw new Exception(e.getMessage + ". When processing file: " + file.getCanonicalPath, e)
}
} else {
// let's copy the file across if its not a template
debug(" copying " + file + " with uri: " + uri + " extension: " + ext + " not in " + engine.extensions)
val sourceFile = new File(targetDirectory, uri.stripPrefix("/"))
IOUtil.copy(file, sourceFile)
}
}
}
}
processFile(webappDirectory, "")
}
protected var validFileExtensions = Set("js", "css", "rss", "atom", "htm", "xml", "csv", "json")
protected def appendHtmlPostfix(uri: String): String = {
val answer = Files.dropExtension(uri)
val ext = Files.extension(answer)
if (validFileExtensions.contains(ext)) {
answer
} else {
answer + ".html"
}
}
}
class DummyTemplateEngine(
rootDirectory: File) extends TemplateEngine(Some(rootDirectory)) {
override protected def createRenderContext(
uri: String,
out: PrintWriter) = new DummyRenderContext(uri, this, out)
private[this] val responseClassName = "_root_." + classOf[DummyResponse].getName
bindings = List(
Binding("context", "_root_." + classOf[DummyRenderContext].getName, true, isImplicit = true),
Binding("response", responseClassName, defaultValue = Some("new " + responseClassName + "()")))
ServletTemplateEngine.setLayoutStrategy(this)
}
class DummyRenderContext(
val _requestUri: String,
_engine: TemplateEngine,
_out: PrintWriter) extends DefaultRenderContext(_requestUri, _engine, _out) {
// for static website stuff we must zap the root dir typically
override def uri(name: String) = {
// lets deal with links to / as being to /index.html
val link = if (name == "/") "/index.html" else name
Links.convertAbsoluteLinks(link, requestUri)
}
}
class DummyResponse {
def setContentType(value: String): Unit = {}
}