Hacker News new | past | comments | ask | show | jobs | submit login

Oleg Kiselyov has done some nice work where he defines SXML, a representation of XML in Scheme, and parsers/ pretty printers between XML and Scheme. This allows you to use hygienic macros (hygiene becomes important in the absence of namespaces). See http://www.okmij.org/ftp/Scheme/xml.html#SXML-spec

Lua's table syntax is well-suited to this style of embedding html/xml syntax in a programming language, e.g., if you define the appropriate functions a and i to represent anchors and italics, then you can define an anchor element:

    a {href=url, name="next",
       "Go to the",
       i "next",
       "element"}



Most languages will have literals that can be used to easily represent HTML data:

  [a => {href=>url, name=>'next'}, 'Go to the', [i => 'next'], 'element'];    # perl5

  [a => {:href(url), :name<next>}, 'Go to the', [:i<next>], 'element'];       # perl6

  [:a => {:href=>url, :name=>'next'}, 'Go to the', [:i => 'next'], 'element'] # ruby
  
Your Lua example is very similar to the HTML::AsSubs CPAN module (https://metacpan.org/module/HTML::AsSubs). Here is your example using this module:

    use HTML::AsSubs;

    sub url { 'http://www.anythingfornow.com' }

    my $h = html(
        a( {href=>url, name=>'next'},
           'Go to the',
           i('next'),
           'element',
        ),
    );

    say $h->as_HTML;


   a { ... }
and

   i "next"
are not table elements, they are function applications.

All of your examples use more magic characters than the Lua example I gave.


The HTML::AsSubs example uses functions and works exactly (see [1]) the same way your Lua code does.

The first set of examples are just to show that you can model HTML data within common Hash/Array literals available in most languages.

re: magic characters - The perl6 example does use some extra shortcut niceties :) However the perl5 example will actually work unchanged in perl6. The Ruby example is only slightly different to perl5 because it uses symbol sigil (Perl like Lua allows barewords on LHS of a table/hash key assignment).

[1] - The only difference is the Lua a() function receives all its content via a single Table. Whereas HTML::AsSubs a() is a variadic function and uses the Hash literal {} for assigning HTML attributes.


Which is, of course, just CGI.pm's html functions again.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: