X-Git-Url: http://nitlanguage.org diff --git a/src/examples/nitlight_as_a_service.nit b/src/examples/nitlight_as_a_service.nit index a9cead1..103a3c8 100644 --- a/src/examples/nitlight_as_a_service.nit +++ b/src/examples/nitlight_as_a_service.nit @@ -22,47 +22,7 @@ import highlight import nitcorn import nitcorn::log import template - -# Fully process a content as a nit source file. -fun hightlightcode(hl: HighlightVisitor, content: String): SourceFile -do - # Prepare a stand-alone tool context - var tc = new ToolContext - tc.nit_dir = tc.locate_nit_dir # still use the common lib to have core - tc.keep_going = true # no exit, obviously - tc.opt_warn.value = -1 # no output, obviously - - # Prepare an stand-alone model and model builder. - # Unfortunately, models are enclosing and append-only. - # There is no way (yet?) to have a shared module `core` with - # isolated and throwable user modules. - var model = new Model - var mb = new ModelBuilder(model, tc) - - # Parse the code - var source = new SourceFile.from_string("", content) - var lexer = new Lexer(source) - var parser = new Parser(lexer) - var tree = parser.parse - - # Check syntax error - var eof = tree.n_eof - if eof isa AError then - mb.error(eof, eof.message) - hl.hightlight_source(source) - return source - end - var amodule = tree.n_base.as(not null) - - # Load the AST as a module in the model - # Then process it - mb.load_rt_module(null, amodule, "") - mb.run_phases - - # Highlight the processed module - hl.enter_visit(amodule) - return source -end +import json::serialization_write # Nitcorn service to hightlight code # @@ -74,13 +34,40 @@ class HighlightAction 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 @@ -88,10 +75,7 @@ class HighlightAction

""" - if code != null then - # There is code? Process it - var source = hightlightcode(hl, code) - + if hlcode != null then # Inject highlight page.add "
"
 			page.add hl.html.write_to_string
@@ -99,14 +83,50 @@ class HighlightAction
 			page.add ""
+		else
+			page.add "
" end page.add hl.foot_content + + # Call codemirror + page.add """ + + """