Mercurial Hosting > luan
annotate website/src/tutorial.html.luan @ 2023:544ccce564f6 default tip
minor
| author | Franklin Schmidt <fschmidt@gmail.com> | 
|---|---|
| date | Mon, 20 Oct 2025 17:34:14 -0600 | 
| parents | bd00b36380d9 | 
| children | 
| rev | line source | 
|---|---|
| 1651 | 1 local Luan = require "luan:Luan.luan" | 
| 2 local error = Luan.error | |
| 3 local Io = require "luan:Io.luan" | |
| 1938 | 4 local Site_translator = require "luan:ai/Site_translator.luan" | 
| 1929 | 5 local get_lang = Site_translator.get_lang or error() | 
| 6 local text_writer = Site_translator.text_writer or error() | |
| 1651 | 7 local Shared = require "site:/lib/Shared.luan" | 
| 8 local head = Shared.head or error() | |
| 1735 | 9 local header = Shared.docs_header or error() | 
| 1651 | 10 | 
| 11 | |
| 12 return function() | |
| 1929 | 13 Io.stdout = text_writer() | 
| 1651 | 14 %> | 
| 1216 | 15 <!doctype html> | 
| 1929 | 16 <html lang="<%=get_lang()%>"> | 
| 1651 | 17 <head> | 
| 18 <% head() %> | |
| 19 <title>Luan Tutorial</title> | |
| 20 </head> | |
| 21 <body> | |
| 1735 | 22 <% header() %> | 
| 1651 | 23 <div content> | 
| 1324 
1660136ac451
website - remove Shared.luan
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1323diff
changeset | 24 | 
| 562 
7cc9d4a53d3b
remove SimplyHTML from documentation
 Franklin Schmidt <fschmidt@gmail.com> parents: 
512diff
changeset | 25 <h1>Luan Tutorial</h1> | 
| 382 | 26 | 
| 27 | |
| 28 <p>Create a file <b>hello.luan</b> containing:</p> | |
| 29 | |
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 30 <code block> | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 31 %> | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 32 Hello World | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 33 <% | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 34 </code> | 
| 382 | 35 | 
| 1928 | 36 <p>To run this, type <code>luan hello.luan</code> on the command line. This should print <b>Hello World</b>.</p> | 
| 382 | 37 | 
| 1827 | 38 <p>The syntax here is based on <a href="https://en.wikipedia.org/wiki/Jakarta_Server_Pages">JSP</a>. Let's change it a little:</p> | 
| 382 | 39 | 
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 40 <code block> | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 41 local name = "Bob" | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 42 %> | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 43 Hello <%= name %> | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 44 <% | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 45 </code> | 
| 382 | 46 | 
| 47 <p>This should print <b>Hello Bob</b>. Now let's try a more conventional approach:</p> | |
| 48 | |
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 49 <code block> | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 50 local Io = require "luan:Io.luan" | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 51 local print = Io.print | 
| 382 | 52 | 
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 53 print("Hello World") | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 54 </code> | 
| 382 | 55 | 
| 1928 | 56 <p>In Luan, a function call with one string argument doesn't require parenthesis, so <code>print("Hello World")</code> is the same as <code>print "Hello World"</code> and <code>require "luan:Io.luan"</code> is the same as <code>require("luan:Io.luan")</code>. Both <code>require</code> and <code>print</code> are functions.</p> | 
| 382 | 57 | 
| 1928 | 58 <p>The <code>require</code> function takes a <a href="https://en.wikipedia.org/wiki/Uniform_Resource_Identifier">URI</a> as an argument. Examples of URIs are <code>luan:Io.luan</code> and <code>file:hello.luan</code>. <code>require</code> is used to import a module, which is returned from the <code>require</code> function call. In the case above, we assign the module to the local variable <code>Io</code>. The function <code>print</code> is a member of this module. We could have done <code>Io.print("Hello World")</code> but instead we chose to assign <code>print</code> to a local variable and use that to call the function.</p> | 
| 382 | 59 | 
| 1928 | 60 <p>Luan starts with only one defined function: <code>require</code>. You will use <code>require</code> to import whatever you need. This is a little more work, but makes it clear in each file where each function comes from.</p> | 
| 382 | 61 | 
| 62 <p>Let's a make fancier version:</p> | |
| 63 | |
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 64 <code block> | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 65 local Io = require "luan:Io.luan" | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 66 local print = Io.print | 
| 382 | 67 | 
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 68 local function hello(name) | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 69 print("Hello "..name) | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 70 end | 
| 382 | 71 | 
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 72 hello("Bob") | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 73 </code> | 
| 382 | 74 | 
| 1928 | 75 <p>The <code>..</code> operator does concatenation. This will print <b>Hello Bob</b>.</p> | 
| 382 | 76 | 
| 1647 | 77 <p>Now let's make a web page. First we need a directory for our website. So create a directory <b>src</b>. In this directory, create a file <b>hi.html.luan</b> containing: | 
| 382 | 78 | 
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 79 <code block> | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 80 local Io = require "luan:Io.luan" | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 81 local Http = require "luan:http/Http.luan" | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 82 | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 83 return function() | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 84 Io.stdout = Http.response.text_writer() | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 85 %> | 
| 1938 | 86 <!doctype html> | 
| 87 <html> | |
| 88 <body> | |
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 89 Hello World | 
| 1938 | 90 </body> | 
| 91 </html> | |
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 92 <% | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 93 end | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 94 </code> | 
| 382 | 95 | 
| 1928 | 96 <p>Now go back to the parent directory and do <code>luan luan:http/serve.luan src</code>. 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. In fact the page that you are currently looking at <b>tutorial.html</b> is generated from <a href="tutorial.html.luan">tutorial.html.luan</a>.</p> | 
| 382 | 97 | 
| 1928 | 98 <p>The Luan webserver expects the file to return a function and calls it to generate the page. Code of the form <code>%>...<%</code> writes its output to <code>Io.stdout</code> which by default is the standard output of the command line. So in the returned function one usually starts by setting <code>Io.stdout</code> to a <code>text_writer</code> which writes its output to the HTTP response (to the web browser).</p> | 
| 382 | 99 | 
| 1659 | 100 <p>You can find this example and others in the <a href="examples/">examples directory</a>. Take a look at <a href="examples/hi2.html.luan">hi2.html.luan</a> next. Remember to remove the <b>.luan</b> from the URL to run the code.</p> | 
| 382 | 101 | 
| 1647 | 102 <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.software</b>. If you don't have a domain, just use a domain like <b>bob.s1.luan.software</b> (anything of the form <b>*.s1.luan.software</b>). Assuming your directory is <b>src</b> and you will use the password <b>secret</b>, do the following from the command line: | 
| 382 | 103 | 
| 1812 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 104 <code block> | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 105 luan luan:host/push.luan bob.s1.luan.software secret src | 
| 
f44dcb3fedf7
docs - add code block
 Franklin Schmidt <fschmidt@gmail.com> parents: 
1735diff
changeset | 106 </code> | 
| 382 | 107 | 
| 1928 | 108 <p>The form is <code>luan luan:host/push.luan domain password directory</code>. If you change your site, just run this again and your site will be updated. To delete your site, do <code>luan luan:host/delete.luan domain password</code>.</p> | 
| 382 | 109 | 
| 110 <p>Hopefully this short tutorial gave you an idea of how to use Luan to make a website.</p> | |
| 111 | |
| 1651 | 112 </div> | 
| 113 </body> | |
| 391 
2f5cc9c2cbf0
replace Html.simply_html_page with simply_html_head and simply_html_body_bottom
 Franklin Schmidt <fschmidt@gmail.com> parents: 
387diff
changeset | 114 </html> | 
| 1651 | 115 <% | 
| 116 end | 
