lib/html: adapt HTMLPage & HTMLTag to Streamable
authorJean Privat <jean@pryen.org>
Tue, 18 Mar 2014 15:03:58 +0000 (11:03 -0400)
committerJean Privat <jean@pryen.org>
Tue, 18 Mar 2014 15:03:58 +0000 (11:03 -0400)
And update clients to the new API

Signed-off-by: Jean Privat <jean@pryen.org>

examples/html_page.nit
lib/html.nit
src/nitdoc.nit
src/nitlight.nit
src/nitunit.nit
tests/sav/html_page.res
tests/sav/nitlight_args1.res

index b44f5cc..cf76665 100644 (file)
@@ -101,5 +101,5 @@ class NitHomepage
 end
 
 var page = new NitHomepage
-print page.render
-page.save("nit.html")
+page.write_to stdout
+page.write_to_file("nit.html")
index 48de688..729f529 100644 (file)
@@ -27,6 +27,7 @@ module html
 # HTMLPage use fluent interface so you can chain calls as:
 #      add("div").attr("id", "mydiv").text("My Div")
 class HTMLPage
+       super Streamable
 
        # Define head content
        fun head do end
@@ -37,8 +38,7 @@ class HTMLPage
        private var current: HTMLTag = root
        private var stack = new List[HTMLTag]
 
-       # Render the page as a html string
-       fun render: String do
+       redef fun write_to(stream) do
                root.children.clear
                open("head")
                head
@@ -46,7 +46,8 @@ class HTMLPage
                open("body")
                body
                close("body")
-               return "<!DOCTYPE html>{root.html}"
+               stream.write "<!DOCTYPE html>"
+               root.write_to(stream)
        end
 
        # Add a html tag to the current element
@@ -81,16 +82,11 @@ class HTMLPage
                end
                current = stack.pop
        end
-
-       # Save html page in the specified file
-       fun save(file: String) do
-               var out = new OFStream.open(file)
-               out.write(self.render)
-               out.close
-       end
 end
 
 class HTMLTag
+       super Streamable
+
        # HTML tagname: 'div' for <div></div>
        var tag: String
        init(tag: String) do
@@ -164,7 +160,7 @@ class HTMLTag
        # Clear all child and set the text of element
        #     var p = new HTMLTag("p")
        #     p.text("Hello World!")
-       #     assert p.html      ==  "<p>Hello World!</p>"
+       #     assert p.write_to_string      ==  "<p>Hello World!</p>"
        # Text is escaped see: `standard::String::html_escape`
        fun text(txt: String): HTMLTag do
 
@@ -178,7 +174,7 @@ class HTMLTag
        #     p.append("Hello")
        #     p.add(new HTMLTag("br"))
        #     p.append("World!")
-       #     assert p.html      ==  "<p>Hello<br/>World!</p>"
+       #     assert p.write_to_string      ==  "<p>Hello<br/>World!</p>"
        # Text is escaped see: standard::String::html_escape
        fun append(txt: String): HTMLTag do
                add(new HTMLRaw(txt.html_escape))
@@ -196,22 +192,12 @@ class HTMLTag
                return self
        end
 
-       # Render the element as HTML string
-       fun html: String do
-               var res = new Array[String]
-               render_in(res)
-               return res.to_s
-       end
-
-       # Save html page in the specified file
-       fun save(file: String) do
-               var out = new OFStream.open(file)
+       redef fun write_to(stream) do
                var res = new Array[String]
                render_in(res)
                for r in res do
-                       out.write(r)
+                       stream.write(r)
                end
-               out.close
        end
 
        # In order to avoid recursive concatenation,
@@ -280,6 +266,5 @@ private class HTMLRaw
 
        private var content: String
        init(content: String) do self.content = content
-       redef fun html do return content
        redef fun render_in(res) do res.add content
 end
index 4a5e68e..4eadd19 100644 (file)
@@ -1814,7 +1814,7 @@ redef class AModule
 
        private fun full_markdown: String do
                if n_moduledecl != null and n_moduledecl.n_doc != null then
-                       return n_moduledecl.n_doc.full_markdown.html
+                       return n_moduledecl.n_doc.full_markdown.write_to_string
                end
                return ""
        end
@@ -1841,7 +1841,7 @@ redef class AStdClassdef
        end
 
        private fun full_markdown: String do
-               if n_doc != null then return n_doc.full_markdown.html
+               if n_doc != null then return n_doc.full_markdown.write_to_string
                return ""
        end
 
@@ -1865,7 +1865,7 @@ redef class APropdef
        end
 
        private fun full_markdown: String do
-               if n_doc != null then return n_doc.full_markdown.html
+               if n_doc != null then return n_doc.full_markdown.write_to_string
                return ""
        end
 
index 4243ca9..64bea58 100644 (file)
@@ -86,9 +86,9 @@ for mm in mmodules do
        end
 
        if dir != null then
-               page.save("{dir}/{mm.name}.html")
+               page.write_to_file("{dir}/{mm.name}.html")
        else
-               print page.html
+               page.write_to(stdout)
        end
 end
 
@@ -109,7 +109,7 @@ if dir != null then
                n2.text(mm.name)
        end
        page.add_raw_html "</li></body>"
-       page.save("{dir}/index.html")
+       page.write_to_file("{dir}/index.html")
 
        var v = new HighlightVisitor
        toolcontext.info("write {dir}/style.css", 1)
index 5bb2b67..54e9302 100644 (file)
@@ -247,4 +247,4 @@ end
 
 var file = toolcontext.opt_output.value
 if file == null then file = "nitunit.xml"
-page.save(file)
+page.write_to_file(file)
index 3f14103..079146d 100644 (file)
@@ -1,2 +1,2 @@
 <!DOCTYPE html><html><head><meta charset="utf-8"/><title>Nit</title><link rel="icon" href="http://nitlanguage.org/favicon.ico" type="image/x-icon"/><link rel="stylesheet" href="http://nitlanguage.org/style.css" type="text/css"/><link rel="stylesheet" href="http://nitlanguage.org/local.css" type="text/css"/></head><body><article class="page"><section class="pageheader"><a id='toptitle_first' class='toptitle'>the</a><a id='toptitle_second' class='toptitle' href=''>Nit</a><a id='toptitle_third' class='toptitle' href=''>Programming Language</a><header class="header"><div class="topsubtitle"><p>A Fun Language for Serious Programming</p></div></header></section><div id="pagebody"><section id="content"><h1># What is Nit?</h1><p>Nit is an object-oriented programming language. The goal of Nit is to propose a robust statically typed programming language where structure is not a pain.</p><p>So, what does the famous hello world program look like, in Nit?</p><pre><tt><span class='normal'>print </span><span class='string'>'Hello, World!'</span></tt></pre><h1># Feature Highlights</h1><h2>Usability</h2><p>Nit's goal is to be usable by real programmers for real projects</p><ul><li><a href="http://en.wikipedia.org/wiki/KISS_principle">KISS principle</a></li><li>Script-like language without verbosity nor cryptic statements</li><li>Painless static types: static typing should help programmers</li><li>Efficient development, efficient execution, efficient evolution.</li></ul><h2>Robustness</h2><p>Nit will help you to write bug-free programs</p><ul><li>Strong static typing</li><li>No more NullPointerException</li></ul><h2>Object-Oriented</h2><p>Nit's guideline is to follow the most powerful OO principles</p><ul><li><a href="./everything_is_an_object/">Everything is an object</a></li><li><a href="./multiple_inheritance/">Multiple inheritance</a></li><li><a href="./refinement/">Open classes</a></li><li><a href="./virtual_types/">Virtual types</a></li></ul><h1># Getting Started</h1><p>Get Nit from its Git repository:</p><pre><code>$ git clone http://nitlanguage.org/nit.git</code></pre><p>Build the compiler (may be long):</p><pre><code>$ cd nit
-$ make</code></pre><p>Compile a program:</p><pre><code>$ bin/nitc examples/hello_world.nit</code></pre><p>Execute the program:</p><pre><code>$ ./hello_world</code></pre></section></div></article></body></html>
+$ make</code></pre><p>Compile a program:</p><pre><code>$ bin/nitc examples/hello_world.nit</code></pre><p>Execute the program:</p><pre><code>$ ./hello_world</code></pre></section></div></article></body></html>
\ No newline at end of file
index b680b80..6f3e0b4 100644 (file)
@@ -64,4 +64,4 @@
 <span class="nc_k">var</span> <span class="nc_v nc_i" title="c: C">c</span> = <span class="nc_k">new</span> <a href="base_simple3.html#base_simple3#C"><span class="nc_t" title="base_simple3::C">C</span></a>(<span class="nc_l">9</span>)
 <span class="nc_v nc_i" title="c: C">c</span><span class="nc_o">.</span><a href="base_simple3.html#base_simple3#C#val1"><span class="nc_i" title="base_simple3#C#val1: Int">val1</span></a><span class="nc_o">.</span><a href="base_simple3.html#base_simple3#Int#output"><span class="nc_i" title="base_simple3#Int#output">output</span></a>
 <span class="nc_v nc_i" title="c: C">c</span><span class="nc_o">.</span><a href="base_simple3.html#base_simple3#C#val2"><span class="nc_i" title="base_simple3#C#val2: Int">val2</span></a><span class="nc_o">.</span><a href="base_simple3.html#base_simple3#Int#output"><span class="nc_i" title="base_simple3#Int#output">output</span></a></span>
-</span>
+</span>
\ No newline at end of file