<?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://www.luanova.org/tag/lua/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.luanova.org</link>
	<description>welcome to the moon</description>
	<lastBuildDate>Thu, 23 Sep 2010 12:46:53 +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>iOS Programming with Lua</title>
		<link>http://www.luanova.org/ioswithlua/</link>
		<comments>http://www.luanova.org/ioswithlua/#comments</comments>
		<pubDate>Thu, 23 Sep 2010 12:46:53 +0000</pubDate>
		<dc:creator>mathewburke</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Lua iOS]]></category>

		<guid isPermaLink="false">http://luanova.org/?p=81</guid>
		<description><![CDATA[Matthew Burke, Bluedino Software In this article I will discuss three methods for using Lua to build iOS apps. The techniques range from using Lua to build the entire application (Corona) to using Lua as a component for scripting your application (do it yourself or Wax). Before getting into the details, there are two main [...]]]></description>
			<content:encoded><![CDATA[<h3>Matthew Burke, Bluedino Software</h3>

<p>In this article I will discuss three methods for using Lua to build iOS apps.  The techniques range from using Lua to build the entire application (<a href="#corona">Corona</a>) to using Lua as a component for scripting your application (<a href="#diy">do it yourself</a> or <a href="#wax">Wax</a>).  Before getting into the details, there are two main questions to answer:</p>

<ol>
<li>Why use Lua?</li>
<li>Will Apple let you use Lua?</li>
</ol>

<p>The answers to these questions are intertwined.</p>

<p>In case you landed here without knowing anything about Lua, let me hit the high points of the language.  If you already are familiar with Lua, you can <a href="#iphone-script">skip ahead</a>.</p>

<h3>About Lua</h3>

<p><a href="http://www.lua.org">Lua</a> is a fast, lightweight, embedded scripting language.  It is similar to languages such as JavaScript, Ruby or Python.  Many of its users, including myself, feel Lua is a particularly clean and elegant language.</p>

<p>Lua was created in 1993 by Roberto Ierusalimschy, Waldemar Celes and Luiz Henrique de Figueiredo at the Pontifical Catholic University in Rio de Janeiro, Brazil.  It is used in a range of applications including <a href="http://micasaverde.com/">Mi Casa Verde</a>, <a href="http://since1968.com/article/190/mark-hamburg-interview-adobe-photoshop-lightroom-part-2-of-2">Adobe Lightroom</a>, <a href="http://www.shatters.net/celestia/">Celestia</a>, <a href="http://www.lighttpd.net/">lighttpd</a>, <a href="http://www.luatex.org/">LuaTeX</a>, <a href="http://nmap.org/">nmap</a>, <a href="http://www.wireshark.org/">Wireshark</a>, Cisco&#8217;s Adaptive Security Appliance, <a href="http://www.hempeldesigngroup.com/lego/pbLua/">pbLua</a>, and a ton of games including <a href="http://en.wikipedia.org/wiki/Grim_Fandango">Grim Fandango</a>, <a href="http://en.wikipedia.org/wiki/World_of_Warcraft">World of Warcraft</a>, <a href="http://en.wikipedia.org/wiki/Tap_Tap_Revenge">Tap Tap Revenge</a>, and many others.  <a href="http://www.lua.org/license.html">Lua&#8217;s license</a> is a variation of the MIT License&mdash;this means that there are essentially no hurdles to including Lua in both commerical and non-commerical projects.</p>

<p>Lua&#8217;s main data structuring mechanism is the table&mdash;a combination resizable array and hash.  Listing 1 shows a table we might use in a hypothetical application to keep track of automobiles and their gas mileage.  We can store information about the automobile using string keys such as <strong>license</strong> and <strong>make</strong>.  A series of mileage readings are stored using integer indices.</p>

<div class="panelHeader">Listing 1: A Lua Table</div>

<p><code></p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">&nbsp;
car_data <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> license <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'XVW1942'</span>, make <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Volvo'</span>,
                model <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'XC70'</span>, <span style="color: #cc66cc;">30</span>, <span style="color: #cc66cc;">31.3</span>, <span style="color: #cc66cc;">32.4</span>, <span style="color: #cc66cc;">34.0</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>car_data<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>                <span style="color: #808080; font-style: italic;">-- 30</span>
<span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>car_data<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'license'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">-- XVW1942</span>
<span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>car_data.license<span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">-- XVW1942 (also!)</span></pre></div></div>


<p></code></p>

<div class="panelFooter">In Lua, array indices start at one, not zero.
Comments are indicated by &#8216;&#8211;&#8217; and run to the end of the line.
</div>

<p>Out of the box, Lua is neither an object-oriented (OO) programming language nor a functional programming language.  Rather, it provides a small set of mechanisms with which you can build your own higher-level features.  A number of different object systems have been built in Lua including more traditional OO systems as well as classless OO systems (<em>&agrave; la</em> languages like <a href="http://research.sun.com/self/language.html">Self</a> or <a href="http://www.iolanguage.com/">Io</a>).  Lua supports first-class functions and lexical closures and has a meta-programming facility (known as metatables and metamethods).  Lua can be used in a functional programming style</p>

<p>For a gentle introduction to OO in Lua, read <a href="http://www.lua.org/pil/16.html">Programming in Lua</a> (<span class="tiny">in fact, PIL is a very well-written book on Lua in particular and programming in general</span>).  You should also look at the several examples available on <a href="http://lua-users.org/wiki/ObjectOrientedProgramming">Lua wiki</a>.</p>

<p>If you <em>like</em> drinking from a firehose, Listing 2 shows one possible implementation for a linked list class.  The table stored in the variable <strong>List</strong> serves as a metatable for all linked list objects.  It  provides a fall-back location for looking up table indexes and thus serves as a class dispatch mechanism.  The line &#8220;<strong>List.__index = List</strong>&#8221; is what allows us to create methods for our list objects.  Methods are implemented as functions stored in the <strong>List</strong> metatable.  When we attempt to call one of these functions on our list object, the lookup mechanism will lead us to the function defined on the <strong>List</strong> metatable and that&#8217;s what will get run.</p>

<p>The code shows a number of additional features of Lua: multiple assignment (<span class="tiny">and functions can return multiple results</span>), syntactic sugar for method calls (the &#8216;:&#8217; notation, this performs the common technique&mdash;used in languages ranging from Python to Objective-C&mdash;of rewriting the function call to have an additional parameter which points to <strong>self</strong>).</p>

<div class="panelHeader">Listing 2: Linked List Class</div>

<p><code></p>

<pre>
List = {}
List.__index = List

function List:new()
  local l = { head = {}, tail = {}, size = 0 }
  l.head.__next, l.tail.__prev = l.tail, l.head
  return setmetatable(l, self)
end

function List:first()
  if self.size > 0 then
    return self.head.next
  else
    return nil
  end
end

function List:addFirst(elem)
   local node = { prev = self.head, value = elem, 
                        next = self.head.next }
   node.next.prev = node
   self.head.next = node
   self.size = self.size + 1
end

mylist = List:new()
mylist:addFirst(12)
print(mylist:first())

</pre>

<p></code></p>

<p>I&#8217;m sure I&#8217;ve left out the really interesting/important things (<span class="tiny">such as lexical closures</span>), but this at least gives you a little taste of Lua.  There are more samplings below when we get to actual iPhone coding.  For further details on Lua, see the <a href="">web site</a>.</p>

<p><a name="iphone-script"></a></p>

<h3>Can I Script on iOS?</h3>

<p>Of the two questions listed at the beginning of this article, the most important question to address is are you allowed to use Lua (or any interpreted language) on the iPhone?  After all, early on the iPhone Developer Program License Agree stated that &#8220;[n]o interpreted code may be downloaded or used in an Application except for code that is interpreted and run by Apple&#8217;s Documented APIs and built- in interpreter(s).&#8221;</p>

<p>In fact, an earlier version of this article was tabled when Apple made changes to the developer license  (circa April 2010) which forbade developing iOS applications in any language other than Objective-C and Javascript (<span class="tiny">the Javascript could be used either to build web apps or native apps via the UIWebView</span>).  Recently (September 2010), Apple again changed the developer license to allow the use of scripting languages..</p>

<p>There are still several important constraints in place.  In particluar, although you can use Lua and other scripting languages, you cannot create an application where users could download plugins for your app from your website (<span class="tiny">in-app purchasing anyone?</span>), nor can you allow the user to write scripts, download scripts, etc.  There are (<span class="tiny">and have been, e.g. Tap Tap Revenge</span>) quite a number of apps available on the app store that use Lua as well as other languages.</p>

<p>Of course, creating a plugin system and allowing users to write scripts are two major use cases for including a language like Lua in your application, so what&#8217;s left?  Plenty!</p>

<h3>Why Use Lua for iOS development?</h3>

<p>Although you cannot expose a plugin system to the end user, nor can you give her the ability to write her own scripts, you can still develop your system using a plugin architecture!  This can both speed up initial development as well as be a big help when it&#8217;s time to add functionality for the next version.</p>

<p>There are other benefits from using Lua.  It allows you to develop using rapid prototyping (<span class="tiny">beware my pet peeve: don&#8217;t devolve into seat-of-your-pants programming</span>), reduces/eliminates the need to worry about memory allocation, allows more of your team to participate in development (<span class="tiny">many Lua projects have non-programmers writing code</span>), makes it easier to <em>tune</em> your application, and provides a powerful persistence mechanism.</p>

<p>In short, using Lua can reduce development time and lower entry barriers. And it&#8217;s just plain <em>fun</em>!</p>

<p>Assuming you&#8217;re sold you on the idea of using Lua, how do we go about it?</p>

<p><a name="corona"></p>

<h3>Corona</h3>

<p></a></p>

<p>Ansca Mobile&#8217;s Corona allows you to develop your iOS app entirely in Lua.  And it&#8217;s not just for iOS.  You can also develop apps for Android.  In fact, you can use the same source code to build both an iOS and an Android app.  This adds a compelling reason to use Lua (<span class="tiny">and, in particular, to use Corona</span>): the ability to easily build cross-platform applications.</p>

<p>Listing 3 is the complete source for an app.</p>

<div class="panelHeader">Listing 3: main.lua from the Swirly Text app</div>

<p><code></p>

<pre>
local w, h = display.stageWidth, display.stageHeight
local dx, dy, dtheta = 5, 5, 5


local background = display.newRect(0, 0, w, h)
background:setFillColor(255, 255, 255)


local message = display.newText('Hello from Corona', w/2, h/2)
message:setTextColor(0, 0, 200)


local function update(event)
   local counter_spin = false
   message:translate(dx, dy)
   message:rotate(dtheta)
   if message.x > w or message.x < 0 then
      dx = -1 * dx
      counter_spin = true
   end
   if message.y > h or message.y < 0 then
      dy = -1 * dy
      counter_spin = true
   end
   if counter_spin then
      dtheta = -1 * dtheta
   end
end


Runtime:addEventListener('enterFrame', update)

</pre>

<p></code></p>

<p>Corona apps are developed using your favorite text editor&mdash;I use Emacs.  All Lua source code and any necessary resources (images, sounds, and data) must reside in a single directory and Corona expects a Lua file, <strong>main.lua</strong> which is where your app starts executing.  You test your code in Corona's simulator which runs on both Intel and PPC Macs.  Figure 1 shows my Corona 'IDE': namely Emacs with two windows (a Lua file and the project directory), the Corona terminal (you can print debugging info to the terminal), and the Corona simulator.</p>

<div class="panelHeader">Figure 1: My Corona 'IDE'</div>

<p><a href="http://mysite.mweb.co.za/residents/sdonovan/lua/CoronaIDE.png">
<img src="http://mysite.mweb.co.za/residents/sdonovan/lua/CoronaIDE.png" width="760px" style="margin: 10px" /></a></p>

<div class="panelFooter">Clockwise from the left: Corona simulator, Emacs with two windows (a source file and project directory), the Corona terminal (for debugging info).</div>

<p>When you are ready to run your app on actual hardware, you use the Corona Simulator's <strong>Open for Build</strong> option.  For an iOS build, you must have a provisioning profile (either development or distribution)&mdash;<span class="tiny">yes, Virginia, you do need a membership in the iOS Developer Program</span>&mdash;which is uploaded, along with your app's source and resources, to Ansca's servers.  A compiled app is returned to you. For Android builds, you will need a suitable signing certificate.  Again the build process is handled by uploading your source to Ansca's servers.  You do not need to have the Android SDK installed.</p>

<p>I haven't done any in-depth poking around, but both the .apk file from the Android build process and the iOS .app bundle contain a file containing all your Lua code pre-processed in some fashion.  Approximately seven seconds of examination suggests that it's not <em>standard</em> byte-compiled Lua code, but I'm guessing it must be a similar format.</p>

<p>Corona's event system lets you handle touches (<span class="tiny">including multi-touch</span>), access the GPS and the acceleratometer, handle animation, and define custom events.  There is a powerful graphics system which allows you to draw circles, rectangles and text.  They've recently added polylines so you can draw regular lines and polygons.  You can also display images.  Corona allows you to group these objects and do transformations on them.  Listing 4, an excerpt from a Solar System simulator, shows a (crude) example of grouping graphics objects.  Other Corona features include playing audio and video clips, a cryptography library, networking using the LuaSocket library, and access to SQLite databases (using LuaSQLite).  There is some access to native widgets including textfields, alerts and activity indicators. There's a nice feature where you can overlay a web view for doing things like login screens and a sample application provides a library for connecting to Facebook.  The last thing I'll mention is that there is a (more expensive) game edition that includes the Box2D physics engine, sprites and some OpenFeint functionality such as leaderboards.</p>

<div class="panelHeader">Listing 4: excerpt from Solar System app</div>

<p><code></p>

<pre>
function new(params)
   local color = params.color or planet_colors[random(#planet_colors)]
   local radius = params.radius or planetRadius()
   local planet = display.newGroup()

   planet.theta = 0
   local x = params.x - ox
   local y = params.y - oy
   planet.orbital_radius = sqrt(x*x+y*y)

   local body = display.newCircle(x + ox, y + oy, radius, radius)
   body:setFillColor(unpack(color))
   planet:insert(body, true)
   planet.body = body

   planet.delta_theta = (40/planet.orbital_radius) * 0.1

   return planet
end

</pre>

<p></code></p>

<div class="panelFooter">By passing a table as a function's paramter, we can make use of named
parameters and default values.  Thus, the idiom of local radius = params.radius or planetRadius()
</div>

<p>Corona gives you a lot.  However, there are still a lot of things missing with Corona.  The biggest item is the limited access to native controls.  Not to mention the access that is provided is awkward to use because of limited support in Corona's simulator.  In the simulator, native alerts and activity indicators are implemented using OS X equivalents, rather than the iOS widgets.  However, text fields, text boxes and web popups are unusable when running in the simulator. This makes development, to say the least, painful.</p>

<p>Finally, there is no mechanism to access the Objective-C API except for what ANSCA has specifically provided.  Not only does this mean you cannot access large portions of the standard libraries, but you cannot make use of third-party libraries like Three20, or one of the mobile ads APIs.  Of course, with the release of the Android version of Corona, you may not want to access the Objective-C API since it limits (<span class="tiny">or complicates</span>) your ability to do multi-platform applications.  It would be nice, however, to be able to add in Lua extensions that use Lua's C API, as many of these are multi-platform.</p>

<p>I've found the ANSCA staff to be quite helpful and responsive to questions posted in the forums on their website.  With the release of version 2.0 (September 2010), Corona costs $249 per developer per year.  The Game Edition is $349 per developer per year.  Ansca's website indicates that the game edition prices is pre-release.  I assume that means it will be higher when it is officially released.</p>

<p><a name="diy"></p>

<h3>DIY</h3>

<p></a></p>

<p>Including the Lua interpeter in your iOS app is simple. Open an Xcode project and add the Lua source files (<span class="tiny">except <strong>lua.c</strong> and <strong>luac.c</strong>&mdash;source for the command line programs</span>).  Compile.  You can now make use of the standard Lua C API to create an interpreter and run some code.  An example project, iLua, may be downloaded from <a href="http://github.com/profburke/ilua">http://github.com/profburke/ilua</a>.  iLuaShell is a simple, view-based application that presents the user with two text fields&mdash;an editable one where the user can enter Lua code and a non-editable one where the results of evaluating the Lua code are displayed.</p>

<p>The work is done in the method, <strong>evaluate</strong>, which is shown in Listing 5.  The method retrieves the text of the first text field, hands it off to the Lua interpreter which parses and executes it, and then sets the Lua output as the text of the output field.</p>

<div class="panelHeader">Listing 5: LuaTrial's <strong>evaluate</strong> method.</div>

<p><code></p>


<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>evaluate <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> err<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>input resignFirstResponder<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    lua_settop<span style="color: #009900;">&#40;</span>L<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    err <span style="color: #339933;">=</span> luaL_loadstring<span style="color: #009900;">&#40;</span>L<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>input.<span style="color: #202020;">text</span> 
                     cStringUsingEncoding<span style="color: #339933;">:</span>NSASCIIStringEncoding<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">!=</span> err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        output.<span style="color: #202020;">text</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>NSString stringWithCString<span style="color: #339933;">:</span>lua_tostring<span style="color: #009900;">&#40;</span>L<span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> 
                             encoding<span style="color: #339933;">:</span>NSASCIIStringEncoding<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        lua_pop<span style="color: #009900;">&#40;</span>L<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    err <span style="color: #339933;">=</span> lua_pcall<span style="color: #009900;">&#40;</span>L<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> LUA_MULTRET<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">!=</span> err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        output.<span style="color: #202020;">text</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>NSString stringWithCString<span style="color: #339933;">:</span>lua_tostring<span style="color: #009900;">&#40;</span>L<span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> 
                             encoding<span style="color: #339933;">:</span>NSASCIIStringEncoding<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        lua_pop<span style="color: #009900;">&#40;</span>L<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #993333;">int</span> nresults <span style="color: #339933;">=</span> lua_gettop<span style="color: #009900;">&#40;</span>L<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">==</span> nresults<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        output.<span style="color: #202020;">text</span> <span style="color: #339933;">=</span> @<span style="color: #ff0000;">&quot;&lt;no results&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        NSString <span style="color: #339933;">*</span>outputNS <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>NSString <span style="color: #993333;">string</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> nresults<span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            outputNS <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>outputNS stringByAppendingFormat<span style="color: #339933;">:</span>@<span style="color: #ff0000;">&quot;%s &quot;</span><span style="color: #339933;">,</span> 
                                               lua_tostring<span style="color: #009900;">&#40;</span>L<span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">*</span> i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        lua_pop<span style="color: #009900;">&#40;</span>L<span style="color: #339933;">,</span> nresults<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        output.<span style="color: #202020;">text</span> <span style="color: #339933;">=</span> outputNS<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p></code></p>

<p>Note the error checking and handling is minimal.  A little additional effort could yield a nice Lua shell&mdash;not that you could actually put it in the App Store...</p>

<p>What are the drawbacks to this approach?  The biggest one is the lack of a bridge to Objective-C.  Ideally we want to call Objective-C methods from Lua. Going the other direction is important also.  Being able to code callbacks and delegate methods in Lua would be a big win.</p>

<p>Pursuing that leads us to...</p>

<p><a name="wax"></p>

<h3>iPhone Wax</h3>

<p></a></p>

<p>Powerful interoperability between Lua and Objective-C is the star attraction of iOS Wax by Corey Johnson.  Using Wax, you can easily subclass Objective-C classes <em>in Lua</em>!  Listing 6 shows a Wax implementation of a custom view controller.  In particular, this code is a port of the application described in the DIY section.  Details after the listing.</p>

<div class="panelHeader">Listing 6: RootViewController.lua</div>

<p><code></p>

<pre>
waxClass{'RootViewController', UI.ViewController }

function init(self)
        self.super:init()

    self.input = UI.TextView:initWithFrame(CGRect(20, 20, 280, 114))

    self.output = UI.TextView:initWithFrame(CGRect(20, 184, 280, 225))

    local evalButton = UI.Button:buttonWithType(UIButtonTypeRoundedRect)
    evalButton:setTitle_forState('Evaluate', UIControlStateNormal)
    evalButton:setFrame(CGRect(200, 142, 100, 32))
    evalButton:addTarget_action_forControlEvents(self, 'eval:', 
                            UIControlEventTouchUpInside)
    self.evalButton = evalButton
    
    self:view():addSubview(self.input)
    self:view():addSubview(self.output)
    self:view():addSubview(self.evalButton)
    
    return self
end

function eval(self, sender)
    self.input:resignFirstResponder()

    local code, errmsg = loadstring(self.input:text())  
    if not code then
        self.output:setText(errmsg)
        return
    end
    
    local success, result = pcall(code)
    print('result is ' .. tostring(result))
    if not success then
        self.output:setText('Error: ' .. tostring(result))
    else
        self.output:setText(tostring(result))
    end 
    
end

</pre>

<p></code></p>

<p>The <strong>waxClass</strong> function essentially defines a new Objective-C class.  In this instance we are defining a class named <strong>RootViewController</strong> which is a sub-class of <strong>UIViewController</strong>.  (<span class="tiny">In Wax, the Objective-C classes have been put into namespaces, hence <strong>UI.ViewController</strong>, rather than <strong>UIViewController</strong></span>).  The Lua representation of instances of this class is a table (<span class="tiny">well, really a <strong>userdata</strong>, but you can think table...</span>), hence items like <strong>self.input</strong> are Lua table fields and <strong>not</strong> Objective-C properties.  To access properties you use setters and getters, e.g. <strong>self.output:setText()</strong>.  If you're like me, this will trip you up until you embarass yourself by asking about it in the <a href="http://groups.google.com/group/iphonewax">mailing list</a>.  Aftewards, you won't mix it up again. (<span class="tiny">Actually, the people on the mailing list are quite nice.</span>)</p>

<p>Wax classes can also implement protocols.  For instance, the Wax sample project, <strong>States</strong>, demonstrates handling a <strong>UITableView</strong> with two custom <strong>UITableViewController</strong> classes.  Each of which implement the <strong>UITableViewDelegate</strong> and <strong>UITableViewDataSource</strong> protocols.  Listing 7 is a minor variation on this theme and presents a class that implements the <strong>UITableViewDataSource</strong> protocol for a multi-section table.</p>

<p><code></p>

<pre>
<div class="panelHeader">Listing 7: SortedDataSource.lua</div>
waxClass{'SortedDataSource', NS.Object, protocols = {'UITableViewDataSource'}, }


function init(self, source_table)
    self.source_table = source_table
    return self
end


function numberOfSectionsInTableView(self, tableView)
    return #self.source_table.headers
end


function tableView_numberOfRowsInSection(self, tableView, section)
    local index = self.source_table.headers[section+1]
    return #self.source_table[index]
end


function tableView_cellForRowAtIndexPath(self, tableView, indexPath)  
    local identifier = 'TableViewCell'
    local cell = tableView:dequeueReusableCellWithIdentifier(identifier)
    cell = cell or UI.TableViewCell:initWithStyle_reuseIdentifier(UITableViewCellStyleDefault, 
                                                      identifier)

    local key = self.source_table.headers[indexPath:section()+1]
    local component = self.source_table[key]
    local player = component[indexPath:row()+1]
    cell:setText(player[1] .. ' ' .. player[2] .. ' ' .. player[3])

    return cell
end

function tableView_titleForHeaderInSection(self, tableView, section)
    return self.source_table.headers[section+1]
end

</pre>

<p></code></p>

<div class="panelFooter">
Note the <em>toll-free</em> conversion of Lua strings to Objective-C strings in functions such as <strong>tableView_titleForHeaderInSection</strong>.
</div>

<p>The uniform naming scheme Wax uses allows you to easily predict the name to use for accessing an Objective-C function from Lua.  Wax comes with a TextMate bundle which makes it very easy to manipulate the Objective-C calls.  For example, you can paste method signatures copied from Xcode's documentation and have them automatically transformed into Lua calls. (<span class="tiny">I'm debating writing Emacs functions to do this or just drink the Kool-ade and start using TextMate.</span>)</p>

<p>Wax has a number of other goodies including extensions for working with SQLite, easy HTTP requests, XML and JSON handling, and working with Core Graphics transforms and gradients.  In addition, recent updates include the ability to write the App delegate in Lua (rather than having a small Objective-C implementation that launches Wax), and the ability to run tests from the command line (in a headless simulator).  Perhaps the most interesting (and powerful) new development is that Wax now provides an interactive console so you can telnet into the simulator (or a device!) and interact with a running application: tweak its parameters or inspect its current state.</p>

<p>Wax is open source and is also licensed with an MIT-style license.  The project is being actively developed and used by a number of developers.</p>

<h3>Summary</h3>

<p>Lua-powered apps <em>are</em> available in the app store.  The Ansca forum for listing Corona apps has over 150 topics (<span class="tiny">I haven't read them all...they may not <strong>all</strong> announce new apps</span>).   Reading through the Wax mailing list, you'll see several developers announcing apps they've written using Wax, and there are likely to be many others who have not posted to the list.  And there are many apps that have taken the DIY approach.</p>

<p>Corona offers a very nice alternative for building iOS apps, but only if you don't need native UI elements.  It's great that they've added native text fields, but the fact that you can't see them in the simulator is a real show-stopper.  But throw its Android capabilities into the mix, and Corona is certainly worth considering.  I like using Corona and I hope/expect to see lots of improvements over time.  Just to weasel out of making an endorsement, I should add that although I have not had any major problems building apps with Corona, the dependency on Ansca and their servers to do builds is something you should seriously think about.</p>

<p>Of course the DIY approach is the complete opposite: you have total control.  But if you need a lot of interaction between your Lua code and your Objective-C code, doing the bindings can be a fair amount of effort.  Johnson's Wax does a fantastic job of bridging Lua and Objective-C.  Wax also plays well with Lua C libraries&mdash;something which Corona doesn't handle.</p>

<p>Although they may not go as far as we'd like, the recent changes to the iOS developer agreement mean that you can now use Lua in iOS development without the stress of worrying that you're setting youself up for app rejection.  I believe the range of techniques available for using Lua in your iOS project means that using Lua will often be a useful tool for successfully completing your iOS project.  Given the active communities surrounding Corona and Wax, as well as the ease of plotting your own course if you want more direct control of your use of Lua, I encourage you to take advantage of this gem of a language.</p>

<h3>References</h3>

<ul>
<li><a href="http://www.lua.org">Lua's Website</a> (<span class="tiny">http://www.lua.org</span>)</li>
<li><a href="http://developer.anscamobile.com">Corona Developer Portal</a> (<span class="tiny">http://developer.anscamobile.com</span>)</li>
<li><a href="http://github.com/probablycorey/wax/">Wax's Repository</a> (<span class="tiny">http://github.com/probablycorey/wax/</span>)</li>
<li><a href="http://probablyinteractive.com/2009/10/19/How%20does%20iPhone%20Wax%20work.html">Wax Syntax Tips</a> (<span class="tiny">http://probablyinteractive.com/2009/10/19/How%20does%20iPhone%20Wax%20work.html</span>)</li>
<li><a href="http://groups.google.com/group/iphonewax">http://groups.google.com/group/iphonewax</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.luanova.org/ioswithlua/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting Orbit to use Apache2</title>
		<link>http://www.luanova.org/setting-orbit-to-use-apache2/</link>
		<comments>http://www.luanova.org/setting-orbit-to-use-apache2/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 13:44:23 +0000</pubDate>
		<dc:creator>ryanpusztai</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[orbit]]></category>
		<category><![CDATA[web frameworks]]></category>
		<category><![CDATA[webserver]]></category>

		<guid isPermaLink="false">http://luanova.org/?p=70</guid>
		<description><![CDATA[This article will cover how to install Lua Orbit on an Ubuntu 8.04+ server. This will explain how to setup Apache2 and Xavante on the server. This allows for production code to run through Apache2 and then develop using Xavante. To hook Orbit to Apache2 we will be using FastCGI for the best performance. NOTE: [...]]]></description>
			<content:encoded><![CDATA[<p>This article will cover how to install <a href="http://keplerproject.github.com/orbit/index.html">Lua Orbit</a> on an Ubuntu 8.04+ server. This will explain how to setup Apache2 and Xavante on the server. This allows for production code to run through Apache2 and then develop using Xavante. To hook Orbit to Apache2 we will be using <strong>FastCGI</strong> for the best performance.</p>

<p>NOTE: This is just one way to configure your server to run Orbit applications; it does require that you have root access.  There are also local sandboxed methods which we will cover in a future article.</p>

<h2>Installing Apache</h2>

<p>Connect to your server. Use ssh or however your host instructs you, and install Apache2 and support modules:</p>

<pre><code>$ sudo apt-get install apache2 libapache2-mod-fcgid libfcgi-dev build-essential
</code></pre>

<p>Enable required Apache2 modules:</p>

<pre><code> $ sudo a2enmod rewrite
 $ sudo a2enmod fcgid
 $ sudo /etc/init.d/apache2 force-reload
</code></pre>

<h2>Install LuaRocks</h2>

<p>If you are running a fresh distribution (Ubuntu Lucid or Debian Squeeze) then you can get a suitable LuaRocks from the repositories &#8211; otherwise you must install it directly, as described below. If unsure, look at ‘aptitude show luarocks’ &#8211; the luarocks version must be 2.0.x.  This will also make sure you have Lua and its development library:</p>

<pre><code>$ sudo apt-get install luarocks
</code></pre>

<p>Otherwise, you have to do it by hand:</p>

<p>Install Lua, using the appropriate methods for your system.</p>

<pre><code>$ sudo apt-get install lua5.1 liblua5.1-0-dev
</code></pre>

<p>Download and unpack the LuaRocks tarball (http://luarocks.org/releases).</p>

<pre><code>$ wget http://luarocks.org/releases/luarocks-2.0.2.tar.gz
$ tar xvfz luarocks-2.0.2.tar.gz
</code></pre>

<p>Change directories to the new extracted LuaRocks directory and configure. (This will attempt to detect your installation of Lua. If you get any error messages, the main problem will probably be the location of the Lua include files &#8211; this uses the standard Debian/Ubuntu locations.</p>

<pre><code>$ cd luarocks-2.0.2
$ ./configure --with-lua-include=/usr/include/lua5.1"
$ make
$ make install
</code></pre>

<p>Install <strong>WSAPI</strong>, <strong>fcgi</strong>, <strong>Orbit</strong>, and <strong>Xavante</strong></p>

<pre><code>$ sudo luarocks install orbit
$ sudo luarocks install wsapi-xavante
$ sudo luarocks install wsapi-fcgi
</code></pre>

<p>[Optional] Install support libraries; <strong>LuaSQL</strong>,<strong>Sqlite3</strong> and <strong>md5</strong>:</p>

<pre><code>$ sudo apt-get install libmysqlclient15-dev libsqlite3-dev
$ sudo luarocks install md5
$ sudo luarocks install luasql-sqlite3
$ sudo luarocks install luasql-mysql MYSQL_INCDIR=/usr/include/mysql
</code></pre>

<h2>Setting up Apache2</h2>

<p>Edit the site config file with your favourite editor. (default site is named ‘default’)</p>

<pre><code>$ sudo joe /etc/apache2/sites-available/default
</code></pre>

<p>Add this following section below the <code>&lt;Directory /var/www/&gt;</code> section of the config file. If this section has a ‘AllowOverride None’ then you need to change the ‘None’ to ‘All’ so that the .htaccess file can override the configuration locally.</p>

<pre><code>&lt;IfModule mod_fcgid.c&gt;
    AddHandler fcgid-script .lua
    AddHandler fcgid-script .ws
    AddHandler fcgid-script .op
    FCGIWrapper "/usr/local/bin/wsapi.fcgi" .ws
    FCGIWrapper "/usr/local/bin/wsapi.fcgi" .lua
    FCGIWrapper "/usr/local/bin/op.fcgi" .op
    #FCGIServer "/usr/local/bin/wsapi.fcgi" -idle-timeout 60 -processes 1
    #IdleTimeout 60
    #ProcessLifeTime 60
&lt;/IfModule&gt;
</code></pre>

<p>Restart the server.</p>

<p>To enable your application you need to add <strong>+ExecCGI</strong> to an .htaccess file in the root of your Orbit application &#8211; in this case, /var/www.</p>

<pre><code>Options +ExecCGI
DirectoryIndex index.ws
</code></pre>

<p>Here is a simple example of a “Hello World” orbit application. Put this in /var/www/index.ws</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    #<span style="color: #66cc66;">!/</span>usr<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>env wsapi.fcgi
&nbsp;
    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Orbit applications are usually modules,</span>
    <span style="color: #808080; font-style: italic;">-- orbit.new does the necessary initialization</span>
&nbsp;
    module<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;hello&quot;</span>, package.seeall, orbit.new <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- These are the controllers, each receives a web object</span>
    <span style="color: #808080; font-style: italic;">-- that is the request/response, plus any extra captures from the</span>
    <span style="color: #808080; font-style: italic;">-- dispatch pattern. The controller sets any extra headers and/or</span>
    <span style="color: #808080; font-style: italic;">-- the status if it's not 200, then return the response. It's</span>
    <span style="color: #808080; font-style: italic;">-- good form to delegate the generation of the response to a view</span>
    <span style="color: #808080; font-style: italic;">-- function</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: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> say<span style="color: #66cc66;">&#40;</span> web, name <span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> render_say<span style="color: #66cc66;">&#40;</span> web, name <span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Builds the application's dispatch table, you can</span>
    <span style="color: #808080; font-style: italic;">-- pass multiple patterns, and any captures get passed to</span>
    <span style="color: #808080; font-style: italic;">-- the controller</span>
&nbsp;
    hello: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>
    hello:dispatch_get<span style="color: #66cc66;">&#40;</span> say, <span style="color: #ff0000;">&quot;/say/(%a+)&quot;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- These are the view functions referenced by the controllers.</span>
    <span style="color: #808080; font-style: italic;">-- orbit.htmlify does through the functions in the table passed</span>
    <span style="color: #808080; font-style: italic;">-- as the first argument and tries to match their name against</span>
    <span style="color: #808080; font-style: italic;">-- the provided patterns (with an implicit ^ and $ surrounding</span>
    <span style="color: #808080; font-style: italic;">-- the pattern. Each function that matches gets an environment</span>
    <span style="color: #808080; font-style: italic;">-- where HTML functions are created on demand. They either take</span>
    <span style="color: #808080; font-style: italic;">-- nil (empty tags), a string (text between opening and</span>
    <span style="color: #808080; font-style: italic;">-- closing tags), or a table with attributes and a list</span>
    <span style="color: #808080; font-style: italic;">-- of strings that will be the text. The indexing the</span>
    <span style="color: #808080; font-style: italic;">-- functions adds a class attribute to the tag. Functions</span>
    <span style="color: #808080; font-style: italic;">-- are cached.</span>
    <span style="color: #808080; font-style: italic;">--</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- This is a convenience function for the common parts of a page</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_layout<span style="color: #66cc66;">&#40;</span> inner_html <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;Hello&quot;</span> <span style="color: #66cc66;">&#125;</span>,
            body<span style="color: #66cc66;">&#123;</span> inner_html <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_hello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> p.hello <span style="color: #ff0000;">&quot;Hello World!&quot;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <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> render_layout<span style="color: #66cc66;">&#40;</span> render_hello<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;
    <span style="color: #b1b100;">function</span> render_say<span style="color: #66cc66;">&#40;</span> web, name <span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> render_layout<span style="color: #66cc66;">&#40;</span> render_hello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> .. p.hello<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span>
    eb.input.greeting <span style="color: #b1b100;">or</span> <span style="color: #ff0000;">&quot;Hello &quot;</span> <span style="color: #66cc66;">&#41;</span> .. name .. <span style="color: #ff0000;">&quot;!&quot;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    orbit.htmlify<span style="color: #66cc66;">&#40;</span> hello, <span style="color: #ff0000;">&quot;render_.+&quot;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> _M</pre></div></div>


<p>Now you should be able to launch your web browser and goto                http://hostname and you should see “Hello World!”</p>

<p>If you go to http://hostname/index.ws/say/yourname you should see:</p>

<pre><code>“Hello World!
Hello yourname!”
</code></pre>

<p>You are done!  You can now look at LuaNova&#8217;s <a href="http://luanova.org/orbit1-2/">Orbit Introduction</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.luanova.org/setting-orbit-to-use-apache2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Authentication-based permission groups in Sputnik</title>
		<link>http://www.luanova.org/authentication-based-permission-groups-in-sputnik/</link>
		<comments>http://www.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://www.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://www.luanova.org/sputnik/</link>
		<comments>http://www.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://www.luanova.org/sputnik/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lua and the web: an overview</title>
		<link>http://www.luanova.org/lua-web-overview/</link>
		<comments>http://www.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://www.luanova.org/lua-web-overview/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Launch: getting Lua installed</title>
		<link>http://www.luanova.org/launch-getting-lua-installed/</link>
		<comments>http://www.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://www.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://www.luanova.org/a-computer-scientist-a-mathematician-and-an-engineer/</link>
		<comments>http://www.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://www.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://www.luanova.org/3/</link>
		<comments>http://www.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://www.luanova.org/3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

