[Rails] [ANN] yhtml plugin

Yehuda Katz wycats at gmail.com
Wed Jul 26 03:25:15 GMT 2006


I have released a yhtml plugin, which is available at 
http://http://svn.visualjquery.com/admin_console/trunk/admin_console/vendor/plugins/yhtml

You can also get my other plugins, specificlly: the pre-alpha 
administrative console (AJAX-based scaffolding engine), 
acts_as_automatic, which allows you to define associations in a single 
yaml file, and not need to define has_many, has_one, belongs_to, or 
acts_as_list at 
http://svn.visualjquery.com/admin_console/trunk/admin_console/vendor/plugins/

Incidentally, the code in the plugins directly is my own, with the 
exception of the Engines plugin, which is needed for the admin console 
engine, as well as file_column, which I stuck in the engine to provide 
future file_column support. The time_select lib that I included in the 
admin console plugin is cannibalized from a number of sources, but is 
substantially my own.

The yhtml plugin adds a rendering engine based on yaml.

Example yhtml
=============

NOTE: The controller defines @hello as "- Title"

- html:
  - head:
    - title: My Title <%= @hello %>
    - style: |
        body { font-family: verdana, sans-serif }
        p { font-weight: bold }
        li { list-style-type: none }
  - body:
    - ul:
      - li:
        - {!id: One, !class: One Two}
        - Text
      <% 1.upto(10) do |count| %>
      - li: <%= count %>
      <% end %>
      - li: Text
      - li: [{!id: Two, !class: One Two, !style: "height: 30px; width: 
20px"}, LI]
      - li:
        - img: {
          !src: 
http://ec1.images-amazon.com/images/G/01/nav2/images/skins/teal/logo-on.gif 
}
    - p: Some More Text
    - table:
      - {!border: 1}
      - tr:
        - td: 1,1
        - td: 1,2
      - tr:
        - td: 2,1
        - td: |
            <%= 10.days.ago %>

Result from the yhtml renderer
==============================

<html>
  <head>
    <title>My Title - Title</title>
    <style>body { font-family: verdana, sans-serif }
     p { font-weight: bold }
     li { list-style-type: none }
    </style>
  </head>
  <body>
    <ul>
      <li class="One Two" id="One">Text</li>

      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>

      <li>7</li>
      <li>8</li>
      <li>9</li>
      <li>10</li>
      <li>Text</li>
      <li class="One Two" id="Two" style="height: 30px; width: 
20px">LI</li>

      <li>
        <img 
src="http://ec1.images-amazon.com/images/G/01/nav2/images/skins/teal/logo-on.gif" 
/>
      </li>
    </ul>
    <p>Some More Text</p>
    <table border="1">
      <tr>
        <td>11</td>

        <td>12</td>
      </tr>
      <tr>
        <td>21</td>
        <td>Sat Jul 15 23:09:43 Eastern Standard Time 2006</td>
      </tr>
    </table>

  </body>
</html>

Discussion
==========

The yhtml parser converts the hash returned by the YHTML parser into a 
tree of YamlNodes.

A YamlNode contains:
 * A tag name (html, body, head, li, etc.)
 * Text contents
 * Parameters
 * Children that are YamlNodes

The parser then recursively parses the YamlNode tree, creating HTML with 
appropriate indents.

You can put plain text into the YAML, which will not be parsed, by using 
YAML's "|" operator
for literals. The example above uses the operator.

Limitation
==========

A YamlNode cannot contain both plain-text contents and child YamlNodes. 
At the moment, this
will cause strange issues. If you want plain text and child nodes, wrap 
the text in span, or
other tags. This limitation will be resolved in a future version.

The limitation is the result of the fact that the parser stores content 
as an attribute, and
children as another (to make the recursive parsing easier), so the HTML 
builder has no way of
knowing where in the list of children to put the content.

-- 
Posted via http://www.ruby-forum.com/.


More information about the Rails mailing list