highlight: move hightlightcode inside HighlightVisitor
authorJean Privat <jean@pryen.org>
Wed, 29 Nov 2017 19:27:40 +0000 (14:27 -0500)
committerJean Privat <jean@pryen.org>
Wed, 29 Nov 2017 19:27:40 +0000 (14:27 -0500)
Signed-off-by: Jean Privat <jean@pryen.org>

src/examples/nitlight_as_a_service.nit
src/highlight.nit
src/web/api_light.nit

index 103a3c8..cf72574 100644 (file)
@@ -24,7 +24,7 @@ import nitcorn::log
 import template
 import json::serialization_write
 
-# Nitcorn service to hightlight code
+# Nitcorn service to highlight code
 #
 # It's a single stand-alone page that has to form to itself.
 class HighlightAction
@@ -38,7 +38,7 @@ class HighlightAction
                # 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 code != null then hlcode = hl.highlightcode(code)
 
                if http_request.post_args.get_or_null("json") == "true" and hlcode != null then
                        var response = new HttpResponse(200)
index da18cce..7b2f718 100644 (file)
@@ -21,51 +21,6 @@ import pipeline
 import astutil
 import serialization
 
-# Fully process `content` as a Nit source file.
-#
-# Set `print_errors = true` to print errors in the code to the console.
-fun hightlightcode(hl: HighlightVisitor, content: String, print_errors: nullable Bool): HLCode
-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
-       if print_errors != true then tc.opt_warn.value = -1 # no output
-
-       # 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 + "\n")
-       var lexer = new Lexer(source)
-       var parser = new Parser(lexer)
-       var tree = parser.parse
-
-       var hlcode = new HLCode(hl, content, source)
-
-       # Check syntax error
-       var eof = tree.n_eof
-       if eof isa AError then
-               mb.error(eof, eof.message)
-               hl.hightlight_source(source)
-               return hlcode
-       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 hlcode
-end
-
 # A standalone highlighted piece of code
 class HLCode
        super Serializable
@@ -432,6 +387,51 @@ class HighlightVisitor
 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
 <script>$(".popupable").popover({html:true, placement:'top'})/*initialize bootstrap popover*/</script>"""
        end
+
+       # Fully process `content` as a Nit source file.
+       #
+       # Set `print_errors = true` to print errors in the code to the console.
+       fun highlightcode(content: String, print_errors: nullable Bool): HLCode
+       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
+               if print_errors != true then tc.opt_warn.value = -1 # no output
+
+               # 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 + "\n")
+               var lexer = new Lexer(source)
+               var parser = new Parser(lexer)
+               var tree = parser.parse
+
+               var hlcode = new HLCode(self, content, source)
+
+               # Check syntax error
+               var eof = tree.n_eof
+               if eof isa AError then
+                       mb.error(eof, eof.message)
+                       highlight_source(source)
+                       return hlcode
+               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
+               highlight_node(amodule)
+               return hlcode
+       end
 end
 
 redef class HTMLTag
index 784d683..835589e 100644 (file)
@@ -31,7 +31,7 @@ class APILight
 
        redef fun post(req, res) do
                var hl = new HighlightVisitor
-               var hlcode = hightlightcode(hl, req.body)
+               var hlcode = hl.highlightcode(req.body)
                res.json(hlcode)
        end
 end