PageRenderTime 206ms CodeModel.GetById 185ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/scalate-jruby/src/main/scala/org/fusesource/scalate/jruby/Sass.scala

http://github.com/scalate/scalate
Scala | 69 lines | 53 code | 10 blank | 6 comment | 4 complexity | e6bb891b8ba28f0aaf294478bbd0494a MD5 | raw file
 1package org.fusesource.scalate.jruby
 2
 3import org.fusesource.scalate.util.Log
 4import org.fusesource.scalate.{ TemplateException, RenderContext, TemplateEngine, TemplateEngineAddOn }
 5import org.fusesource.scalate.filter.{ NoLayoutFilter, CssFilter, Pipeline, Filter }
 6
 7/**
 8 * <p>
 9 * </p>
10 *
11 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
12 */
13object Sass extends TemplateEngineAddOn with Log {
14
15  def apply(te: TemplateEngine) = {
16    val jruby = new JRuby
17    if (!te.filters.contains("sass")) {
18      val sass = new Sass(jruby, te)
19      te.filters += "sass" -> Pipeline(List(sass, CssFilter))
20      te.pipelines += "sass" -> List(NoLayoutFilter(sass, "text/css"))
21      te.templateExtensionsFor("css") += "sass"
22    }
23
24    if (!te.filters.contains("scss")) {
25      val scss = new Scss(jruby, te)
26      te.filters += "scss" -> Pipeline(List(scss, CssFilter))
27      te.pipelines += "scss" -> List(NoLayoutFilter(scss, "text/css"))
28      te.templateExtensionsFor("css") += "scss"
29    }
30  }
31}
32
33@deprecated("Since Ruby saaa is no longer maintained, Ruby sass's filter will be removed in the next major version", "1.9.6")
34class Sass(val jruby: JRuby, val engine: TemplateEngine) extends Filter {
35
36  def syntax = "sass"
37
38  def filter(context: RenderContext, content: String) = {
39
40    val result: StringBuffer = new StringBuffer
41    jruby.put("@result", result)
42    jruby.run(
43      "require 'haml-3.0.25/lib/sass'",
44      "options = {}",
45      "options[:load_paths] = [" + context.engine.sourceDirectories.map("'" + _ + "'").mkString(",") + "]",
46      "options[:cache_location] = '" + engine.workingDirectory + "/sass'",
47      "options[:style] = " + (if (engine.isDevelopmentMode) ":expanded" else ":compressed") + "",
48      "options[:line_comments] = " + (if (engine.isDevelopmentMode) "true" else "false") + "",
49      "options[:syntax] = :" + syntax,
50      "content = <<SCALATE_SASS_EOF\n" + content + "\nSCALATE_SASS_EOF",
51      "tree = ::Sass::Engine.new(content, options).to_tree",
52      "@result.append(tree.render)") match {
53        case Right(result) => result.toString
54        case Left((exception, errors)) =>
55          val err1 = """(?m)([a-zA-Z_0-9-]+[.]s[ca]ss:\d+:.+)$""".r
56          val err2 = """([(]s[ca]ss[)]:\d+:.+)$""".r
57          errors match {
58            case err1(msg) => throw new TemplateException(msg, exception)
59            case err2(msg) => throw new TemplateException(msg, exception)
60          }
61          throw new TemplateException(errors, exception)
62      }
63  }
64
65}
66
67class Scss(jruby: JRuby, engine: TemplateEngine) extends Sass(jruby, engine) {
68  override def syntax = "scss"
69}