PageRenderTime 16ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/scalate-core/src/main/scala/org/fusesource/scalate/filter/CssFilter.scala

http://github.com/scalate/scalate
Scala | 69 lines | 21 code | 7 blank | 41 comment | 2 complexity | 69d64e53a686926935a7fce5aeb1ba50 MD5 | raw file
  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 org.fusesource.scalate.filter
  19. /**
  20. * Copyright (C) 2009-2010 the original author or authors.
  21. * See the notice.md file distributed with this work for additional
  22. * information regarding copyright ownership.
  23. *
  24. * Licensed under the Apache License, Version 2.0 (the "License");
  25. * you may not use this file except in compliance with the License.
  26. * You may obtain a copy of the License at
  27. *
  28. * http://www.apache.org/licenses/LICENSE-2.0
  29. *
  30. * Unless required by applicable law or agreed to in writing, software
  31. * distributed under the License is distributed on an "AS IS" BASIS,
  32. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  33. * See the License for the specific language governing permissions and
  34. * limitations under the License.
  35. */
  36. import org.fusesource.scalate._
  37. import org.fusesource.scalate.support.RenderHelper
  38. /**
  39. * Surrounds the filtered text with <style> and CDATA tags.
  40. *
  41. * <p>Useful for including inline CSS.</p>
  42. *
  43. * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
  44. */
  45. object CssFilter extends Filter {
  46. def filter(context: RenderContext, content: String) = {
  47. lazy val css = RenderHelper.indent(" ", content)
  48. if (useCData(context)) {
  49. """<style type='text/css'>
  50. | /* <![CDATA[ */
  51. | """.stripMargin + css + """
  52. | /* ]]> */
  53. |</style>""".stripMargin
  54. } else {
  55. """<style type='text/css'>
  56. | """.stripMargin + css + """
  57. |</style>""".stripMargin
  58. }
  59. }
  60. def useCData(context: RenderContext): Boolean =
  61. context.wrapCssInCData
  62. }