Mercurial Hosting > luan
annotate website/src/tutorial.html.luan @ 386:db23f654f87d
make all of website use luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 23 Apr 2015 18:09:12 -0600 |
parents | 4118eb51c816 |
children | 23d075ce1e48 |
rev | line source |
---|---|
382 | 1 local Io = require "luan:Io" |
2 local Html = require "luan:Html" | |
3 local Http = require "luan:web/Http" | |
4 | |
386
db23f654f87d
make all of website use luan
Franklin Schmidt <fschmidt@gmail.com>
parents:
383
diff
changeset
|
5 |
382 | 6 function service() |
7 Io.stdout = Http.response.text_writer() | |
8 Html.simply_html_page{ | |
9 head = function() %> | |
10 <title>Luan Tutorial</title> | |
11 <% end; | |
12 body = function() %> | |
13 | |
14 <div container> | |
15 <div><small><a href="/">Luan</a></small></div> | |
16 <h1 margin-bottom="1em">Luan Tutorial</h1> | |
17 | |
18 | |
19 <p>Create a file <b>hello.luan</b> containing:</p> | |
20 | |
21 <p><tt><pre><%=Html.encode[[ | |
22 %> | |
23 Hello World | |
24 <% | |
25 ]]%></pre></tt></p> | |
26 | |
27 <p>To run this, type <b>luan file:hello</b> on the command line. This should print <b>Hello World</b>.</p> | |
28 | |
29 <p>The syntax here is based on <a href="http://en.wikipedia.org/wiki/JavaServer_Pages">JSP</a>. Let's change it a little:</p> | |
30 | |
31 <p><tt><pre><%=Html.encode[[ | |
32 name = "Bob" | |
33 %> | |
34 Hello <%= name %> | |
35 <% | |
36 ]]%></pre></tt></p> | |
37 | |
38 <p>This should print <b>Hello Bob</b>. Now let's try a more conventional approach:</p> | |
39 | |
40 <p><tt><pre> | |
41 local Io = require "luan:Io" | |
42 local print = Io.print | |
43 | |
44 print("Hello World") | |
45 </pre></tt></p> | |
46 | |
47 <p>In Luan, a function call with one string argument doesn't require parenthesis, so <b>print("Hello World")</b> is the same as <b>print "Hello World"</b> and <b>require "luan:Io"</b> is the same as <b>require("luan:Io")</b>. Both <b>require</b> and <b>print</b> are functions.</p> | |
48 | |
49 <p>The <b>require</b> function takes a <a href="http://en.wikipedia.org/wiki/Uniform_resource_identifier">URI</a> as an argument. Examples of URIs are "<b>luan:Io</b>" and "<b>file:hello</b>". <b>require</b> is used to import a module, which is returned from the <b>require</b> function call. In the case above, we assign the module to the local variable <b>Io</b>. The function <b>print</b> is a member of this module. We could have done <b>Io.print("Hello World")</b> but instead we chose to assign <b>print</b> to a local variable and use that to call the function.</p> | |
50 | |
51 <p>Luan starts with only two defined functions: <b>require</b> and <b>java</b>. You will use <b>require</b> to import whatever you need. This is a little more work, but makes it clear in each file where each function comes from.</p> | |
52 | |
53 <p>Let's a make fancier version:</p> | |
54 | |
55 <p><tt><pre> | |
56 local Io = require "luan:Io" | |
57 local print = Io.print | |
58 | |
383 | 59 function hello(name) |
382 | 60 print("Hello "..name) |
61 end | |
62 | |
63 hello("Bob") | |
64 </pre></tt></p> | |
65 | |
66 <p>The <b>..</b> operator does concatenation. This will print <b>Hello Bob</b>.</p> | |
67 | |
68 <p>Now let's make a web page. First we need a directory for our website. So create a directory <b>site</b>. In this directory, create a file <b>hi.luan</b> containing: | |
69 | |
70 <p><tt><pre><%=Html.encode[[ | |
71 local Io = require "luan:Io" | |
72 local Http = require "luan:web/Http" | |
73 | |
74 function service() | |
75 Io.stdout = Http.response.text_writer() | |
76 %> | |
77 <html> | |
78 <body> | |
79 Hello World | |
80 </body> | |
81 </html> | |
82 <% | |
83 end | |
84 ]]%></pre></tt></p> | |
85 | |
86 <p>Now go back to the parent directory and do <b>luan luan:web/serve file:site</b>. This will run the Luan web server on port 8080. Try going to <a href="http://localhost:8080/">http://localhost:8080/</a>. You should see the directory. If you click on <b>hi.luan</b> you will see the source. But if you remove the <b>.luan</b> and just go to <a href="http://localhost:8080/hi">http://localhost:8080/hi</a> then you will run the program which will generate the web page.</p> | |
87 | |
88 <p>The Luan webserver looks for function named <b>service</b> in the file and calls it to generate the page. Code of the form <b><%=Html.encode[[%>...<%]]%></b> writes its output to <b>Io.stdout</b> which by default is the standard output of the command line. So in <b>service</b> one usually starts by setting <b>Io.stdout</b> to a <tt>text_writer</tt> which writes its output to the HTTP response (to the web browser).</p> | |
89 | |
90 <p>You can find this example and others in the <a href="examples">examples directory</a>. Take a look at <a href="examples/hi2.luan">hi2.luan</a> next. Remember to remove the <b>.luan</b> from the URL to run the code. And by the way, you can see the source for this page at <a href="tutorial.html.luan">tutorial.html.luan</a>.</p> | |
91 | |
92 <p>So now you have built your website and you want publish it to the web. If you have your own domain, create a CNAME record for it pointing to <b>s1.luanhost.com</b>. If you don't have a domain, just use a domain like <b>bob.s1.luanhost.com</b> (anything of the form <b>*.s1.luanhost.com</b>). Assuming your directory is <b>site</b> and you will use the password <b>secret</b>, do the following from the command line: | |
93 | |
94 <p><tt><pre> | |
95 luan luan:host/push bob.s1.luanhost.com secret site | |
96 </pre></tt></p> | |
97 | |
98 <p>The form is <b>luan luan:host/push domain password directory</b>. If you change your site, just run this again and your site will be updated. To delete your site, do <b>luan luan:host/delete domain password</b>.</p> | |
99 | |
100 <p>Hopefully this short tutorial gave you an idea of how to use Luan to make a website.</p> | |
101 | |
102 </div> | |
103 | |
104 <% end; | |
105 } | |
106 end |