Skip to content
Philip Cali edited this page Nov 21, 2011 · 4 revisions

Everything within LMXML is extensible, from parsing to converting.

Extending the Parser

All the parsing is held within the LmxmlParsers trait. Here is an example where one might want to more in-depth HTML output features.

trait HtmlShortcuts extends LmxmlParsers {
  val js: Parser[TopLevel] = "~" ~ "js" ~> inlineParams ^^ {
    case attrs => 
      LmxmlNode("script", Map("type" -> "text/javascript") ++ attrs, _)
  }

  override def topLevel = super.topLevel | js
}

/*
Now you could do things like:

html
  head
    ~ js @src = "jquery.js"
    ~ js
      ```
$(document).ready(function() {
  alert("Page is ready");
});
      ``` is unescaped
*/

val indention = 2

val parser = new PlainLmxmlParser(indention) with HtmlShortcuts

val nodes = parser.parseNodes(contents)

Extensible Factories

Hooking the custom parser to a factory like the Lmxml object is also very easy.

object CustomLmxml extends LmxmlFactory with FileLoading {
  def createParser(step: Int) = new PlainLmxmlParser(step) with HtmlShortcuts
}

val format = new xml.PrettyPrinter(200, 2).formatNodes(_: xml.NodeSeq)

CustomLmxml.fromFile("test.lmxml")(XmlConvert andThen format andThen println)

Try to keep things as modular as possible.

Clone this wiki locally