nitweb: add /uml/:namespace route
authorAlexandre Terrasa <alexandre@moz-code.org>
Wed, 20 Apr 2016 22:54:47 +0000 (18:54 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 20 Apr 2016 22:55:07 +0000 (18:55 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/nitweb.nit
src/web/web_actions.nit
src/web/web_views.nit

index e62d57d..b924ec2 100644 (file)
@@ -52,6 +52,7 @@ private class NitwebPhase
                srv.routes.add new Route("/doc/:namespace", new DocAction(srv, model, modelbuilder))
                srv.routes.add new Route("/code/:namespace", new CodeAction(srv, model, modelbuilder))
                srv.routes.add new Route("/search/:namespace", new SearchAction(srv, model))
+               srv.routes.add new Route("/uml/:namespace", new UMLDiagramAction(srv, model, mainmodule))
                srv.routes.add new Route("/", new TreeAction(srv, model))
 
                srv.listen
index 1357d37..f4c4584 100644 (file)
@@ -16,6 +16,7 @@
 module web_actions
 
 import web_views
+import uml
 
 # Display the tree of all loaded mentities.
 class TreeAction
@@ -98,6 +99,41 @@ class DocAction
        end
 end
 
+# Return an UML diagram for `namespace`.
+class UMLDiagramAction
+       super ModelAction
+
+       # Mainmodule used for hierarchy flattening.
+       var mainmodule: MModule
+
+       redef fun answer(request, url) do
+               var namespace = request.param("namespace")
+               if namespace == null or namespace.is_empty then
+                       return render_error(400, "Missing :namespace.")
+               end
+               var model = init_model_view(request)
+               var mentities = model.mentities_by_namespace(namespace)
+               if mentities.is_empty then
+                       return render_error(404, "No mentity matching this namespace.")
+               end
+               var mentity = mentities.first
+               if mentity isa MClassDef then mentity = mentity.mclass
+
+               var dot
+               if mentity isa MClass then
+                       var uml = new UMLModel(model, mainmodule)
+                       dot = uml.generate_class_uml.write_to_string
+               else if mentity isa MModule then
+                       var uml = new UMLModel(model, mentity)
+                       dot = uml.generate_package_uml.write_to_string
+               else
+                       return render_error(404, "No diagram matching this namespace.")
+               end
+               var view = new HtmlDotPage(dot, mentity.html_name)
+               return render_view(view)
+       end
+end
+
 # Return a random list of MEntities.
 class RandomAction
        super ModelAction
index 5f49f84..950c839 100644 (file)
@@ -119,3 +119,29 @@ class HtmlDocPage
                return tpl
        end
 end
+
+# Display the source for each mentities
+class HtmlDotPage
+       super NitView
+
+       # Dot to process.
+       var dot: Text
+
+       # Page title.
+       var title: String
+
+       redef fun render(srv) do
+               var tpl = new Template
+               tpl.add new Header(1, title)
+               tpl.add render_dot
+               return tpl
+       end
+
+       private fun render_dot: String do
+               var proc = new ProcessDuplex("dot", "-Tsvg", "-Tcmapx")
+               var svg = proc.write_and_read(dot)
+               proc.close
+               proc.wait
+               return svg
+       end
+end