Debian Codegraph @

jansi-website/ext/scalate/Boot.scala

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