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