Debian Codegraph @

hawtjni-website/ext/scalate/Boot.scala

raw
1 /**
2 * Copyright (C) 2009-2011 the original author or authors.
3 * See the notice.md file distributed with this work for additional
4 * information regarding copyright ownership.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package scalate
19
20 import org.fusesource.scalate.util.Logging
21 import java.util.concurrent.atomic.AtomicBoolean
22 import _root_.Website._
23 import org.fusesource.scalate._
24 import org.fusesource.scalamd.{MacroDefinition, Markdown}
25 import java.util.regex.Matcher
26 import org.fusesource.scalate.wikitext.Pygmentize
27
28 class Boot(engine: TemplateEngine) extends Logging {
29
30 private var _initialised = new AtomicBoolean(false)
31
32 def run: Unit = {
33 if (_initialised.compareAndSet(false, true)) {
34
35 def filter(m:Matcher):String = {
36 val filter_name = m.group(1)
37 val body = m.group(2)
38 engine.filter(filter_name) match {
39 case Some(filter)=>
40 filter.filter(RenderContext(), body)
41 case None=> "<div class=\"macro error\"><p>filter not found: %s</p><pre>%s</pre></div>".format(filter_name, body)
42 }
43 }
44
45 def pygmentize(m:Matcher):String = Pygmentize.pygmentize(m.group(2), m.group(1))
46
47 // add some macros to markdown.
48 Markdown.macros :::= List(
49 MacroDefinition("""\{filter::(.*?)\}(.*?)\{filter\}""", "s", filter, true),
50 MacroDefinition("""\{pygmentize::(.*?)\}(.*?)\{pygmentize\}""", "s", pygmentize, true),
51 MacroDefinition("""\{pygmentize\_and\_compare::(.*?)\}(.*?)\{pygmentize\_and\_compare\}""", "s", pygmentize, true),
52 MacroDefinition("""\$\{project_version\}""", "", _ => project_version.toString, true),
53 MacroDefinition("""\$\{project_name\}""", "", _ => project_name.toString, true),
54 MacroDefinition("""\$\{project_id\}""", "", _ => project_id.toString, true),
55 MacroDefinition("""\$\{project_issue_url\}""", "", _ => project_issue_url.toString, true),
56 MacroDefinition("""\$\{website_base_url\}""", "", _ => website_base_url.toString, true)
57 )
58
59 for( ssp <- engine.filter("ssp"); md <- engine.filter("markdown") ) {
60 engine.pipelines += "ssp.md"-> List(ssp, md)
61 engine.pipelines += "ssp.markdown"-> List(ssp, md)
62 }
63 info("Bootstrapped website gen for: %s".format(project_name))
64 }
65 }
66 }