nitweb: migrate /code to /api/code/:entity
authorAlexandre Terrasa <alexandre@moz-code.org>
Fri, 20 May 2016 02:48:48 +0000 (22:48 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Mon, 30 May 2016 15:04:21 +0000 (11:04 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/nitweb.nit
src/web/model_api.nit
src/web/web_actions.nit

index 48aaaa4..8fb2094 100644 (file)
@@ -51,7 +51,6 @@ private class NitwebPhase
 
                app.use("/api", new APIRouter(model, modelbuilder, mainmodule))
                app.use("/doc/:namespace", new DocAction(model, mainmodule, modelbuilder))
-               app.use("/code/:namespace", new CodeAction(model, modelbuilder))
                app.use("/uml/:namespace", new UMLDiagramAction(model, mainmodule))
                app.use("/", new TreeAction(model, mainmodule))
 
index c4fd56d..78d6063 100644 (file)
@@ -70,6 +70,7 @@ class APIRouter
                use("/list", new APIList(model, mainmodule))
                use("/search", new APISearch(model, mainmodule))
                use("/random", new APIRandom(model, mainmodule))
+               use("/code/:id", new APIEntityCode(model, mainmodule, modelbuilder))
        end
 end
 
@@ -168,3 +169,33 @@ class APIRandom
                res.json arr
        end
 end
+
+# Return the source code of MEntity.
+#
+# Example: `GET /entity/core::Array/code`
+class APIEntityCode
+       super APIHandler
+
+       # Modelbuilder used to access sources.
+       var modelbuilder: ModelBuilder
+
+       redef fun get(req, res) do
+               var mentity = mentity_from_uri(req, res)
+               if mentity == null then return
+               var source = render_source(mentity)
+               if source == null then
+                       res.error 404
+                       return
+               end
+               res.send source
+       end
+
+       # Highlight `mentity` source code.
+       private fun render_source(mentity: MEntity): nullable HTMLTag do
+               var node = modelbuilder.mentity2node(mentity)
+               if node == null then return null
+               var hl = new HighlightVisitor
+               hl.enter_visit node
+               return hl.html
+       end
+end
index ffa5649..0e8175f 100644 (file)
@@ -29,26 +29,6 @@ class TreeAction
        end
 end
 
-# Display a MEntity source code.
-class CodeAction
-       super ModelHandler
-
-       # Modelbuilder used to access sources.
-       var modelbuilder: ModelBuilder
-
-       redef fun get(req, res) do
-               var namespace = req.param("namespace")
-               var model = init_model_view(req)
-               var mentity = find_mentity(model, namespace)
-               if mentity == null then
-                       res.error(404)
-                       return
-               end
-               var view = new HtmlSourcePage(modelbuilder, mentity)
-               res.send_view(view)
-       end
-end
-
 # Display the doc of a MEntity.
 class DocAction
        super ModelHandler