1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # This is an example of a client of the frontend without command-line processing.
17 # It offers a simple nitcorn web server that offers a textarea and nitpick and nitlignt it.
18 module nitlight_as_a_service
25 import json
::serialization_write
27 # Nitcorn service to hightlight code
29 # It's a single stand-alone page that has to form to itself.
33 redef fun answer
(http_request
, turi
)
35 var hl
= new HighlightVisitor
36 var page
= new Template
38 # There is code? Process it
39 var code
= http_request
.post_args
.get_or_null
("code")
41 if code
!= null then hlcode
= hightlightcode
(hl
, code
)
43 if http_request
.post_args
.get_or_null
("json") == "true" and hlcode
!= null then
44 var response
= new HttpResponse(200)
45 response
.header
["Content-Type"] = "text/json"
46 response
.body
= hlcode
.to_json
50 if http_request
.post_args
.get_or_null
("ajax") == "true" and hlcode
!= null then
51 page
.add hlcode
.code_mirror_update
53 document.getElementById("lightcode").innerHTML = "{{{hl.html.write_to_string.escape_to_c}}}";
57 var response
= new HttpResponse(200)
58 response
.header
["Content-Type"] = "application/javascript"
59 response
.body
= page
.write_to_string
64 <!doctype html><html><head>{{{hl.head_content}}}
65 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.16.0/codemirror.css">
68 textarea {width:100%;}
69 .lint-error {font-family: arial; font-size: 70%; background: #ffa; color: #a00; padding: 2px 5px 3px; }
70 .lint-error-icon {color: red; padding: 0 3px; margin-right: 7px;}
73 # Add the form+textarea
75 <form action="#light" method=post><textarea id=code name=code rows=10>{{{code or else ""}}}</textarea><br><input type=submit></form>
78 if hlcode
!= null then
80 page
.add
"<pre id=light><code id=lightcode>"
81 page
.add hl
.html
.write_to_string
82 page
.add
"</code></pre><hr>"
86 for m
in hlcode
.source
.messages
do
87 page
.add
"<li>{m.location.as(not null)}: {m.text}</li>"
91 page
.add
"<pre id=light><code id=lightcode></code></pre>"
94 page
.add hl
.foot_content
98 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.16.0/codemirror.min.js"></script>
100 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
105 # Callback to update codemirror messages
106 if hlcode
!= null then
107 page
.add hlcode
.code_mirror_update
109 page
.add
"function nitmessage()\{\}"
115 function updatePage() {
116 $.post("", { ajax: true, code: editor.getValue()}, function(data) {
118 $(".popupable").popover({html:true, placement:'top'});
123 editor.on("change", function() {
124 clearTimeout(waiting);
125 waiting = setTimeout(updatePage, 500);
127 waiting = setTimeout(updatePage, 500);
133 var response
= new HttpResponse(200)
134 response
.header
["Content-Type"] = "text/html"
135 response
.body
= page
.write_to_string
140 var host
= "localhost:8080"
141 if args
.length
> 0 then host
= args
.first
143 var vh
= new VirtualHost(host
)
144 vh
.routes
.add
new Route("/", new HighlightAction)
145 var factory
= new HttpFactory.and_libevent
146 factory
.config
.virtual_hosts
.add vh