comparison website/src/tutorial.html.luan @ 562:7cc9d4a53d3b

remove SimplyHTML from documentation
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 24 Jun 2015 14:20:00 -0600
parents d96944467ffc
children 33f1b4ad2c9d
comparison
equal deleted inserted replaced
561:363d07e26549 562:7cc9d4a53d3b
7 return function() 7 return function()
8 Io.stdout = Http.response.text_writer() 8 Io.stdout = Http.response.text_writer()
9 %> 9 %>
10 <html> 10 <html>
11 <head> 11 <head>
12 <% Html.simply_html_head() %>
13 <title>Luan Tutorial</title> 12 <title>Luan Tutorial</title>
13 <style>
14 @import "/site.css";
15 </style>
14 </head> 16 </head>
15 <body> 17 <body>
16 18
17 <div container> 19 <div container>
18 <% Shared.header() %> 20 <% Shared.header() %>
19 <h1 margin-bottom="1em">Luan Tutorial</h1> 21 <h1>Luan Tutorial</h1>
20 22
21 23
22 <p>Create a file <b>hello.luan</b> containing:</p> 24 <p>Create a file <b>hello.luan</b> containing:</p>
23 25
24 <p><tt><pre><%=Html.encode[[ 26 <pre><%=Html.encode[[
25 %> 27 %>
26 Hello World 28 Hello World
27 <% 29 <%
28 ]]%></pre></tt></p> 30 ]]%></pre>
29 31
30 <p>To run this, type <b>luan file:hello</b> on the command line. This should print <b>Hello World</b>.</p> 32 <p>To run this, type <b>luan file:hello</b> on the command line. This should print <b>Hello World</b>.</p>
31 33
32 <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> 34 <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>
33 35
34 <p><tt><pre><%=Html.encode[[ 36 <pre><%=Html.encode[[
35 name = "Bob" 37 name = "Bob"
36 %> 38 %>
37 Hello <%= name %> 39 Hello <%= name %>
38 <% 40 <%
39 ]]%></pre></tt></p> 41 ]]%></pre>
40 42
41 <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>
42 44
43 <p><tt><pre> 45 <pre>
44 local Io = require "luan:Io" 46 local Io = require "luan:Io"
45 local print = Io.print 47 local print = Io.print
46 48
47 print("Hello World") 49 print("Hello World")
48 </pre></tt></p> 50 </pre>
49 51
50 <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"</b> is the same as <b>require("luan:Io")</b>. Both <b>require</b> and <b>print</b> are functions.</p>
51 53
52 <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</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>
53 55
54 <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>
55 57
56 <p>Let's a make fancier version:</p> 58 <p>Let's a make fancier version:</p>
57 59
58 <p><tt><pre> 60 <pre>
59 local Io = require "luan:Io" 61 local Io = require "luan:Io"
60 local print = Io.print 62 local print = Io.print
61 63
62 local function hello(name) 64 local function hello(name)
63 print("Hello "..name) 65 print("Hello "..name)
64 end 66 end
65 67
66 hello("Bob") 68 hello("Bob")
67 </pre></tt></p> 69 </pre>
68 70
69 <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>
70 72
71 <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:
72 74
73 <p><tt><pre><%=Html.encode[[ 75 <pre><%=Html.encode[[
74 local Io = require "luan:Io" 76 local Io = require "luan:Io"
75 local Http = require "luan:http/Http" 77 local Http = require "luan:http/Http"
76 78
77 return function() 79 return function()
78 Io.stdout = Http.response.text_writer() 80 Io.stdout = Http.response.text_writer()
82 Hello World 84 Hello World
83 </body> 85 </body>
84 </html> 86 </html>
85 <% 87 <%
86 end 88 end
87 ]]%></pre></tt></p> 89 ]]%></pre>
88 90
89 <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 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>
90 92
91 <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 <tt>text_writer</tt> 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>
92 94
93 <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>
94 96
95 <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:
96 98
97 <p><tt><pre> 99 <pre>
98 luan luan:host/push bob.s1.luanhost.com secret site 100 luan luan:host/push bob.s1.luanhost.com secret site
99 </pre></tt></p> 101 </pre>
100 102
101 <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 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>
102 104
103 <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>
104 106
105 </div> 107 </div>
106 108
107 <% Html.simply_html_body_bottom() %>
108 </body> 109 </body>
109 </html> 110 </html>
110 <% 111 <%
111 end 112 end