/scalate-core/src/main/scala/org/fusesource/scalate/jade/JadeParser.scala

http://github.com/scalate/scalate · Scala · 64 lines · 32 code · 7 blank · 25 comment · 0 complexity · d7e5381b08e5aa31217bbc9f142ce751 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.jade
  19. import java.io.File
  20. import org.fusesource.scalate.scaml._
  21. import org.fusesource.scalate.util.IOUtil
  22. import scala.util.parsing.input.CharSequenceReader
  23. /**
  24. * <p>
  25. * Parser for a more concise version of haml/scaml inspired by jade:
  26. * http://github.com/visionmedia/jade
  27. * </p>
  28. *
  29. * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
  30. */
  31. class JadeParser extends ScamlParser {
  32. override def full_element_statement: Parser[Element] =
  33. opt(tag_ident) ~ attributes ~ opt(trim) <~ ("/" ~! opt_space ~ nl) ^^ {
  34. case (tag ~ attributes ~ wsc) => Element(tag, attributes, None, List(), wsc, true)
  35. } |
  36. opt(tag_ident) ~ attributes ~ opt(trim) ~ element_text ~ statement_block ^^ {
  37. case ((tag ~ attributes ~ wsc ~ text) ~ body) => Element(tag, attributes, text, body, wsc, false)
  38. }
  39. override def element_statement: Parser[Element] = full_element_statement
  40. override def text_statement = (
  41. prefixed("""\""", literal_text(None)) |
  42. prefixed("&==" ~ opt_space, literal_text(Some(true))) |
  43. prefixed("!==" ~ opt_space, literal_text(Some(false))) |
  44. prefixed("&" ~ space, literal_text(Some(true))) |
  45. prefixed("!" ~ space, literal_text(Some(false))) |
  46. prefixed("|" ~ opt_space, literal_text(None)) |
  47. guarded("<", literal_text(None))) <~ any_space_then_nl
  48. }
  49. object JadeParser {
  50. def main(args: Array[String]) = {
  51. val in = IOUtil.loadTextFile(new File(args(0)))
  52. val p = new JadeParser
  53. println(p.phrase(p.parser)(new CharSequenceReader(in)))
  54. }
  55. }