/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

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