<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lua nova &#187; Lua</title>
	<atom:link href="http://luanova.org/category/lua/feed/" rel="self" type="application/rss+xml" />
	<link>http://luanova.org</link>
	<description>welcome to the moon</description>
	<lastBuildDate>Thu, 20 May 2010 16:18:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Authentication-based permission groups in Sputnik</title>
		<link>http://luanova.org/authentication-based-permission-groups-in-sputnik/</link>
		<comments>http://luanova.org/authentication-based-permission-groups-in-sputnik/#comments</comments>
		<pubDate>Thu, 20 May 2010 07:17:34 +0000</pubDate>
		<dc:creator>jimwhitehead</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[sputnik]]></category>

		<guid isPermaLink="false">http://luanova.org/?p=63</guid>
		<description><![CDATA[Sputnik is a novel document-storage-based wiki written in Lua which has been previously featured on LuaNova. Today I&#8217;d like to share my experiences adding authentication-based permission groups to my running Sputnik installation at wowprogramming.com. Over the course of the past two months I have had two spam posts on our forums which were fixed by [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sputnik.freewisdom.org/">Sputnik</a> is a novel document-storage-based wiki written in Lua which
has been previously featured on LuaNova. Today I&#8217;d like to share my
experiences adding authentication-based permission groups to my
running Sputnik installation at <a href="http://wowprogramming.com">wowprogramming.com</a>. Over the
course of the past two months I have had two spam posts on our forums
which were fixed by removing those nodes from the document store, but
this left me with a question of what to do with the user account that
posted the spam message.</p>

<p>First, I should state that we have had very few instances of spam on
our site. Any user accounts must be verified by email and require the
user to input a captcha from the <a href="http://recaptcha.net/">reCaptcha</a> project. Once this is done,
the user is free to make edits to the API documentation and to post
new topics and replies on the discussion forums. However, the process
of posting spam on the site is very difficult to automate due to the
honeypots, field hashing, and post tokens that a default Sputnik
installation employs. That&#8217;s not to say that the software is
bullet-proof, but in practice it seems to work for my needs on a real
site.</p>

<p>The first few accounts, I simply removed from my authentication system
via a simple mysql query (although removing it from a default
installation would be a easier, a simple edit of the sputnik/passwords
node.) But then I realised I was giving up information about the
spammers, including the email addresses they were using to register,
something I could share with some of the spam registration databases
that exist. It was clear that I needed an easier way to stop those
user accounts from posting on the site without simply nuking their
user accounts from the system. Really what I wanted was a way to &#8216;ban&#8217;
a user account and prevent them from making edits on the site.</p>

<h2>Permissions in Sputnik</h2>

<p>Each node in a Sputnik document store is a Lua script that is compiled
and assembled into a Lua object at runtime. Consider the following
simple node:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    title <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Some Page&quot;</span>
    content <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>This is a simple test node <span style="color: #b1b100;">for</span> a Sputnik installation.
&nbsp;
    Hello World<span style="color: #66cc66;">!</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span></pre></div></div>


<p>Sputnik nodes can be more than just a symbolic representation of text
data. For example, you can restrict the permissions of a node by
adding a permissions field. Consider the following definition:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    permissions    <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
      deny<span style="color: #66cc66;">&#40;</span>all_users, all_actions<span style="color: #66cc66;">&#41;</span>
      allow<span style="color: #66cc66;">&#40;</span>all_users, show<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">-- show, show_content, cancel</span>
      allow<span style="color: #66cc66;">&#40;</span>Authenticated, edit_and_save<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- edit, save, preview</span>
      allow<span style="color: #66cc66;">&#40;</span>all_users, <span style="color: #ff0000;">&quot;post&quot;</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">-- needed for login</span>
      allow<span style="color: #66cc66;">&#40;</span>all_users, <span style="color: #ff0000;">&quot;rss&quot;</span><span style="color: #66cc66;">&#41;</span>
      allow<span style="color: #66cc66;">&#40;</span>Admin, all_actions<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span></pre></div></div>


<p>When this field is loaded, it&#8217;s obviously a string that we can then
further process. Specifically, we load it in an environment where a
few helpers functions are defined. The all_users function returns true
for all users, all_actions does the same for all action types.
Authenticated is a special function that returns true if the current
user is authenticated, while Anonymous can be used when the user is
not logged in. There&#8217;s also a special function called Admin that
queries the authentication metadata for the current user and checks if
the is_admin flag is set.</p>

<p>When I started looking for a way to ban users, I started looking in
this section of the code and found the following gem that Yuri sneaked
in after the definition for the Admin group:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">      <span style="color: #b1b100;">local</span> groups <span style="color: #66cc66;">=</span> self.saci.permission_groups <span style="color: #808080; font-style: italic;">-- just a local alias</span>
      <span style="color: #808080; font-style: italic;">-- Define group &quot;Admin&quot; as any user that has is_admin set to true</span>
      groups.Admin <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>user<span style="color: #66cc66;">&#41;</span>
         <span style="color: #b1b100;">return</span> user <span style="color: #b1b100;">and</span> self.auth:get_metadata<span style="color: #66cc66;">&#40;</span>user, <span style="color: #ff0000;">&quot;is_admin&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;true&quot;</span>
      <span style="color: #b1b100;">end</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">-- Define any group that starts with &quot;is.&lt;group&gt;&quot; as including all users</span>
      <span style="color: #808080; font-style: italic;">-- for whom &quot;is_&lt;group&gt;&quot; is set to true. For example, if is_clown is set</span>
      <span style="color: #808080; font-style: italic;">-- to true than user is a member of group &quot;is.clown&quot; and we can set:</span>
      <span style="color: #808080; font-style: italic;">--</span>
      <span style="color: #808080; font-style: italic;">--     allow(is.clown, &quot;show&quot;)</span>
      <span style="color: #808080; font-style: italic;">--</span>
      <span style="color: #b1b100;">local</span> groups_mt <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
         __index <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">table</span>, key<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>user<span style="color: #66cc66;">&#41;</span>
                      <span style="color: #b1b100;">return</span> user <span style="color: #b1b100;">and</span> self.auth:get_metadata<span style="color: #66cc66;">&#40;</span>user,
    <span style="color: #ff0000;">&quot;is_&quot;</span>..key<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;true&quot;</span>
                   <span style="color: #b1b100;">end</span>
         <span style="color: #b1b100;">end</span>
      <span style="color: #66cc66;">&#125;</span>
      groups.is <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">setmetatable</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>, groups_mt<span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>Bingo! With this bit of code, without making any changes to the
Sputnik core, I had a way to ban users in my system, by setting the
<strong>is_banned</strong> flag in their authentication metadata. For a simple
authentication system (the default) this is just a matter of setting
the given flag in the user&#8217;s authentication entry, whereas in a mysql
authentication system I just add a row to the database. This was only
part of the puzzle, since Sputnik doesn&#8217;t know anything about the new
user group. I had to make a few edits to the @Root prototype to add
the following at the end:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    deny<span style="color: #66cc66;">&#40;</span>is.banned, edit_and_save<span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>Since earlier in the permissions script the edit_and_save permissions
are granted to all authenticated users, I needed to make sure the
denial runs after that point, since the final &#8216;permission&#8217; is what
really matters. Since I have custom forums in my system, I needed to
make the same one-line change in a few other places.</p>

<p>It took a total of about 6 minutes to familiarize myself with the code
and make the necessary changes, and now I have an easy way to ban a
user from being able to post anywhere on my website.</p>

<p>Down with the spammers!</p>
]]></content:encoded>
			<wfw:commentRss>http://luanova.org/authentication-based-permission-groups-in-sputnik/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Guardian of the Imperial Inkstand</title>
		<link>http://luanova.org/imperialguardian/</link>
		<comments>http://luanova.org/imperialguardian/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 12:34:33 +0000</pubDate>
		<dc:creator>wra1th</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://luanova.org/?p=46</guid>
		<description><![CDATA[Some experiences of using Lua to create and maintain websites for other people. Making a website just for yourself is one matter; doing it for others brings into play a range of quite separate considerations, that affect even the technical matters of design and implementation. You are the pig in the middle, the bacon in [...]]]></description>
			<content:encoded><![CDATA[<p><em>Some experiences of using Lua to create and maintain websites
for other people</em>.</p>

<p>Making a website just for yourself is one matter; doing it for others
brings into play a range of quite separate considerations, that affect
even the technical matters of design and implementation. You are the
pig in the middle, the bacon in the sandwich, sitting between the site
on one side and its commissioners on the other. I am a slow learner, but
after a dozen years of experience of different commissions I have come to
some definite general conclusions.</p>

<p>First, even for modest websites, plenty of time is needed for preparation,
before you even start thinking about design or implementation; time for
establishing a proper protocol between yourself and those who have
commissioned you. Ensure that all communications between yourself and them
pass through a single intermediary, a spokesperson who can shield you from
the disagreements and internal rivalries into which it may be fatal to be
drawn. Your commissioners may well have a committee charged with
responsibility for the proposed website, and it will probably delegate its
duties to a number of people: Mr X the timetable, Miss Y the graphics,
Dr Z the document archive, &#8230; . Fine. Nice to know you, Mr X, Miss Y,
Dr Z, &#8230;
Have nothing to do with these arrangements. Insist that they all communicate
only through the spokesperson. The spokesperson will have to channel
information in both directions. That way, with any luck, you will not
have to explain more than a dozen times why web pages look different in
different browsers, and why enormous TIFF files intended for high quality
printing are not appropriate for graphics on web pages, because they
will take rather a long time to download.</p>

<p>To be realistic, it is too much to expect the more important members
of the website committee to stick to this regime. In any case, out
of friendliness they may want to express themselves personally. It
is for that reason that I recommend that during the development phase
of the website each page should contain within its header the tags</p>

<pre><code>&lt;meta  http-equiv="pragma" content="no-cache" /&gt;
&lt;meta  http-equiv="cache-control" content="no-cache" /&gt;
</code></pre>

<p>because otherwise you will have the committee chairman sending you
messages accusing you of not getting on with the job: &#8220;Hi Gavin,
Mr X says he sent you that stuff about emergency car-parking
three weeks ago and it still is not up on the website.&#8221;; and then
you will have to explain to a deeply suspicious technophobe all about
how browsers cache webpages and that he is still looking at the pages
that his browser cached three weeks ago, and no, you cannot tell him
how to clear the cache for himself because you do not know what operating
system or browser he is using (&#8220;operating system? what is that,
old boy?&#8221;).</p>

<p>I struck an extraordinary glitch when I was making the <a href="http://www.wra1th.plus.com/byzcong/">website</a> for the
twenty-first International Congress of Byzantine Studies, which took
place in London from 21 to 26 August 2006. It was a prestigious affair,
with the Prince of Wales as patron and a long list of eminent sponsoring
organizations. Clarence House had been persuaded to send me a facsimile
of a signed letter of welcome from HRH, in receipt of which I had to
sign an undertaking that I would not permit any improper use of it. I
put a link to it from the sponsors&#8217; page.</p>

<p>The organizing committee of the conference had obtained a lovely jpeg
from the British Museum, of a Byzantine wooden carving of an angel.
They used this to make a poster. I used a jpeg of that to make
the entry page of the site. Clicking on the angel was to bring one
to a page listing the sponsors, including HRH, and inviting the viewer
to choose either the English or the French version of the website.
In my innocence, I called the page &#8220;sponsor.html&#8221;. When it was up for
review by the organizing committee I began to get puzzled queries from
them. &#8220;I clicked on the angel, and nothing happened&#8221;, about half of them
reported. I had no problem, nor did the other half. It was very mysterious.
Eventually research showed the culprit to be an arrogant piece of Norton
AntiVirus software running on the computers of some committee members.
It had decided that &#8220;sponsor&#8221; was obscene or dangerous, apparently.
Thus was HRH&#8217;s letter censored by Norton. In the end I changed the file&#8217;s
name to &#8220;spons.html&#8221; and the problem went away. The moral, I suppose,
is that you have to fathom some pretty sick minds if your webpages
are to be viewable by the public.</p>

<p>It is time to turn from the human to the coding side of things. I have
always believed that the best principle is to keep all as simple as
possible. As none of my sites have been designed for huge traffic I have
always shunned client-side scripting. It is usually unnecessary. One
should also keep in mind that many users may not have the latest browser.
In any case, adoption of WAI (the <a href="http://www.w3.org/WAI/">Web Accessibility Initiative</a> ) is
mandatory for .gov.uk websites, and it makes sense for any site
that is to be used by the public, especially an academic public
that is not likely to have the latest hardware.</p>

<p>I always create my websites on my own machine. I generally divide
the directory for website-creation into two subdirectories: &#8220;site&#8221;,
to contain all the stuff to be uploaded to the server, and &#8220;data&#8221;
to hold the gubbins from which the website is to be created, usually
as a &#8220;make&#8221; project. The contents of the webpages I classify into
three groups:</p>

<ol>
<li><strong>furniture</strong> &#8211; the graphics and css stuff that determine a common theme;</li>
<li><strong>permanent content</strong> &#8211; usually text;</li>
<li><strong>formatted mutable data</strong> &#8211; usually held as a textual database, as lists of labelled records, for ease of updating with a text editor.</li>
</ol>

<p>I use Lua format for the databases and Lua scripts to input from the
data-directory and output the finished pages to the site-directory.
To begin with, for the Art Historians&#8217; Millennium bash, I used a Lua
program, called Weave, which I had developed for creating web pages.
Over time I improved it by generalizing to the production of any
sort of marked-up text. Eventually it simply got replaced by a Lua
library. I must make another blog about this, because Lua&#8217;s way
with strings was a key factor in the design of this software.
I also produced a Lua program, Infuse, to perform the abstract
task of pouring content into template files &#8211; a sort of mail-merge.
It was the actual experience of coping with the commissioned websites
that drove me to keep reinventing. The frustrating thing was that I
dared not change over to a newer regime until the project was completed.
By starting sufficiently early I could create my templates for
the approval of the commissioners, before the flood of content
hit me. It was important to have a design sufficiently flexible to
cope with sudden changes of tack. The Lua format for labelled
records was good in this respect, because extra fields could always
be added. By contrast, CSV format would have been hopeless.</p>

<p>Lua&#8217;s extreme portability was handy. My own machine ran neither
Windows, Linux nor MacOS. Yet when I did a website for an artist,
whose agent needed to take over the running of it, even though the
agent used Windows, there was no problem getting everything working
on his machine.</p>

<p>For the Byzantine Congress I was told that I had been appointed
&#7952;&#960;&#8054; &#964;&#959;&#8160; &#954;&#945;&#957;&#953;&#954;&#955;&#949;&#8055;&#959;&#965; ,
guardian of the imperial inkstand, so named because it was in the shape
of a little dog (canicula). To find out more about this prestigious
office, use Google.</p>

<p><a href="http://www.wra1th.plus.com/">Gavin Wraith</a></p>

<h2>Ropes are better than Strings: A Technical Appendix</h2>

<p>In Lua strings are immutable values. This means that you
should avoid building up strings out of small pieces,
because although they will eventually be garbage-collected,
all the intermediate stages of your construction will still be
there taking up storage. Building up a string of n characters
by consecutive concatenations will use a number of bytes
varying quadratically with n. So concatenations are something
to be avoided. What about the construction of web pages,
where markup and text must be intertwined? Surely that will
involve lots of concatenations?</p>

<p>Actually, no &#8211; it need not. Whether we think of an html document
as text with markup inserted, or whether we think of it as a
structure of nested tag-pairs with text inserted, either way we
are led to the concept of strings-with-holes. To be more formal
we define the datatype of &#8220;ropes&#8221; recursively by:</p>

<pre><code>rope = string | list of rope
</code></pre>

<p>In Lua syntax, a rope either looks like</p>

<pre><code>[[ ..... ]]
</code></pre>

<p>or like</p>

<pre><code>{ ........ }
</code></pre>

<p>where the contents of the braces are a comma separated list of
ropes.</p>

<p>Put another way, a rope is a tree whose leaves are strings. We
can think of it as a string that has been broken apart leaving gaps.
If we walk over a rope, depth first, printing out the leaves to a file,
then we can construct our marked-up text without doing any
string-concatenation at all.</p>

<p>So this code</p>

<pre><code>&lt;a href="http://luanova.org/"&gt;LuaNova&lt;/a&gt;
</code></pre>

<p>can be obtained by printing out the rope</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">     <span style="color: #66cc66;">&#123;</span>
     <span style="color: #66cc66;">&#123;</span>
      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&lt;</span>a <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
      <span style="color: #66cc66;">&#123;</span>
       <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>href<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;]],
       [[http://luanova.org/]],
       [[&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
      <span style="color: #66cc66;">&#125;</span>,
      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
     <span style="color: #66cc66;">&#125;</span>,
     <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>LuaNova<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
     <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&lt;/</span>a<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
     <span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>If we define</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">     link <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>url<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>label<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#123;</span>
     <span style="color: #66cc66;">&#123;</span>
      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&lt;</span>a <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
      <span style="color: #66cc66;">&#123;</span>
       <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>href<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;]],
       url,
       [[&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
      <span style="color: #66cc66;">&#125;</span>,
      <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
     <span style="color: #66cc66;">&#125;</span>,
     label,
     <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&lt;/</span>a<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
     <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">end</span> <span style="color: #b1b100;">end</span></pre></div></div>


<p>then our rope is just the value of</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    link <span style="color: #ff0000;">&quot;http://luanova.org/&quot;</span> <span style="color: #ff0000;">&quot;LuaNova&quot;</span></pre></div></div>


<p>There are two sorts of markup: &#8220;monotags&#8221; and &#8220;tags&#8221;.</p>

<p>By a monotag I mean something like</p>

<pre><code>&lt;tag_name attr /&gt;
</code></pre>

<p>and we can implement this as a function from ropes to ropes:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>attr<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;&lt;tag_name &quot;</span>,attr,<span style="color: #ff0000;">&quot; /&gt;&quot;</span><span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">end</span></pre></div></div>


<p>By a tag I mean something like</p>

<pre><code>&lt;tag_id attr&gt;stuff&lt;/tag_id&gt;
</code></pre>

<p>which we can implement as a function from ropes to functions from
ropes to ropes:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">     <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>attr<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>stuff<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span>
             <span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;&lt;tag_id &quot;</span>,attr,<span style="color: #ff0000;">&quot;&gt;&quot;</span>,stuff,<span style="color: #ff0000;">&quot;&lt;/tag_id&gt;&quot;</span><span style="color: #66cc66;">&#125;</span>
             <span style="color: #b1b100;">end</span>
     <span style="color: #b1b100;">end</span></pre></div></div>


<p>To simplify construction of webpages I invented a little language,
which I called <em>Weave</em>, as a fragment of Lua using strings and ropes
and named functions for the tags and monotags of HTML (see <a href="http://www.lua.org/notes/ltn010a.html">Lua technical
note 30</a> .)
After a while I broadened Weave&#8217;s abilities
so that it could cope with any sort of markup and no longer had
HTML&#8217;s tags and monotags built in. I introduced empty tables called
&#8220;TAG&#8221; and &#8220;MONOTAG&#8221; with metatables, so that using the __index key
I could define tag and monotag functions with notations like
&#8220;TAG.a&#8221; and &#8220;MONOTAG.img&#8221; and so on. This was more flexible, but it
did mean that the user had to define her own tags before building
a webpage. That did not matter, because by then CSS had appeared and
most webpages needed only a handful of tags and monotags. Eventually
I did away with Weave as a separate entity and simply used the Lua
module &#8220;xhtml&#8221; instead.</p>

<p>To create a very trivial webpage &#8220;foo.html&#8221; in the current directory,
run a Lua script as follows:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">     #<span style="color: #66cc66;">!</span> lua
     <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">&quot;xhtml&quot;</span>
     <span style="color: #b1b100;">do</span>
     <span style="color: #b1b100;">local</span> mydoc <span style="color: #66cc66;">=</span> BEGIN <span style="color: #ff0000;">&quot;foo.html&quot;</span>
     <span style="color: #b1b100;">local</span> hstyle <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>style<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text-align: centre;&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
     <span style="color: #b1b100;">local</span> myheader, link <span style="color: #66cc66;">=</span> xhtml.TAG.h1, xhtml.link
     mydoc.BODY <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
                   myheader <span style="color: #66cc66;">&#40;</span>hstyle<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;Try this&quot;</span><span style="color: #66cc66;">;</span>
                   link <span style="color: #ff0000;">&quot;http://luanova.org/&quot;</span> <span style="color: #ff0000;">&quot;Lua Nova&quot;</span><span style="color: #66cc66;">;</span>
                  <span style="color: #66cc66;">&#125;</span>
     END<span style="color: #66cc66;">&#40;</span>mydoc<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- write the rope out to file</span>
     <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- do</span></pre></div></div>


<p>OK, this takes almost as long as creating the webpage by hand, for
this trivial example. But it should not take too much imagination
to see the possibilities. Practically every webpage I have created
for the last ten years has used this method.</p>

<p>Here are the relevant modules:</p>

<p><strong>weave.lua</strong></p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">     <span style="color: #808080; font-style: italic;">--[[
         The rope datatype is defined recursively:
&nbsp;
         data rope = string | list of rope.
&nbsp;
       Use:  status,err = rope.walk(rope,function)
             print(table.concat(err,&quot;.&quot;))
       Error message prints out index at each level.
     ]]</span>
     <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">pcall</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">pcall</span>
     <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">type</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">type</span>
     <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">ipairs</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">ipairs</span>
     <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">tostring</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">tostring</span>
     <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">assert</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">assert</span>
     <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">setmetatable</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">setmetatable</span>
     <span style="color: #b1b100;">local</span> open <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">io.open</span>
&nbsp;
     module <span style="color: #ff0000;">&quot;weave&quot;</span>
&nbsp;
     <span style="color: #808080; font-style: italic;">-- ropes -------</span>
&nbsp;
     <span style="color: #b1b100;">local</span> t_err <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&lt;bad type: %s&gt;&quot;</span>
     <span style="color: #b1b100;">local</span> a_err <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&lt;bad arg: %s&gt;&quot;</span>
&nbsp;
     walk <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>x,f<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">-- apply f to leaves of tree x, depth first</span>
          <span style="color: #b1b100;">local</span> g
          g <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>x,e<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- e = error stack</span>
             <span style="color: #b1b100;">local</span> x_type, status <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span>
             <span style="color: #b1b100;">if</span> x_type <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #b1b100;">then</span>
              status <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">pcall</span><span style="color: #66cc66;">&#40;</span>f,x<span style="color: #66cc66;">&#41;</span>
              <span style="color: #b1b100;">if</span> <span style="color: #b1b100;">not</span> status <span style="color: #b1b100;">then</span> e<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#e<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> a_err:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- if</span>
                  <span style="color: #b1b100;">return</span> status,e
              <span style="color: #b1b100;">elseif</span> x_type <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;table&quot;</span> <span style="color: #b1b100;">then</span>
                  e<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#e<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>
                  <span style="color: #b1b100;">for</span> i,y <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">ipairs</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
                    e<span style="color: #66cc66;">&#91;</span>#e<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">tostring</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>
                    status <span style="color: #66cc66;">=</span> g<span style="color: #66cc66;">&#40;</span>y,e<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #b1b100;">if</span> <span style="color: #b1b100;">not</span> status <span style="color: #b1b100;">then</span> <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">nil</span>,e <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- if</span>
                  <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- for</span>
                  e<span style="color: #66cc66;">&#91;</span>#e<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">nil</span>
                  <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">true</span>
                <span style="color: #b1b100;">else</span>
                  e<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#e<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> t_err:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>x_type<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">nil</span>,e
                <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- if</span>
              <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
              <span style="color: #b1b100;">return</span> g<span style="color: #66cc66;">&#40;</span>x,<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>
&nbsp;
       <span style="color: #b1b100;">local</span> file_err <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;cannot open %s&quot;</span>
       out <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>x,file<span style="color: #66cc66;">&#41;</span>
             <span style="color: #b1b100;">local</span> o <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">assert</span><span style="color: #66cc66;">&#40;</span>open<span style="color: #66cc66;">&#40;</span>file,<span style="color: #ff0000;">&quot;w&quot;</span><span style="color: #66cc66;">&#41;</span>,file_err:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #b1b100;">local</span> f <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span>
                       o:<span style="color: #b1b100;">write</span><span style="color: #66cc66;">&#40;</span>s <span style="color: #b1b100;">or</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>
                       <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">true</span>
                       <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
             <span style="color: #b1b100;">local</span> status,err <span style="color: #66cc66;">=</span> walk<span style="color: #66cc66;">&#40;</span>x,f<span style="color: #66cc66;">&#41;</span>
             o:close<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #b1b100;">return</span> status,err
             <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
&nbsp;
       <span style="color: #808080; font-style: italic;">------ markup stuff --------------</span>
       <span style="color: #808080; font-style: italic;">--[[
       Two types of tag:
&nbsp;
         monotag    &lt;foo attributestring /&gt;
         tag    &lt;foo attributestring&gt; rope &lt;/foo&gt;
&nbsp;
       where attributestring could be empty.
         ]]</span>
&nbsp;
       <span style="color: #b1b100;">local</span> monotagger <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
       __index <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>_,tag<span style="color: #66cc66;">&#41;</span>
                 <span style="color: #b1b100;">assert</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;string&quot;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>attr<span style="color: #66cc66;">&#41;</span>
                        <span style="color: #b1b100;">local</span> o <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;&lt;&quot;</span>,tag<span style="color: #66cc66;">&#125;</span>
                        <span style="color: #b1b100;">if</span> attr <span style="color: #b1b100;">and</span> <span style="color: #66cc66;">&#40;</span>#attr <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span>
                          o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; &quot;</span>
                          o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> attr
                        <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- if</span>
                        o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; /&gt;&quot;</span>
                        <span style="color: #b1b100;">return</span> o
                        <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
                  <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
                 <span style="color: #66cc66;">&#125;</span>
&nbsp;
       <span style="color: #b1b100;">local</span> bitagger <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
       __index <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>_,tag<span style="color: #66cc66;">&#41;</span>
                 <span style="color: #b1b100;">assert</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;string&quot;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>attr<span style="color: #66cc66;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>rope<span style="color: #66cc66;">&#41;</span>
                         <span style="color: #b1b100;">local</span> o <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;&lt;&quot;</span>,tag<span style="color: #66cc66;">&#125;</span>
                         <span style="color: #b1b100;">if</span> attr <span style="color: #b1b100;">and</span> <span style="color: #66cc66;">&#40;</span>#attr <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span>
                          o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot; &quot;</span>
                          o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> attr
                         <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- if</span>
                         o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&gt;&quot;</span>
                         o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> rope
                         o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&lt;/&quot;</span>
                         o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> tag
                         o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&gt;&quot;</span>
                         <span style="color: #b1b100;">return</span> o
                  <span style="color: #b1b100;">end</span> <span style="color: #b1b100;">end</span> <span style="color: #b1b100;">end</span>
                  <span style="color: #66cc66;">&#125;</span>
       MONOTAG, TAG <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
       <span style="color: #b1b100;">setmetatable</span><span style="color: #66cc66;">&#40;</span>MONOTAG,monotagger<span style="color: #66cc66;">&#41;</span>
       <span style="color: #b1b100;">setmetatable</span><span style="color: #66cc66;">&#40;</span>TAG,bitagger<span style="color: #66cc66;">&#41;</span></pre></div></div>


<p><strong>xhtml.lua</strong></p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">       <span style="color: #808080; font-style: italic;">--[[ The xhtml library exports
&nbsp;
         TAG, MONOTAG, BEGIN, END, TEXT, SPACE, REM and link
&nbsp;
       ]]</span>
&nbsp;
&nbsp;
       <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">&quot;weave&quot;</span>
&nbsp;
       <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">assert</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">assert</span>
       <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">type</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">type</span>
       <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">pairs</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">pairs</span>
       <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">print</span> <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">print</span>
       <span style="color: #b1b100;">local</span> this <span style="color: #66cc66;">=</span> arg<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>
       <span style="color: #b1b100;">local</span> leaf <span style="color: #66cc66;">=</span> this:match<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>([^<span style="color: #000099; font-weight: bold;">\\</span>]*)$&quot;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #b1b100;">local</span> weave <span style="color: #66cc66;">=</span> weave
       <span style="color: #b1b100;">local</span> walk <span style="color: #66cc66;">=</span> weave.walk
       <span style="color: #b1b100;">local</span> out <span style="color: #66cc66;">=</span> weave.out
&nbsp;
       <span style="color: #b1b100;">local</span> concat <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">table.concat</span>
&nbsp;
       module <span style="color: #ff0000;">&quot;xhtml&quot;</span>
&nbsp;
       MONOTAG <span style="color: #66cc66;">=</span> weave.MONOTAG
       TAG <span style="color: #66cc66;">=</span> weave.TAG
&nbsp;
       <span style="color: #b1b100;">local</span> doctype <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
       <span style="color: #66cc66;">&lt;!</span>DOCTYPE html PUBLIC <span style="color: #ff0000;">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
       <span style="color: #ff0000;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
       <span style="color: #b1b100;">local</span> HTML <span style="color: #66cc66;">=</span> TAG.html <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
       xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml:lang<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span> lang<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
       <span style="color: #b1b100;">local</span> nl <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
&nbsp;
       BEGIN <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>filename<span style="color: #66cc66;">&#41;</span>
               <span style="color: #b1b100;">if</span> filename <span style="color: #b1b100;">then</span>
                 <span style="color: #b1b100;">assert</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>filename<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;string&quot;</span>,<span style="color: #ff0000;">&quot;BEGIN takes a string or nil&quot;</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- if</span>
               <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#123;</span> FILE <span style="color: #66cc66;">=</span> filename <span style="color: #b1b100;">or</span> this..<span style="color: #ff0000;">&quot;.html&quot;</span><span style="color: #66cc66;">;</span> <span style="color: #66cc66;">&#125;</span>
               <span style="color: #b1b100;">end</span>
&nbsp;
       <span style="color: #b1b100;">local</span> META,LINK <span style="color: #66cc66;">=</span> MONOTAG.meta,MONOTAG.link
       <span style="color: #b1b100;">local</span> HEAD,BODY,TITLE <span style="color: #66cc66;">=</span> TAG.head <span style="color: #ff0000;">&quot;&quot;</span>,TAG.body,TAG.title <span style="color: #ff0000;">&quot;&quot;</span>
       <span style="color: #b1b100;">local</span> css <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>rel<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> media<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span> href<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
       END <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>rope<span style="color: #66cc66;">&#41;</span>
             <span style="color: #b1b100;">local</span> file <span style="color: #66cc66;">=</span> rope.FILE
             <span style="color: #b1b100;">local</span> style <span style="color: #66cc66;">=</span> rope.STYLE
             <span style="color: #b1b100;">local</span> doc <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
              rope.DOCTYPE <span style="color: #b1b100;">or</span> doctype<span style="color: #66cc66;">;</span>
              nl<span style="color: #66cc66;">;</span>
              HTML <span style="color: #66cc66;">&#123;</span>
               nl<span style="color: #66cc66;">;</span>
               HEAD <span style="color: #66cc66;">&#123;</span>
                nl<span style="color: #66cc66;">;</span>
                TITLE <span style="color: #66cc66;">&#40;</span>rope.TITLE <span style="color: #b1b100;">or</span> leaf<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
                nl<span style="color: #66cc66;">;</span>
                META <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Generator&quot;</span> content<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Lua&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">;</span>
                nl<span style="color: #66cc66;">;</span>
                META <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;MSSmartTagsPreventParsing&quot;</span> content<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;TRUE&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">;</span>
                nl<span style="color: #66cc66;">;</span>
                rope.HEAD <span style="color: #b1b100;">or</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
                style <span style="color: #b1b100;">and</span> LINK <span style="color: #66cc66;">&#123;</span>css<span style="color: #66cc66;">;</span>style<span style="color: #66cc66;">;</span><span style="color: #ff0000;">'&quot;'</span><span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">or</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
                    <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">;</span>
                nl<span style="color: #66cc66;">;</span>
                BODY <span style="color: #66cc66;">&#40;</span>rope.ATTR <span style="color: #b1b100;">or</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>rope.BODY <span style="color: #b1b100;">or</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
                nl<span style="color: #66cc66;">;</span>
                  <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">;</span>
                          <span style="color: #66cc66;">&#125;</span>
              status, err <span style="color: #66cc66;">=</span> out<span style="color: #66cc66;">&#40;</span>doc,file<span style="color: #66cc66;">&#41;</span>
              <span style="color: #b1b100;">if</span> <span style="color: #b1b100;">not</span> status <span style="color: #b1b100;">then</span> <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>concat<span style="color: #66cc66;">&#40;</span>err,<span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- if</span>
              <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">--function</span>
&nbsp;
       <span style="color: #b1b100;">local</span> entity <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;&lt;&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&amp;lt;&quot;</span>,
         <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;&gt;&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&amp;gt;&quot;</span>,
         <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;&amp;&amp;&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&amp;amp;&quot;</span>,
                      <span style="color: #66cc66;">&#125;</span>
       <span style="color: #b1b100;">local</span> catchall <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;(.)&quot;</span>
       <span style="color: #b1b100;">local</span> entify <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span>
                      <span style="color: #b1b100;">local</span> n,fmts <span style="color: #66cc66;">=</span> c:byte<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #ff0000;">&quot;&amp;#%s;&quot;</span>
                      <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>n <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">127</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">and</span> fmts:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">or</span> c
                      <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
&nbsp;
       TEXT <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>rope<span style="color: #66cc66;">&#41;</span>
              <span style="color: #b1b100;">local</span> o <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
              <span style="color: #b1b100;">local</span> f <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #b1b100;">local</span> status <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;string&quot;</span>
                  <span style="color: #b1b100;">if</span> status <span style="color: #b1b100;">then</span>
                    <span style="color: #b1b100;">for</span> symb,ent <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">pairs</span><span style="color: #66cc66;">&#40;</span>entity<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
                      s <span style="color: #66cc66;">=</span> s:<span style="color: #b1b100;">gsub</span><span style="color: #66cc66;">&#40;</span>symb,ent<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- for</span>
                    s <span style="color: #66cc66;">=</span> s:<span style="color: #b1b100;">gsub</span><span style="color: #66cc66;">&#40;</span>catchall,entify<span style="color: #66cc66;">&#41;</span>
                    o<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>+#o<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> s
                  <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- if</span>
                   <span style="color: #b1b100;">return</span> status
                 <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
              walk<span style="color: #66cc66;">&#40;</span>rope,f<span style="color: #66cc66;">&#41;</span>
              <span style="color: #b1b100;">return</span> o
              <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
&nbsp;
       SPACE <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>
               <span style="color: #b1b100;">assert</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;number&quot;</span>,<span style="color: #ff0000;">&quot;SPACE takes a number&quot;</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #b1b100;">local</span> s <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&amp;nbsp;&quot;</span>
               <span style="color: #b1b100;">return</span> s:rep<span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>
               <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
&nbsp;
       REM <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&lt;!-- &quot;</span><span style="color: #66cc66;">;</span>s<span style="color: #66cc66;">;</span><span style="color: #ff0000;">&quot; --&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span>
       link <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>url<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> TAG.a <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">'href=&quot;'</span><span style="color: #66cc66;">;</span> url<span style="color: #66cc66;">;</span> <span style="color: #ff0000;">'&quot;'</span><span style="color: #66cc66;">;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">end</span> <span style="color: #808080; font-style: italic;">-- function</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://luanova.org/imperialguardian/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An introduction to Orbit</title>
		<link>http://luanova.org/orbit1-2/</link>
		<comments>http://luanova.org/orbit1-2/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 16:57:41 +0000</pubDate>
		<dc:creator>stevejdonovan</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Kepler]]></category>
		<category><![CDATA[orbit]]></category>
		<category><![CDATA[web frameworks]]></category>

		<guid isPermaLink="false">http://luanova.org/?p=41</guid>
		<description><![CDATA[Installing Orbit Orbit is a lightweight framework for Web applications written in Lua. Unlike Sputnik, Orbit gives you the basic tools for constructing applications built on WSAPI. If you want authentication, standard look-and-feel, wiki-like functionality, you are better off starting with Sputnik; but Orbit is a satisfying way to build web applications from scratch. The [...]]]></description>
			<content:encoded><![CDATA[<h2>Installing Orbit</h2>

<p><a href="http://orbit.luaforge.net/">Orbit</a> is a lightweight framework for Web applications written in Lua. Unlike <a href="http://luanova.org/sputnik/">Sputnik</a>, Orbit gives you the basic tools for constructing applications built on <a href="http://wsapi.luaforge.net/">WSAPI</a>.  If you want authentication, standard look-and-feel, wiki-like functionality, you are better off starting with Sputnik; but Orbit is a satisfying way to build web applications from scratch.</p>

<p>The easiest way to install Orbit on a Unix machine is using <a href="http://luarocks.org/">LuaRocks</a>, but here for a change I will assume that you already have <a href="http://luaforwindows.luaforge.net/">Lua for Windows</a> which already has most of the dependencies for Orbit.  Then download <a href="http://mysite.mweb.co.za/residents/sdonovan/lua/orbit-2.0.2-all.zip"> orbit-2.0.2-all.zip</a>, unzip it somewhere preserving folder names, and run  <strong>lua install-orbit.lua</strong> from this directory in a command prompt.</p>

<p>The examples in this tutorial are then in the <strong>orbit-2.0.2\samples\basic</strong> folder.</p>

<h2>The Basics</h2>

<p>The simplest Orbit application is one that serves up a single HTML page:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">-- basic1.lua</span>
    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- declaration</span>
    module<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;basic1&quot;</span>, package.seeall, orbit.new<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- handler</span>
    <span style="color: #b1b100;">function</span> index<span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> render_index<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- dispatch</span>
    basic1:dispatch_get<span style="color: #66cc66;">&#40;</span>index, <span style="color: #ff0000;">&quot;/&quot;</span>, <span style="color: #ff0000;">&quot;/index&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- render</span>
    <span style="color: #b1b100;">function</span> render_index<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
        <span style="color: #66cc66;">&lt;</span>head<span style="color: #66cc66;">&gt;&lt;/</span>head<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>html<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>Pretty Easy<span style="color: #66cc66;">!&lt;/</span>h2<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;/</span>html<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>An Orbit application is a Lua module, declared in the usual way except for the extra &#8216;orbit.new&#8217;. Lua&#8217;s <strong>module</strong> function can take a number of extra arguments, which are all functions which modify the module table in some way: <strong>package.seeall</strong> makes the global environment accessible to the module, and <strong>orbit.new</strong> adds some extra methods to the new module (you may think of this as &#8216;inheritance&#8217;.)</p>

<p>Any HTML request is mapped onto handler functions &#8211; in this case, the result of doing a GET request on &#8216;/&#8217; or &#8216;/index&#8217;. <strong>index</strong> just asks <strong>render_index</strong> to actually generate the HTML for this particular page. You can then run this application using the default Xavante server:</p>

<pre><code>c:\basic&gt; orbit basic1.lua
Starting Orbit server at port 8080
</code></pre>

<p>And you can view the result by going to <strong>http://localhost:8080</strong> in your favourite browser.</p>

<p>Not perhaps very impressive, seeing as the result could be achieved by a simple static page, but the render function can return <em>any</em> dynamically created string. And this example will work with Xavante, FastCGI or CGI, because Orbit is a WSAPI application.</p>

<p>All the usual web request variables are parsed and available to you. The <strong>web</strong> variable returned from the handler contains a table <strong>GET</strong> which has all the parameters passed to the server from a <strong>GET</strong> request. Call this script with <strong>http://localhost:8080/?cat=felix&amp;dog=fido</strong> and you will see that <strong>cat</strong> and <strong>dog</strong> are fields of <strong>web.GET</strong>, appropriately translated from URL encoding.</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">-- basic3.lua</span>
    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    module<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;basic3&quot;</span>, package.seeall, orbit.new<span style="color: #66cc66;">&#41;</span>
&nbsp;
    basic3:dispatch_get<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">local</span> ls <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">for</span> k,v <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">pairs</span><span style="color: #66cc66;">&#40;</span>web.GET<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
            <span style="color: #b1b100;">table.insert</span><span style="color: #66cc66;">&#40;</span>ls,<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&lt;li&gt;%s = %s&lt;/li&gt;'</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>k,v<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>
        ls <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'&lt;ul&gt;'</span>..<span style="color: #b1b100;">table.concat</span><span style="color: #66cc66;">&#40;</span>ls,<span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #66cc66;">&#41;</span>..<span style="color: #ff0000;">'&lt;/ul&gt;'</span>
        <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
        <span style="color: #66cc66;">&lt;</span>html<span style="color: #66cc66;">&gt;&lt;</span>head<span style="color: #66cc66;">&gt;&lt;/</span>head<span style="color: #66cc66;">&gt;&lt;</span>body<span style="color: #66cc66;">&gt;</span>
        Web Variables <span style="color: #66cc66;">&lt;</span>br<span style="color: #66cc66;">/&gt;</span>
        <span style="color: #66cc66;">%</span>s
        <span style="color: #66cc66;">&lt;/</span>body<span style="color: #66cc66;">&gt;&lt;/</span>html<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>ls<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>, <span style="color: #ff0000;">&quot;/&quot;</span>, <span style="color: #ff0000;">&quot;/index&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>This is not a pretty script; it&#8217;s only virtue is that it is short. It is ugly for two basic reasons; first, generating HTML with regular string operations is awkward, and second, it does not separate the logical parts out neatly. The HTML problem will be discussed in the next section, but the next example separates the various operations into their own sections.</p>

<p>This also shows how Orbit can serve up static content as well, in this case anything inside the <strong>/images</strong> directory:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">-- basic2.lua</span>
    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    module<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;basic2&quot;</span>, package.seeall, orbit.new<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">function</span> index<span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> render_index<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">collectgarbage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;count&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    basic2:dispatch_get<span style="color: #66cc66;">&#40;</span>index, <span style="color: #ff0000;">&quot;/&quot;</span>, <span style="color: #ff0000;">&quot;/index&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #808080; font-style: italic;">-- any file in this directory will be served statically</span>
    basic2:dispatch_static <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/images/.+&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">local</span> template <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
    <span style="color: #66cc66;">&lt;</span>head<span style="color: #66cc66;">&gt;&lt;/</span>head<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>html<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">%</span>s
    <span style="color: #66cc66;">&lt;/</span>html<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_page<span style="color: #66cc66;">&#40;</span>contents<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> template:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>contents<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_index<span style="color: #66cc66;">&#40;</span>mem<span style="color: #66cc66;">&#41;</span>
       <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
        <span style="color: #66cc66;">&lt;</span>img src<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/images/lua.png&quot;</span><span style="color: #66cc66;">/&gt;</span>
        <span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>Memory used by Lua is <span style="color: #66cc66;">%</span>6.0f kB<span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>
       <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>mem<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>Note that <strong>dispatch_static</strong> is passed a Lua <em>string pattern</em>, not a file mask; in this case, anything in the directory.</p>

<p>We are now presenting true dynamic content (the amount of memory managed by Lua) and made a first start to prettifying the result. It is convenient to only generate the &#8216;inner&#8217; HTML and use a template to generate the full page, so <strong>render_page</strong> has been factored out.</p>

<p>Orbit follows the Model-View-Controller (MVC) pattern that separates an application into three distinct parts. The Controller acts as the executive, the Model manages the data, and the View presents the data to the user; here the dispatch rules calls <strong>index</strong> which uses the renderer <strong>render_index</strong> to present the HTML form of the data. This pattern comes from a basic principle, <a href="http://en.wikipedia.org/wiki/Separation_of_concerns">Separation of Concerns</a>. You do not want to mix database access, application logic and visual presentation together because the result is not clear and may become unmaintainable quickly.</p>

<h2>HTML the Orbit Way</h2>

<p>The HTML problem is usually solved using templates, such as <a href="http://cosmo.luaforge.net/">Cosmo</a> as discussed in the last <a href="http://luanova.org/sputnik">article</a> on Sputnik. Orbit provides another option, which is that Lua code can generate HTML:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">-- html1.lua</span>
    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    <span style="color: #b1b100;">function</span> generate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> html <span style="color: #66cc66;">&#123;</span>
            head<span style="color: #66cc66;">&#123;</span>title <span style="color: #ff0000;">&quot;HTML Example&quot;</span><span style="color: #66cc66;">&#125;</span>,
            body<span style="color: #66cc66;">&#123;</span>
                h2<span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;Here we go again!&quot;</span><span style="color: #66cc66;">&#125;</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    orbit.htmlify<span style="color: #66cc66;">&#40;</span>generate<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>generate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">==&gt;</span>
    C:\basic<span style="color: #66cc66;">&gt;</span>lua html1.lua
    <span style="color: #66cc66;">&lt;</span>html <span style="color: #66cc66;">&gt;&lt;</span>head <span style="color: #66cc66;">&gt;&lt;</span>title <span style="color: #66cc66;">&gt;</span>HTML Example<span style="color: #66cc66;">&lt;/</span>title<span style="color: #66cc66;">&gt;&gt;&lt;/</span>head<span style="color: #66cc66;">&gt;&lt;</span>body <span style="color: #66cc66;">&gt;&lt;</span>h2 <span style="color: #66cc66;">&gt;</span>
    Here we go again<span style="color: #66cc66;">!&lt;/</span>h2<span style="color: #66cc66;">&gt;&lt;/</span>body<span style="color: #66cc66;">&gt;&lt;/</span>html<span style="color: #66cc66;">&gt;</span></pre></div></div>


<p>This certainly reads better if you are used to Lua, but the magic involved needs some explanation. You may imagine that Orbit has introduced a whole bunch of little functions into the module, but this is not how it is done.  Instead, <strong>orbit.htmlify</strong> modifies the environment of the function <strong>generate</strong> so that any undefined variable is assumed to be a HTML tag and becomes a suitable function. So you can generate anything, even with unknown tags. So if <strong>generate</strong> was:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> generate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> sensors <span style="color: #66cc66;">&#123;</span>
            sensor <span style="color: #66cc66;">&#123;</span>name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;one&quot;</span><span style="color: #66cc66;">&#125;</span>,
            sensor <span style="color: #66cc66;">&#123;</span>name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;two&quot;</span><span style="color: #66cc66;">&#125;</span>,
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>Then the result would be this syntactically valid XML:</p>

<pre><code>&lt;sensors &gt;&lt;sensor name="one"&gt;&lt;/sensor&gt;
&lt;sensor name="two"&gt;&lt;/sensor&gt;&lt;/sensors&gt;
</code></pre>

<p>In presenting the resulting markup I&#8217;ve inserted a few line breaks for making reading a bit easier.  Naturally you might like to <a href="http://n2.nabble.com/Orbit-htmlify-beautification-tc3889818.html#a3889818">beautify</a> the output for debugging purposes; web browsers do not care. <a href="http://infohound.net/tidy/">HTML Tidy</a> is another option. which will also give you a host of other diagnostic checks.</p>

<p>One problem to watch out for is that a mistyped tag name will give you odd HTML, not a Lua error. And there are some HTML tags which are valid Lua globals, such as <strong>table</strong> &#8211; the solution is to say <strong>H(&#8216;table&#8217;)</strong>, etc.  But HTML can now be generated without making your eyes bleed, as with the ugly web variables script. This little snippet uses the fact that tag functions take table arguments:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> generate <span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">local</span> list <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">for</span> name,value <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">pairs</span><span style="color: #66cc66;">&#40;</span>web.GET<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
            <span style="color: #b1b100;">table.insert</span><span style="color: #66cc66;">&#40;</span>list,li<span style="color: #66cc66;">&#40;</span>name..<span style="color: #ff0000;">&quot; = &quot;</span>..value<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>
        <span style="color: #b1b100;">return</span> ul<span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>Any technique can produce over-elaborate code, as this program which generates an HTML form formatted with a table:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    <span style="color: #b1b100;">function</span> wrap <span style="color: #66cc66;">&#40;</span>inner<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> html<span style="color: #66cc66;">&#123;</span> head<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, body<span style="color: #66cc66;">&#40;</span>inner<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> test <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> wrap<span style="color: #66cc66;">&#40;</span>form <span style="color: #66cc66;">&#40;</span>H<span style="color: #ff0000;">'table'</span> <span style="color: #66cc66;">&#123;</span>
            tr<span style="color: #66cc66;">&#123;</span>td<span style="color: #ff0000;">&quot;First name&quot;</span>,td<span style="color: #66cc66;">&#40;</span> input<span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text'</span>, name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'first'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>,
            tr<span style="color: #66cc66;">&#123;</span>td<span style="color: #ff0000;">&quot;Second name&quot;</span>,td<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text'</span>, name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'second'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>,
            tr<span style="color: #66cc66;">&#123;</span> td<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'submit'</span>, value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Submit!'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>,
                td<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'submit'</span>,value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Cancel'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#125;</span>,
        <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
&nbsp;
    orbit.htmlify<span style="color: #66cc66;">&#40;</span>wrap,test<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>Not quite eye-bleeding, but getting there!  The solution is to capture common patterns in a library. The <strong>util</strong> module in the <strong>basic</strong> folder makes for much more readable code:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> show_form <span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> wrap<span style="color: #66cc66;">&#40;</span>form <span style="color: #66cc66;">&#123;</span>
            htable <span style="color: #66cc66;">&#123;</span>
                <span style="color: #66cc66;">&#123;</span>text<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;First name&quot;</span>,<span style="color: #ff0000;">'first'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>,
                <span style="color: #66cc66;">&#123;</span>text<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Second name&quot;</span>,<span style="color: #ff0000;">'second'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>,
                <span style="color: #66cc66;">&#123;</span>submit <span style="color: #ff0000;">'Submit'</span>, submit <span style="color: #ff0000;">'Cancel'</span><span style="color: #66cc66;">&#125;</span>,
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<h2>Databases the Orbit Way</h2>

<p>Orbit can create an Object-Relational Mapping (ORM) between the tables of a relational database and Lua tables. The restriction is that the SQL table is required to have a numeric key called <strong>id</strong>. In the following discussion, we must switch between two very different meanings of the word <strong>table</strong>, so I&#8217;ll use &#8216;array&#8217; to mean &#8216;an array-like Lua table&#8217; and &#8216;map&#8217; to mean &#8216;a hash-like Lua table&#8217;.</p>

<p>Consider the following SQL table definition found in the <strong>blog</strong> example database:</p>

<pre><code>CREATE TABLE blog_post
       ("id" INTEGER PRIMARY KEY NOT NULL,
       "title" VARCHAR(255) DEFAULT NULL,
       "body" TEXT DEFAULT NULL,
       "n_comments" INTEGER DEFAULT NULL,
       "published_at" DATETIME DEFAULT NULL);
</code></pre>

<p>This code uses LuaSQL&#8217;s sqlite3 driver to dump out a particular row of this table by defining a mapper:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">-- ORM1.lua</span>
    <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">&quot;orbit&quot;</span>
    <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">&quot;luasql.sqlite3&quot;</span>
    <span style="color: #b1b100;">local</span> env <span style="color: #66cc66;">=</span> luasql.sqlite3<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">function</span> dump <span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">if</span> #t <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">then</span>
            <span style="color: #b1b100;">for</span> _,row <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">ipairs</span><span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
                dump<span style="color: #66cc66;">&#40;</span>row<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">end</span>
        <span style="color: #b1b100;">else</span>
            <span style="color: #b1b100;">for</span> field,value <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">pairs</span><span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
                <span style="color: #b1b100;">if</span> <span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">'string'</span> <span style="color: #b1b100;">then</span>
                    value <span style="color: #66cc66;">=</span> value:sub<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">end</span>
                <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>field,<span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span>,value<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">end</span>
            <span style="color: #b1b100;">print</span> <span style="color: #ff0000;">'-----'</span>
        <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    mapper <span style="color: #66cc66;">=</span> orbit.model.new<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    mapper.conn <span style="color: #66cc66;">=</span> env:connect<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;../blog/blog.db&quot;</span><span style="color: #66cc66;">&#41;</span>
    mapper.driver <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;sqlite3&quot;</span>
    mapper.table_prefix <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'blog_'</span>
&nbsp;
    posts <span style="color: #66cc66;">=</span> mapper:new <span style="color: #ff0000;">'post'</span>  <span style="color: #808080; font-style: italic;">-- maps to blog_post table</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- print out the second post</span>
    second <span style="color: #66cc66;">=</span> posts:find<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    dump<span style="color: #66cc66;">&#40;</span>second<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">==&gt;</span>
    published_at	number	<span style="color: #cc66cc;">1096923480</span>
    title	<span style="color: #b1b100;">string</span>	The Care And Feeding Of Your Sleeping Bicycle
    id	number	<span style="color: #cc66cc;">2</span>
    n_comments	number	<span style="color: #cc66cc;">0</span>
    body	<span style="color: #b1b100;">string</span>
    Thunder, thunder, thundercats, Ho<span style="color: #66cc66;">!</span> Thundercats are on the m
    <span style="color: #808080; font-style: italic;">-----</span></pre></div></div>


<p><strong>posts:find(2)</strong> gets the database row with <strong>id</strong> equal to 2 and returns a map with corresponding fields and values.</p>

<p>SQL is a more strongly-typed language than Lua, so that both <strong>published_at</strong> and <strong>n_comments</strong> are mapped to the <strong>number</strong> type. However, the mapper does preserve this information in its field <strong>meta</strong>; for instance, <strong>mapper.meta.published_at.type</strong> is &#8216;datetime&#8217;.</p>

<p>Orbit&#8217;s ORM mappers simplify database access by bridging the gap between the underlying database and the language; the most involved code here is just to dump out Lua maps and arrays.</p>

<p>As always, the interactive prompt is the best way to explore the available mapper methods:</p>

<pre><code>C:\basic&gt;lua -lmapper
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
&gt; posts = mapper:new 'post'
&gt; dump(posts:find_first('n_comments &gt; 0'))
published_at    number  1097769720
title   string  'Star Wars' As Written By A Princess
id      number  4
n_comments      number  1
body    string
Ten years ago a crack commando unit was sent to prison by a
-----
</code></pre>

<p><strong>find_first</strong> also returns a row, but by an explicit condition; <strong>find_all</strong> is similar, but returns an array of all matching rows. Note that the condition is a SQL expression, not Lua; this is effectively a more friendly way to create an SQL query:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    dump<span style="color: #66cc66;">&#40;</span>posts:find_all<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id = ? or id = ?&quot;</span>,
        <span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span>,order <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;published_at asc&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>ORM mapping works particularly nicely with dynamic languages, but there are some downsides.  consider the case that the table <strong>blog_post</strong> had many thousands of rows; the default mapper will grab all the columns, including the potentially rather large <strong>body</strong>. So opportunities for optimizing queries are lost, if we were only interested in searching the title for a match. It&#8217;s possible to reorganize things so that <strong>blog_post</strong> only contained &#8216;meta&#8217; data about each post (plus perhaps a short summary) and a new table <strong>blog_text</strong> would contain the actual text of each post, indexable using the same id.</p>

<h2>Putting it Together</h2>

<p>The next example will bring together these three concerns: request pattern matching (Controller), ORM mapping to a database (Model) and Orbit htmlification (View). It uses the database from the blog example but presents it in summary form.  For each row, it puts out a nicely formatted summary.  The actual rows are selected by a user set of keywords.</p>

<p>All our HTML-generating functions begin with &#8216;render_&#8217;. <strong>orbit.htmlify</strong> can be called in another way, where you give the module first, and then specify a string pattern to match the functions in the module that you want to htmlify.</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">	orbit.htmlify<span style="color: #66cc66;">&#40;</span>blog1,<span style="color: #ff0000;">'render_.+'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>Initially, the actual model-controller part of the application is simply this:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> index<span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> render_index<span style="color: #66cc66;">&#40;</span>posts:find_all<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    blog1:dispatch_get<span style="color: #66cc66;">&#40;</span>index, <span style="color: #ff0000;">&quot;/&quot;</span>, <span style="color: #ff0000;">&quot;/index&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>And the view part:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">function</span> limit <span style="color: #66cc66;">&#40;</span>s,maxlen<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">if</span> #s <span style="color: #66cc66;">&gt;</span> maxlen <span style="color: #b1b100;">then</span>
            <span style="color: #b1b100;">return</span> s:sub<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,maxlen<span style="color: #66cc66;">&#41;</span>..<span style="color: #ff0000;">'...'</span>
        <span style="color: #b1b100;">else</span>
            <span style="color: #b1b100;">return</span> s
        <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_page<span style="color: #66cc66;">&#40;</span>contents<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> html <span style="color: #66cc66;">&#123;</span>
            head <span style="color: #66cc66;">&#123;</span> title <span style="color: #ff0000;">'Blog Example'</span> <span style="color: #66cc66;">&#125;</span>,
            body <span style="color: #66cc66;">&#40;</span>contents<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_post<span style="color: #66cc66;">&#40;</span>post<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> div <span style="color: #66cc66;">&#123;</span>
            h3 <span style="color: #66cc66;">&#40;</span>post.id .. <span style="color: #ff0000;">' '</span> .. post.title<span style="color: #66cc66;">&#41;</span>,
            p <span style="color: #66cc66;">&#40;</span>limit<span style="color: #66cc66;">&#40;</span>post.body,<span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
            em<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">os.date</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'%a %b %d %H:%M %Y'</span>,post.published_at<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
            <span style="color: #ff0000;">' '</span>,post.n_comments,<span style="color: #ff0000;">' comments'</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_index<span style="color: #66cc66;">&#40;</span>posts<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">local</span> res <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">for</span> i,post <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">ipairs</span><span style="color: #66cc66;">&#40;</span>posts<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
            res<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> render_post<span style="color: #66cc66;">&#40;</span>post<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>
        <span style="color: #b1b100;">return</span> render_page<span style="color: #66cc66;">&#40;</span>res<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>The heart of the view is <strong>render_post</strong>, which formats each row from the model. You will only achieve date formatting happiness once you have learned the formating characters for <strong>os.date</strong>; these are the same as those used in the C <a href="http://msdn.microsoft.com/en-us/library/fe06s4ak%28VS.80%29.aspx">strftime</a> function.</p>

<p>All in all, not bad for a 55-line program. The next step is to add simple keyword searching, which takes an extra line:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> index<span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">local</span> keyword <span style="color: #66cc66;">=</span> web.GET.keyword <span style="color: #b1b100;">or</span> <span style="color: #ff0000;">''</span>
      <span style="color: #b1b100;">if</span> keyword <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">''</span> <span style="color: #b1b100;">then</span> <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
      <span style="color: #b1b100;">else</span>
        <span style="color: #b1b100;">return</span> render_index<span style="color: #66cc66;">&#40;</span>posts:find_all<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title like ?'</span>,<span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'%'</span>..keyword..<span style="color: #ff0000;">'%'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>The SQL <strong>like</strong> operator works with patterns, so that finding a title containing the word &#8216;Chicken&#8217; would be &#8220;title like &#8216;%Chicken%&#8217;&#8221;, where &#8216;%&#8217; means &#8216;any sequence of characters&#8217;.  The keyword is passed as a GET request variable: enter the following URL to see the results of the search: <strong>http://localhost:8080/?keyword=Chicken</strong>.</p>

<p>It would be useful to have <em>some</em> user interface, especially when we start passing multiple keywords.  <strong>render_index</strong> becomes:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> render_index<span style="color: #66cc66;">&#40;</span>posts<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">local</span> res <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
        append<span style="color: #66cc66;">&#40;</span>res,p <span style="color: #66cc66;">&#40;</span>form <span style="color: #66cc66;">&#123;</span>
            input <span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text'</span>,name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'keyword'</span><span style="color: #66cc66;">&#125;</span>,
            input <span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'submit'</span>,value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Submit'</span><span style="color: #66cc66;">&#125;</span>,
        <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        append<span style="color: #66cc66;">&#40;</span>res,hr<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        append<span style="color: #66cc66;">&#40;</span>res,h2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Found %d posts'</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>#posts<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">for</span> _,post <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">ipairs</span><span style="color: #66cc66;">&#40;</span>posts<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
            append<span style="color: #66cc66;">&#40;</span>res,render_post<span style="color: #66cc66;">&#40;</span>post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>
        <span style="color: #b1b100;">return</span> render_page<span style="color: #66cc66;">&#40;</span>res<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>By default, HTML forms submit their values to the same URL as the page, so this works fine.</p>

<p>This introduction was intended to help you begin reading the Orbit samples, documentation and tutorials. In the next in this series, we will discuss building more complicated applications, caching generated pages, and Orbit Pages, which is a powerful Lua-powered template engine for generating HTML.</p>
]]></content:encoded>
			<wfw:commentRss>http://luanova.org/orbit1-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lua and the web: an overview</title>
		<link>http://luanova.org/lua-web-overview/</link>
		<comments>http://luanova.org/lua-web-overview/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 01:50:03 +0000</pubDate>
		<dc:creator>nathany</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Kepler]]></category>
		<category><![CDATA[webserver]]></category>
		<category><![CDATA[Wiki]]></category>

		<guid isPermaLink="false">http://luanova.wordpress.com/?p=11</guid>
		<description><![CDATA[Lua is among the top 20 most popular programming languages, according to the TIOBE Programming Community index. Lua is also faster and has a smaller memory footprint than other interpreted scripting languages (compare with Ruby, Python, PHP and JavaScript SpiderMonkey). We haven&#8217;t heard a lot about Lua on the web, even though there are a [...]]]></description>
			<content:encoded><![CDATA[<p>Lua is among the top 20 most popular programming languages, according to the <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html">TIOBE Programming Community index</a>. Lua is also faster and has a smaller memory footprint than other interpreted scripting languages (compare with <a href="http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&amp;lang=lua&amp;lang2=ruby">Ruby</a>, <a href="http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&amp;lang=lua&amp;lang2=python">Python</a>, <a href="http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&amp;lang=lua&amp;lang2=php">PHP</a> and <a href="http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&amp;lang=lua&amp;lang2=javascript">JavaScript SpiderMonkey</a>).</p>

<p>We haven&#8217;t heard a lot about Lua on the web, even though
there are a number of developers working with Lua on the server-side. Lets take a brief look.</p>

<h2>The Kepler Project</h2>

<p><a href="http://www.keplerproject.org/">Kepler</a> is a &#8220;web development platform&#8221; named after astronomer Johannes Kepler. It looks to be the most ambitious project of the bunch. Kepler is written as a number of modules that fit together, and includes its&#8217; own web server named Xavante.</p>

<p>There is also an endeavor to build <em>WSAPI,</em> in similar fashion to
Python&#8217;s <a href="http://www.python.org/dev/peps/pep-0333/">WSGI</a> interface, that support pretty much any web server and configuration (CGI, FastCGI, SCGI, etc, etc.)</p>

<h2>Nanoki</h2>

<p>When I joined to Kepler mailing list, &#8220;Petite Abeille&#8221; was the first person to respond. He has been developing a Wiki called <a href="http://alt.textdrive.com/nanoki/">Nanoki</a>.</p>

<p>However, it is not built on top of Kepler. Instead there is a small built-in web server or will run atop the Unix tcp command. All the source code is released under a MIT license, and is instructive in building a light, focussed web application vs. the broad scope of Kepler.</p>

<h2>mod_wombat</h2>

<p><a href="http://kasparov.skife.org/blog/">Brian McCallister</a> spear-headed a project to embed Lua in an Apache HTTP Server module. Going through Brian&#8217;s <a href="http://kasparov.skife.org/wombat_ac_us_07.pdf">slides</a> is quite inspiring as to just how good a fit this is. Apache does all the heavy-lifting, such as providing a fully multi-threaded &#8220;worker MPM&#8221; (multi-processing module) and a portable runtime (APR) environment. Lua is a thread-safe language, with &#8220;share-nothing&#8221; Lua <em>states</em>, so it fits right in and is super-efficient.</p>

<p>Skimming through <a href="http://www.informit.com/store/product.aspx?isbn=0132409674">The Apache Modules Book</a> is even more inspiring, as you can see the potential of mod_wombat if it took advantage of Apache&#8217;s database connection pooling (DBD) and the various other facilities of Apache HTTP Server.</p>

<p>Wombat is still a little rough around the edges, but you can find the Apache-licensed source code at:
<a href="http://svn.apache.org/repos/asf/httpd/mod_wombat/trunk">http://svn.apache.org/repos/asf/httpd/mod_wombat/trunk</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://luanova.org/lua-web-overview/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Launch: getting Lua installed</title>
		<link>http://luanova.org/launch-getting-lua-installed/</link>
		<comments>http://luanova.org/launch-getting-lua-installed/#comments</comments>
		<pubDate>Sun, 11 Nov 2007 06:47:35 +0000</pubDate>
		<dc:creator>nathany</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[Mac OS X]]></category>

		<guid isPermaLink="false">http://luanova.org/2007/11/10/launch-getting-lua-installed/</guid>
		<description><![CDATA[To play with Lua you need to download the source code and compile it. This isn&#8217;t as scary as it sounds, as Lua is a positively tiny download and doesn&#8217;t depend on anything more than a C compiler. Lua is written in standard ANSI C, so any one should work. I will give instructions for [...]]]></description>
			<content:encoded><![CDATA[<p>To play with Lua you need to download the source code and compile it. This isn&#8217;t as scary as it sounds, as Lua is a positively <em>tiny</em> download and doesn&#8217;t depend on anything more than a C compiler. Lua is written in standard ANSI C, so any one should work. I will give instructions for <strong>Mac OS X</strong>, because that&#8217;s what I use.</p>

<h2>Building</h2>

<p>First, you need to have Apple&#8217;s Developer Tools installed. They should be your Mac OS X disc, under Optional Installs -&gt; Xcode Tools. Run the XcodeTools package to install. You can also <a href="http://developer.apple.com/tools/download/">download Xcode</a> if you sign up for a free ADC account (but note: they are large!).</p>

<p>Next, <a href="http://www.lua.org/download.html">download Lua source code</a> and unpack the archive in Finder. Or you can use <strong>Terminal</strong> to download the current  release (5.1.2) to your Downloads folder (or a suitable location of your choice):</p>

<p><code>cd Downloads
curl -O http://www.lua.org/ftp/lua-5.1.2.tar.gz
tar xzvf lua-5.1.2.tar.gz
cd lua-5.1.2</code></p>

<p>Either way, you need <strong>Terminal</strong> (from Application/Utilities) to do the rest. Make sure you are in the lua-5.1.2/ folder, and type:
<code>make macosx</code></p>

<p>Several lines should scroll by. On Leopard you will see some <em>deprecated</em> warnings in loadlib.c. Don&#8217;t worry about it. To make sure everything is okay, run:
<code>make test</code>
You should see <strong>&#8220;Hello world, from Lua 5.1!&#8221;</strong></p>

<h2>Installing</h2>

<p>It is possible to run the Lua interpreter from right here, but let&#8217;s install it the rest of the way. For this we will need to run &#8220;make install&#8221;. By default Lua installs to /usr. This will work, but it&#8217;s recommended to install under /usr/local.</p>

<p>So from the lua-5.1.2/ folder, run:
<code>sudo make install INSTALL_TOP=/usr/local</code>
and provide your password.</p>

<p>It appears that Leopard ships with /usr/local/bin in your path. You can check by running: (make sure PATH is uppercase)
<code>env | grep PATH</code></p>

<p>If you don&#8217;t see it there, you need to modify your .profile file.
<code>pico ~/.profile</code></p>

<p>and include:</p>

<p><code>export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
export MANPATH="/usr/local/man:$MANPATH"</code></p>

<p>If you are using <a href="http://macromates.com">TextMate</a>, and will be installing the Lua bundle later, you may also want to include:</p>

<p><code>export SVN_EDITOR="mate -w"
export LC_CTYPE=en_US.UTF-8</code></p>

<p>With pico, press Ctrl-X followed by Y to exit and save.</p>

<p>The changes will take affect when you open a new Terminal window.</p>

<h2>Running</h2>

<p>With Lua installed in your path, you can run it in Terminal from any folder. Just type:
<code>lua</code></p>

<p>This brings up the interactive interpreter. You can type in Lua code:
<code>print "Hi"</code>
<code>= 2 + 3</code></p>

<p>Press <strong>Ctrl-C</strong> to exit when you&#8217;re done.</p>

<p>Now that Lua is installed, you <em>could</em> remove the Downloads/lua-5.1.2 folder. But you may want to check out the test/ folder for some example code. You can run these examples from within the lua-5.1.2/test/ folder like this:</p>

<p><code>lua factorial.lua</code></p>

<p>A local copy of the <a href="http://www.lua.org/manual/5.1/">Reference Manual</a> can be found under doc/manual.html.</p>

<h2>TextMate</h2>

<p>If you are using <a href="http://macromates.com/">TextMate</a>, there is a Lua bundle to give it color syntax highlighting and a few snippets. Unfortunately the Lua bundle isn&#8217;t included by default. You can either use <a href="http://projects.validcode.net/getbundle">GetBundle</a>, or you can grab it with the command line as follows.</p>

<p>The bundles are stored in a Subversion repository, which requires Subversion (svn) on your computer. Leopard comes with Subversion, but for Tiger or prior you need to install it. I&#8217;ve had good success with <a href="http://homepage.mac.com/martinott/">Martin Ott&#8217;s Subversion package</a>.</p>

<p>As per the <a href="http://macromates.com/textmate/manual/bundles#getting_more_bundles">TextMate manual</a>:</p>

<p><code>mkdir -p /Library/Application\ Support/TextMate/Bundles</code>
<code>cd /Library/Application\ Support/TextMate/Bundles</code>
<code>svn co http://svn.textmate.org/trunk/Bundles/Lua.tmbundle</code></p>

<p>You need to restart TextMate or navigate to to Bundles -&gt; Bundle Editor -&gt; <strong>Reload Bundles</strong> in the menu.</p>

<p>Files with a .lua extension will be associated with the Lua language bundle. One nice feature is that you can press Command-R to run them.</p>

<p>So that&#8217;s about it for getting setup. Now we just need to write some code!</p>
]]></content:encoded>
			<wfw:commentRss>http://luanova.org/launch-getting-lua-installed/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A computer scientist, a mathematician and an engineer&#8230;</title>
		<link>http://luanova.org/a-computer-scientist-a-mathematician-and-an-engineer/</link>
		<comments>http://luanova.org/a-computer-scientist-a-mathematician-and-an-engineer/#comments</comments>
		<pubDate>Sun, 11 Nov 2007 02:44:33 +0000</pubDate>
		<dc:creator>nathany</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[history of programming languages]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://luanova.org/2007/11/10/a-computer-scientist-a-mathematician-and-an-engineer/</guid>
		<description><![CDATA[The Lua programming language was developed by three members of Tecgraf, the Computer Graphics Technology Group of PUC-Rio, a University in Rio de Janeiro. Not merely an academic pursuit, it was developed primarily as a replacement for SOL (meaning &#8220;sun&#8221;) and DEL (data entry language) for Brazilian oil company Petrobras. Lua was started in 1993, [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>Lua programming language</strong> was developed by <a href="http://www.lua.org/authors.html">three members</a> of Tecgraf, the Computer Graphics Technology Group of PUC-Rio, a University in Rio de Janeiro. Not merely an academic pursuit, it was developed primarily as a replacement for SOL (meaning &#8220;sun&#8221;) and DEL (data entry language) for Brazilian oil company Petrobras.</p>

<p>Lua was started in 1993, near the same time as Matz began working on Ruby. It was designed to be a small and efficient scripting language that could easily be embedded in C/C++ on practically any platform.</p>

<p>Aiming for <strong>simplicity</strong>, Lua implements a single collection type called &#8220;tables&#8221;. PHP also takes this approach, with Arrays that can act as hashes (associative arrays) and standard arrays. Python uses its dictionaries (hashes) for namespaces and passing named parameters to functions. Lua does this and more, making extensive use of tables.</p>

<p>One of Lua&#8217;s precepts is to <em>&#8220;provide mechanisms instead of policies.&#8221;</em> You won&#8217;t find Ruby&#8217;s emphasis on object-oriented programming in Lua, but it still is easy enough to <em>OOP</em>. In fact, Lua works somewhat like the prototype system in today&#8217;s JavaScript.</p>

<p>First-class functions allow functions to be passed as arguments, stored in tables, and basically treated like any other value. Proper tail calls allow for recursion without stack overflows. Closures are a bit hard to explain without an example. You are free to make use of all these mechanisms, which originated in <strong>functional programming</strong>, a distinct discipline from the typical C-style of programming.</p>

<p>Lua isn&#8217;t alone in mixing programming paradigms, but its overall simplicity as a language makes it a good place to <strong>begin</strong> computer programming.</p>

<p>In 1996, Lua found its way into <em>Dr. Dobb&#8217;s Journal</em> and from there into adventure game Grim Fandango and <a href="http://www.lua.org/uses.html">many other games</a> since the 1999 Game Developers&#8217; Conference, including the ever-popular World of Warcraft.</p>

<p>Lua has grown in response to the demands of game programmers. It now posesses an <strong>incremental</strong> garbage collector to avoid long pauses. Coroutines provide programmers with <strong>co-operative multitasking</strong>. Lua&#8217;s virtual machine is register-based, making it one of the fastest interpreted languages available.</p>

<p>Due to a rather strict adherence to ANSI C, Lua doesn&#8217;t run multi-core out of the box. But Lua can take advantage of your OS-dependent threading code, having a fully reentrant API. <strong>Lua states</strong> isolate each thread, so scripters don&#8217;t need to concern themselves with the typical locking complexities of concurrency. <ins datetime="2007-11-11T04:32:08+00:00">Perhaps a future version of</ins> Lua <del datetime="2007-11-11T04:45:14+00:00">5.2</del> could see an <em>optional</em> library with OS-specific threading support.</p>

<p>If you want to dig deep into Lua&#8217;s history, take a look at <a href='http://luanova.files.wordpress.com/2007/11/hopl.pdf' title='hopl.pdf'>The Evolution of Lua</a> paper (26 pages), as was presented at the 2007 History of Programming Languages (HOPL) Conference. I will conclude with a quote from the paper:</p>

<blockquote>&#8220;<em>It is much easier to add features later than to remove them.</em> This development process has been essential to keep the language simple, and <strong>simplicity is our most important asset</strong>. Most other qualities of Lua &#8212; speed, small size, portability &#8212; derive from its simplicity.&#8221;</blockquote>

<p>And contrast it with a quote from Yukihiro &#8220;Matz&#8221; Matsumoto from <em>Beautiful Code</em>:</p>

<blockquote>&#8220;When simpler tools are used to solve a complex problem, complexity is merely shifted to the programmer, which is really putting the cart before the horse.&#8221;</blockquote>

<p>While there is validity to his statement, <strong>the beauty of Lua</strong> is how it manages to be a quite powerful language, despite its simplicity.</p>
]]></content:encoded>
			<wfw:commentRss>http://luanova.org/a-computer-scientist-a-mathematician-and-an-engineer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Lua: how we first met</title>
		<link>http://luanova.org/3/</link>
		<comments>http://luanova.org/3/#comments</comments>
		<pubDate>Sun, 04 Nov 2007 14:08:09 +0000</pubDate>
		<dc:creator>nathany</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Adobe Lightroom]]></category>
		<category><![CDATA[coroutines]]></category>

		<guid isPermaLink="false">http://luanova.wordpress.com/2007/11/09/3/</guid>
		<description><![CDATA[This past summer I evaluated both Apple Aperture and Adobe Lightroom for managing my digital photographs. You see, iPhoto 6 was getting a bit sluggish, Photoshop Elements even more so (not yet having Intel support). To top it off, I was considering a Digital SLR, so I&#8217;d need something that could read RAW. Adobe Lightroom [...]]]></description>
			<content:encoded><![CDATA[<p><img src='http://luanova.files.wordpress.com/2007/11/moonlove.png' alt='moonlove.png' align='right' alt='Minnie and Mickey mouse in love on the moon' />
This past summer I evaluated both Apple Aperture and Adobe Lightroom for managing my digital photographs. You see, iPhoto 6 was getting a bit sluggish, Photoshop Elements even more so (not yet having Intel support). To top it off, I was considering a Digital SLR, so I&#8217;d need something that could read RAW.</p>

<p>Adobe Lightroom won out for me. It provided enough of the Photoshop functionality (curves, etc.) that I often wouldn&#8217;t need to launch Elements. Just as important, the reviews proclaimed Lightroom to be more responsive and less of a resource hog. <em>So what does all this have to do with <strong>Lua?</strong></em></p>

<p>Adobe Lightroom is programmed with C/C++/Objective-C and&#8230; <strong>40% Lua</strong>. An interpreted scripting language making up 40% of the code, and it feels more responsive and uses less resources?! That deserves a second look&#8230;</p>

<p>Adobe Lightroom puts Lua&#8217;s coroutines to good use, keeping the user interface responsive while images visually rotate or exports process. Coroutines are a form of &#8220;cooperative multitasking,&#8221; where the programmer controls task-switching rather then being preempted as with &#8220;real&#8221; threads.</p>

<p>I&#8217;ve taken a great interest in the Lua language. The more I learn, the more intrigued I am. This blog exists in order to share these findings with you. <strong>Lua</strong> is Portuguese for moon, and &#8220;lua nova&#8221; means <em><strong>new moon</strong></em>.</p>

<p>Welcome to Lua, welcome to the moon&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://luanova.org/3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
