simonl / Html-Java

Html literals (quasi quote) for java (2012)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

We've been learning JSPs at school and it's atrocious, so I did something better.

Basically, the (Haskell) function XML.entirety takes a string representing java code (+ extensions) 
and returns the parsed result. The Show instance for this AST will return plain java text (using the XML class).

Main Extension:
  quasi-quoted XML between <% and %>.
  Inside the XML quotes, you can unquote Java code between {% and %}.
  You can alternately nest them arbitrarily deep.
  You can splice any Java object into the XML, or any collection of XML elements as a list of child nodes,
    and any Java object as an attribute value (though not keys)

  public static final XML.Node hello(final String name) {
    return <%
      <H1>Hello, {% name %}</H1>
    %>
  }


Bonus:
  More concise notation for closures.
  The body is either a block or an expression between parenthesis, or between quasi-quotes.
  That way it's easy (for the parser) to find where it ends

  #Integer(Integer n) => (n * n)
  #Integer(Integer n) => { return n * n; }
  #XML.Node(String name) => <% <H1>Hello, {% name %}</H1> %>





final XML.Node widgets = <%
  <DIV id="widgets">
    {%
      map(getAllWidgets(), #XML.Node(Widget w) => <%
        <DIV class={% w.getModel() %}>
          {% w %}
        </DIV>
      %>)
    %}
  </DIV>
%>;

widgets.print(response.getWriter());

About

Html literals (quasi quote) for java (2012)


Languages

Language:Haskell 59.3%Language:Java 40.7%