src/doc/commands: move `render_code` to clients modules
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 12 Jun 2018 15:25:31 +0000 (11:25 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 22 Jun 2018 03:42:10 +0000 (23:42 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/commands/commands_html.nit
src/doc/commands/commands_json.nit
src/doc/commands/commands_md.nit
src/doc/commands/commands_model.nit

index 88ddfd1..3d74a6b 100644 (file)
@@ -127,9 +127,35 @@ end
 
 redef class CmdCode
        redef fun to_html do
-               var output = render_code(node)
-               if output == null then return ""
-               return "<pre>{output.write_to_string}</pre>"
+               var node = self.node
+               if node == null then return ""
+
+               var code = render_code(node)
+               return "<pre>{code.write_to_string}</pre>"
+       end
+
+       redef fun render_code(node) do
+               if format == "html" then
+                       var hl = new CmdHtmlightVisitor
+                       hl.show_infobox = false
+                       hl.highlight_node node
+                       return hl.html
+               end
+               return super
+       end
+end
+
+# Custom HtmlightVisitor for commands
+#
+# We create a new subclass so its behavior can be refined in clients without
+# breaking the main implementation.
+class CmdHtmlightVisitor
+       super HtmlightVisitor
+
+       redef fun hrefto(mentity) do
+               if mentity isa MClassDef then return mentity.mclass.html_url
+               if mentity isa MPropDef then return mentity.mproperty.html_url
+               return mentity.html_url
        end
 end
 
@@ -333,13 +359,3 @@ redef class CmdTesting
                return "<pre>{command}</pre>"
        end
 end
-
-# Misc
-
-redef class CmdHtmlightVisitor
-       redef fun hrefto(mentity) do
-               if mentity isa MClassDef then return mentity.mclass.html_url
-               if mentity isa MPropDef then return mentity.mproperty.html_url
-               return mentity.html_url
-       end
-end
index cdab14e..79a49be 100644 (file)
@@ -90,13 +90,11 @@ redef class CmdCode
        redef fun to_json do
                var obj = new JsonObject
                var node = self.node
-               if node != null then
-                       obj["location"] = node.location
-               end
-               var output = render_code(node)
-               if output != null then
-                       obj["code"] = output.write_to_string
-               end
+               if node == null then return obj
+
+               var code = render_code(node)
+               obj["location"] = node.location
+               obj["code"] = code.write_to_string
                return obj
        end
 end
index b2af510..2670ebd 100644 (file)
@@ -112,15 +112,25 @@ end
 
 redef class CmdCode
        redef fun to_md do
-               var output = render_code(node)
-               if output == null then return ""
+               var node = self.node
+               if node == null then return ""
 
+               var code = render_code(node)
                var tpl = new Template
                tpl.addn "~~~nit"
-               tpl.add output.write_to_string
+               tpl.add code.write_to_string
                tpl.addn "~~~"
                return tpl.write_to_string
        end
+
+       redef fun render_code(node) do
+               if format == "ansi" then
+                       var hl = new AnsiHighlightVisitor
+                       hl.highlight_node node
+                       return hl.result
+               end
+               return super
+       end
 end
 
 redef class CmdAncestors
index ff7862e..3a7215b 100644 (file)
 module commands_model
 
 import commands_base
-
-import model::model_collect
 import modelize
-import modelbuilder
-import htmlight
 
 # Retrieve the MDoc related to a MEntity
 class CmdComment
@@ -439,30 +435,11 @@ abstract class CmdCode
        var format = "raw" is optional, writable
 
        # Render `node` depending on the selected `format`
-       fun render_code(node: nullable ANode): nullable Writable do
-               if node == null then return null
-               if format == "html" then
-                       var hl = new CmdHtmlightVisitor
-                       hl.show_infobox = false
-                       hl.highlight_node node
-                       return hl.html
-               else if format == "ansi" then
-                       var hl = new AnsiHighlightVisitor
-                       hl.highlight_node node
-                       return hl.result
-               end
+       fun render_code(node: ANode): Writable do
                return node.location.text
        end
 end
 
-# Custom HtmlightVisitor for commands
-#
-# We create a new subclass so its behavior can be refined in clients without
-# breaking the main implementation.
-class CmdHtmlightVisitor
-       super HtmlightVisitor
-end
-
 # Cmd that finds the source code related to an `mentity`
 class CmdEntityCode
        super CmdEntity