# This file is part of NIT ( http://www.nitlanguage.org ). # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # This is an example of a client of the frontend without command-line processing. # # It offers a simple nitcorn web server that offers a textarea and nitpick and nitlignt it. module nitlight_as_a_service import frontend import highlight import nitcorn import nitcorn::log import template import json::serialization_write # Nitcorn service to hightlight code # # It's a single stand-alone page that has to form to itself. class HighlightAction super Action redef fun answer(http_request, turi) do var hl = new HighlightVisitor var page = new Template # There is code? Process it var code = http_request.post_args.get_or_null("code") var hlcode = null if code != null then hlcode = hightlightcode(hl, code) if http_request.post_args.get_or_null("json") == "true" and hlcode != null then var response = new HttpResponse(200) response.header["Content-Type"] = "text/json" response.body = hlcode.to_json return response end if http_request.post_args.get_or_null("ajax") == "true" and hlcode != null then page.add hlcode.code_mirror_update page.add """ document.getElementById("lightcode").innerHTML = "{{{hl.html.write_to_string.escape_to_c}}}"; nitmessage(); """ var response = new HttpResponse(200) response.header["Content-Type"] = "application/javascript" response.body = page.write_to_string return response end page.add """
{{{hl.head_content}}} """ # Add the form+textarea page.add """ """ if hlcode != null then # Inject highlight page.add ""
page.add hl.html.write_to_string
page.add "
"
end
page.add hl.foot_content
# Call codemirror
page.add """
"""
var response = new HttpResponse(200)
response.header["Content-Type"] = "text/html"
response.body = page.write_to_string
return response
end
end
var host = "localhost:8080"
if args.length > 0 then host = args.first
var vh = new VirtualHost(host)
vh.routes.add new Route("/", new HighlightAction)
var factory = new HttpFactory.and_libevent
factory.config.virtual_hosts.add vh
factory.run