nitx: introduce doc page search
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 4 May 2015 14:43:59 +0000 (10:43 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 21 May 2015 18:29:05 +0000 (14:29 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/doc_phases/doc_console.nit
src/nitx.nit

index a7ef6b1..c304dab 100644 (file)
@@ -59,6 +59,7 @@ class Nitx
        fun help do
                print "\nCommands:"
                print "\tname\t\tlookup module, class and property with the corresponding 'name'"
+               print "\tdoc: <name::space>\tdisplay the documentation page of 'namespace'"
                print "\t:h\t\tdisplay this help message"
                print "\t:q\t\tquit interactive mode"
                print ""
@@ -110,6 +111,8 @@ interface NitxQuery
                        return new NitxHelp
                else if query_string.has_prefix("comment:") then
                        return new CommentQuery(query_string)
+               else if query_string.has_prefix("doc:") then
+                       return new DocQuery(query_string)
                end
                return new CommentQuery("comment: {query_string}")
        end
@@ -207,6 +210,56 @@ class CommentQuery
        end
 end
 
+# A query to search a Nitdoc documentation page by its name.
+class DocQuery
+       super MetaQuery
+
+       redef fun perform(nitx, doc) do
+               var res = new Array[NitxMatch]
+               var name = args.first
+               for page in doc.pages do
+                       if name == "*" then # FIXME dev only
+                               res.add new PageMatch(self, page)
+                       else if page.title == name then
+                               res.add new PageMatch(self, page)
+                       else if page isa MEntityPage and page.mentity.cs_namespace == name then
+                               res.add new PageMatch(self, page)
+                       end
+               end
+               return res
+       end
+
+       redef fun make_results(nitx, results) do
+               var len = results.length
+               # FIXME how to render the pager for one worded namespaces like "standard"?
+               if len == 1 then
+                       var page = results.first.as(PageMatch).page
+                       var pager = new Pager
+                       pager.add page.write_to_string
+                       pager.render
+                       return page
+               else
+                       return super
+               end
+       end
+end
+
+# A match between a `DocPage` and a `MEntity`.
+class PageMatch
+       super NitxMatch
+
+       # `DocPage` matched.
+       var page: DocPage
+
+       redef fun make_list_item do
+               var page = self.page
+               if page isa MEntityPage then
+                       return page.mentity.cs_list_item
+               end
+               return " * {page.title}"
+       end
+end
+
 # A query that contains a nitx command.
 #
 # These commands are prefixed with `:` and are used to control the execution of
index c1ce189..4c08b29 100644 (file)
@@ -17,6 +17,7 @@
 # Features:
 #
 # * Display comment from name/namespace
+# * Display documentation page from Nitdoc in console
 module nitx
 
 import modelbuilder