Mercurial Hosting > luan
annotate website/src/tutorial.html @ 1325:28c1fc6d9d29
website - make docs html
| author | Franklin Schmidt <fschmidt@gmail.com> | 
|---|---|
| date | Thu, 07 Feb 2019 23:00:20 -0700 | 
| parents | website/src/tutorial.html.luan@1660136ac451 | 
| children | 29d6d7d79c41 | 
| rev | line source | 
|---|---|
| 1216 | 1 <!doctype html> | 
| 391 
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
 Franklin Schmidt <fschmidt@gmail.com> parents: 
387diff
changeset | 2 <html> | 
| 
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
 Franklin Schmidt <fschmidt@gmail.com> parents: 
387diff
changeset | 3 <head> | 
| 
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
 Franklin Schmidt <fschmidt@gmail.com> parents: 
387diff
changeset | 4 <title>Luan Tutorial</title> | 
| 562 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 5 <style> | 
| 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 6 @import "/site.css"; | 
| 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 7 </style> | 
| 391 
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
 Franklin Schmidt <fschmidt@gmail.com> parents: 
387diff
changeset | 8 </head> | 
| 
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
 Franklin Schmidt <fschmidt@gmail.com> parents: 
387diff
changeset | 9 <body> | 
| 382 | 10 | 
| 1324 
1660136ac451
website - remove Shared.luan
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1323diff
changeset | 11 <div small><a href="/">Luan</a></div> | 
| 
1660136ac451
website - remove Shared.luan
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1323diff
changeset | 12 | 
| 562 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 13 <h1>Luan Tutorial</h1> | 
| 382 | 14 | 
| 15 | |
| 16 <p>Create a file <b>hello.luan</b> containing:</p> | |
| 17 | |
| 1325 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 18 <pre> | 
| 382 | 19 %> | 
| 20 Hello World | |
| 21 <% | |
| 1325 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 22 </pre> | 
| 382 | 23 | 
| 1045 | 24 <p>To run this, type <b>luan hello.luan</b> on the command line. This should print <b>Hello World</b>.</p> | 
| 382 | 25 | 
| 26 <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> | |
| 27 | |
| 1325 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 28 <pre> | 
| 686 
33f1b4ad2c9d
more documentation fixes
 Franklin Schmidt <fschmidt@gmail.com> parents: 
562diff
changeset | 29 local name = "Bob" | 
| 382 | 30 %> | 
| 31 Hello <%= name %> | |
| 32 <% | |
| 1325 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 33 </pre> | 
| 382 | 34 | 
| 35 <p>This should print <b>Hello Bob</b>. Now let's try a more conventional approach:</p> | |
| 36 | |
| 562 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 37 <pre> | 
| 693 
ca169567ce07
module URIs must now include ".luan"
 Franklin Schmidt <fschmidt@gmail.com> parents: 
686diff
changeset | 38 local Io = require "luan:Io.luan" | 
| 382 | 39 local print = Io.print | 
| 40 | |
| 41 print("Hello World") | |
| 562 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 42 </pre> | 
| 382 | 43 | 
| 693 
ca169567ce07
module URIs must now include ".luan"
 Franklin Schmidt <fschmidt@gmail.com> parents: 
686diff
changeset | 44 <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 | 45 | 
| 693 
ca169567ce07
module URIs must now include ".luan"
 Franklin Schmidt <fschmidt@gmail.com> parents: 
686diff
changeset | 46 <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 | 47 | 
| 48 <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> | |
| 49 | |
| 50 <p>Let's a make fancier version:</p> | |
| 51 | |
| 562 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 52 <pre> | 
| 693 
ca169567ce07
module URIs must now include ".luan"
 Franklin Schmidt <fschmidt@gmail.com> parents: 
686diff
changeset | 53 local Io = require "luan:Io.luan" | 
| 382 | 54 local print = Io.print | 
| 55 | |
| 512 
d96944467ffc
update documentation for luan changes
 Franklin Schmidt <fschmidt@gmail.com> parents: 
505diff
changeset | 56 local function hello(name) | 
| 382 | 57 print("Hello "..name) | 
| 58 end | |
| 59 | |
| 60 hello("Bob") | |
| 562 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 61 </pre> | 
| 382 | 62 | 
| 63 <p>The <b>..</b> operator does concatenation. This will print <b>Hello Bob</b>.</p> | |
| 64 | |
| 1217 | 65 <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 | 66 | 
| 1325 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 67 <pre> | 
| 693 
ca169567ce07
module URIs must now include ".luan"
 Franklin Schmidt <fschmidt@gmail.com> parents: 
686diff
changeset | 68 local Io = require "luan:Io.luan" | 
| 
ca169567ce07
module URIs must now include ".luan"
 Franklin Schmidt <fschmidt@gmail.com> parents: 
686diff
changeset | 69 local Http = require "luan:http/Http.luan" | 
| 382 | 70 | 
| 512 
d96944467ffc
update documentation for luan changes
 Franklin Schmidt <fschmidt@gmail.com> parents: 
505diff
changeset | 71 return function() | 
| 382 | 72 Io.stdout = Http.response.text_writer() | 
| 736 | 73 %> | 
| 1325 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 74 <!doctype html> | 
| 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 75 <html> | 
| 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 76 <body> | 
| 736 | 77 Hello World | 
| 1325 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 78 </body> | 
| 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 79 </html> | 
| 736 | 80 <% | 
| 382 | 81 end | 
| 1325 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 82 </pre> | 
| 382 | 83 | 
| 1217 | 84 <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 | 85 | 
| 1325 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 86 <p>The Luan webserver expects the file to return a function and calls it to generate the page. Code of the form <b>%>...<%</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 | 87 | 
| 1325 
28c1fc6d9d29
website - make docs html
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1324diff
changeset | 88 <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.</p> | 
| 382 | 89 | 
| 1309 
5684a14582c4
move website to www.luan.ws
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1217diff
changeset | 90 <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.luan.ws</b>. If you don't have a domain, just use a domain like <b>bob.s1.luan.ws</b> (anything of the form <b>*.s1.luan.ws</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 | 91 | 
| 562 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 92 <pre> | 
| 1309 
5684a14582c4
move website to www.luan.ws
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1217diff
changeset | 93 luan luan:host/push.luan bob.s1.luan.ws secret site | 
| 562 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 94 </pre> | 
| 382 | 95 | 
| 693 
ca169567ce07
module URIs must now include ".luan"
 Franklin Schmidt <fschmidt@gmail.com> parents: 
686diff
changeset | 96 <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 | 97 | 
| 98 <p>Hopefully this short tutorial gave you an idea of how to use Luan to make a website.</p> | |
| 99 | |
| 391 
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
 Franklin Schmidt <fschmidt@gmail.com> parents: 
387diff
changeset | 100 </body> | 
| 
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
 Franklin Schmidt <fschmidt@gmail.com> parents: 
387diff
changeset | 101 </html> | 
