4 fun html_document
(body
: String): String do
9 <title>Nitiwiki Edit</title>
17 class EditMarkdownAction
20 redef fun answer
(http_request
, turi
)
22 var response
= new HttpResponse(200)
23 var file_path
= turi
.substring
(1, turi
.length
)
24 var md_file
= new FileReader.open
(file_path
)
25 response
.body
= html_document
("""
26 <form method="POST" action="/preview">
27 You may edit the file. When you are done, click on "Submit".<br/>
28 <textarea name="content" rows="30" cols="80" style="font-family: Courier">""" + md_file
.read_all
+ """</textarea><br/>
29 <input type="submit" name="action" value="Preview">
30 <input type="submit" name="action" value="Submit">
31 <input type="hidden" name="filepath" value="""" + file_path + """">
38 class PreviewMarkdown2HTMLAction
41 redef fun answer(http_request, turi)
43 var response = new HttpResponse(200)
44 var content = http_request.post_args["content"]
45 var action = http_request.post_args["action"]
46 var file_path = http_request.post_args["filepath"]
47 if action == "Preview" then
48 response.body = html_document("""
50 """ + content.md_to_html.to_s + """
53 else if action == "Submit" then
54 var md_file = new FileWriter.open(file_path)
55 md_file.write(content)
56 response.body = html_document("""
65 var vh = new VirtualHost("localhost:8080")
67 # Serve Markdown editing
68 vh.routes.add new Route("/edit", new EditMarkdownAction)
69 vh.routes.add new Route("/preview", new PreviewMarkdown2HTMLAction)
71 # Serve everything else with a standard `FileServer`
72 vh.routes.add new Route(null, new FileServer("/var/www/"))
74 var factory = new HttpFactory.and_libevent
75 factory.config.virtual_hosts.add vh