From 9be129d9addeb7c308fed421f235527b4a84e7c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexandre=20Blondin=20Mass=C3=A9?= Date: Sun, 29 Nov 2015 01:21:30 -0500 Subject: [PATCH] Allowing user edit in Nitiwiki (not finished). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexandre Blondin Massé --- contrib/nitiwiki/src/wiki_edit.nit | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 contrib/nitiwiki/src/wiki_edit.nit diff --git a/contrib/nitiwiki/src/wiki_edit.nit b/contrib/nitiwiki/src/wiki_edit.nit new file mode 100644 index 0000000..725f788 --- /dev/null +++ b/contrib/nitiwiki/src/wiki_edit.nit @@ -0,0 +1,76 @@ +import nitcorn +import markdown + +fun html_document(body: String): String do + return """ + + + + Nitiwiki Edit + + + """ + body + """ + + """ +end + +class EditMarkdownAction + super Action + + redef fun answer(http_request, turi) + do + var response = new HttpResponse(200) + var file_path = turi.substring(1, turi.length) + var md_file = new FileReader.open(file_path) + response.body = html_document(""" +
+ You may edit the file. When you are done, click on "Submit".
+
+ + + +
""") + md_file.close + return response + end +end + +class PreviewMarkdown2HTMLAction + super Action + + redef fun answer(http_request, turi) + do + var response = new HttpResponse(200) + var content = http_request.post_args["content"] + var action = http_request.post_args["action"] + var file_path = http_request.post_args["filepath"] + if action == "Preview" then + response.body = html_document(""" +

+ """ + content.md_to_html.to_s + """ +

+ """) + else if action == "Submit" then + var md_file = new FileWriter.open(file_path) + md_file.write(content) + response.body = html_document(""" +

Updated file!

+ """) + md_file.close + end + return response + end +end + +var vh = new VirtualHost("localhost:8080") + +# Serve Markdown editing +vh.routes.add new Route("/edit", new EditMarkdownAction) +vh.routes.add new Route("/preview", new PreviewMarkdown2HTMLAction) + +# Serve everything else with a standard `FileServer` +vh.routes.add new Route(null, new FileServer("/var/www/")) + +var factory = new HttpFactory.and_libevent +factory.config.virtual_hosts.add vh +factory.run -- 1.7.9.5