comparison website/src/tutorial.html.luan @ 1928:e16f38f4fdfc default tip

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 27 Apr 2025 04:24:08 -0600
parents 50e570b598b2
children
comparison
equal deleted inserted replaced
1927:1461449b6074 1928:e16f38f4fdfc
29 %&gt; 29 %&gt;
30 Hello World 30 Hello World
31 &lt;% 31 &lt;%
32 </code> 32 </code>
33 33
34 <p>To run this, type <b>luan hello.luan</b> on the command line. This should print <b>Hello World</b>.</p> 34 <p>To run this, type <code>luan hello.luan</code> on the command line. This should print <b>Hello World</b>.</p>
35 35
36 <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> 36 <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>
37 37
38 <code block> 38 <code block>
39 local name = "Bob" 39 local name = "Bob"
49 local print = Io.print 49 local print = Io.print
50 50
51 print("Hello World") 51 print("Hello World")
52 </code> 52 </code>
53 53
54 <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> 54 <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>
55 55
56 <p>The <b>require</b> function takes a <a href="https://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> 56 <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>
57 57
58 <p>Luan starts with only one defined function: <b>require</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> 58 <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>
59 59
60 <p>Let's a make fancier version:</p> 60 <p>Let's a make fancier version:</p>
61 61
62 <code block> 62 <code block>
63 local Io = require "luan:Io.luan" 63 local Io = require "luan:Io.luan"
68 end 68 end
69 69
70 hello("Bob") 70 hello("Bob")
71 </code> 71 </code>
72 72
73 <p>The <b>..</b> operator does concatenation. This will print <b>Hello Bob</b>.</p> 73 <p>The <code>..</code> operator does concatenation. This will print <b>Hello Bob</b>.</p>
74 74
75 <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: 75 <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:
76 76
77 <code block> 77 <code block>
78 local Io = require "luan:Io.luan" 78 local Io = require "luan:Io.luan"
89 &lt;/html> 89 &lt;/html>
90 &lt;% 90 &lt;%
91 end 91 end
92 </code> 92 </code>
93 93
94 <p>Now go back to the parent directory and do <b>luan luan:http/serve.luan src</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. 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> 94 <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>
95 95
96 <p>The Luan webserver expects the file to return a function and calls it to generate the page. Code of the form <b>%&gt;...&lt;%</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> 96 <p>The Luan webserver expects the file to return a function and calls it to generate the page. Code of the form <code>%&gt;...&lt;%</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>
97 97
98 <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> 98 <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>
99 99
100 <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: 100 <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:
101 101
102 <code block> 102 <code block>
103 luan luan:host/push.luan bob.s1.luan.software secret src 103 luan luan:host/push.luan bob.s1.luan.software secret src
104 </code> 104 </code>
105 105
106 <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> 106 <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>
107 107
108 <p>Hopefully this short tutorial gave you an idea of how to use Luan to make a website.</p> 108 <p>Hopefully this short tutorial gave you an idea of how to use Luan to make a website.</p>
109 109
110 </div> 110 </div>
111 </body> 111 </body>