annotate website/src/tutorial.html.luan @ 1283:503bde9a7c80

add luan.require() and table.call()
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 21 Dec 2018 09:12:09 -0700
parents 4c2972f4d862
children 5684a14582c4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
1 local Io = require "luan:Io.luan"
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
2 local Html = require "luan:Html.luan"
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
3 local Http = require "luan:http/Http.luan"
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
4 local Shared = require "site:/Shared.luan"
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5
386
db23f654f87d make all of website use luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 383
diff changeset
6
505
7bc63886d4f2 web page modules now return a function
Franklin Schmidt <fschmidt@gmail.com>
parents: 503
diff changeset
7 return function()
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 Io.stdout = Http.response.text_writer()
391
2f5cc9c2cbf0 replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents: 387
diff changeset
9 %>
1216
5dbb552075ff doctype
Franklin Schmidt <fschmidt@gmail.com>
parents: 1045
diff changeset
10 <!doctype html>
391
2f5cc9c2cbf0 replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents: 387
diff changeset
11 <html>
2f5cc9c2cbf0 replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents: 387
diff changeset
12 <head>
2f5cc9c2cbf0 replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents: 387
diff changeset
13 <title>Luan Tutorial</title>
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
14 <style>
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
15 @import "/site.css";
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
16 </style>
391
2f5cc9c2cbf0 replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents: 387
diff changeset
17 </head>
2f5cc9c2cbf0 replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents: 387
diff changeset
18 <body>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 <div container>
387
23d075ce1e48 add website/src/Shared.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 386
diff changeset
21 <% Shared.header() %>
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
22 <h1>Luan Tutorial</h1>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 <p>Create a file <b>hello.luan</b> containing:</p>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
27 <pre><%=Html.encode[[
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 %>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 Hello World
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 <%
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
31 ]]%></pre>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
1045
48506d03e230 tutorial fix
Franklin Schmidt <fschmidt@gmail.com>
parents: 736
diff changeset
33 <p>To run this, type <b>luan hello.luan</b> on the command line. This should print <b>Hello World</b>.</p>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 <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>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
37 <pre><%=Html.encode[[
686
33f1b4ad2c9d more documentation fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 562
diff changeset
38 local name = "Bob"
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 %>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 Hello <%= name %>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 <%
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
42 ]]%></pre>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 <p>This should print <b>Hello Bob</b>. Now let's try a more conventional approach:</p>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
46 <pre>
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
47 local Io = require "luan:Io.luan"
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 local print = Io.print
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 print("Hello World")
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
51 </pre>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
53 <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>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
55 <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>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 <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>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 <p>Let's a make fancier version:</p>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
61 <pre>
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
62 local Io = require "luan:Io.luan"
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 local print = Io.print
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64
512
d96944467ffc update documentation for luan changes
Franklin Schmidt <fschmidt@gmail.com>
parents: 505
diff changeset
65 local function hello(name)
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 print("Hello "..name)
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 end
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 hello("Bob")
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
70 </pre>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 <p>The <b>..</b> operator does concatenation. This will print <b>Hello Bob</b>.</p>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73
1217
4c2972f4d862 .html in examples
Franklin Schmidt <fschmidt@gmail.com>
parents: 1216
diff changeset
74 <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.html.luan</b> containing:
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
76 <pre><%=Html.encode[[
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
77 local Io = require "luan:Io.luan"
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
78 local Http = require "luan:http/Http.luan"
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79
512
d96944467ffc update documentation for luan changes
Franklin Schmidt <fschmidt@gmail.com>
parents: 505
diff changeset
80 return function()
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 Io.stdout = Http.response.text_writer()
736
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
82 %>
1216
5dbb552075ff doctype
Franklin Schmidt <fschmidt@gmail.com>
parents: 1045
diff changeset
83 <!doctype html>
736
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
84 <html>
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
85 <body>
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
86 Hello World
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
87 </body>
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
88 </html>
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
89 <%
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 end
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
91 ]]%></pre>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92
1217
4c2972f4d862 .html in examples
Franklin Schmidt <fschmidt@gmail.com>
parents: 1216
diff changeset
93 <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.html.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.html">http://localhost:8080/hi.html</a> then you will run the program which will generate the web page.</p>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
95 <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>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96
1217
4c2972f4d862 .html in examples
Franklin Schmidt <fschmidt@gmail.com>
parents: 1216
diff changeset
97 <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>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98
401
Franklin Schmidt <fschmidt@gmail.com>
parents: 391
diff changeset
99 <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:
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
101 <pre>
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
102 luan luan:host/push.luan bob.s1.luanhost.com secret site
562
7cc9d4a53d3b remove SimplyHTML from documentation
Franklin Schmidt <fschmidt@gmail.com>
parents: 512
diff changeset
103 </pre>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104
693
ca169567ce07 module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents: 686
diff changeset
105 <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>
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 <p>Hopefully this short tutorial gave you an idea of how to use Luan to make a website.</p>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 </div>
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110
391
2f5cc9c2cbf0 replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents: 387
diff changeset
111 </body>
2f5cc9c2cbf0 replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents: 387
diff changeset
112 </html>
2f5cc9c2cbf0 replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
Franklin Schmidt <fschmidt@gmail.com>
parents: 387
diff changeset
113 <%
382
8557581740db added tutorial
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 end