<?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/tag/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>Sputnik: An Introduction I</title>
		<link>http://luanova.org/sputnik/</link>
		<comments>http://luanova.org/sputnik/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 06:08:07 +0000</pubDate>
		<dc:creator>stevejdonovan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[web frameworks]]></category>
		<category><![CDATA[Wiki]]></category>

		<guid isPermaLink="false">http://luanova.org/?p=21</guid>
		<description><![CDATA[Beyond WikiWiki Sputnik is a second-generation extensible wiki engine written in Lua. First generation wikis (like the original WikiWIki) opened our eyes to the possibility of easy collaborative content generation, with automatic revision control. However, anybody who has been involved with a Wiki knows that they are not self-organizing, and so behind any Wiki is [...]]]></description>
			<content:encoded><![CDATA[<h2>Beyond WikiWiki</h2>

<p><a href="http://sputnik.freewisdom.org/">Sputnik</a> is a second-generation extensible wiki engine written in Lua. First generation wikis (like the original <a href="http://c2.com/cgi/wiki">WikiWIki</a>) opened our eyes to the possibility of easy collaborative content generation, with automatic revision control.  However, anybody who has been involved with a Wiki knows that they are not self-organizing, and so behind any Wiki is a core of busy elves correcting and &#8216;refactoring&#8217; content.  Plus, all content is usually in the form of marked-up text, plus uploaded binary data like images.  For instance,  Wiki pages often turn into discussions, but there is usually no way to structure these discussions and no support for creating them.</p>

<p>A second-generation wiki allows for &#8216;virtual&#8217; pages, pages with explicit data fields, structured discussions, complete control of permissions, and namespace management.  The site designer can choose the right balance between freedom and structure that is appropriate for the community and its common purpose.</p>

<h2>Frameworks and Libraries</h2>

<p>There are two basic approaches to software development; either start small, pulling as much functionality in using libraries, or to start with a framework, which does most of the job, and customize it to fit your functionality.  Frameworks have a bad <a href="http://discuss.joelonsoftware.com/default.asp?joel.3.219431.12">reputation</a> because framework developers can get just a little mad in the process of writing them.  But if you want something that can do wiki-like things, manage the content and control the revisions, handle authentication, then it&#8217;s time to get a Wiki framework, because these are not easy applications to get right. As the <a href="http://gitorious.org/sputnik?page=4">Sputnik git page</a> says, &#8220;Sputnik provides a good foundation for anything that&#8217;s kind of like a wiki but not quite.&#8221;</p>

<p>It is true that the first trade-off is the freedom to do things <em>your</em> way, since you must learn how the framework does things, and cooperate with it. In a way, it is like collaboration with another developer on a project which you have just joined.  I am assuming here that you want to get something done, which is not a million miles from a Wiki, and don&#8217;t want to rewrite a whole bunch of wheels, just maybe update the hubcaps.  A better analogy would be this: if you want to equip a kitchen, then using a framework that includes the kitchen sink is appropriate (and not just a joke). Maybe you just want different fittings for your kitchen sink.</p>

<h2>Getting Sputnik</h2>

<p><a href="http://sputnik.freewisdom.org/en/Installation">Installing Sputnik</a> is straightforward, providing you are on a Unix-like platform (like Linux or OS X) although it does also run on Windows.  In this introduction, I assume that it is an Unix environment (although out of convenience, not religious fervour; it is very easy to get a Linux virtual machine and set it up for Sputnik testing.)   After following instructions, you will have a Sputnik install in <strong>~/sputnik</strong>, no special permissions necessary.  Starting the webserver is then just:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  ~<span style="color: #000000; font-weight: bold;">/</span>sputnik$ .<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>sputnik.lua start-xavante sputnik.ws</pre></div></div>


<p>Sputnik comes with the <a href="http://www.keplerproject.org">Kepler stack</a>, including the Lua webserver Xavante.  This is quite good enough for testing and experimentation.</p>

<p>A useful change is to first edit <strong>sputnik.ws</strong> and set <strong>BASE_URL</strong> to &#8216;/&#8217; and to add <strong>SHOW_STACK_TRACE = true</strong>. Sputnik will then give you Lua error traces when some hitch occurs. Then just open <strong>http://localhost:8080</strong> in your favourite browser and start playing. Create a special user <strong>Admin</strong> to view and edit the configuration nodes.</p>

<p>All Sputnik configuration is via nodes with Lua content. For instance, <strong>sputnik/config</strong>  assigns a set of fields to values. If you are editing this file (as Admin) and make a syntax error (such as <strong>&#8216;Sputnik&#8221;</strong>, mismatching string delimiters) the edit field will turn pink. <strong>sputnik/navigation</strong> controls the navigation bar menu:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  NAVIGATION <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
     <span style="color: #66cc66;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index&quot;</span>, title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Start&quot;</span>,
       <span style="color: #66cc66;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;snippets&quot;</span>, title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Snippets&quot;</span><span style="color: #66cc66;">&#125;</span>,
       <span style="color: #66cc66;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tags&quot;</span>, title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Tags&quot;</span><span style="color: #66cc66;">&#125;</span>,
       <span style="color: #66cc66;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sputnik&quot;</span>,title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Configure&quot;</span><span style="color: #66cc66;">&#125;</span>,
     <span style="color: #66cc66;">&#125;</span>,
     <span style="color: #66cc66;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;News&quot;</span>, title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Timeline&quot;</span>,
       <span style="color: #66cc66;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;News&quot;</span><span style="color: #66cc66;">&#125;</span>,
       <span style="color: #66cc66;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Future Plans&quot;</span><span style="color: #66cc66;">&#125;</span>,
       <span style="color: #66cc66;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;history&quot;</span>, title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Recent Wiki Edits&quot;</span><span style="color: #66cc66;">&#125;</span>,
       <span style="color: #66cc66;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;history/edits_by_recent_users&quot;</span>, title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Edits by Recent Users&quot;</span><span style="color: #66cc66;">&#125;</span>,
       <span style="color: #66cc66;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;history.rss&quot;</span>, title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;RSS Feed&quot;</span><span style="color: #66cc66;">&#125;</span>,
     <span style="color: #66cc66;">&#125;</span>,
  <span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>Creating a new node is easy; if you ask for a node <strong>test</strong> then it will tell you that this node does not exist, but then will give you several types to choose from.  The &#8216;Basic&#8217; type is a plain Wiki page, and the link provided is something like <strong>http://localhost:8080/?p=test.edit&amp;prototype=</strong>.  In general, a Sputnik request has a <em>node id</em> (&#8216;test&#8217;), an <em>action</em> (&#8216;edit&#8217;) and <em>parameters</em> (&#8216;prototype=&#8217;)</p>

<p>If you click on this link you can edit your new Wiki node.  The markup used is <a href="http://daringfireball.net/projects/markdown/">Markdown</a> which has a clean, readable syntax. In addition, Sputnik supports Wiki links; a link to your new page would be <strong>[[test]]</strong> and a link with some text would be <strong>[[test|My First Page]]</strong> . There is a convenient toolbar providing the most common operations when editing.</p>

<h2>Customizing Sputnik</h2>

<p>A lot can be done with basic Sputnik, just by editing the configuration nodes.  (And, yes, the configuration nodes are Wiki nodes, so you can revert to an earlier version.)  Also, like any modern Web framework, style and functionality are kept separate as CSS and HTML; these <a href="http://sputnik.freewisdom.org/en/Sightings">Sputnik sites</a> show that you can get just about any look and feel.</p>

<p>Since the configuration is done with Lua data, it <em>technically</em> involves programming, but not in any serious sense of the word.  Lua is particularly well-suited to expressing configuration, since it was originally conceived as a data-description language.  This data is converted into Lua table structures using Lua itself, using a <a href="http://lua-users.org/wiki/SandBoxes">sandbox</a> so that it will not execute any dangerous stuff.</p>

<p>The first &#8216;real&#8217; customization we will do is make a whole set of pages default to a particular node type. That is, any node like <strong>pages/GeneralInfo</strong> or <strong>pages/Introduction</strong> will be created as &#8216;Basic&#8217; nodes.</p>

<p>If Sputnik cannot find a node, it will first attempt to find a <em>node default</em> with that name.  The request for &#8216;pages&#8217; results in Sputnik attempting to load a Lua module like so: <strong>require &#8220;sputnik.node_defaults.pages&#8221;</strong>. So we have to create a file <strong>pages.lua</strong> in a directory so that Sputnik can resolve this module.</p>

<p>Sputnik itself is distributed as a <a href="www.luarocks.org">LuaRocks</a> application. In my system, the Sputnik package sits at <strong>~/sputnik/rocks/sputnik/9.03.16-0/lua</strong> &#8211; call this <strong>$SPUTNIK</strong>.  The relevant package directory structure is:</p>

<pre><code>    sputnik
        actions
        hooks
        node_defaults
        ....
</code></pre>

<p>It <em>is</em> possible to customize Sputnik by putting a suitable <strong>pages.lua</strong> in <strong>$SPUTNIK/sputnik/node_defaults</strong>, but that is a road of misery that will end in tears.  However, it&#8217;s useful to acquaint yourself with the contents of this directory because it defines the standard namespaces.</p>

<p>The best quick solution is to add directories to the <strong>LUA_PATH</strong> environment variable before launching Sputnik:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  ~<span style="color: #000000; font-weight: bold;">/</span>sputnik$ <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LUA_PATH</span>=<span style="color: #ff0000;">&quot;;;<span style="color: #007800;">$HOME</span>/sputnik/examples/?.lua&quot;</span></pre></div></div>


<p>Then create a directory <strong>~/sputnik/examples/sputnik/node_defaults</strong> containing this file, <strong>pages.lua</strong>:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  module<span style="color: #66cc66;">&#40;</span>..., package.seeall<span style="color: #66cc66;">&#41;</span>
&nbsp;
  NODE <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
     title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Pages&quot;</span>,
     content <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>,
     child_defaults <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
          any<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'prototype = &quot;&quot;'</span>
      <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
  <span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>It is structured as a Lua module, which contains a single exported table, <strong>NODE</strong> .  The definition of the field <strong>child_defaults</strong> may appear a little hairy at first, but it&#8217;s now time to get the three different ways to do string literals in Lua straight.  Whether you use single or double quotes for a string, does not matter; it is a convenience so that you can embed the other kind of quotes: <strong>&#8216;prototype = &#8220;&#8221;&#8216;</strong>. This string is then further embedded in a Lua &#8216;long string&#8217; literal.</p>

<p>After restarting Sputnik, go to <strong>pages</strong>: nothing much to see at this point &#8211; but note that Sputnik can find the node. If you go to <strong>pages/Introduction</strong> you will get another blank page with a title, which you can edit directly as a Wiki page.  The <strong>child_defaults</strong> field tells Sputnik that any &#8216;child&#8217; of <strong>pages</strong> like <strong>pages/Introduction</strong> has a particular prototype value which corresponds to the &#8216;Basic&#8217; node type.</p>

<p>If you enter this text</p>

<pre><code>    Some text for the Introduction node. See [[pages/Basic]]
</code></pre>

<p>and save, the result will have a link to a new page, which you can in turn edit.</p>

<p>To see how Sputnik saves pages by default, look at the <strong>~/sputnik/wiki-data</strong> directory. There will be a subdirectory <strong>pages%2FIntroduction</strong> with files <strong>000001</strong> and <strong>index</strong>. (The subdirectory is just the URL-encoded form of &#8216;pages/Introduction&#8217; ).  <strong>index</strong> will contain something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  add_version<span style="color: #66cc66;">&#123;</span>
  version   <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;000001&quot;</span>,
  timestamp <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;2009-11-01 14:33:07&quot;</span>,
  author    <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>,
  comment   <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>,
   <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;minor&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>,
   <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;ip&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;127.0.0.1&quot;</span>,
  <span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>And <strong>000001</strong> contains the node text:</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;pages/Introduction&quot;</span>
  category       <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>
  content        <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#91;</span>Some text <span style="color: #b1b100;">for</span> the Introduction node. See <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>pages<span style="color: #66cc66;">/</span>Basic<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
  <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#93;</span>
  breadcrumb     <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span></pre></div></div>


<p>As the node is edited, each revision is saved in a similar format, <strong>000002</strong>, etc.  In this way, edit history is managed in a simple way, easily readable by humans, as opposed to being stored in some relational database. However, using a database for storage is also supported by the Sputnik content manager, which is called <a href="http://sputnik.freewisdom.org/en/Saci">Saci</a>.</p>

<h2>Custom Output</h2>

<p>Before actually starting with code, it&#8217;s useful to have a handy shortcut to the Lua executable used by Sputnik.  I suggest creating a little executable script like this on your path:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  $<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> ~<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>slua
  ~<span style="color: #000000; font-weight: bold;">/</span>sputnik<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>lua -lluarocks.require <span style="color: #007800;">$*</span></pre></div></div>


<p>Before we actually get round to generating dynamic content, here is a quick review of <a href="http://cosmo.luaforge.net/">Cosmo</a>,  which is a powerful template engine used by Sputnik.</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  ~$ slua</pre></div></div>



<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  Lua 5.1.4  Copyright <span style="color: #66cc66;">&#40;</span>C<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1994</span>-<span style="color: #cc66cc;">2008</span> Lua.org, PUC-Rio
  <span style="color: #66cc66;">&gt;</span> <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">&quot;cosmo&quot;</span>
  <span style="color: #66cc66;">&gt;</span> template <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;$rank of $suit&quot;</span>
  <span style="color: #66cc66;">&gt;</span> values <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>rank<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Ace&quot;</span>,suit<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Spades&quot;</span><span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">=</span> cosmo.fill<span style="color: #66cc66;">&#40;</span>template,values<span style="color: #66cc66;">&#41;</span>
  Ace of Spades</pre></div></div>


<p>That&#8217;s useful, although only a little more friendly than Lua&#8217;s <strong>string.format</strong> function.  However, Cosmo goes way beyond simple <a href="http://lua-users.org/wiki/StringInterpolation">string interpolation</a>.</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  <span style="color: #808080; font-style: italic;">-- testcosmo.lua</span>
  <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">&quot;cosmo&quot;</span>
  template <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">==</span><span style="color: #66cc66;">&#91;</span>
  <span style="color: #66cc66;">&lt;</span>h1<span style="color: #66cc66;">&gt;</span>$list_name<span style="color: #66cc66;">&lt;/</span>h1<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;</span>ul<span style="color: #66cc66;">&gt;</span>
   $do_items<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&lt;</span>li<span style="color: #66cc66;">&gt;</span>$item<span style="color: #66cc66;">&lt;/</span>li<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&lt;/</span>ul<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">==</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
  <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>cosmo.fill<span style="color: #66cc66;">&#40;</span>template, <span style="color: #66cc66;">&#123;</span>
      list_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;My List&quot;</span>,
      do_items  <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #b1b100;">for</span> i<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">5</span> <span style="color: #b1b100;">do</span>
             cosmo.yield <span style="color: #66cc66;">&#123;</span> item <span style="color: #66cc66;">=</span> i <span style="color: #66cc66;">&#125;</span>
          <span style="color: #b1b100;">end</span>
      <span style="color: #b1b100;">end</span>
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>



<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  ~$ slua testcosmo.lua</pre></div></div>



<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">  &lt;h1&gt;My List&lt;/h1&gt;
  &lt;ul&gt;
   &lt;li&gt;1&lt;/li&gt;&lt;li&gt;2&lt;/li&gt;&lt;li&gt;3&lt;/li&gt;&lt;li&gt;4&lt;/li&gt;&lt;li&gt;5&lt;/li&gt;
  &lt;/ul&gt;</pre></div></div>


<p><em>Subtemplates</em> are a powerful feature which makes generating HTML straightforward.</p>

<p>Now, we create a node which creates custom HTML output.  First, put <strong>Memory.lua</strong> in your  <strong>node_defaults</strong> directory:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  module<span style="color: #66cc66;">&#40;</span>..., package.seeall<span style="color: #66cc66;">&#41;</span>
&nbsp;
  NODE <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
      title <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Lua Memory&quot;</span>,
      content <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>,
      actions <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
          show<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Memory.show_memory&quot;</span>
      <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
  <span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>Create a directory <strong>actions</strong> (that is, <strong>~/sputnik/examples/sputnik/actions</strong>) and put this <strong>Memory.lua</strong> in it:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  module<span style="color: #66cc66;">&#40;</span>..., package.seeall<span style="color: #66cc66;">&#41;</span>
&nbsp;
  actions <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">local</span> template <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#91;</span>
     <span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>Memory used by Lua is $mem kb<span style="color: #66cc66;">&lt;/</span>h2<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
  <span style="color: #b1b100;">function</span> actions.show_memory <span style="color: #66cc66;">&#40;</span>node, request, sputnik<span style="color: #66cc66;">&#41;</span>
      node.inner_html <span style="color: #66cc66;">=</span> cosmo.f<span style="color: #66cc66;">&#40;</span>template<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          mem <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'%6.0f'</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">collectgarbage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'count'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
      <span style="color: #66cc66;">&#125;</span>
      <span style="color: #b1b100;">return</span> node.wrappers.default<span style="color: #66cc66;">&#40;</span>node, request, sputnik<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">end</span></pre></div></div>


<p>The convention is that any <em>action</em> functions are in the <strong>actions</strong> directory; these functions must be in a nested <strong>actions</strong> table.  Visiting the node <strong>Memory</strong> will show the memory managed by Lua (as returned by the <strong>collectgarbage(&#8216;count&#8217;)</strong> call).</p>

<p>The node&#8217;s <strong>inner_html</strong> is the source for the node&#8217;s display area, that is, not including the menu and all other frame decorations.  <strong>node.wrappers.default</strong> is the actual function which generates the source for the <em>whole</em> page.</p>

<p>So, we now have a customized node with generated output.  Please note that it does not appear in <strong>wiki-data</strong>; the node is fully &#8216;virtual&#8217; and has no storage associated with it.</p>

<p>To take this example further, let us wrap the output of the <strong>ps</strong> command:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  ~$ <span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #660033;">--cols</span> <span style="color: #000000;">256</span>
  USER       PID <span style="color: #000000; font-weight: bold;">%</span>CPU <span style="color: #000000; font-weight: bold;">%</span>MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
  ...
  sdonovan  <span style="color: #000000;">2867</span>  <span style="color: #000000;">0.8</span>  <span style="color: #000000;">2.3</span>  <span style="color: #000000;">41972</span> <span style="color: #000000;">15932</span> pts<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1</span>    Sl   <span style="color: #000000;">10</span>:<span style="color: #000000;">47</span>   <span style="color: #000000;">3</span>:<span style="color: #000000;">57</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>sdonovan<span style="color: #000000; font-weight: bold;">/</span>lua<span style="color: #000000; font-weight: bold;">/</span>scite<span style="color: #000000; font-weight: bold;">/</span>SciTE
  sdonovan  <span style="color: #000000;">2902</span>  <span style="color: #000000;">1.2</span> <span style="color: #000000;">12.4</span> <span style="color: #000000;">179152</span> <span style="color: #000000;">83160</span> ?        Sl   <span style="color: #000000;">10</span>:<span style="color: #000000;">48</span>   <span style="color: #000000;">5</span>:<span style="color: #000000;">19</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>iceweasel<span style="color: #000000; font-weight: bold;">/</span>firefox-bin <span style="color: #660033;">-a</span> firefox
  sdonovan  <span style="color: #000000;">5993</span>  <span style="color: #000000;">0.0</span>  <span style="color: #000000;">0.4</span>   <span style="color: #000000;">5360</span>  <span style="color: #000000;">2844</span> pts<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2</span>    Ss   <span style="color: #000000;">16</span>:<span style="color: #000000;">13</span>   <span style="color: #000000;">0</span>:00 <span style="color: #c20cb9; font-weight: bold;">bash</span>
  sdonovan  <span style="color: #000000;">7118</span>  <span style="color: #000000;">0.0</span>  <span style="color: #000000;">0.3</span>   <span style="color: #000000;">5360</span>  <span style="color: #000000;">2024</span> pts<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>    S+   <span style="color: #000000;">18</span>:03   <span style="color: #000000;">0</span>:00 <span style="color: #c20cb9; font-weight: bold;">bash</span>
  sdonovan  <span style="color: #000000;">7119</span>  <span style="color: #000000;">0.4</span>  <span style="color: #000000;">1.1</span>   <span style="color: #000000;">8384</span>  <span style="color: #000000;">7380</span> pts<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>    S+   <span style="color: #000000;">18</span>:03   <span style="color: #000000;">0</span>:01 <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>sdonovan<span style="color: #000000; font-weight: bold;">/</span>sputnik<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>lua -lluarocks.require <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>sdonovan<span style="color: #000000; font-weight: bold;">/</span>sputnik<span style="color: #000000; font-weight: bold;">/</span>rocks<span style="color: #000000; font-weight: bold;">/</span>sputnik<span style="color: #000000; font-weight: bold;">/</span>9.03.16-<span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>sputnik.lua start-xavante sputnik.ws</pre></div></div>


<p>(The <strong>&#8211;cols</strong> flag is necessary to prevent <strong>ps</strong> from thoughtfully truncating the line length to fit the terminal screen)</p>

<p>Chopping lines up is easy using <strong>sputnik.util.split</strong> &#8211; notice that it returns multiple values, not a table:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  ~$ slua</pre></div></div>



<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  Lua 5.1.4  Copyright <span style="color: #66cc66;">&#40;</span>C<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1994</span>-<span style="color: #cc66cc;">2008</span> Lua.org, PUC-Rio
  <span style="color: #66cc66;">&gt;</span> util <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">'sputnik.util'</span>
  <span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">=</span> util.split<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'one two three'</span>,<span style="color: #ff0000;">'%s+'</span><span style="color: #66cc66;">&#41;</span>
  one     two     three</pre></div></div>


<p>With this, a more exciting version of <strong>actions/Memory.lua</strong> can be written:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  module<span style="color: #66cc66;">&#40;</span>..., package.seeall<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">local</span> util <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">'sputnik.util'</span>
  actions <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">local</span> template <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#91;</span>
     <span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>Memory used by Lua is $mem kb<span style="color: #66cc66;">&lt;/</span>h2<span style="color: #66cc66;">&gt;</span>
     <span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>Processes<span style="color: #66cc66;">&lt;/</span>h2<span style="color: #66cc66;">&gt;</span>
     <span style="color: #66cc66;">&lt;</span>table<span style="color: #66cc66;">&gt;</span>
     <span style="color: #66cc66;">&lt;</span>tr<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>th<span style="color: #66cc66;">&gt;</span>User<span style="color: #66cc66;">&lt;/</span>th<span style="color: #66cc66;">&gt;&lt;</span>th<span style="color: #66cc66;">&gt;</span>CPU<span style="color: #66cc66;">&lt;/</span>th<span style="color: #66cc66;">&gt;&lt;</span>th<span style="color: #66cc66;">&gt;</span>Mem<span style="color: #66cc66;">&lt;/</span>th<span style="color: #66cc66;">&gt;&lt;</span>th<span style="color: #66cc66;">&gt;</span>Command<span style="color: #66cc66;">&lt;/</span>th<span style="color: #66cc66;">&gt;</span>
     <span style="color: #66cc66;">&lt;/</span>tr<span style="color: #66cc66;">&gt;</span>
     $do_processes<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
          <span style="color: #66cc66;">&lt;</span>tr<span style="color: #66cc66;">&gt;</span>
          <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;</span>$user<span style="color: #66cc66;">&lt;/</span>td<span style="color: #66cc66;">&gt;&lt;</span>td<span style="color: #66cc66;">&gt;</span>$cpu<span style="color: #66cc66;">&lt;/</span>td<span style="color: #66cc66;">&gt;&lt;</span>td<span style="color: #66cc66;">&gt;</span>$mem<span style="color: #66cc66;">&lt;/</span>td<span style="color: #66cc66;">&gt;&lt;</span>td<span style="color: #66cc66;">&gt;</span>$cmd<span style="color: #66cc66;">&lt;/</span>td<span style="color: #66cc66;">&gt;</span>
          <span style="color: #66cc66;">&lt;/</span>tr<span style="color: #66cc66;">&gt;</span>
     <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
     <span style="color: #66cc66;">&lt;/</span>table<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
  <span style="color: #b1b100;">function</span> actions.show_memory <span style="color: #66cc66;">&#40;</span>node, request, sputnik<span style="color: #66cc66;">&#41;</span>
      node.inner_html <span style="color: #66cc66;">=</span> cosmo.f<span style="color: #66cc66;">&#40;</span>template<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          mem <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'%6.0f'</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">collectgarbage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'count'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
          do_processes <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #b1b100;">local</span> user <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">os.getenv</span> <span style="color: #ff0000;">'USER'</span>
              <span style="color: #808080; font-style: italic;">-- this works on LInux, may need some mods for BSD/OS X.</span>
              <span style="color: #b1b100;">local</span> f <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">io</span>.popen<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ps --cols 256 aux'</span>,<span style="color: #ff0000;">'r'</span><span style="color: #66cc66;">&#41;</span>
              f:<span style="color: #b1b100;">read</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- not interested in column headers</span>
              <span style="color: #b1b100;">for</span> line <span style="color: #b1b100;">in</span> f:lines<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
                  <span style="color: #b1b100;">local</span> fields <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>util.split<span style="color: #66cc66;">&#40;</span>line,<span style="color: #ff0000;">'%s+'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>
                  <span style="color: #b1b100;">if</span> fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> user <span style="color: #b1b100;">then</span>
                      cosmo.yield <span style="color: #66cc66;">&#123;</span>
                          user <span style="color: #66cc66;">=</span> fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>, cpu <span style="color: #66cc66;">=</span> fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span>, mem <span style="color: #66cc66;">=</span> fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span>, cmd <span style="color: #66cc66;">=</span> fields<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">11</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>
          <span style="color: #b1b100;">end</span>
      <span style="color: #66cc66;">&#125;</span>
      <span style="color: #b1b100;">return</span> node.wrappers.default<span style="color: #66cc66;">&#40;</span>node, request, sputnik<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">end</span></pre></div></div>


<p>Now the node <strong>Memory</strong> is actually useful for the administrator of the website &#8211; but probably not a good idea to expose to the world!</p>

<h2>Permissions</h2>

<p>The node <strong>Memory</strong> should be restricted; only the Admin user should be able to view it. Also, it does not make sense to edit it, even as an administrator.  <strong>actions/Memory.lua</strong> needs to have a <em>permissions</em> field:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  module<span style="color: #66cc66;">&#40;</span>..., package.seeall<span style="color: #66cc66;">&#41;</span>
&nbsp;
  NODE <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
      title <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Lua Memory&quot;</span>,
      content <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>,
      actions <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
          show<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Memory.show_memory&quot;</span>
      <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
      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>Admin,show<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>We start by prohibiting <em>everything</em>, and then only let Admin view the page.  Notice that all of the usual little action icons on the right-hand side have disappeared.</p>

<p>To get back to the <strong>pages</strong> example; we may insist that only authenticated users can edit the pages.  There are two kinds of solution to this, make it site-wide policy or only for children of <strong>pages</strong>.  The first solution requires no code; as Admin, go to the <strong>@Root</strong> node, and choose the &#8216;configure&#8217; action (which is usually the little gear icon next to &#8216;edit&#8217; on the actions toolbar)  Now open the &#8216;Advanced Fields&#8217; section, and you can then edit the &#8216;Permissions&#8217; field.  By default, Sputnik comments out this line:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  <span style="color: #808080; font-style: italic;">-- deny(Anonymous, edit_and_save)</span></pre></div></div>


<p>Remove the comment and save.  Now anonymous users have lost their power to make edits anywhere on the site.</p>

<p>We&#8217;ve already seen with <strong>Memory</strong> how to enforce permissions for a single node. But how to do this for a group of nodes?  Sputnik <em>prototypes</em> provide the solution.  A prototype acts as the &#8216;type&#8217; of a node. When a node is retrieved from storage, Sputnik copies default values from its prototype node.  The basic prototype of all nodes is <strong>@Root</strong>.</p>

<p>Previously, the <strong>pages</strong> node has insisted that its children have an &#8216;empty&#8217; prototype.  The idea is to create an explicit prototype that will apply to the children, which will be called <strong>@pages</strong>. (By convention, all prototypes begin with &#8216;@&#8217;)</p>

<p><strong>node_defaults/pages.lua</strong> becomes:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  module<span style="color: #66cc66;">&#40;</span>..., package.seeall<span style="color: #66cc66;">&#41;</span>
&nbsp;
  NODE <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
     title<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Pages&quot;</span>,
     content <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>,
     child_defaults <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
          any<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'prototype = &quot;@pages&quot;'</span>
      <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>and create a new file <strong>node_defaults/@pages.lua</strong> to define the <strong>@pages</strong> prototype:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  module<span style="color: #66cc66;">&#40;</span>..., package.seeall<span style="color: #66cc66;">&#41;</span>
&nbsp;
  NODE <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
     content <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>,
     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>Anonymous,edit_and_save<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>It does very little, just explicitly overrides the permissions.</p>

<h2>Hooks</h2>

<p>Continuing with the <strong>pages</strong> example, it would be useful if we could track the author of a particular page, defined simply as the user that first edited it.  Also, we would like to track the creation date.  So the prototype for all pages must include these new_fields and define a <em>save hook</em> which will be called when the node is saved:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  <span style="color: #808080; font-style: italic;">-- node_defaults/@pages.lua</span>
  module<span style="color: #66cc66;">&#40;</span>..., package.seeall<span style="color: #66cc66;">&#41;</span>
&nbsp;
  NODE <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
     content <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>,
     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>Anonymous,edit_and_save<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
     fields <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
        author <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">1.1</span><span style="color: #66cc66;">&#125;</span>
        creation_time <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">1.2</span><span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>,
     save_hook <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;pages.save_page&quot;</span>
  <span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>Create a directory &#8216;sputnik/hooks&#8217; as before, and put <strong>pages.lua</strong> in it:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  <span style="color: #808080; font-style: italic;">-- hooks/pages.lua</span>
  module<span style="color: #66cc66;">&#40;</span>..., package.seeall<span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #b1b100;">function</span> save_page<span style="color: #66cc66;">&#40;</span>node, request, sputnik<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #b1b100;">not</span> node.creation_time <span style="color: #b1b100;">then</span>
         <span style="color: #b1b100;">local</span> params <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
         params.author <span style="color: #66cc66;">=</span> request.user
         params.creation_time <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">tostring</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">os.time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         node <span style="color: #66cc66;">=</span> sputnik:update_node_with_params<span style="color: #66cc66;">&#40;</span>node, params<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">end</span>
      <span style="color: #b1b100;">return</span> node
  <span style="color: #b1b100;">end</span></pre></div></div>


<p>When a node&#8217;s <strong>save_hook</strong> field is set to <strong>MODULE.FUNCTION</strong> , then Sputnik will try to load <strong>sputnik.hooks.MODULE</strong> and use the <strong>FUNCTION</strong> defined by that module.  In this case, the save hook is only interested in a new node, where the author and creation time fields have not been assigned yet.  It uses the <strong>update_node_with_params</strong> method to write the new key/value pairs into the node.</p>

<p>Now, after saving <strong>pages/fred</strong>, the revision in the <strong>pages%2Ffred</strong> directory will look like this:</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;pages/fred&quot;</span>
  category       <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>
  prototype      <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;@pages&quot;</span>
  content        <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>Some content<span style="color: #66cc66;">!</span>
  <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  breadcrumb     <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>
  author         <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;sdonovan&quot;</span>
  creation_time  <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;1257157113&quot;</span></pre></div></div>


<p>This shows that we are saving the new information, although these fields are not displayed yet.</p>

<h2>Wrapping up</h2>

<p>Currently, <strong>pages</strong> is blank, and like <strong>Memory</strong>  we can output something sensible by defining a &#8216;show&#8217; action that will display a table of the existing pages.</p>

<p>If you are accustomed to regular Web frameworks, you are probably tempted to maintain and use a relational database at this point.  Sputnik does not exclude that, you are free to store things as you wish, but it&#8217;s best to work with the underlying &#8216;document-oriented&#8217; database machinery provided by Saci.</p>

<p>The existing pages can be found by querying Saci, which provides a <strong>get_nodes_by_prefix</strong> method. The prefix identifies the <em>namespace</em> for the nodes, which is <strong>pages</strong> in this case. This method returns a table where the keys are the <em>identifiers</em> (e.g. <strong>pages/fred</strong>) and the values are the node objects. Here is code that creates a list of nodes from this table, and sorts it by creation time.</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> pages_in_order <span style="color: #66cc66;">&#40;</span>sputnik<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">local</span> pages <span style="color: #66cc66;">=</span> sputnik.saci:get_nodes_by_prefix <span style="color: #ff0000;">'pages'</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> id,page <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">pairs</span><span style="color: #66cc66;">&#40;</span>pages<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
          res<span style="color: #66cc66;">&#91;</span>#res+<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> page
      <span style="color: #b1b100;">end</span>
      <span style="color: #b1b100;">table.sort</span><span style="color: #66cc66;">&#40;</span>res,<span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>p1,p2<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> p1.creation_time <span style="color: #66cc66;">&lt;</span> p2.creation_time <span style="color: #b1b100;">end</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> res
  <span style="color: #b1b100;">end</span></pre></div></div>


<p><strong>node_defaults/pages.lua</strong> gets an actions field, just as with <strong>node_defaults/Memory.lua</strong>:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  actions<span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
      show<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pages.show_pages&quot;</span>
  <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span></pre></div></div>


<p>And <strong>actions/pages.lua</strong> will be:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">  module <span style="color: #66cc66;">&#40;</span>...,package.seeall<span style="color: #66cc66;">&#41;</span>
&nbsp;
  actions <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">local</span> template <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#91;</span>
     <span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>Existing Pages<span style="color: #66cc66;">&lt;/</span>h2<span style="color: #66cc66;">&gt;</span>
     <span style="color: #66cc66;">&lt;</span>table<span style="color: #66cc66;">&gt;</span>
     <span style="color: #66cc66;">&lt;</span>tr<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>th<span style="color: #66cc66;">&gt;</span>Page<span style="color: #66cc66;">&lt;/</span>th<span style="color: #66cc66;">&gt;&lt;</span>th<span style="color: #66cc66;">&gt;</span>Author<span style="color: #66cc66;">&lt;/</span>th<span style="color: #66cc66;">&gt;&lt;</span>th<span style="color: #66cc66;">&gt;</span>Created<span style="color: #66cc66;">&lt;/</span>th<span style="color: #66cc66;">&gt;</span>
     <span style="color: #66cc66;">&lt;/</span>tr<span style="color: #66cc66;">&gt;</span>
     $do_pages<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
          <span style="color: #66cc66;">&lt;</span>tr<span style="color: #66cc66;">&gt;</span>
          <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;&lt;</span>a href<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;?p=$id&quot;</span><span style="color: #66cc66;">&gt;</span>$name<span style="color: #66cc66;">&lt;/</span>a<span style="color: #66cc66;">&gt;&lt;/</span>td<span style="color: #66cc66;">&gt;&lt;</span>td<span style="color: #66cc66;">&gt;</span>$author<span style="color: #66cc66;">&lt;/</span>td<span style="color: #66cc66;">&gt;&lt;</span>td<span style="color: #66cc66;">&gt;</span>$created<span style="color: #66cc66;">&lt;/</span>td<span style="color: #66cc66;">&gt;</span>
          <span style="color: #66cc66;">&lt;/</span>tr<span style="color: #66cc66;">&gt;</span>
     <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
     <span style="color: #66cc66;">&lt;/</span>table<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">=</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
  <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">function</span> pages_in_order<span style="color: #66cc66;">&#40;</span>sputnik<span style="color: #66cc66;">&#41;</span>
  ....
  <span style="color: #b1b100;">end</span>
&nbsp;
  <span style="color: #b1b100;">function</span> actions.show_pages<span style="color: #66cc66;">&#40;</span>node, request, sputnik<span style="color: #66cc66;">&#41;</span>
      node.inner_html <span style="color: #66cc66;">=</span> cosmo.f<span style="color: #66cc66;">&#40;</span>template<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          do_pages <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #b1b100;">local</span> pages <span style="color: #66cc66;">=</span> pages_in_order<span style="color: #66cc66;">&#40;</span>sputnik<span style="color: #66cc66;">&#41;</span>
              <span style="color: #b1b100;">for</span> _,page <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">ipairs</span><span style="color: #66cc66;">&#40;</span>pages<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
                  cosmo.yield<span style="color: #66cc66;">&#123;</span>
                      id <span style="color: #66cc66;">=</span> page.id,
                      name <span style="color: #66cc66;">=</span> page.id:<span style="color: #b1b100;">gsub</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'pages/'</span>,<span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>,
                      author <span style="color: #66cc66;">=</span> page.author,
                      created <span style="color: #66cc66;">=</span> sputnik:format_time<span style="color: #66cc66;">&#40;</span>page.creation_time,<span style="color: #ff0000;">&quot;%d %b %Y&quot;</span><span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#125;</span>
              <span style="color: #b1b100;">end</span>
          <span style="color: #b1b100;">end</span>
      <span style="color: #66cc66;">&#125;</span>
      <span style="color: #b1b100;">return</span> node.wrappers.default<span style="color: #66cc66;">&#40;</span>node, request, sputnik<span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">end</span></pre></div></div>


<h2>Beyond the Basics</h2>

<p>If you are curious about how Sputnik creates the whole page, note that <strong>node.wrappers.default</strong>
is usually implemented by the <strong>wrappers.default</strong> function in <strong>sputnik/actions/wiki.lua</strong> which does a Cosmo expansion of the <strong>node.html_main</strong> template (see <strong>NODE.html_main</strong> in <strong>sputnik/node_defaults/@Root.lua</strong>)</p>

<p>If you don&#8217;t like a visual feature, then it is often easy to remove it.  For instance, editing <strong>@Root</strong> as Admin (using <strong>@Root.configure</strong> as before) you can open up the &#8216;HTML Fields&#8217; and modify the templates directly. Removing the menu bar is as easy as clearing out the text in the &#8216;Menu&#8217; field.</p>

<p>For a good overview of Sputnik permissions, see this <a href="http://sputnik.freewisdom.org/en/list/Way_to_lockdown_editing_features_">list question</a> and its reply.</p>

<p>The next part of this tutorial will deal with further customizations, like internationalization, providing a custom form for editing a node&#8217;s fields, and using the built-in <strong>@Collection</strong> prototype to simplify the common pattern of groups of content nodes.  And Sputnik&#8217;s ability to create custom actions will change the way you look at file extensions forever.</p>
]]></content:encoded>
			<wfw:commentRss>http://luanova.org/sputnik/feed/</wfw:commentRss>
		<slash:comments>2</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>
