PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-website/ext/scalate/Boot.scala

http://github.com/scalate/scalate
Scala | 54 lines | 41 code | 11 blank | 2 comment | 2 complexity | 9b93a12c2b9f938fdb506b45df9fe9f7 MD5 | raw file
 1package scalate
 2
 3import java.util.concurrent.atomic.AtomicBoolean
 4import _root_.Website._
 5import org.fusesource.scalamd.{MacroDefinition, Markdown}
 6import java.util.regex.Matcher
 7import org.fusesource.scalate._
 8import util.Log
 9import wikitext._
10
11object Boot extends Log; import Boot._
12
13class Boot(engine: TemplateEngine) {
14
15  private[this] var _initialised = new AtomicBoolean(false)
16
17  def run: Unit = {
18    if (_initialised.compareAndSet(false, true)) {
19
20      def filter(m:Matcher):String = {
21        val filter_name = m.group(1)
22        val body = m.group(2)
23        engine.filter(filter_name) match {
24          case Some(filter)=>
25            filter.filter(RenderContext(), body)
26          case None=> "<div class=\"macro error\"><p>filter not found: %s</p><pre>%s</pre></div>".format(filter_name, body)
27        }
28      }
29
30      def pygmentize(m:Matcher):String = Pygmentize.pygmentize(m.group(2), m.group(1))
31
32      // add some macros to markdown.
33      Markdown.macros :::= List(
34        MacroDefinition("""\{filter::(.*?)\}(.*?)\{filter\}""", "s", filter, true),
35        MacroDefinition("""\{pygmentize::(.*?)\}(.*?)\{pygmentize\}""", "s", pygmentize, true),
36        MacroDefinition("""\{pygmentize\_and\_compare::(.*?)\}(.*?)\{pygmentize\_and\_compare\}""", "s", pygmentize, true),
37        MacroDefinition("""\$\{project_version\}""", "", _ => project_version.toString, true),
38        MacroDefinition("""\$\{project_name\}""", "", _ => project_name.toString, true),
39        MacroDefinition("""\$\{project_id\}""", "", _ => project_id.toString, true),
40        MacroDefinition("""\$\{scala_compat_tag\}""", "", _ => scala_compat_tag, true)
41      )
42
43      for( ssp <- engine.filter("ssp"); md <- engine.filter("markdown") ) {
44        engine.pipelines += "ssp.md"-> List(ssp, md)
45        engine.pipelines += "ssp.markdown"-> List(ssp, md)
46      }
47
48      // lets add the confluence macros...
49      ConfluenceLanguageExtensions.extensions ++= List(ExpressionTag("project_version", () => project_version))
50
51      info("Bootstrapped website gen for: %s", project_name)
52    }
53  }
54}