Merge: Functional api
[nit.git] / src / doc / term / term.nit
index a82d7b8..7d490e4 100644 (file)
@@ -15,7 +15,8 @@
 module term
 
 import commands::commands_parser
-import templates::templates_term
+import templates::term_model
+import templates::md_commands
 
 redef class CommandParser
 
@@ -23,11 +24,10 @@ redef class CommandParser
                var cmd = self.parse(query)
                var error = self.error
 
-               # If not a command, try a comment query
-               if cmd == null and error isa CmdParserError then
-                       error = null
-                       cmd = new CmdComment(view, mentity_name = query)
-                       var opts = new HashMap[String, String]
+               # Translate links to doc commands
+               if cmd isa CmdEntityLink then
+                       cmd = new CmdComment(model, mentity_name = query)
+                       var opts = new CmdOptions
                        var status = cmd.parser_init(query, opts)
                        if not status isa CmdSuccess then error = status
                end
@@ -36,7 +36,8 @@ redef class CommandParser
                        error.print_message(no_color)
                        print ""
                end
-               cmd.as(not null).execute(no_color)
+               if cmd == null then return
+               cmd.execute(no_color)
        end
 end
 
@@ -221,13 +222,12 @@ redef class CmdEntityCode
                else
                        print title
                end
-               if no_color == null or not no_color then
+               var node = self.node
+               if (no_color == null or not no_color) and node != null then
                        var ansi = render_code(node)
-                       if ansi != null then
-                               print "~~~"
-                               print ansi.write_to_string
-                               print "~~~"
-                       end
+                       print "~~~"
+                       print ansi.write_to_string
+                       print "~~~"
                else
                        printn mentity.cs_source_code
                end
@@ -347,3 +347,196 @@ redef class CmdCatalogContributing
                print_list("Packages contributed by `{name}`:", results, no_color)
        end
 end
+
+# CmdIni
+
+redef class CmdIni
+       # Print ini data
+       fun print_ini(title: String, data: nullable String, no_color: nullable Bool) do
+               if data == null then return
+               if no_color == null or not no_color then
+                       print title.bold
+               else
+                       print title
+               end
+               print ""
+               print data
+       end
+end
+
+redef class CmdIniDescription
+       redef fun execute(no_color) do
+               var title = "Description from ini file:"
+               print_ini(title, desc, no_color)
+       end
+end
+
+redef class CmdIniGitUrl
+       redef fun execute(no_color) do
+               var title = "Git URL from ini file:"
+               print_ini(title, url, no_color)
+       end
+end
+
+redef class CmdIniCloneCommand
+       redef fun execute(no_color) do
+               var title = "Git clone command from ini file:"
+               print_ini(title, command, no_color)
+       end
+end
+
+redef class CmdIniIssuesUrl
+       redef fun execute(no_color) do
+               var title = "Issues URL from ini file:"
+               print_ini(title, url, no_color)
+       end
+end
+
+redef class CmdIniMaintainer
+       redef fun execute(no_color) do
+               var title = "Maintainer from ini file:"
+               print_ini(title, maintainer, no_color)
+       end
+end
+
+redef class CmdIniContributors
+       redef fun execute(no_color) do
+               var contributors = self.contributors
+               if contributors == null then return
+               var title = "Contributors list from ini file:"
+               if no_color == null or not no_color then
+                       print title.bold
+               else
+                       print title
+               end
+               print ""
+               for contributor in contributors do
+                       print " * {contributor}"
+               end
+       end
+end
+
+redef class CmdIniLicense
+       redef fun execute(no_color) do
+               var title = "License from ini file:"
+               print_ini(title, license, no_color)
+       end
+end
+
+redef class CmdEntityFile
+
+       # Print file
+       fun print_file(title: String, no_color: nullable Bool) do
+               var file = self.file
+               if file == null then return
+               title = "{title} `{file}`:"
+               if no_color == null or not no_color then
+                       print title.bold
+               else
+                       print title
+               end
+               print ""
+               print file.to_path.read_all
+               print ""
+       end
+end
+
+redef class CmdLicenseFile
+       redef fun execute(no_color) do
+               print_file("License from", no_color)
+       end
+end
+
+redef class CmdContribFile
+       redef fun execute(no_color) do
+               print_file("Contributing rules from", no_color)
+       end
+end
+
+# CmdMain
+
+redef class CmdMains
+       redef fun execute(no_color) do
+               var mentity = self.mentity.as(not null).full_name
+               if no_color == null or not no_color then mentity = mentity.blue.bold
+               print_list("Mains in `{mentity}`:", results, no_color)
+       end
+end
+
+redef class CmdMainCompile
+       redef fun execute(no_color) do
+               var mentity = self.mentity.as(not null).full_name
+               if no_color == null or not no_color then mentity = mentity.blue.bold
+               var title = "Compiling `{mentity}`:"
+
+               if no_color == null or not no_color then
+                       print title.bold
+               else
+                       print title
+               end
+
+               print ""
+               var command = self.command
+               if command != null then print command
+       end
+end
+
+redef class CmdTesting
+       redef fun execute(no_color) do
+               var mentity = self.mentity.as(not null).full_name
+               if no_color == null or not no_color then mentity = mentity.blue.bold
+               var title = "Testing `{mentity}`:"
+
+               if no_color == null or not no_color then
+                       print title.bold
+               else
+                       print title
+               end
+
+               print ""
+               var command = self.command
+               if command != null then print command
+       end
+end
+
+redef class CmdManSynopsis
+       redef fun execute(no_color) do
+               var mentity = self.mentity.as(not null).full_name
+               if no_color == null or not no_color then mentity = mentity.blue.bold
+               var title = "Synopsis for `{mentity}`:"
+
+               if no_color == null or not no_color then
+                       print title.bold
+               else
+                       print title
+               end
+
+               print ""
+               var synopsis = self.synopsis
+               if synopsis != null then print synopsis
+       end
+end
+
+redef class CmdManOptions
+       redef fun execute(no_color) do
+               var mentity = self.mentity.as(not null).full_name
+               if no_color == null or not no_color then mentity = mentity.blue.bold
+               var title = "Options for `{mentity}`:"
+
+               if no_color == null or not no_color then
+                       print title.bold
+               else
+                       print title
+               end
+
+               print ""
+               var options = self.options.as(not null)
+               for opt, desc in options do
+                       if no_color == null or not no_color then
+                               print " * {opt.blue.bold}: {desc}"
+                       else
+                               print " * {opt}: {desc}"
+                       end
+               end
+       end
+end