lib/popcorn: add pug rendering
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 16 May 2017 18:33:25 +0000 (14:33 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 17 May 2017 13:59:14 +0000 (09:59 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/popcorn/examples/templates/example_template.pug [new file with mode: 0644]
lib/popcorn/examples/templates/example_templates.nit
lib/popcorn/examples/templates/tests/test_example_template_pug.res [new file with mode: 0644]
lib/popcorn/examples/templates/tests/test_example_template_pug_file.res [new file with mode: 0644]
lib/popcorn/examples/templates/tests/test_example_templates.nit
lib/popcorn/pop_templates.nit

diff --git a/lib/popcorn/examples/templates/example_template.pug b/lib/popcorn/examples/templates/example_template.pug
new file mode 100644 (file)
index 0000000..0505cee
--- /dev/null
@@ -0,0 +1,2 @@
+h1 Hello #{user}!
+p Welcome on #{mysite}.
index b0f5ee5..bddd3ed 100644 (file)
@@ -71,8 +71,43 @@ class MyTemplateFileHandler
        end
 end
 
+class MyTemplatePugHandler
+       super Handler
+
+       redef fun get(req, res) do
+               # Values to add in the template
+               var json = new JsonObject
+               json["user"] = "Morriar"
+               json["mysite"] = "My super website"
+
+               res.pug("""
+h1 Hello #{user}
+p Welcome to #{mysite}
+               """, json)
+       end
+end
+
+class MyTemplatePugFileHandler
+       super Handler
+
+       # Use template from external file
+       var pug_file = "example_template.pug"
+
+       redef fun get(req, res) do
+               # Values to add in the template
+               var json = new JsonObject
+               json["user"] = "Morriar"
+               json["mysite"] = "My super website"
+
+               # Render it from an external file
+               res.pug_file(pug_file, json)
+       end
+end
+
 var app = new App
 app.use("/", new MyTemplateHandler)
 app.use("/string", new MyTemplateStringHandler)
 app.use("/file", new MyTemplateFileHandler)
+app.use("/pug", new MyTemplatePugHandler)
+app.use("/pug_file", new MyTemplatePugFileHandler)
 app.listen("localhost", 3000)
diff --git a/lib/popcorn/examples/templates/tests/test_example_template_pug.res b/lib/popcorn/examples/templates/tests/test_example_template_pug.res
new file mode 100644 (file)
index 0000000..6e6c247
--- /dev/null
@@ -0,0 +1,3 @@
+
+[Client] curl -s localhost:*****/
+<h1>Hello Morriar</h1><p>Welcome to My super website</p>
diff --git a/lib/popcorn/examples/templates/tests/test_example_template_pug_file.res b/lib/popcorn/examples/templates/tests/test_example_template_pug_file.res
new file mode 100644 (file)
index 0000000..b4676fa
--- /dev/null
@@ -0,0 +1,3 @@
+
+[Client] curl -s localhost:*****/
+<h1>Hello Morriar!</h1><p>Welcome on My super website.</p>
index c976087..3334320 100644 (file)
@@ -62,3 +62,33 @@ class TestExampleTemplateFile
                run_test(app)
        end
 end
+
+class TestExampleTemplatePug
+       super TestPopcorn
+
+       redef fun client_test do
+               system "curl -s {host}:{port}/"
+       end
+
+       fun test_example_template_pug do
+               var app = new App
+               app.use("/", new MyTemplatePugHandler)
+               run_test(app)
+       end
+end
+
+class TestExampleTemplatePugFile
+       super TestPopcorn
+
+       redef fun client_test do
+               system "curl -s {host}:{port}/"
+       end
+
+       fun test_example_template_pug_file do
+               var app = new App
+               var handler = new MyTemplatePugFileHandler
+               handler.pug_file = test_path / "../example_template.pug"
+               app.use("/", handler)
+               run_test(app)
+       end
+end
index 1ba2165..802dd4b 100644 (file)
@@ -43,4 +43,26 @@ redef class HttpResponse
                var tpl = new TemplateString.from_file(file)
                template(tpl, values, status)
        end
+
+       # Render `pug_string` with the command cli `pug` and use data from `json`
+       #
+       # See https://pugjs.org/api/getting-started.html for more details on pug.
+       fun pug(pug_string: nullable String, json: nullable Serializable, status: nullable Int) do
+               var process
+               if json == null then
+                       process = new ProcessDuplex("pug", "-D")
+               else
+                       process = new ProcessDuplex("pug", "-D", "-O", json.to_json)
+               end
+               var out = process.write_and_read(pug_string or else "")
+               process.close
+               html(out, status)
+       end
+
+       # Render `file` with the command cli `pug` and use data from `json`
+       #
+       # See https://pugjs.org/api/getting-started.html for more details on pug.
+       fun pug_file(file: String, json: nullable Serializable, status: nullable Int) do
+               pug(file.to_path.read_all, json, status)
+       end
 end