From: Alexandre Blondin Massé Date: Sun, 29 Nov 2015 06:21:30 +0000 (-0500) Subject: Allowing user edit in Nitiwiki (not finished). X-Git-Tag: v0.8~50^2~7 X-Git-Url: http://nitlanguage.org Allowing user edit in Nitiwiki (not finished). Signed-off-by: Alexandre Blondin Massé --- 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