nitdoc: introduce pretty printing of composite tree
authorAlexandre Terrasa <alexandre@moz-code.org>
Fri, 22 May 2015 07:09:47 +0000 (03:09 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 29 May 2015 23:05:55 +0000 (19:05 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/doc_base.nit

index 3bb736e..f517955 100644 (file)
@@ -75,6 +75,15 @@ class DocPage
        var root = new DocRoot
 
        redef fun to_s do return title
+
+       # Pretty prints the content of this page.
+       fun pretty_print: Writable do
+               var res = new Template
+               res.addn "page: {title}"
+               res.addn ""
+               root.pretty_print_in(res)
+               return res
+       end
 end
 
 # `DocPage` elements that can be nested in another.
@@ -128,6 +137,20 @@ abstract class DocComposite
                if parent == null then return 0
                return parent.depth + 1
        end
+
+       # Pretty prints this composite recursively.
+       fun pretty_print: Writable do
+               var res = new Template
+               pretty_print_in(res)
+               return res
+       end
+
+       # Appends the Pretty print of this composite in `res`.
+       private fun pretty_print_in(res: Template) do
+               res.add "#" * depth
+               res.addn " {id}"
+               for child in children do child.pretty_print_in(res)
+       end
 end
 
 # The `DocComposite` element that contains all the other.