Mercurial Hosting > luan
comparison website/src/tutorial.html.luan @ 693:ca169567ce07
module URIs must now include ".luan"
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 19 Apr 2016 15:54:11 -0600 |
parents | 33f1b4ad2c9d |
children | 2113294ccc6d |
comparison
equal
deleted
inserted
replaced
692:7bd97d642f37 | 693:ca169567ce07 |
---|---|
1 local Io = require "luan:Io" | 1 local Io = require "luan:Io.luan" |
2 local Html = require "luan:Html" | 2 local Html = require "luan:Html.luan" |
3 local Http = require "luan:http/Http" | 3 local Http = require "luan:http/Http.luan" |
4 local Shared = require "site:/Shared" | 4 local Shared = require "site:/Shared.luan" |
5 | 5 |
6 | 6 |
7 return function() | 7 return function() |
8 Io.stdout = Http.response.text_writer() | 8 Io.stdout = Http.response.text_writer() |
9 %> | 9 %> |
41 ]]%></pre> | 41 ]]%></pre> |
42 | 42 |
43 <p>This should print <b>Hello Bob</b>. Now let's try a more conventional approach:</p> | 43 <p>This should print <b>Hello Bob</b>. Now let's try a more conventional approach:</p> |
44 | 44 |
45 <pre> | 45 <pre> |
46 local Io = require "luan:Io" | 46 local Io = require "luan:Io.luan" |
47 local print = Io.print | 47 local print = Io.print |
48 | 48 |
49 print("Hello World") | 49 print("Hello World") |
50 </pre> | 50 </pre> |
51 | 51 |
52 <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> | 52 <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.luan"</b> is the same as <b>require("luan:Io.luan")</b>. Both <b>require</b> and <b>print</b> are functions.</p> |
53 | 53 |
54 <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> | 54 <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.luan</b>" and "<b>file:hello.luan</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> |
55 | 55 |
56 <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> | 56 <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> |
57 | 57 |
58 <p>Let's a make fancier version:</p> | 58 <p>Let's a make fancier version:</p> |
59 | 59 |
60 <pre> | 60 <pre> |
61 local Io = require "luan:Io" | 61 local Io = require "luan:Io.luan" |
62 local print = Io.print | 62 local print = Io.print |
63 | 63 |
64 local function hello(name) | 64 local function hello(name) |
65 print("Hello "..name) | 65 print("Hello "..name) |
66 end | 66 end |
71 <p>The <b>..</b> operator does concatenation. This will print <b>Hello Bob</b>.</p> | 71 <p>The <b>..</b> operator does concatenation. This will print <b>Hello Bob</b>.</p> |
72 | 72 |
73 <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: | 73 <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: |
74 | 74 |
75 <pre><%=Html.encode[[ | 75 <pre><%=Html.encode[[ |
76 local Io = require "luan:Io" | 76 local Io = require "luan:Io.luan" |
77 local Http = require "luan:http/Http" | 77 local Http = require "luan:http/Http.luan" |
78 | 78 |
79 return function() | 79 return function() |
80 Io.stdout = Http.response.text_writer() | 80 Io.stdout = Http.response.text_writer() |
81 %> | 81 %> |
82 <html> | 82 <html> |
86 </html> | 86 </html> |
87 <% | 87 <% |
88 end | 88 end |
89 ]]%></pre> | 89 ]]%></pre> |
90 | 90 |
91 <p>Now go back to the parent directory and do <b>luan luan:http/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> | 91 <p>Now go back to the parent directory and do <b>luan luan:http/serve.luan 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> |
92 | 92 |
93 <p>The Luan webserver expects the file to return a function 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 the returned function one usually starts by setting <b>Io.stdout</b> to a <code>text_writer</code> which writes its output to the HTTP response (to the web browser).</p> | 93 <p>The Luan webserver expects the file to return a function 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 the returned function one usually starts by setting <b>Io.stdout</b> to a <code>text_writer</code> which writes its output to the HTTP response (to the web browser).</p> |
94 | 94 |
95 <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> | 95 <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> |
96 | 96 |
97 <p>So now you have built your website and you want to 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: | 97 <p>So now you have built your website and you want to 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: |
98 | 98 |
99 <pre> | 99 <pre> |
100 luan luan:host/push bob.s1.luanhost.com secret site | 100 luan luan:host/push.luan bob.s1.luanhost.com secret site |
101 </pre> | 101 </pre> |
102 | 102 |
103 <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> | 103 <p>The form is <b>luan luan:host/push.luan 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.luan domain password</b>.</p> |
104 | 104 |
105 <p>Hopefully this short tutorial gave you an idea of how to use Luan to make a website.</p> | 105 <p>Hopefully this short tutorial gave you an idea of how to use Luan to make a website.</p> |
106 | 106 |
107 </div> | 107 </div> |
108 | 108 |