lib/standard/ropes: Added a final forward iterator on the chars.
[nit.git] / src / nitdoc.nit
index 23d4684..7fd49a4 100644 (file)
@@ -1,12 +1,10 @@
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
-# Copyright 2008 Jean Privat <jean@pryen.org>
-#
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#     http://www.apache.org/licenses/LICENSE-2.0
+# http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# The main module of the nitdoc program
-package nitdoc
+# Documentation generator for the nit language.
+# Generate API documentation in HTML format from nit source code.
+module nitdoc
+
+import model_utils
+import modelize_property
+import markdown
+import doc_template
+
+# The NitdocContext contains all the knowledge used for doc generation
+class NitdocContext
+
+       private var toolcontext = new ToolContext
+       private var mbuilder: ModelBuilder
+       private var mainmodule: MModule
+       private var output_dir: String
+       private var min_visibility: MVisibility
+
+       private var opt_dir = new OptionString("output directory", "-d", "--dir")
+       private var opt_source = new OptionString("link for source (%f for filename, %l for first line, %L for last line)", "--source")
+       private var opt_sharedir = new OptionString("directory containing nitdoc assets", "--sharedir")
+       private var opt_shareurl = new OptionString("use shareurl instead of copy shared files", "--shareurl")
+       private var opt_nodot = new OptionBool("do not generate graphes with graphviz", "--no-dot")
+       private var opt_private = new OptionBool("also generate private API", "--private")
+
+       private var opt_custom_title = new OptionString("custom title for homepage", "--custom-title")
+       private var opt_custom_menu = new OptionString("custom items added in top menu (each item must be enclosed in 'li' tags)", "--custom-menu-items")
+       private var opt_custom_intro = new OptionString("custom intro text for homepage", "--custom-overview-text")
+       private var opt_custom_footer = new OptionString("custom footer text", "--custom-footer-text")
+
+       private var opt_github_upstream = new OptionString("Git branch where edited commits will be pulled into (ex: user:repo:branch)", "--github-upstream")
+       private var opt_github_base_sha1 = new OptionString("Git sha1 of base commit used to create pull request", "--github-base-sha1")
+       private var opt_github_gitdir = new OptionString("Git working directory used to resolve path name (ex: /home/me/myproject/)", "--github-gitdir")
+
+       private var opt_piwik_tracker = new OptionString("Piwik tracker URL (ex: nitlanguage.org/piwik/)", "--piwik-tracker")
+       private var opt_piwik_site_id = new OptionString("Piwik site ID", "--piwik-site-id")
+
+       init do
+               var opts = toolcontext.option_context
+               opts.add_option(opt_dir, opt_source, opt_sharedir, opt_shareurl, opt_nodot, opt_private)
+               opts.add_option(opt_custom_title, opt_custom_footer, opt_custom_intro, opt_custom_menu)
+               opts.add_option(opt_github_upstream, opt_github_base_sha1, opt_github_gitdir)
+               opts.add_option(opt_piwik_tracker, opt_piwik_site_id)
+
+               var tpl = new Template
+               tpl.add "Usage: nitdoc [OPTION]... <file.nit>...\n"
+               tpl.add "Generates HTML pages of API documentation from Nit source files."
+               toolcontext.tooldescription = tpl.write_to_string
+               toolcontext.process_options(args)
+
+               self.process_options
+               self.parse(toolcontext.option_context.rest)
+       end
 
-import syntax
-private import utils
-import abstracttool
+       private fun process_options do
+               if opt_private.value then
+                       min_visibility = none_visibility
+               else
+                       min_visibility = protected_visibility
+               end
+               var gh_upstream = opt_github_upstream.value
+               var gh_base_sha = opt_github_base_sha1.value
+               var gh_gitdir = opt_github_gitdir.value
+               if not gh_upstream == null or not gh_base_sha == null or not gh_gitdir == null then
+                       if gh_upstream == null or gh_base_sha == null or gh_gitdir == null then
+                               print "Error: Options {opt_github_upstream.names.first}, {opt_github_base_sha1.names.first} and {opt_github_gitdir.names.first} are required to enable the GitHub plugin"
+                               abort
+                       end
+               end
+       end
 
+       private fun parse(arguments: Array[String]) do
+               var model = new Model
+               mbuilder = new ModelBuilder(model, toolcontext)
+               var mmodules = mbuilder.parse(arguments)
+               if mmodules.is_empty then return
+               mbuilder.run_phases
+               if mmodules.length == 1 then
+                       mainmodule = mmodules.first
+               else
+                       mainmodule = new MModule(model, null, "<main>", new Location(null, 0, 0, 0, 0))
+                       mainmodule.set_imported_mmodules(mmodules)
+               end
+       end
 
-# Store knowledge and facilities to generate files
-class DocContext
-       super AbstractCompiler
-       # Destination directory
-       readable writable var _dir: String = "doc"
+       private fun generate_nitdoc do
+               init_output_dir
+               overview
+               search
+               modules
+               classes
+               quicksearch_list
+       end
 
-       # Content of a generated file
-       var _stage_context: StageContext = new StageContext(null)
+       private fun init_output_dir do
+               # location output dir
+               var output_dir = opt_dir.value
+               if output_dir == null then
+                       output_dir = "doc"
+               end
+               self.output_dir = output_dir
+               # create destination dir if it's necessary
+               if not output_dir.file_exists then output_dir.mkdir
+               # locate share dir
+               var sharedir = opt_sharedir.value
+               if sharedir == null then
+                       var dir = toolcontext.nit_dir
+                       if dir == null then
+                               print "Error: Cannot locate nitdoc share files. Uses --sharedir or envvar NIT_DIR"
+                               abort
+                       end
+                       sharedir = "{dir}/share/nitdoc"
+                       if not sharedir.file_exists then
+                               print "Error: Cannot locate nitdoc share files. Uses --sharedir or envvar NIT_DIR"
+                               abort
+                       end
+               end
+               # copy shared files
+               if opt_shareurl.value == null then
+                       sys.system("cp -r {sharedir.to_s}/* {output_dir.to_s}/")
+               else
+                       sys.system("cp -r {sharedir.to_s}/resources/ {output_dir.to_s}/resources/")
+               end
 
-       # Add a string in the content
-       fun add(s: String) do
-               _stage_context.content.add(s)
-               _stage_context.validate = true
        end
 
-       # Add a string in the content iff some other string are added
-       fun stage(s: String) do _stage_context.content.add(s)
+       private fun overview do
+               var overviewpage = new NitdocOverview(self)
+               overviewpage.render.write_to_file("{output_dir.to_s}/index.html")
+       end
 
-       # Create a new stage in the content
-       fun open_stage do _stage_context = new StageContext(_stage_context)
+       private fun search do
+               var searchpage = new NitdocSearch(self)
+               searchpage.render.write_to_file("{output_dir.to_s}/search.html")
+       end
 
-       # Close the current stage in the content
-       fun close_stage
-       do
-               var s = _stage_context.parent
-               if _stage_context.validate then
-                       s.content.add_all(_stage_context.content)
-                       s.validate = true
+       private fun modules do
+               for mmodule in mbuilder.model.mmodules do
+                       if mmodule.name == "<main>" then continue
+                       var modulepage = new NitdocModule(mmodule, self)
+                       modulepage.render.write_to_file("{output_dir.to_s}/{mmodule.nitdoc_url}")
                end
-               assert s != null
-               _stage_context = s
        end
 
-       # Write the content to a new file
-       fun write_to(filename: String)
-       do
-               var f = new OFStream.open(filename)
-               for s in _stage_context.content do
-                       f.write(s)
+       private fun classes do
+               for mclass in mbuilder.model.mclasses do
+                       var classpage = new NitdocClass(mclass, self)
+                       classpage.render.write_to_file("{output_dir.to_s}/{mclass.nitdoc_url}")
                end
-               f.close
        end
 
-       # Start a new file
-       fun clear
-       do
-               _stage_context = new StageContext(null)
+       private fun quicksearch_list do
+               var quicksearch = new QuickSearch(self)
+               quicksearch.render.write_to_file("{output_dir.to_s}/quicksearch-list.js")
        end
+end
 
-       # Sorter of entities in alphabetical order
-       var _sorter: AlphaSorter[MMEntity] = new AlphaSorter[MMEntity]
+# Nitdoc QuickSearch list generator
+#
+# Create a JSON object containing links to:
+#  * modules
+#  * mclasses
+#  * mpropdefs
+# All entities are grouped by name to make the research easier.
+class QuickSearch
+
+       private var mmodules = new HashSet[MModule]
+       private var mclasses = new HashSet[MClass]
+       private var mpropdefs = new HashMap[String, Set[MPropDef]]
+
+       init(ctx: NitdocContext) do
+               for mmodule in ctx.mbuilder.model.mmodules do
+                       if mmodule.name == "<main>" then continue
+                       mmodules.add mmodule
+               end
+               for mclass in ctx.mbuilder.model.mclasses do
+                       if mclass.visibility < ctx.min_visibility then continue
+                       mclasses.add mclass
+               end
+               for mproperty in ctx.mbuilder.model.mproperties do
+                       if mproperty.visibility < ctx.min_visibility then continue
+                       if mproperty isa MAttribute then continue
+                       if not mpropdefs.has_key(mproperty.name) then
+                               mpropdefs[mproperty.name] = new HashSet[MPropDef]
+                       end
+                       mpropdefs[mproperty.name].add_all(mproperty.mpropdefs)
+               end
+       end
 
-       # Sort entities in the alphabetical order
-       fun sort(array: Array[MMEntity])
-       do
-               _sorter.sort(array)
+       fun render: Template do
+               var tpl = new Template
+               tpl.add "var nitdocQuickSearchRawList=\{ "
+               for mmodule in mmodules do
+                       tpl.add "\"{mmodule.name}\":["
+                       tpl.add "\{txt:\"{mmodule.full_name}\",url:\"{mmodule.nitdoc_url}\"\},"
+                       tpl.add "],"
+               end
+               for mclass in mclasses do
+                       var full_name = mclass.intro.mmodule.full_name
+                       tpl.add "\"{mclass.name}\":["
+                       tpl.add "\{txt:\"{full_name}\",url:\"{mclass.nitdoc_url}\"\},"
+                       tpl.add "],"
+               end
+               for mproperty, mprops in mpropdefs do
+                       tpl.add "\"{mproperty}\":["
+                       for mpropdef in mprops do
+                               var full_name = mpropdef.mclassdef.mclass.full_name
+                               tpl.add "\{txt:\"{full_name}\",url:\"{mpropdef.nitdoc_url}\"\},"
+                       end
+                       tpl.add "],"
+               end
+               tpl.add " \};"
+               return tpl
        end
+end
 
-       readable var _opt_dir: OptionString = new OptionString("Directory where doc is generated", "-d", "--dir")
-       readable var _opt_source: OptionString = new OptionString("What link for source (%f for filename, %l for first line, %L for last line)", "--source")
-       readable var _opt_public: OptionBool = new OptionBool("Generate only the public API", "--public")
-       readable var _opt_private: OptionBool = new OptionBool("Generate the private API", "--private")
-       readable var _opt_nodot: OptionBool = new OptionBool("Do not generate graphes with graphviz", "--no-dot")
-       readable var _opt_sharedir: OptionString = new OptionString("Directory containing the nitdoc files", "--sharedir")
-       var sharedir: nullable String
+# Nitdoc base page
+# Define page structure and properties
+abstract class NitdocPage
 
-       fun public_only: Bool
-       do
-               if self._opt_public.value == true then return true
-               return false
+       private var ctx: NitdocContext
+       private var shareurl = "."
+
+       init(ctx: NitdocContext) do
+               self.ctx = ctx
+               if ctx.opt_shareurl.value != null then shareurl = ctx.opt_shareurl.value.as(not null)
        end
 
-       fun with_private: Bool
-       do
-               if self._opt_private.value == true then return true
-               return false
+       # Render the page as a html template
+       fun render: Template do
+               var tpl = new TplNitdocPage
+               tpl.head = tpl_head
+               tpl.topmenu = tpl_topmenu
+               tpl.sidebar = tpl_sidebar
+               tpl.content = tpl_content
+               tpl.footer = tpl_footer
+
+               tpl.body_attrs.add(new TagAttribute("data-bootstrap-share", shareurl))
+               if ctx.opt_github_upstream.value != null and ctx.opt_github_base_sha1.value != null then
+                       tpl.body_attrs.add(new TagAttribute("data-github-upstream", ctx.opt_github_upstream.value))
+                       tpl.body_attrs.add(new TagAttribute("data-github-base-sha1", ctx.opt_github_base_sha1.value))
+               end
+               var requirejs = new TplScript
+               requirejs.attrs.add(new TagAttribute("data-main", "{shareurl}/js/nitdoc"))
+               requirejs.attrs.add(new TagAttribute("src", "{shareurl}/js/lib/require.js"))
+               tpl.scripts.add requirejs
+
+               # piwik tracking
+               var tracker_url = ctx.opt_piwik_tracker.value
+               var site_id = ctx.opt_piwik_site_id.value
+               if tracker_url != null and site_id != null then
+                       tpl.scripts.add new TplPiwikScript(tracker_url, site_id)
+               end
+               return tpl
        end
 
-       # The current processed filename
-       var filename: String
+       # Page title string
+       fun tpl_title: String do
+               if ctx.opt_custom_title.value != null then
+                       return ctx.opt_custom_title.value.to_s
+               else
+                       return "Nitdoc"
+               end
+       end
 
-       # The main virtual module
-       var mainmod: nullable MMVirtualModule
+       # Page <head> template
+       fun tpl_head: TplHead do return new TplHead(tpl_title, shareurl)
 
-       redef fun perform_work(mods)
-       do
-               mainmod = new MMVirtualModule(self, mods)
+       # Top menu template
+       fun tpl_topmenu: TplTopMenu do
+               var topmenu = new TplTopMenu
+               var custom_elt = ctx.opt_custom_menu.value
+               if custom_elt != null then topmenu.add_raw(custom_elt)
+               return topmenu
+       end
 
-               dir.mkdir
+       # Page sidebar template
+       # return null if no sidebar for this page
+       fun tpl_sidebar: nullable TplSidebar do return null
 
-               sys.system("cp -r '{sharedir.to_s}'/* {dir}/")
+       # Page content template
+       fun tpl_content: Template is abstract
 
-               # Compute the set of direct owned nested modules
-               var owns = new HashMap[MMModule, Array[MMModule]]
-               for mod in modules do
-                       owns[mod] = new Array[MMModule]# [mod]
-               end
-               for mod in modules do
-                       if mod == mainmod then continue
-                       var d = mod.directory
-                       loop
-                               var o = d.owner
-                               if o != null and o != mod then
-                                       owns[o].add(mod)
-                               end
-                               var dp = d.parent
-                               if dp == null or dp == d then break
-                               d = dp
-                       end
+       # Page footer template
+       # return null if no footer for this page
+       fun tpl_footer: nullable TplFooter do
+               if ctx.opt_custom_footer.value != null then
+                       return new TplFooter(ctx.opt_custom_footer.value.to_s)
                end
+               return null
+       end
 
-               # Builds the various module hierarchies
-               var mnh = new PartialOrder[MMModule] # nested module hierarchy
-               var tmh = new PartialOrder[MMModule] # top module import hierrchy
-               var ms = mainmod.mhe.linear_extension.reversed
-               for m in ms do
-                       if ms == mainmod then continue
-                       m.mnhe_ = mnh.add(m, owns[m])
-                       var pub = new Array[MMModule]
-                       for m2 in m.mhe.greaters do
-                               if m2.toplevel_owner != m2 and m2.toplevel_owner != m.toplevel_owner then continue
-                               if m.mnhe <= m2 then continue
-                               if m.visibility_for(m2) <= 0 then
-                                       # nothing
-                               else if m.visibility_for(m2) == 1 then
-                               else
-                                       pub.add(m2)
-                               end
-                       end
-                       m.tmhe_ = tmh.add(m, pub)
-               end
+       # Clickable graphviz image using dot format
+       # return null if no graph for this page
+       fun tpl_graph(dot: FlatBuffer, name: String, alt: String): nullable TplGraph do
+               if ctx.opt_nodot.value then return null
+               var output_dir = ctx.output_dir
+               var file = new OFStream.open("{output_dir}/{name}.dot")
+               file.write(dot)
+               file.close
+               sys.system("\{ test -f {output_dir}/{name}.png && test -f {output_dir}/{name}.s.dot && diff {output_dir}/{name}.dot {output_dir}/{name}.s.dot >/dev/null 2>&1 ; \} || \{ cp {output_dir}/{name}.dot {output_dir}/{name}.s.dot && dot -Tpng -o{output_dir}/{name}.png -Tcmapx -o{output_dir}/{name}.map {output_dir}/{name}.s.dot ; \}")
+               var fmap = new IFStream.open("{output_dir}/{name}.map")
+               var map = fmap.read_all
+               fmap.close
+               return new TplGraph(name, alt, map)
+       end
 
-               var head = "<meta charset=\"utf-8\">" +
-                       "<script type=\"text/javascript\" src=\"scripts/jquery-1.7.1.min.js\"></script>\n" + 
-                       "<script type=\"text/javascript\" src=\"scripts/js-facilities.js\"></script>\n" +
-                       "<link rel=\"stylesheet\" href=\"styles/main.css\" type=\"text/css\"  media=\"screen\" />"
+       # A (source) link template for a given location
+       fun tpl_showsource(location: nullable Location): nullable String
+       do
+               if location == null then return null
+               var source = ctx.opt_source.value
+               if source == null then return "({location.file.filename.simplify_path})"
+               # THIS IS JUST UGLY ! (but there is no replace yet)
+               var x = source.split_with("%f")
+               source = x.join(location.file.filename.simplify_path)
+               x = source.split_with("%l")
+               source = x.join(location.line_start.to_s)
+               x = source.split_with("%L")
+               source = x.join(location.line_end.to_s)
+               source = source.simplify_path
+               return " (<a target='_blank' title='Show source' href=\"{source.to_s}\">source</a>)"
+       end
 
-               var action_bar = "<header><nav class='main'><ul><li class=\"current\">Overview</li><li><a href='full-index.html'>Full Index</a></li></ul></nav></header>\n"
+       # MClassDef description template
+       fun tpl_mclassdef_article(mclassdef: MClassDef): TplArticle do
+               var article = mclassdef.tpl_article
+               article.content = new Template
+               if not mclassdef.is_intro then
+                       # add intro synopsys
+                       var intro = mclassdef.mclass.intro
+                       var location = intro.location
+                       var sourcelink = tpl_showsource(location)
+                       var intro_def = intro.tpl_short_definition
+                       intro_def.location = sourcelink
+                       intro_def.github_area = tpl_github(intro.full_namespace, intro.mdoc, location)
+                       article.content.add intro_def
+               end
+               # add mclassdef full description
+               var location = mclassdef.location
+               var sourcelink = tpl_showsource(location)
+               var prop_def = mclassdef.tpl_definition
+               prop_def.location = sourcelink
+               prop_def.github_area = tpl_github(mclassdef.full_namespace, mclassdef.mdoc, location)
+               article.content.add prop_def
+               return article
+       end
 
-               # generate the index
-               self.filename = "index.html"
-               clear
-               add("<!DOCTYPE html>")
-               add("<html><head>{head}<title>Index</title></head><body>\n")
-               add(action_bar)
-               add("<div class=\"page\">")
-               add("<div class=\"content fullpage\">")
-               add("<h1>Modules</h1>\n<article class='overview'><ul>")
-               var modss = mainmod.mhe.greaters_and_self.to_a
-               sort(modss)
-               for mod in modss do
-                       if not mod.is_toplevel then continue
-                       if not mod.require_doc(self) then continue
-                       assert mod isa MMSrcModule
-                       add("<li>{mod.html_link(self)} {mod.short_doc}</li>")
+       # MPropDef description template
+       fun tpl_mpropdef_article(mpropdef: MPropDef): TplArticle do
+               var article = mpropdef.tpl_article
+               article.content = new Template
+               if not mpropdef.is_intro then
+                       # add intro synopsys
+                       var intro = mpropdef.mproperty.intro
+                       var location = intro.location
+                       var sourcelink = tpl_showsource(location)
+                       var intro_def = intro.tpl_short_definition
+                       intro_def.location = sourcelink
+                       intro_def.github_area = tpl_github(intro.full_namespace, intro.mdoc, location)
+                       article.content.add intro_def
+               end
+               # add mpropdef description
+               var location = mpropdef.location
+               var sourcelink = tpl_showsource(location)
+               var prop_def = mpropdef.tpl_definition
+               prop_def.location = sourcelink
+               prop_def.github_area = tpl_github(mpropdef.full_namespace, mpropdef.mdoc, location)
+               article.content.add prop_def
+               return article
+       end
 
+       # Github area (for Github comment edition plugin)
+       # return null if no github plugin for this page
+       fun tpl_github(namespace: String, mdoc: nullable MDoc, loc: nullable Location): nullable TplGithubArea do
+               if loc == null then return null
+               if ctx.opt_github_gitdir.value == null then return null
+               var gitdir = ctx.opt_github_gitdir.value.as(not null)
+               var location = loc.github(gitdir)
+               var comment: String
+               if mdoc != null then
+                       comment = mdoc.full_comment
+               else
+                       comment = ""
                end
-               add("</ul>")
+               return new TplGithubArea(comment, namespace, location)
+       end
+end
 
-               var op = new Buffer
-               op.append("digraph dep \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
-               for mod in modss do
-                       if not mod.is_toplevel then continue
-                       if not mod.require_doc(self) then continue
-                       op.append("\"{mod.name}\"[URL=\"{mod.html_name}.html\"];\n")
-                       for mod2 in mod.tmhe.direct_greaters do
-                               if not modss.has(mod2) then continue
-                               op.append("\"{mod.name}\"->\"{mod2.name}\";\n")
+# The overview page
+# Display a list of modules contained in program
+class NitdocOverview
+       super NitdocPage
+
+       private var mmodules = new Array[MModule]
+
+       init(ctx: NitdocContext) do
+               super(ctx)
+               # get modules
+               var mmodules = new HashSet[MModule]
+               for mmodule in ctx.mbuilder.model.mmodule_importation_hierarchy do
+                       if mmodule.name == "<main>" then continue
+                       var owner = mmodule.public_owner
+                       if owner != null then
+                               mmodules.add(owner)
+                       else
+                               mmodules.add(mmodule)
                        end
                end
-               op.append("\}\n")
-               self.gen_dot(op.to_s, "dep", "Modules hierarchy")
-               add("</article></div>")
-               add("<div class='clear'></div>")
-               add("</div>")
-               add("</body></html>\n")
-               write_to("{dir}/index.html")
-
-               # Generate page for modules
-               for mod in modules do
-                       if mod == mainmod then continue
-                       assert mod isa MMSrcModule
-                       if not mod.require_doc(self) then continue
-                       self.filename = mod.html_name
-                       action_bar = "<header><nav class='main'><ul><li><a href='./'>Overview</a></li><li class=\"current\">{mod.name}</li><li><a href='full-index.html'>Full Index</a></li></ul></nav></header>\n"
-                       clear
-                       add("<!DOCTYPE html>")
-                       add("<html><head>{head}<title>Module {mod.name}</title></head><body>\n")
-                       add(action_bar)
-                       add("<div class=\"page\">")
-                       mod.file_page_doc(self)
-                       add("</div>")
-                       add("</body></html>\n")
-                       write_to("{dir}/{mod.html_name}.html")
-               end
-
-               # Generate pages for global classes
-               for c in mainmod.local_classes do
-                       if not c.require_doc(self) then continue
-                       self.filename = c.html_name
-                       action_bar = "<header><nav class='main'><ul><li><a href='./'>Overview</a></li><li>{c.global.intro.mmmodule.toplevel_owner.html_link(self)}</li><li class=\"current\">{c.name}</li><li><a href='full-index.html'>Full Index</a></li></ul></nav></header>\n"
-                       clear
-                       add("<!DOCTYPE html>")
-                       add("<html><head>{head}<title>Class {c.name}</title></head><body>\n")
-                       add(action_bar)
-                       add("<div class=\"page\">")
-                       c.file_page_doc(self)
-                       add("</div>")
-                       add("</body></html>\n")
-                       write_to("{dir}/{c.html_name}.html")
-               end
-
-               self.filename = "fullindex"
-               action_bar = "<header><nav class='main'><ul><li><a href='./'>Overview</a></li><li class=\"current\">Full Index</li></ul></nav></header>\n"
-               clear
-               add("<!DOCTYPE html>")
-               add("<html><head>{head}<title>Full Index</title></head><body>\n")
-               add(action_bar)
-               add("<div class=\"page\">")
-               add("<div class=\"content fullpage\">")
-               mainmod.file_index_page_doc(self)
-               add("</div>")
-               add("</div>")
-               add("</body></html>\n")
-               write_to("{dir}/full-index.html")
-       end
-
-
-       # Add a (source) link fo a given location
-       fun show_source(l: Location)
-       do
-               var s = opt_source.value
-               if s == null then
-                       add("in #{l.file.filename.simplify_path}")
-               else
-                       # THIS IS JUST UGLY ! (but there is no replace yet)
-                       var x = s.split_with("%f")
-                       s = x.join(l.file.filename.simplify_path)
-                       x = s.split_with("%l")
-                       s = x.join(l.line_start.to_s)
-                       x = s.split_with("%L")
-                       s = x.join(l.line_end.to_s)
-                       add(" (<a href=\"{s}\">show code</a>)")
-               end
+               # sort modules
+               var sorter = new MModuleNameSorter
+               self.mmodules.add_all(mmodules)
+               sorter.sort(self.mmodules)
        end
 
-       # Generate a clicable graphiz image using a dot content.
-       # `name' refer to the filename (without extension) and the id name of the map.
-       # `name' must also match the name of the graph in the dot content (eg. digraph NAME {...)
-       fun gen_dot(dot: String,  name: String, alt: String)
-       do
-               if opt_nodot.value then return
-               var f = new OFStream.open("{self.dir}/{name}.dot")
-               f.write(dot)
-               f.close
-               sys.system("\{ test -f {self.dir}/{name}.png && test -f {self.dir}/{name}.s.dot && diff {self.dir}/{name}.dot {self.dir}/{name}.s.dot >/dev/null 2>&1 ; \} || \{ cp {self.dir}/{name}.dot {self.dir}/{name}.s.dot && dot -Tpng -o{self.dir}/{name}.png -Tcmapx -o{self.dir}/{name}.map {self.dir}/{name}.s.dot ; \}")
-               self.add("<article class=\"graph\"><img src=\"{name}.png\" usemap=\"#{name}\" style=\"margin:auto\" alt=\"{alt}\"/></article>")
-               var fmap = new IFStream.open("{self.dir}/{name}.map")
-               self.add(fmap.read_all)
-               fmap.close
+       redef fun tpl_title do return "Overview | {super}"
+
+       redef fun tpl_topmenu do
+               var topmenu = super
+               topmenu.add_elt("#", "Overview", true)
+               topmenu.add_elt("search.html", "Search", false)
+               return topmenu
        end
 
-       init
-       do
-               keep_ast = true
-               super("nitdoc")
-               filename = "-unset-"
-               option_context.add_option(opt_public)
-               option_context.add_option(opt_private)
-               option_context.add_option(opt_dir)
-               option_context.add_option(opt_source)
-               option_context.add_option(opt_nodot)
-               option_context.add_option(opt_sharedir)
-       end
-
-       redef fun process_options
-       do
-               super
-               var d = opt_dir.value
-               if d != null then dir = d
-
-               if not opt_nodot.value then
-                       # Test if dot is runable
-                       var res = sys.system("sh -c dot </dev/null >/dev/null 2>&1")
-                       if res != 0 then
-                               stderr.write "--no-dot implied since `dot' is not available. Try to install graphviz.\n"
-                               opt_nodot.value = true
-                       end
-               end
-               
-               sharedir = opt_sharedir.value
-               if sharedir == null then
-                       var dir = once ("NIT_DIR".to_symbol).environ
-                       if dir.is_empty then
-                               dir = "{sys.program_name.dirname}/../share/nitdoc"
-                               if dir.file_exists then sharedir = dir
-                       else
-                               dir = "{dir}/share/nitdoc"
-                               if dir.file_exists then sharedir = dir
-                       end
-                       if sharedir == null then
-                               fatal_error(null, "Error: Cannot locate nitdoc shared files. Uses --sharedir or envvar NIT_DIR.")
-                       end
-                       dir = "{sharedir.to_s}/scripts/js-facilities.js"
-                       if sharedir == null then
-                               fatal_error(null, "Error: Invalid nitdoc shared files. Check --sharedir or envvar NIT_DIR.")
+       redef fun tpl_content do
+               var tpl = new TplOverviewPage
+               # title
+               if ctx.opt_custom_title.value != null then
+                       tpl.title = ctx.opt_custom_title.value.to_s
+               else
+                       tpl.title = "Overview"
+               end
+               # intro text
+               if ctx.opt_custom_intro.value != null then
+                       tpl.text = ctx.opt_custom_intro.value.to_s
+               end
+               # module list
+               for mmodule in mmodules do
+                       if mmodule.mdoc != null then
+                               var mtpl = new Template
+                               mtpl.add mmodule.tpl_link
+                               mtpl.add "&nbsp;"
+                               mtpl.add mmodule.mdoc.short_comment
+                               tpl.modules.add mtpl
                        end
-
                end
+               # module graph
+               tpl.graph = tpl_dot
+               return tpl
        end
 
-       redef fun handle_property_conflict(lc, impls)
-       do
-               # THIS IS SO UGLY! See MMVirtualModule
-               if lc.mmmodule == self.mainmod then 
-                       return # We just accept, so one in impls is arbitrary inherited
+       # Genrate dot and template for module hierarchy
+       fun tpl_dot: nullable TplGraph do
+               # build poset with public owners
+               var poset = new POSet[MModule]
+               for mmodule in mmodules do
+                       poset.add_node(mmodule)
+                       for omodule in mmodules do
+                               if mmodule == omodule then continue
+                               if mmodule.in_importation < omodule then
+                                       poset.add_node(omodule)
+                                       poset.add_edge(mmodule, omodule)
+                               end
+                       end
+               end
+               # build graph
+               var op = new FlatBuffer
+               op.append("digraph dep \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
+               for mmodule in poset do
+                       op.append("\"{mmodule.name}\"[URL=\"{mmodule.nitdoc_url}\"];\n")
+                       for omodule in poset[mmodule].direct_greaters do
+                               op.append("\"{mmodule.name}\"->\"{omodule.name}\";\n")
+                       end
                end
-               super
+               op.append("\}\n")
+               return tpl_graph(op, "dep", "Modules hierarchy")
        end
 end
 
-redef class String
-       # Replace all occurence of pattern ith string
-       fun replace(p: Pattern, string: String): String
-       do
-               return self.split_with(p).join(string)
+# The search page
+# Display a list of modules, classes and properties
+class NitdocSearch
+       super NitdocPage
+
+       init(ctx: NitdocContext) do super(ctx)
+
+       redef fun tpl_title do return "Search | {super}"
+
+       redef fun tpl_topmenu do
+               var topmenu = super
+               topmenu.add_elt("index.html", "Overview", false)
+               topmenu.add_elt("#", "Search", true)
+               return topmenu
        end
 
-       # Escape the following characters < > & and " with their html counterpart
-       fun html_escape: String
-       do
-               var ret = self
-               if ret.has('&') then ret = ret.replace('&', "&amp;")
-               if ret.has('<') then ret = ret.replace('<', "&lt;")
-               if ret.has('>') then ret = ret.replace('>', "&gt;")
-               if ret.has('"') then ret = ret.replace('"', "&quot;")
-               return ret
+       redef fun tpl_content do
+               var tpl = new TplSearchPage
+               # title
+               tpl.title = "Search"
+               # modules list
+               for mmodule in modules_list do
+                       tpl.modules.add mmodule.tpl_link
+               end
+               # classes list
+               for mclass in classes_list do
+                       tpl.classes.add mclass.tpl_link
+               end
+               # properties list
+               for mproperty in mprops_list do
+                       var m = new Template
+                       m.add mproperty.intro.tpl_link
+                       m.add " ("
+                       m.add mproperty.intro.mclassdef.mclass.tpl_link
+                       m.add ")"
+                       tpl.props.add m
+               end
+               return tpl
        end
 
-       # Remove "/./", "//" and "bla/../"
-       fun simplify_path: String
-       do
-               var a = self.split_with("/")
-               var a2 = new Array[String]
-               for x in a do
-                       if x == "." then continue
-                       if x == "" and not a2.is_empty then continue
-                       if x == ".." and not a2.is_empty then
-                               a2.pop
-                               continue
-                       end
-                       a2.push(x)
+       # Extract mmodule list to display (sorted by name)
+       private fun modules_list: Array[MModule] do
+               var sorted = new Array[MModule]
+               for mmodule in ctx.mbuilder.model.mmodule_importation_hierarchy do
+                       if mmodule.name == "<main>" then continue
+                       sorted.add mmodule
                end
-               return a2.join("/")
+               var sorter = new MModuleNameSorter
+               sorter.sort(sorted)
+               return sorted
        end
-end
 
-# A virtual module is used to work as an implicit main module that combine unrelated modules
-# Since conflict may arrise in a virtual module (the main method for instance) conflicts are disabled
-class MMVirtualModule
-       super MMModule
-       init(ctx: MMContext, mods: Array[MMModule])
-       do
-               # We need to compute the whole metamodel since there is no mmbuilder to do it
-               super(" main".to_symbol, mods.first.directory, ctx, new Location(null,0,0,0,0))
-               ctx.add_module(self, mods)
-               for m in mods do
-                       self.add_super_module(m, 1)
-               end
-               self.import_global_classes
-               self.import_local_classes
-               for c in self.local_classes do
-                       c.compute_super_classes
-               end
-               for c in self.local_classes do
-                       c.compute_ancestors
+       # Extract mclass list to display (sorted by name)
+       private fun classes_list: Array[MClass] do
+               var sorted = new Array[MClass]
+               for mclass in ctx.mbuilder.model.mclasses do
+                       if mclass.visibility < ctx.min_visibility then continue
+                       sorted.add mclass
                end
+               var sorter = new MClassNameSorter
+               sorter.sort(sorted)
+               return sorted
+       end
 
+       # Extract mproperty list to display (sorted by name)
+       private fun mprops_list: Array[MProperty] do
+               var sorted = new Array[MProperty]
+               for mproperty in ctx.mbuilder.model.mproperties do
+                       if mproperty.visibility < ctx.min_visibility then continue
+                       if mproperty isa MAttribute then continue
+                       sorted.add mproperty
+               end
+               var sorter = new MPropertyNameSorter
+               sorter.sort(sorted)
+               return sorted
        end
-       redef fun require_doc(dctx) do return false
 end
 
-# Conditionnal part of the text content of a DocContext
-class StageContext
-       # Content of the current stage
-       readable var _content: Array[String] = new Array[String]
+# A module page
+# Display the list of introduced and redefined classes in module
+class NitdocModule
+       super NitdocPage
+
+       private var mmodule: MModule
+       private var intro_mclassdefs: Set[MClassDef]
+       private var redef_mclassdefs: Set[MClassDef]
+       private var sorted_intro_mclassdefs: Array[MClassDef]
+       private var sorted_redef_mclassdefs: Array[MClassDef]
+
+       init(mmodule: MModule, ctx: NitdocContext) do
+               self.mmodule = mmodule
+               var sorter = new MClassDefNameSorter
+               intro_mclassdefs = in_nesting_intro_mclassdefs(ctx.min_visibility)
+               sorted_intro_mclassdefs = intro_mclassdefs.to_a
+               sorter.sort sorted_intro_mclassdefs
+               redef_mclassdefs = in_nesting_redef_mclassdefs(ctx.min_visibility)
+               sorted_redef_mclassdefs = redef_mclassdefs.to_a
+               sorter.sort sorted_redef_mclassdefs
+               super(ctx)
+       end
+
+       redef fun tpl_title do
+               if mmodule.mdoc != null then
+                       return "{mmodule.nitdoc_name} module | {mmodule.mdoc.short_comment} | {super}"
+               else
+                       return "{mmodule.nitdoc_name} module | {super}"
+               end
+       end
 
-       # Is a normal string already added?
-       readable writable var _validate: Bool = false
+       redef fun tpl_topmenu do
+               var topmenu = super
+               topmenu.add_elt("index.html", "Overview", false)
+               topmenu.add_elt("#", "{mmodule.nitdoc_name}", true)
+               topmenu.add_elt("search.html", "Search", false)
+               return topmenu
+       end
 
-       # Parent stage is any
-       readable var _parent: nullable StageContext = null
+       redef fun tpl_sidebar do
+               var sidebar = new TplSidebar
+               tpl_sidebar_classes(sidebar)
+               tpl_sidebar_inheritance(sidebar)
+               return sidebar
+       end
 
-       init(parent: nullable StageContext) do _parent = parent
-end
+       # Classes to display on sidebar
+       fun tpl_sidebar_classes(sidebar: TplSidebar) do
+               var box = new TplSidebarBox("Class Definitions")
+               var group = new TplSidebarGroup("Introductions")
+               for mclassdef in sorted_intro_mclassdefs do
+                       tpl_sidebar_item(mclassdef, group)
+               end
+               box.elts.add group
+               group = new TplSidebarGroup("Refinements")
+               for mclassdef in sorted_redef_mclassdefs do
+                       if intro_mclassdefs.has(mclassdef.mclass.intro) then continue
+                       tpl_sidebar_item(mclassdef, group)
+               end
+               box.elts.add group
+               sidebar.boxes.add box
+       end
 
+       # Module inheritance to display on sidebar
+       fun tpl_sidebar_inheritance(sidebar: TplSidebar) do
+               var box = new TplSidebarBox("Module Hierarchy")
+               box.elts.add tpl_sidebar_group("Nested Modules", mmodule.in_nesting.direct_greaters.to_a)
+               var dependencies = new Array[MModule]
+               for dep in mmodule.in_importation.greaters do
+                       if dep == mmodule or
+                               dep.direct_owner == mmodule or
+                               dep.public_owner == mmodule then continue
+                       dependencies.add(dep)
+               end
+               if dependencies.length > 0 then
+                       box.elts.add tpl_sidebar_group("All dependencies", dependencies)
+               end
+               var clients = new Array[MModule]
+               for dep in mmodule.in_importation.smallers do
+                       if dep.name == "<main>" then continue
+                       if dep == mmodule then continue
+                       clients.add(dep)
+               end
+               if clients.length > 0 then
+                       box.elts.add tpl_sidebar_group("All clients", clients)
+               end
+               sidebar.boxes.add box
+       end
 
-# Efficiently sort object with their to_s method
-class AlphaSorter[E: Object]
-       super AbstractSorter[E]
-       redef fun compare(a, b)
-       do
-               var sa: String
-               var sb: String
-               var d = _dico
-               if d.has_key(a) then
-                       sa = d[a]
-               else
-                       sa = a.to_s
-                       d[a] = sa
-               end
-               if d.has_key(b) then
-                       sb = d[b]
+       private fun tpl_sidebar_item(mclassdef: MClassDef, group: TplSidebarGroup) do
+               if mclassdef.is_intro then
+                       group.add_bullet("I", "Introduced", mclassdef.tpl_link_anchor, ["intro"])
                else
-                       sb = b.to_s
-                       d[b] = sb
+                       group.add_bullet("R", "Redefined", mclassdef.tpl_link_anchor, ["redef"])
                end
-               return sa <=> sb
        end
 
-       # Keep track of to_s values
-       var _dico: HashMap[Object, String] = new HashMap[Object, String]
+       private fun tpl_sidebar_group(name: String, elts: Array[MModule]): TplSidebarGroup do
+               var group = new TplSidebarGroup(name)
+               for elt in elts do
+                       group.add_elt(elt.tpl_link, new Array[String])
+               end
+               return group
+       end
 
-       init do end
-end
+       redef fun tpl_content do
+               var class_sorter = new MClassNameSorter
+               var tpl = new TplModulePage
+               tpl.title = mmodule.nitdoc_name
+               tpl.subtitle = mmodule.tpl_signature
+               tpl.definition = mmodule.tpl_definition
+               var location = mmodule.location
+               tpl.definition.location = tpl_showsource(location)
+               tpl.definition.github_area = tpl_github(mmodule.full_namespace, mmodule.mdoc, location)
+               tpl.graph = tpl_dot
+               for mclassdef in sorted_intro_mclassdefs do tpl.intros.add tpl_mclassdef_article(mclassdef)
+               for mclassdef in sorted_redef_mclassdefs do
+                       if intro_mclassdefs.has(mclassdef.mclass.intro) then continue
+                       tpl.redefs.add tpl_mclassdef_article(mclassdef)
+               end
+               return tpl
+       end
 
-# Generalization of metamodel entities
-class MMEntity
-       # Return a link to
-       fun html_link(dctx: DocContext): String is abstract
+       # Genrate dot hierarchy for class inheritance
+       fun tpl_dot: nullable TplGraph do
+               # build poset with public owners
+               var poset = new POSet[MModule]
+               for mmodule in self.mmodule.in_importation.poset do
+                       if mmodule.name == "<main>" then continue
+                       #if mmodule.public_owner != null then continue
+                       if not mmodule.in_importation < self.mmodule and not self.mmodule.in_importation < mmodule and mmodule != self.mmodule then continue
+                       poset.add_node(mmodule)
+                       for omodule in mmodule.in_importation.poset do
+                               if mmodule == omodule then continue
+                               if omodule.name == "<main>" then continue
+                               if not omodule.in_importation < self.mmodule and not self.mmodule.in_importation < omodule then continue
+                               if omodule.in_importation < mmodule then
+                                       poset.add_node(omodule)
+                                       poset.add_edge(omodule, mmodule)
+                               end
+                               if mmodule.in_importation < omodule then
+                                       poset.add_node(omodule)
+                                       poset.add_edge(mmodule, omodule)
+                               end
+                               #if omodule.public_owner != null then continue
+                               #if mmodule.in_importation < omodule then
+                                       #poset.add_node(omodule)
+                                       #poset.add_edge(mmodule, omodule)
+                               #end
+                       end
+               end
+               # build graph
+               var op = new FlatBuffer
+               var name = "dep_{mmodule.name}"
+               op.append("digraph {name} \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
+               for mmodule in poset do
+                       if mmodule == self.mmodule then
+                               op.append("\"{mmodule.name}\"[shape=box,margin=0.03];\n")
+                       else
+                               op.append("\"{mmodule.name}\"[URL=\"{mmodule.nitdoc_url}\"];\n")
+                       end
+                       for omodule in poset[mmodule].direct_greaters do
+                               op.append("\"{mmodule.name}\"->\"{omodule.name}\";\n")
+                       end
+               end
+               op.append("\}\n")
+               return tpl_graph(op, name, "Dependency graph for module {mmodule.name}")
+       end
 
-       # Return a one liner description
-       fun short_doc: String do return "&nbsp;"
+       private fun in_nesting_intro_mclassdefs(min_visibility: MVisibility): Set[MClassDef] do
+               var res = new HashSet[MClassDef]
+               for mmodule in self.mmodule.in_nesting.greaters do
+                       res.add_all mmodule.intro_mclassdefs(min_visibility)
+               end
+               return res
+       end
 
-       # The doc node from the AST
-       # Return null is none
-       fun doc: nullable ADoc do return null
+       private fun in_nesting_redef_mclassdefs(min_visibility: MVisibility): Set[MClassDef] do
+               var res = new HashSet[MClassDef]
+               for mmodule in self.mmodule.in_nesting.greaters do
+                       res.add_all mmodule.redef_mclassdefs(min_visibility)
+               end
+               return res
+       end
 end
 
-redef class MMModule
-       super MMEntity
-       redef fun html_link(dctx) do 
-               return "<a href=\"{html_name}.html\" title=\"{short_doc}\">{self}</a>"
+# A class page
+# Display a list properties defined or redefined for this class
+class NitdocClass
+       super NitdocPage
+
+       private var mclass: MClass
+       private var inherited_mpropdefs: Set[MPropDef]
+       private var intro_mpropdefs: Set[MPropDef]
+       private var redef_mpropdefs: Set[MPropDef]
+       private var all_mpropdefs: Set[MPropDef]
+
+       init(mclass: MClass, ctx: NitdocContext) do
+               self.mclass = mclass
+               super(ctx)
+               intro_mpropdefs = mclass_intro_mpropdefs
+               redef_mpropdefs = mclass_redef_mpropdefs
+               inherited_mpropdefs = in_nesting_inherited_mpropedefs
+               all_mpropdefs = new HashSet[MPropDef]
+               all_mpropdefs.add_all intro_mpropdefs
+               all_mpropdefs.add_all redef_mpropdefs
+               all_mpropdefs.add_all inherited_mpropdefs
        end
 
-       fun require_doc(dctx: DocContext): Bool
-       do
-               if dctx.public_only and not is_toplevel then return false
-               return true
+       private fun in_nesting_inherited_mpropedefs: Set[MPropDef] do
+               var res = new HashSet[MPropDef]
+               var local = mclass.local_mproperties(ctx.min_visibility)
+               for mprop in mclass.inherited_mproperties(ctx.mainmodule, ctx.min_visibility) do
+                       if local.has(mprop) then continue
+                       if mprop isa MMethod and mprop.is_init then continue
+                       res.add mprop.intro
+               end
+               return res
        end
 
-       # Return true if the module is a top-level owner or a top-level module
-       fun is_toplevel: Bool
-       do
-               var pd = directory.parent
-               return pd == null or (pd.owner == null and directory.owner == self)
+       private fun mclass_intro_mpropdefs: Set[MPropDef] do
+               var res = new HashSet[MPropDef]
+               for mclassdef in mclass.mclassdefs do
+                       var owner = mclassdef.mmodule.public_owner
+                       if owner == null then owner = mclassdef.mmodule
+                       for mpropdef in mclassdef.mpropdefs do
+                               if not mpropdef.is_intro then continue
+                               if owner != mclass.public_owner then continue
+                               var mprop = mpropdef.mproperty
+                               if mprop isa MMethod and mprop.is_init and mclass.is_abstract then continue
+                               if mprop.visibility < ctx.min_visibility then continue
+                               res.add mpropdef
+                       end
+               end
+               return res
        end
 
-       # Element in the module nesting tree
-       fun mnhe: PartialOrderElement[MMModule] do return mnhe_.as(not null)
-       var mnhe_: nullable PartialOrderElement[MMModule] = null
+       private fun mclass_redef_mpropdefs: Set[MPropDef] do
+               var res = new HashSet[MPropDef]
+               for mclassdef in mclass.mclassdefs do
+                       if mclassdef == mclass.intro then continue
+                       var owner = mclassdef.mmodule.public_owner
+                       if owner == null then owner = mclassdef.mmodule
+                       for mpropdef in mclassdef.mpropdefs do
+                               if owner == mclass.public_owner then continue
+                               if mpropdef.mproperty.visibility < ctx.min_visibility then continue
+                               res.add mpropdef
+                       end
+               end
+               return res
+       end
 
-       # Element in the top level module importation hierarchy
-       fun tmhe: PartialOrderElement[MMModule] do return tmhe_.as(not null)
-       var tmhe_: nullable PartialOrderElement[MMModule] = null
+       redef fun tpl_title do
+               if mclass.mdoc != null then
+                       return "{mclass.nitdoc_name} class | {mclass.mdoc.short_comment} | {super}"
+               else
+                       return "{mclass.nitdoc_name} class | {super}"
+               end
+       end
 
-       fun toplevel_owner: MMModule
-       do
-               var m = self
-               loop
-                       var ds = m.mnhe.direct_smallers
-                       if ds.length == 0 then return m
-                       if ds.length == 1 then m = ds.first else abort
+       redef fun tpl_topmenu do
+               var topmenu = super
+               var mmodule: MModule
+               if mclass.public_owner == null then
+                       mmodule = mclass.intro_mmodule
+               else
+                       mmodule = mclass.public_owner.as(not null)
                end
+               topmenu.add_elt("index.html", "Overview", false)
+               topmenu.add_elt("{mmodule.nitdoc_url}", "{mmodule.nitdoc_name}", false)
+               topmenu.add_elt("#", "{mclass.nitdoc_name}", true)
+               topmenu.add_elt("search.html", "Search", false)
+               return topmenu
        end
 
-       fun html_name: String
-       do
-               return "{name}"
+       redef fun tpl_sidebar do
+               var sidebar = new TplSidebar
+               tpl_sidebar_properties(sidebar)
+               tpl_sidebar_inheritance(sidebar)
+               return sidebar
        end
 
-       fun direct_owner: nullable MMModule
-       do
-               var d = directory
-               while d.owner == self do d = d.parent.as(not null)
-               return d.owner
+       # Property list to display in sidebar
+       fun tpl_sidebar_properties(sidebar: TplSidebar) do
+               var kind_map = sort_by_kind(all_mpropdefs)
+               var sorter = new MPropDefNameSorter
+               var box = new TplSidebarBox("Properties")
+               # virtual types
+               var elts = kind_map["type"].to_a
+               sorter.sort(elts)
+               var group = new TplSidebarGroup("Virtual Types")
+               for mprop in elts do
+                       tpl_sidebar_item(mprop, group)
+               end
+               box.elts.add group
+               # constructors
+               elts = kind_map["init"].to_a
+               sorter.sort(elts)
+               group = new TplSidebarGroup("Constructors")
+               for mprop in elts do
+                       tpl_sidebar_item(mprop, group)
+               end
+               box.elts.add group
+               # methods
+               elts = kind_map["fun"].to_a
+               sorter.sort(elts)
+               group = new TplSidebarGroup("Methods")
+               for mprop in elts do
+                       tpl_sidebar_item(mprop, group)
+               end
+               box.elts.add group
+               sidebar.boxes.add box
        end
 
-       # Fill the body for the page associated to the module 
-       fun file_page_doc(dctx: DocContext)
-       do
-               dctx.add("<div class=\"menu\">\n")
-
-               var mods = new Array[MMModule]
-               mods = self.mhe.greaters.to_a
-               dctx.sort(mods)
-
-               dctx.open_stage
-               dctx.stage("<nav>\n")
-               dctx.stage("<h3>Module Hierarchy</h3>\n")
-               dctx.stage("<h4>All dependencies</h4>\n")
-               dctx.stage("<ul>\n")
-               for mod in mods do
-                       if not mod.require_doc(dctx) then continue
-                       if self.mnhe <= mod then continue # do not want nested stuff
-                       if mod.direct_owner != null and not mod.direct_owner.mnhe <= self then continue # not in the right nesting
-                       dctx.add("<li>{mod.html_link(dctx)}</li>")
-               end
-               dctx.stage("</ul>\n")
-
-               mods = self.mhe.smallers.to_a
-               dctx.sort(mods)
-               dctx.stage("<h4>All clients</h4>\n")
-               dctx.stage("<ul>\n")
-               for mod in mods do
-                       if not mod.require_doc(dctx) then continue
-                       if self.mnhe <= mod then continue # do not want nested stuff
-                       if mod.direct_owner != null and not mod.direct_owner.mnhe <= self then continue # not in the right nesting
-                       dctx.add("<li>{mod.html_link(dctx)}</li>")
-               end
-               dctx.stage("</ul>\n")
-               dctx.stage("</nav>\n")
-               dctx.close_stage
-
-               if not dctx.public_only then
-                       mods = self.mnhe.direct_greaters.to_a
-                       dctx.sort(mods)
-                       dctx.open_stage
-                       dctx.stage("<nav>\n")
-                       dctx.stage("<h3>Nested Modules</h3><ul>\n")
-                       for mod in mods do
-                               if not mod.require_doc(dctx) then continue
-                               dctx.add("<li>{mod.html_link(dctx)}</li>")
-                       end
-                       dctx.stage("</ul></nav>\n")
-                       dctx.close_stage
+       # Class inheritance to display in sidebar
+       fun tpl_sidebar_inheritance(sidebar: TplSidebar) do
+               var sorted = new Array[MClass]
+               var sorterp = new MClassNameSorter
+               var box = new TplSidebarBox("Inheritance")
+               var greaters = mclass.in_hierarchy(ctx.mainmodule).greaters.to_a
+               if greaters.length > 1 then
+                       ctx.mainmodule.linearize_mclasses(greaters)
+                       box.elts.add tpl_sidebar_group("Superclasses", greaters)
+               end
+               var smallers = mclass.in_hierarchy(ctx.mainmodule).smallers.to_a
+               var direct_smallers = mclass.in_hierarchy(ctx.mainmodule).direct_smallers.to_a
+               if smallers.length <= 1 then
+                       box.elts.add(new TplSidebarGroup("No Known Subclasses"))
+               else if smallers.length <= 100 then
+                       ctx.mainmodule.linearize_mclasses(smallers)
+                       box.elts.add tpl_sidebar_group("Subclasses", smallers)
+               else if direct_smallers.length <= 100 then
+                       ctx.mainmodule.linearize_mclasses(direct_smallers)
+                       box.elts.add tpl_sidebar_group("Direct Subclasses Only", direct_smallers)
+               else
+                       box.elts.add(new TplSidebarGroup("Too much Subclasses to list"))
                end
+               sidebar.boxes.add box
+       end
 
-               dctx.add("</div>") # metadata
-
-               dctx.add("<div class=\"content\">\n")
-               dctx.add("<h1>{name}</h1>\n")
-               dctx.add("<div class='subtitle'>module ")
-               for m in mnhe.smallers do
-                       dctx.add("{m.html_link(dctx)}::")
+       private fun tpl_sidebar_item(mprop: MPropDef, group: TplSidebarGroup) do
+               if mprop.is_intro and mprop.mclassdef.mclass == mclass then
+                       group.add_bullet("I", "Introduced", mprop.tpl_link, ["intro"])
+               else if mprop.is_intro and mprop.mclassdef.mclass != mclass then
+                       group.add_bullet("H", "Inherited", mprop.tpl_link, ["inherit"])
+               else
+                       group.add_bullet("R", "Redefined", mprop.tpl_link, ["redef"])
                end
-               dctx.add("{self.name}</div>\n")
-
-               dctx.add("<section class='description'>\n")
+       end
 
-               var doc = doc
-               if doc != null then
-                       dctx.add("<div id=\"description\">\n")
-                       dctx.add("<pre>{doc.to_html}</pre>\n")
-                       dctx.add("</div>\n")
+       private fun tpl_sidebar_group(name: String, elts: Array[MClass]): TplSidebarGroup do
+               var group = new TplSidebarGroup(name)
+               for elt in elts do
+                       if elt == mclass then continue
+                       group.add_elt(elt.tpl_link, new Array[String])
                end
+               return group
+       end
 
-               var op = new Buffer
-               op.append("digraph {name} \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
-               var ms = new Array[nullable MMModule]
-               do
-                       var m0: nullable MMModule = self
-                       while m0 != null do
-                               m0 = m0.direct_owner
-                               ms.add(m0)
-                       end
-               end
-               var cla = new HashSet[MMModule]
-               cla.add(self)
-               for m0 in self.mhe.greaters do
-                       if not m0.require_doc(dctx) then continue
-                       if self.visibility_for(m0) <= 1 then continue # private or hidden
-                       if self.mnhe <= m0 then continue # do not want nested stuff
-                       if m0.direct_owner != null and not m0.direct_owner.mnhe <= self then continue # not in the right nesting
-                       cla.add(m0)
-               end
-               for m0 in self.mhe.smallers do
-                       if not m0.require_doc(dctx) then continue
-                       if m0.visibility_for(self) <= 1 then continue # private or hidden
-                       if m0.direct_owner != null and not m0.direct_owner.mnhe <= self then continue # not in the right nesting
-                       cla.add(m0)
-               end
-               for m0 in self.mnhe.smallers do
-                       cla.add(m0)
-               end
-               ms = ms.reversed
-               for m0 in ms do
-                       if m0 != null then
-                               op.append("subgraph \"cluster_{m0.name}\"\{\n")
-                       end
-                       for c in cla do
-                               if c.direct_owner != m0 then continue
-                               if c == self then
-                                       op.append("\"{c.name}\"[shape=box,margin=0.03];\n")
-                               else
-                                       op.append("\"{c.name}\"[URL=\"{c.html_name}.html\"];\n")
-                               end
+       redef fun tpl_content do
+               var intro = mclass.intro
+               var tpl = new TplClassPage
+               tpl.title = "{mclass.nitdoc_name}{mclass.tpl_short_signature}"
+               tpl.subtitle = mclass.tpl_namespace_with_signature
+               tpl.definition = intro.tpl_definition
+               var location = intro.location
+               tpl.definition.location = tpl_showsource(location)
+               tpl.definition.github_area = tpl_github(intro.full_namespace, intro.mdoc, location)
+               tpl.graph = tpl_dot
+
+               # properties
+               var prop_sorter = new MPropDefNameSorter
+               var kind_map = sort_by_kind(intro_mpropdefs)
+
+               # virtual types
+               var elts = kind_map["type"].to_a
+               prop_sorter.sort(elts)
+               for elt in elts do tpl.types.add tpl_mpropdef_article(elt)
+
+               # constructors
+               elts = kind_map["init"].to_a
+               prop_sorter.sort(elts)
+               for elt in elts do tpl.inits.add tpl_mpropdef_article(elt)
+
+               # intro methods
+               elts = kind_map["fun"].to_a
+               prop_sorter.sort(elts)
+               for elt in elts do tpl.methods.add tpl_mpropdef_article(elt)
+
+               # redef methods
+               kind_map = sort_by_kind(redef_mpropdefs)
+               var module_sorter = new MModuleNameSorter
+               var module_map = sort_by_mmodule(kind_map["fun"])
+               var owner_map = sort_by_public_owner(module_map.keys)
+               var owners = owner_map.keys.to_a
+               module_sorter.sort owners
+
+               var ctpl = new TplConcernList
+               var mtpl = new Template
+               for owner in owners do
+                       # concerns list
+                       var octpl = new TplConcernListElt
+                       octpl.anchor = "#{owner.nitdoc_anchor}"
+                       octpl.name = owner.nitdoc_name
+                       if owner.mdoc != null then
+                               octpl.comment = owner.mdoc.short_comment
                        end
-                       if m0 != null then
-                               op.append("\"{m0.name}\"[URL=\"{m0.html_name}.html\"];\n")
-                               for c in m0.mhe.direct_greaters do
-                                       if not cla.has(c) then continue
-                                       op.append("\"{m0.name}\"->\"{c.name}\";\n")
+                       ctpl.elts.add octpl
+                       # concern section
+                       var otpl = new TplTopConcern
+                       otpl.anchor = owner.nitdoc_anchor
+                       otpl.concern = owner.tpl_link
+                       mtpl.add otpl
+
+                       var mmodules = owner_map[owner].to_a
+                       module_sorter.sort mmodules
+                       var stpl = new TplConcernList
+                       for mmodule in mmodules do
+                               # concerns list
+                               if mmodule != owner then
+                                       var mctpl = new TplConcernListElt
+                                       mctpl.anchor = "#{mmodule.nitdoc_anchor}"
+                                       mctpl.name = mmodule.nitdoc_name
+                                       if mmodule.mdoc != null then
+                                               mctpl.comment = mmodule.mdoc.short_comment
+                                       end
+                                       stpl.elts.add mctpl
+                                       # concern section
+                                       var cctpl = new TplConcern
+                                       cctpl.anchor = mmodule.nitdoc_anchor
+                                       cctpl.concern = mmodule.tpl_link
+                                       if mmodule.mdoc != null then
+                                               cctpl.comment = mmodule.mdoc.short_comment
+                                       end
+                                       mtpl.add cctpl
                                end
+                               var mprops = module_map[mmodule].to_a
+                               prop_sorter.sort mprops
+                               for mprop in mprops do mtpl.add tpl_mpropdef_article(mprop)
                        end
+                       ctpl.elts.add stpl
                end
-               for m0 in ms do
-                       # Close the nesting subgraph
-                       if m0 != null then
-                               op.append("\}\n")
-                       end
-               end
-               for c in cla do
-                       for c2 in c.tmhe.direct_greaters do
-                               if not cla.has(c2) then continue
-                               op.append("\"{c.name}\"->\"{c2.name}\";\n")
-                       end
+               if not owners.is_empty then
+                       tpl.concerns = ctpl
                end
-               op.append("\}\n")
-               dctx.gen_dot(op.to_s, name.to_s, "Dependency graph for module {name}")
-               dctx.add("</section>")
-
-               var clas = new Array[MMLocalClass]
-               var props = new HashMap[MMGlobalProperty, Array[MMLocalProperty]]
-               var gprops = new Array[MMLocalProperty]
-               do
-                       var m = self
-                       for g in m.global_classes do
-                               var lc = m[g]
-                               if not lc.require_doc(dctx) then continue
-                               var im = g.intro.mmmodule
-                               if self.visibility_for(im) <= 1 then continue # private import or invisible import
-                               var keep = false
-                               for lc2 in lc.crhe.greaters_and_self do
-                                       if not lc2 isa MMSrcLocalClass then continue
-                                       if not self.mnhe <= lc2.mmmodule then continue # not introduced/redefined here/stolen
-                                       keep = true
-                               end
-                               if not keep then continue
-                               clas.add(self[g])
-                               lc.compute_super_classes
-                               for gp in lc.global_properties do
-                                       if self.visibility_for(gp.intro.local_class.mmmodule) <= 1 then continue # private import or invisible import
-                                       var lp = lc[gp]
-                                       var mp = lp.local_class.mmmodule
-                                       if not self.mnhe <= mp then continue # not introduced/redefined here/stolen
-                                       lp = self[g][gp]
-                                       if not lp.require_doc(dctx) then continue
-                                       if props.has_key(lp.global) then
-                                               if not props[lp.global].has(lp) then
-                                                       props[lp.global].add(lp)
-                                               end
-                                       else
-                                               props[lp.global] = [lp]
-                                               gprops.add(lp.global.intro)
-                                       end
+               tpl.methods.add mtpl
+               return tpl
+       end
+
+       private fun sort_by_kind(mpropdefs: Set[MPropDef]): Map[String, Set[MPropDef]] do
+               var map = new HashMap[String, Set[MPropDef]]
+               map["type"] = new HashSet[MPropDef]
+               map["init"] = new HashSet[MPropDef]
+               map["fun"] = new HashSet[MPropDef]
+               for mpropdef in mpropdefs do
+                       if mpropdef isa MVirtualTypeDef then
+                               map["type"].add mpropdef
+                       else if mpropdef isa MMethodDef then
+                               if mpropdef.mproperty.is_init then
+                                       map["init"].add mpropdef
+                               else
+                                       map["fun"].add mpropdef
                                end
                        end
                end
-               dctx.add("<section class=\"module\">\n")
-               dctx.open_stage
-               dctx.stage("<article class=\"classes filterable\">\n")
-               dctx.stage("<h2>Classes</h2>\n")
-               dctx.sort(clas)
-               dctx.stage("<ul>\n")
-               for lc in clas do
-                       if self.mnhe <= lc.global.intro.mmmodule then
-                               dctx.add("<li class='intro'><span title='introduced in this module'>I</span>&nbsp;")
-                       else
-                               dctx.add("<li class='redef'><span title='refined in this module'>R</span>&nbsp;")
-                       end
-                       dctx.add("{lc.html_link(dctx)}</li>\n")
-               end
-               dctx.stage("</ul></article>\n")
-               dctx.close_stage
-
-               dctx.open_stage
-               dctx.stage("<article class=\"properties filterable\">\n")
-               dctx.stage("<h2>Properties</h2>\n")
-               dctx.sort(gprops)
-               dctx.stage("<ul>\n")
-               for lgp in gprops do
-                       var gp = lgp.global
-                       var lps = props[gp]
-
-                       if gp.intro isa MMAttribute then continue
-                       
-                       var lpi = self[gp.intro.local_class.global][gp]
-               
-                       if lps.has(lpi) then
-                               dctx.add("<li class='intro'><span title='introduction in an other module'>I</span>&nbsp;{lpi.html_open_link(dctx)}{lpi.html_name}&nbsp;({lpi.local_class})</a></li>\n")
-                               lps.remove(lpi)
-                       else
-                               dctx.add("<li class='intro'><span title='introduction in this module'>I</span>&nbsp;{lpi.html_name}")
-                               dctx.add("&nbsp;({lpi.local_class})</li>\n")
-                       end
-                       if lps.length >= 1 then
-                               dctx.sort(lps)
-                               for lp in lps do
-                                       dctx.add("<li class='redef'><span title='redefinition'>R</span>&nbsp;{lp.html_open_link(dctx)}{lp.html_name}&nbsp;({lp.local_class})</a></li>")
-                               end
-                       end
+               return map
+       end
+
+       private fun sort_by_mmodule(mpropdefs: Collection[MPropDef]): Map[MModule, Set[MPropDef]] do
+               var map = new HashMap[MModule, Set[MPropDef]]
+               for mpropdef in mpropdefs do
+                       var mmodule = mpropdef.mclassdef.mmodule
+                       if not map.has_key(mmodule) then map[mmodule] = new HashSet[MPropDef]
+                       map[mmodule].add mpropdef
                end
-               dctx.stage("</ul></article>\n")
-               dctx.close_stage
-               dctx.add("</section>\n")
-               dctx.add("</div>\n")
+               return map
        end
 
-       # Fill the body for the page associated to the full index
-       fun file_index_page_doc(dctx: DocContext)
-       do
+       private fun sort_by_public_owner(mmodules: Collection[MModule]): Map[MModule, Set[MModule]] do
+               var map = new HashMap[MModule, Set[MModule]]
+               for mmodule in mmodules do
+                       var owner = mmodule
+                       if mmodule.public_owner != null then owner = mmodule.public_owner.as(not null)
+                       if not map.has_key(owner) then map[owner] = new HashSet[MModule]
+                       map[owner].add mmodule
+               end
+               return map
+       end
 
-               dctx.add("<h1>Full Index</h1>\n")
-
-               var clas = new Array[MMLocalClass]
-               var props = new HashMap[MMGlobalProperty, Array[MMLocalProperty]]
-               var gprops = new Array[MMLocalProperty]
-               var mods = new Array[MMModule]
-               for m in mhe.greaters_and_self do
-                       if not m.require_doc(dctx) then continue
-                       mods.add(m)
-               end
-               for g in global_classes do
-                       var lc = self[g]
-                       if not lc.require_doc(dctx) then continue
-                       clas.add(lc)
-                       for gp in lc.global_properties do
-                               var lp = lc[gp]
-                               if not lp.require_doc(dctx) then continue
-                               if props.has_key(lp.global) then
-                                       if not props[lp.global].has(lp) then
-                                               props[lp.global].add(lp)
-                                       end
-                               else
-                                       props[lp.global] = [lp]
-                                       gprops.add(lp.global.intro)
-                               end
+       # Generate dot hierarchy for classes
+       fun tpl_dot: nullable TplGraph do
+               var pe = mclass.in_hierarchy(ctx.mainmodule)
+               var cla = new HashSet[MClass]
+               var sm = new HashSet[MClass]
+               var sm2 = new HashSet[MClass]
+               sm.add(mclass)
+               while cla.length + sm.length < 10 and sm.length > 0 do
+                       cla.add_all(sm)
+                       sm2.clear
+                       for x in sm do
+                               sm2.add_all(pe.poset[x].direct_smallers)
                        end
+                       var t = sm
+                       sm = sm2
+                       sm2 = t
                end
-               dctx.open_stage
-               dctx.stage("<article class=\"modules filterable\">\n")
-               dctx.stage("<h2>Modules</h2>\n")
-               dctx.sort(mods)
-               dctx.stage("<ul>\n")
-               for m in mods do
-                       dctx.add("<li>{m.html_link(dctx)}</li>")
-               end
-               dctx.stage("</ul></article>\n")
-               dctx.close_stage
-
-               dctx.open_stage
-               dctx.stage("<article class=\"classes filterable\">\n")
-               dctx.stage("<h2>Classes</h2>\n")
-               dctx.sort(clas)
-               dctx.stage("<ul>\n")
-               for lc in clas do
-                       dctx.add("<li>{lc.html_link(dctx)}</li>")
-               end
-               dctx.stage("</ul></article>\n")
-               dctx.close_stage
-
-               dctx.open_stage
-               dctx.stage("<article class=\"properties filterable\">\n")
-               dctx.stage("<h2>Properties</h2>\n")
-               dctx.sort(gprops)
-               dctx.stage("<ul>\n")
-               for lgp in gprops do
-                       var gp = lgp.global
-                       var lps = props[gp]
-
-                       if gp.intro isa MMAttribute then continue
-
-                       var lpi = self[gp.intro.local_class.global][gp]
-                       
-                       lps.remove(lpi)
-                               dctx.add("<li class='intro'><span title='introduction'>I</span>&nbsp;{lpi.html_open_link(dctx)}{lpi.html_name}&nbsp;({lpi.local_class})</a></li>\n")
-                       if lps.length >= 1 then
-                               dctx.sort(lps)
-                               for lp in lps do
-                                       dctx.add("<li class='redef'><span title='redefinition'>R</span>&nbsp;{lp.html_open_link(dctx)}{lp.html_name}&nbsp;({lp.local_class})</a></li>\n")
+               cla.add_all(pe.greaters)
+
+               var op = new FlatBuffer
+               var name = "dep_{mclass.name}"
+               op.append("digraph {name} \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
+               for c in cla do
+                       if c == mclass then
+                               op.append("\"{c.name}\"[shape=box,margin=0.03];\n")
+                       else
+                               op.append("\"{c.name}\"[URL=\"{c.nitdoc_url}\"];\n")
+                       end
+                       for c2 in pe.poset[c].direct_greaters do
+                               if not cla.has(c2) then continue
+                               op.append("\"{c.name}\"->\"{c2.name}\";\n")
+                       end
+                       if not pe.poset[c].direct_smallers.is_empty then
+                               var others = true
+                               for c2 in pe.poset[c].direct_smallers do
+                                       if cla.has(c2) then others = false
+                               end
+                               if others then
+                                       op.append("\"{c.name}...\"[label=\"\"];\n")
+                                       op.append("\"{c.name}...\"->\"{c.name}\"[style=dotted];\n")
                                end
                        end
                end
-               dctx.stage("</ul></article>\n")
-               dctx.close_stage
+               op.append("\}\n")
+               return tpl_graph(op, name, "Dependency graph for class {mclass.name}")
        end
 end
 
-redef class MMLocalProperty
-       super MMEntity
-       # Anchor of the property description in the module html file
-       fun html_anchor: String
-       do
-               return "PROP_{local_class}_{cmangle(name)}"
-       end
+#
+# Model redefs
+#
 
-       fun html_open_link(dctx: DocContext): String
-       do
-               if not require_doc(dctx) then print "not required {self}"
-               var title = "{html_name}{signature.to_s}"
-               if short_doc != "&nbsp;" then
-                       title += " #{short_doc}"
+redef class MModule
+       # Return the HTML escaped name of the module
+       private fun nitdoc_name: String do return name.html_escape
+
+       private fun full_namespace: String do
+               if public_owner != null then
+                       return "{public_owner.nitdoc_name}::{nitdoc_name}"
                end
-               return "<a href=\"{local_class.html_name}.html#{html_anchor}\" title=\"{title}\">"
+               return nitdoc_name
        end
 
-       fun html_name: String
-       do
-               return self.name.to_s.html_escape
+       # URL to nitdoc page
+       #       module_owner_name.html
+       private fun nitdoc_url: String do
+               var res = new FlatBuffer
+               res.append("module_")
+               var mowner = public_owner
+               if mowner != null then
+                       res.append("{public_owner.name}_")
+               end
+               res.append("{self.name}.html")
+               return res.to_s
        end
 
-       redef fun html_link(dctx)
-       do
-               if not require_doc(dctx) then print "not required {self}"
-               var title = "{html_name}{signature.to_s}"
-               if short_doc != "&nbsp;" then
-                       title += " #{short_doc}"
+       # html nitdoc_anchor id for the module in a nitdoc page
+       #       MOD_owner_name
+       private fun nitdoc_anchor: String do
+               var res = new FlatBuffer
+               res.append("MOD_")
+               var mowner = public_owner
+               if mowner != null then
+                       res.append("{public_owner.name}_")
                end
-               return "<a href=\"{local_class.html_name}.html#{html_anchor}\" title=\"{title}\">{html_name}</a>"
+               res.append(self.name)
+               return res.to_s
        end
 
-       fun html_link_special(dctx: DocContext, lc: MMLocalClass): String
-       do
-               if not require_doc(dctx) then print "not required {self}"
-               var title = "{html_name}{signature_for(lc.get_type)}"
-               if short_doc != "&nbsp;" then
-                       title += " #{short_doc}"
+       # Return a link (html a tag) to the nitdoc module page
+       private fun tpl_link: TplLink do
+               var tpl = new TplLink
+               tpl.href = nitdoc_url
+               tpl.text = nitdoc_name
+               if mdoc != null then
+                       tpl.title = mdoc.short_comment
                end
-               return "<a href=\"{lc.html_name}.html#{html_anchor}\" title=\"{title}\">{html_name}</a>"
+               return tpl
        end
 
-       # Kind of property (fun, attr, etc.)
-       fun kind: String is abstract
+       # Return the module signature decorated with html
+       private fun tpl_signature: Template do
+               var tpl = new Template
+               tpl.add "<span>module "
+               tpl.add tpl_full_namespace
+               tpl.add "</span>"
+               return tpl
+       end
 
-       redef fun short_doc
-       do
-               var d = doc
-               if d != null then
-                       return d.short
-               else if global.intro == self then
-                       return "&nbsp;"
+       # Return the module full namespace decorated with html
+       private fun tpl_full_namespace: Template do
+               var tpl = new Template
+               tpl.add ("<span>")
+               var mowner = public_owner
+               if mowner != null then
+                       tpl.add public_owner.tpl_namespace
+                       tpl.add "::"
+               end
+               tpl.add tpl_link
+               tpl.add "</span>"
+               return tpl
+       end
+
+       # Return the module full namespace decorated with html
+       private fun tpl_namespace: Template do
+               var tpl = new Template
+               tpl.add "<span>"
+               var mowner = public_owner
+               if mowner != null then
+                       tpl.add public_owner.tpl_namespace
                else
-                       return global.intro.short_doc
+                       tpl.add tpl_link
                end
+               tpl.add "</span>"
+               return tpl
        end
-       
-       redef fun doc
-       do
-               var n = node
-               if n == null or not n isa APropdef then
-                       return null
-               end
-               var d = n.n_doc
-               if d == null then
-                       return null
-               end
-               if d.n_comment.is_empty then
-                       return null
-               else
-                       return d
+
+       # Description with short comment
+       private fun tpl_short_definition: TplDefinition do
+               var tpl = new TplDefinition
+               tpl.namespace = tpl_namespace
+               if mdoc != null then
+                       tpl.comment = mdoc.tpl_short_comment
                end
+               return tpl
        end
 
-       # The most specific module in the nesting hierarchy that exports the intro of self
-       fun intro_module: MMModule
-       do
-               var m = global.intro.mmmodule
-               var mo = m.direct_owner
-               while mo != null and mo.visibility_for(m) >= 2 do 
-                       m = mo
-                       mo = m.direct_owner
+       # Description with full comment
+       private fun tpl_definition: TplDefinition do
+               var tpl = new TplDefinition
+               tpl.namespace = tpl_namespace
+               if mdoc != null then
+                       tpl.comment = mdoc.tpl_comment
                end
-               return m
+               return tpl
        end
+end
 
-       # Is the intro of self exported by the top-level module ?
-       fun is_toplevel: Bool
-       do
-               var m = intro_module
-               return m == m.toplevel_owner
+redef class MClass
+       # Return the HTML escaped name of the module
+       private fun nitdoc_name: String do return name.html_escape
+
+       # URL to nitdoc page
+       #       class_owner_name.html
+       private fun nitdoc_url: String do
+               return "class_{public_owner}_{name}.html"
        end
 
-       # Return true if the global property must be documented according to the visibility configured
-       fun require_doc(dctx: DocContext): Bool
-       do
-               if global.visibility_level == 3 and not dctx.with_private then return false # Private
-               if dctx.public_only then
-                       var m = intro_module
-                       if m != m.toplevel_owner then return false # Unexported
-               end
-               return true
+       # html nitdoc_anchor id for the class in a nitdoc page
+       #       MOD_owner_name
+       private fun nitdoc_anchor: String do
+               return "CLASS_{public_owner.name}_{name}"
        end
 
-       # Document the global property in the global class lc
-       fun full_documentation(dctx: DocContext, lc: MMLocalClass)
-       do
-               var visibility: String
-               if global.visibility_level == 1 then
-                       visibility = "public"
-               else if global.visibility_level == 2 then
-                       visibility = "protected"
-               else if global.visibility_level == 3 then
-                       visibility = "private"
-               else 
-                       abort
-               end
-
-               var intro_class = global.intro.local_class
-               var is_redef = local_class.global != intro_class.global or local_class.mmmodule.toplevel_owner != intro_class.mmmodule.toplevel_owner
-
-               dctx.add("<article id=\"{html_anchor}\" class=\"{kind} {visibility} {if is_redef then "redef" else ""}\">\n")
-               dctx.add("<h3 class=\"signature\">{html_name}{signature.to_html(dctx, true)}</h3>\n")
-               dctx.add("<div class=\"info\">\n")
-               #dctx.add("<p>LP: {self.mmmodule.html_link(dctx)}::{self.local_class.html_link(dctx)}::{self.html_link(dctx)}</p>")
-
-               if is_redef then
-                       dctx.add("redef ")
-               end
-               if not is_toplevel then
-                       dctx.add("(unexported) ")
-               end
-               if global.visibility_level == 2 then
-                       dctx.add("protected ")
-               else if global.visibility_level == 3 then
-                       dctx.add("private ")
-               end
-               dctx.add(kind)
-               dctx.add(" {intro_class.mmmodule.toplevel_owner.name}")
-               if intro_class.global == lc.global then
-                       dctx.add("::{lc.name}")
-               else
-                       dctx.add("::{mmmodule[intro_class.global].html_link(dctx)}")
-               end
-               if is_redef then
-                       dctx.add("::{mmmodule[intro_class.global][global].html_link(dctx)}")
-               else
-                       dctx.add("::{html_name}")
+       # Return a link (with signature) to the nitdoc class page
+       private fun tpl_link: TplLink do
+               var tpl = new TplLink
+               tpl.href = nitdoc_url
+               tpl.text = "{nitdoc_name}{tpl_short_signature.write_to_string}"
+               if intro.mdoc != null then
+                       tpl.title = intro.mdoc.short_comment
                end
-               dctx.add("</div>")
+               return tpl
+       end
 
-               dctx.add("<div class=\"description\">")
+       # Return a short link (without signature) to the nitdoc class page
+       private fun tpl_short_link: TplLink do
+               var tpl = new TplLink
+               tpl.href = nitdoc_url
+               tpl.text = nitdoc_name
+               if intro.mdoc != null then
+                       tpl.title = intro.mdoc.short_comment
+               end
+               return tpl
+       end
 
-               # Collect all refinement of the global property in the same global property
-               var lps = new Array[MMLocalProperty]
-               for l in prhe.greaters_and_self do
-                       lps.add(l)
+       # Return a link (with signature) to the class nitdoc_anchor
+       private fun tpl_link_anchor: TplLink do
+               var tpl = new TplLink
+               tpl.href = "#{nitdoc_anchor}"
+               tpl.text = "{nitdoc_name}{tpl_short_signature.write_to_string}"
+               if intro.mdoc != null then
+                       tpl.title = intro.mdoc.short_comment
                end
+               return tpl
+       end
 
-               var introdoc = false
-               if global.intro.doc != null then
-                       for lp in lps do
-                               if lp.doc == null then introdoc = true
+       # Return the generic signature of the class with bounds
+       private fun tpl_signature: Template do
+               var tpl = new Template
+               if arity > 0 then
+                       tpl.add "["
+                       for i in [0..intro.parameter_names.length[ do
+                               tpl.add "{intro.parameter_names[i]}: "
+                               tpl.add intro.bound_mtype.arguments[i].tpl_link
+                               if i < intro.parameter_names.length - 1 then tpl.add ", "
                        end
+                       tpl.add "]"
                end
-               if introdoc then
-                       dctx.add("<pre>{global.intro.doc.to_html}</pre>")
-               end
+               return tpl
+       end
 
-               var tlmods = new Array[MMModule]
-               for lp in lps do
-                       var bm = lp.mmmodule.toplevel_owner
-                       var lcm = lc.global.intro.mmmodule
-                       if lcm.mhe < lp.mmmodule then bm = lcm.toplevel_owner
-                       if not tlmods.has(bm) then tlmods.add(bm)
+       # Return the generic signature of the class without bounds
+       private fun tpl_short_signature: String do
+               if arity > 0 then
+                       return "[{intro.parameter_names.join(", ")}]"
+               else
+                       return ""
                end
+       end
 
-               for tm in tlmods do
-                       # Document the top level property for the current top level module
-                       var tlp
-                       if tm.global_classes.has(lc.global) then
-                               tlp = tm[lc.global][self.global]
-                               assert lps.has(tlp)
-                       else if tm.global_classes.has(self.local_class.global) then
-                               # Self is the inherited property. Process it
-                               tlp = tm[self.local_class.global][self.global]
-                               assert lps.has(tlp)
-                       else
-                               # We skip this module since the props defined by the module is  
-                               continue
-                       end
+       # Return the class namespace decorated with html
+       private fun tpl_namespace: Template do
+               var tpl = new Template
+               tpl.add intro_mmodule.tpl_namespace
+               tpl.add "::<span>"
+               tpl.add tpl_short_link
+               tpl.add "</span>"
+               return tpl
+       end
 
-                       var tlcm = lc.global.intro.mmmodule.toplevel_owner
-                       if not tlcm.mhe <= tm then
-                               dctx.add("<h4>In module {tm.html_link(dctx)} :</h4>")
-                       end
+       private fun tpl_namespace_with_signature: Template do
+               var tpl = new Template
+               tpl.add intro.tpl_modifiers
+               tpl.add intro.mmodule.tpl_namespace
+               tpl.add "::"
+               tpl.add nitdoc_name
+               tpl.add tpl_signature
+               return tpl
+       end
+end
 
-                       #dctx.add("<p>TLP: {tm} x {lc} : {tlp.full_name}</p>")
+redef class MProperty
+       # Escape name for html output
+       private fun nitdoc_name: String do return name.html_escape
+
+       # Return the property namespace decorated with html
+       private fun tpl_namespace: Template do
+               var tpl = new Template
+               tpl.add intro_mclassdef.mclass.tpl_namespace
+               tpl.add intro_mclassdef.mclass.tpl_short_signature
+               tpl.add "::<span>"
+               tpl.add intro.tpl_link
+               tpl.add "</span>"
+               return tpl
+       end
 
-                       var doc = tlp.doc
-                       if doc != null and (not introdoc or global.intro.doc != doc) then
-                               dctx.add("<pre>{doc.to_html}</pre>")
-                       end
-                       dctx.add("<p>")
-                       if tlp.local_class.global != lc.global then
-                               dctx.add("inherited from {tlp.local_class.html_link(dctx)} ")
-                       end
-                       if tm != tlp.mmmodule then
-                               dctx.add("defined by the module {tlp.mmmodule.html_link(dctx)} ")
-                       end
-                       var n = tlp.node
-                       if n != null then
-                               var l = n.location
-                               dctx.show_source(l)
-                       end
-                       
-                       dctx.open_stage
-                       dctx.stage(". previously defined by:")
-                       for lp in lps do
-                               var tl = lp.mmmodule.toplevel_owner
-                               if tl != tm then continue
-                               if lp == tlp then continue
-                               dctx.add(" {lp.mmmodule.html_link(dctx)}")
-                               if lp.local_class.global != lc.global then
-                                       dctx.add(" for {lp.local_class.html_link(dctx)}")
-                               end
+       private fun tpl_signature: Template is abstract
+end
 
-                               n = lp.node
-                               if n != null then
-                                       var l = n.location
-                                       dctx.show_source(l)
-                               end
+redef class MMethod
+       redef fun tpl_signature do return intro.msignature.tpl_signature
+end
 
-                               #var doc = lp.doc
-                               #if doc != null and (not introdoc or global.intro.doc != doc) then
-                               #       dctx.add("<pre>{doc.to_html}</pre>")
-                               #end
-                       end
-                       dctx.close_stage
-                       dctx.add("</p>")
-               end
-               dctx.add("</div>")
-               dctx.add("</article>")
+redef class MVirtualTypeProp
+       redef fun tpl_signature do
+               var tpl = new Template
+               tpl.add ": "
+               tpl.add  intro.bound.tpl_link
+               return tpl
        end
 end
-redef class MMMethod
-       redef fun kind do return if global.is_init then "init" else "fun"
-end
-redef class MMAttribute
-       redef fun kind do return "var"
-end
-redef class MMTypeProperty
-       redef fun kind do return "type"
+
+redef class MType
+       # Link to the type definition in the nitdoc page
+       private fun tpl_link: Template is abstract
 end
 
-redef class MMSrcModule
-       redef fun short_doc
-       do
-               var d = doc
-               if d != null then
-                       return d.short
-               else
-                       return "&nbsp;"
-               end
-       end
+redef class MClassType
+       redef fun tpl_link do return mclass.tpl_link
+end
 
-       redef fun doc
-       do
-               var n = node
-               if n.n_moduledecl == null then
-                       return null
-               end
-               var np = n.n_moduledecl
-               var d = np.n_doc
-               if d == null then
-                       return null
-               end
-               if d.n_comment.is_empty then
-                       return null
-               else
-                       return d
-               end
+redef class MNullableType
+       redef fun tpl_link do
+               var tpl = new Template
+               tpl.add "nullable "
+               tpl.add mtype.tpl_link
+               return tpl
        end
 end
 
-redef class ADoc
-       # Html transcription of the doc
-       fun to_html: String
-       do
-               var res = new Buffer
-               for c in n_comment do
-                       res.append(c.text.substring_from(1))
-               end
-               return res.to_s.html_escape
+redef class MGenericType
+       redef fun tpl_link: Template do
+               var tpl = new Template
+               tpl.add mclass.tpl_short_link
+               tpl.add "["
+               for i in [0..arguments.length[ do
+                       tpl.add arguments[i].tpl_link
+                       if i < arguments.length - 1 then tpl.add ", "
+               end
+               tpl.add "]"
+               return tpl
        end
+end
 
-       # Oneliner transcription of the doc
-       fun short: String
-       do
-               return n_comment.first.text.substring_from(1).html_escape
+redef class MParameterType
+       redef fun tpl_link do
+               var name = mclass.intro.parameter_names[rank]
+               var tpl = new TplLink
+               tpl.href = "{mclass.nitdoc_url}#FT_{name}"
+               tpl.text = name
+               tpl.title = "formal type"
+               return tpl
        end
 end
 
-redef class MMLocalClass
-       super MMEntity
-
-       # Anchor of the class description in the module html file
-       fun html_anchor: String do return "CLASS_{self}"
-
-       fun html_name: String do return "{self}"
+redef class MVirtualType
+       redef fun tpl_link do return mproperty.intro.tpl_link
+end
 
-       redef fun html_link(dctx)
-       do
-               if not require_doc(dctx) then print "{dctx.filename}: not required {self}"
-               return "<a href=\"{html_name}.html\" title=\"{short_doc}\">{self}</a>"
+redef class MClassDef
+       # Return the classdef namespace decorated with html
+       private fun tpl_namespace: Template do
+               var tpl = new Template
+               tpl.add mmodule.tpl_namespace
+               tpl.add "::<span>"
+               tpl.add mclass.tpl_link
+               tpl.add "</span>"
+               return tpl
        end
 
-       redef fun short_doc do return global.intro.short_doc
-
-       redef fun doc do return global.intro.doc
-
-       fun kind: String
-       do
-               if global.is_interface then
-                       return "interface"
-               else if global.is_abstract then
-                       return "abstract class"
-               else if global.is_enum then
-                       return "enum"
-               else
-                       return "class"
-               end
+       private fun full_namespace: String do
+               return "{mmodule.full_namespace}::{mclass.nitdoc_name}"
        end
 
-       # The most specific module in the nesting hierarchy that exports the intro of self
-       fun intro_module: MMModule
-       do
-               var m = global.intro.mmmodule
-               var mo = m.direct_owner
-               while mo != null and mo.visibility_for(m) >= 2 do 
-                       m = mo
-                       mo = m.direct_owner
-               end
-               return m
+       private fun tpl_link_anchor: TplLink do return mclass.tpl_link_anchor
+
+       private fun tpl_article: TplArticle do
+               var tpl = new TplArticle
+               tpl.id = mclass.nitdoc_anchor
+               tpl.classes.add_all(tpl_css_classes)
+               tpl.title = new Template
+               tpl.title.add mclass.tpl_short_link
+               tpl.title.add mclass.tpl_signature
+               tpl.subtitle = new Template
+               tpl.subtitle.add tpl_modifiers
+               tpl.subtitle.add tpl_namespace
+               tpl.content = new Template
+               tpl.content.add tpl_definition
+               return tpl
        end
 
-       fun menu_link(dctx: DocContext, p: MMLocalProperty)
-       do
-               if p.local_class.global != self.global then
-                       if p.global.intro.local_class.name == "Object".to_symbol then return
-                       if p.global.is_init or p isa MMTypeProperty then
-                               dctx.add("<li class='inherit'><span title='Inherited'>H</span>&nbsp;{p.html_link_special(dctx, self)}</li>\n")
-                       else
-                               dctx.add("<li class='inherit'><span title='Inherited'>H</span>&nbsp;{p.html_link(dctx)}</li>\n")
-                       end
-               else if p.global.intro.local_class.global == self.global then
-                       dctx.add("<li class='intro'><span title='Introduced'>I</span>&nbsp;{p.html_link_special(dctx, self)}</li>\n")
-               else
-                       dctx.add("<li class='redef'><span title='Redefined'>R</span>&nbsp;{p.html_link_special(dctx, self)}</li>\n")
-               end
+       private fun tpl_css_classes: Set[String] do
+               var set = new HashSet[String]
+               set.add_all mclass.intro.modifiers
+               set.add_all modifiers
+               return set
        end
 
-       # Return true if the global class must be documented according to the visibility configured
-       fun require_doc(dctx: DocContext): Bool
-       do
-               if global.visibility_level == 3 and not dctx.with_private then return false # Private
-               if dctx.public_only then
-                       var m = intro_module
-                       if m != m.toplevel_owner then return false # Unexported
+       private fun tpl_modifiers: Template do
+               var tpl = new Template
+               for modifier in modifiers do
+                       if modifier == "public" then continue
+                       tpl.add "{modifier} "
                end
-               return true
+               return tpl
        end
 
-       # Fill the body for the page associated to the global class
-       fun file_page_doc(dctx: DocContext)
-       do
-               dctx.add("<div class=\"menu\">\n")
-
-               var props = new Array[MMLocalProperty]
-               var inh = new HashMap[MMLocalClass, Array[MMLocalProperty]]
-               var inhs = new Array[MMLocalClass]
-               for g in global_properties do
-                       var p = self[g]
-                       if not p.require_doc(dctx) then continue
-                       if p.local_class.global == global or g.is_init_for(self) or p isa MMTypeProperty then
-                               props.add(p)
-                       else
-                               var lc = mmmodule[p.local_class.global]
-                               if inh.has_key(lc) then
-                                       inh[lc].add(p)
-                               else
-                                       inh[lc] = [p]
-                                       inhs.add(lc)
-                               end
-                               props.add(p)
-                       end
+       private fun tpl_short_definition: TplDefinition do
+               var tpl = new TplDefinition
+               tpl.namespace = mmodule.tpl_full_namespace
+               if mdoc != null then
+                       tpl.comment = mdoc.tpl_short_comment
                end
-               dctx.sort(props)
+               return tpl
+       end
 
-               dctx.add("<nav class=\"properties filterable\">\n")
-               dctx.add("<h3>Properties</h3>\n")
-               dctx.open_stage
-               dctx.stage("<h4>Virtual Types</h4>\n<ul>\n")
-               for p in props do
-                       if p isa MMTypeProperty then
-                               menu_link(dctx, p)
-                       end
-               end
-               dctx.stage("</ul>\n")
-               dctx.close_stage
-               dctx.open_stage
-               dctx.stage("<h4>Constructors</h4>\n<ul>\n")
-               for p in props do
-                       if p.global.is_init_for(self) then
-                               menu_link(dctx, p)
-                       end
-               end
-               dctx.stage("</ul>\n")
-               dctx.close_stage
-               dctx.open_stage
-               dctx.stage("<h4>Methods</h4>\n<ul>\n")
-               for p in props do
-                       if not p.global.is_init and p isa MMMethod then
-                               menu_link(dctx, p)
-                       end
-               end
-               dctx.stage("</ul>\n")
-               dctx.close_stage
-               dctx.add("</nav>\n")
-
-               dctx.add("<nav class=\"inheritance filterable\">\n")
-               dctx.add("<h3>Inheritance</h3>\n")
-               dctx.add("<h4>Superclasses</h4>\n<ul>\n")
-               for lc in cshe.linear_extension do
-                       if lc == self then continue
-                       if not lc.require_doc(dctx) then continue
-                       dctx.add("<li>{lc.html_link(dctx)}</li>\n")
-               end
-               dctx.add("</ul>\n")
-               if cshe.smallers.length == 0 then 
-                       dctx.add("<h4>No Known Subclasses</h4>\n")
-               else if cshe.smallers.length <= 100 then 
-                       dctx.add("<h4>Subclasses</h4>\n")
-                       dctx.add("<ul>\n")
-                       for lc in cshe.smallers do
-                               if not lc.require_doc(dctx) then continue
-                               dctx.add("<li>{lc.html_link(dctx)}</li>\n")
-                       end
-                       dctx.add("</ul>\n")
-               else if cshe.direct_smallers.length <= 100 then 
-                       dctx.add("<h4>Direct Subclasses Only</h4>\n<ul>\n")
-                       for lc in cshe.direct_smallers do
-                               if not lc.require_doc(dctx) then continue
-                               dctx.add("<li>{lc.html_link(dctx)}</li>\n")
-                       end
-                       dctx.add("</ul>\n")
-               else
-                       dctx.add("<h4>Too much Subclasses to list</h4>\n")
+       private fun tpl_definition: TplDefinition do
+               var tpl = new TplDefinition
+               tpl.namespace = mmodule.tpl_full_namespace
+               if mdoc != null then
+                       tpl.comment = mdoc.tpl_comment
                end
-               dctx.add("</nav>\n")
+               return tpl
+       end
+end
 
-               dctx.add("</div>\n")
+redef class MPropDef
+       # Return the full qualified name of the mpropdef
+       #       module::classdef::name
+       private fun tpl_namespace: Template do
+               var tpl = new Template
+               tpl.add mclassdef.tpl_namespace
+               tpl.add "::"
+               tpl.add mproperty.name
+               return tpl
+       end
 
+       private fun full_namespace: String do
+               return "{mclassdef.full_namespace}::{mproperty.nitdoc_name}"
+       end
 
-               dctx.add("<div class=\"content\">\n")
-               dctx.add("<h1>{name}</h1>\n")
-               dctx.add("<div class='subtitle'>")
-               if global.visibility_level == 2 then
-                       abort
-               else if global.visibility_level == 3 then
-                       dctx.add("private ")
-               else if self.global.intro.mmmodule.toplevel_owner.visibility_for(self.global.intro.mmmodule) <= 1 then
-                       dctx.add("(unexported) ")
-               end
-               dctx.add("{kind} {global.intro.mmmodule.toplevel_owner.html_link(dctx)}::{name}</div>")
+       # URL into the nitdoc page
+       #       class_owner_name.html#nitdoc_anchor
+       private fun nitdoc_url: String do
+               return "{mclassdef.mclass.nitdoc_url}#{nitdoc_anchor}"
+       end
 
-               dctx.add("<section class=\"description\">\n")
-               var doc = doc
-               if doc != null then
-                       dctx.add("<pre>{doc.to_html}</pre>\n")
-               end
+       # html nitdoc_anchor id for the property in a nitdoc class page
+       #       PROP_mclass_propertyname
+       private fun nitdoc_anchor: String do
+               return "PROP_{mclassdef.mclass.public_owner.nitdoc_name}_{mproperty.name.replace(" ", "_")}"
+       end
 
-               var cla = new HashSet[MMLocalClass]
-               var sm = new HashSet[MMLocalClass]
-               var sm2 = new HashSet[MMLocalClass]
-               sm.add(self)
-               while cla.length + sm.length < 10 and sm.length > 0 do
-                       cla.add_all(sm)
-                       sm2.clear
-                       for x in sm do
-                               sm2.add_all(x.cshe.direct_smallers)
-                       end
-                       var t = sm
-                       sm = sm2
-                       sm2 = t
+       # Return a link to property into the nitdoc class page
+       #       <a href="nitdoc_url" title="short_comment">nitdoc_name</a>
+       private fun tpl_link: TplLink do
+               var tpl = new TplLink
+               tpl.href = nitdoc_url
+               tpl.text = mproperty.nitdoc_name
+               if mproperty.intro.mdoc != null then
+                       tpl.title = mproperty.intro.mdoc.short_comment
                end
-               cla.add_all(cshe.greaters_and_self)
+               return tpl
+       end
 
-               var op = new Buffer
-               op.append("digraph {name} \{ rankdir=BT; node[shape=none,margin=0,width=0,height=0,fontsize=10]; edge[dir=none,color=gray]; ranksep=0.2; nodesep=0.1;\n")
-               for c in cla do
-                       if c == self then
-                               op.append("\"{c.name}\"[shape=box,margin=0.03];\n")
-                       else
-                               op.append("\"{c.name}\"[URL=\"{c.html_name}.html\"];\n")
-                       end
-                       for c2 in c.cshe.direct_greaters do
-                               if not cla.has(c2) then continue
-                               op.append("\"{c.name}\"->\"{c2.name}\";\n")
-                       end
-                       if not c.cshe.direct_smallers.is_empty then
-                               var others = true
-                               for c2 in c.cshe.direct_smallers do
-                                       if cla.has(c2) then others = false
-                               end
-                               if others then
-                                       op.append("\"{c.name}...\"[label=\"\"];\n")
-                                       op.append("\"{c.name}...\"->\"{c.name}\"[style=dotted];\n")
-                               end
-                       end
-               end
-               op.append("\}\n")
-               dctx.gen_dot(op.to_s, name.to_s, "Inheritance graph for class {name}")
+       private fun tpl_article: TplArticle do
+               var tpl = new TplArticle
+               tpl.id = nitdoc_anchor
+               tpl.classes.add_all(tpl_css_classes)
+               tpl.title = new Template
+               tpl.title.add mproperty.nitdoc_name
+               tpl.title.add mproperty.tpl_signature
+               tpl.subtitle = new Template
+               tpl.subtitle.add tpl_modifiers
+               tpl.subtitle.add tpl_namespace
+               tpl.content = new Template
+               tpl.content.add tpl_definition
+               return tpl
+       end
 
+       private fun tpl_css_classes: Set[String] do
+               var set = new HashSet[String]
+               set.add_all mproperty.intro.modifiers
+               set.add_all modifiers
+               return set
+       end
 
-               var mods = new Array[MMModule]
-               mods.add(global.intro.mmmodule.toplevel_owner)
-               for lc in crhe.greaters do
-                       if not lc isa MMSrcLocalClass then continue
-                       var m = lc.mmmodule.toplevel_owner
-                       if not mods.has(m) then mods.add(m)
-               end
-               dctx.sort(mods)
-               for m in mods do
-                       if m == global.intro.mmmodule.toplevel_owner then
-                               dctx.add("<p>Introduced by {m.html_link(dctx)}")
-                       else
-                               dctx.add("<p>Refined by {m.html_link(dctx)}")
-                       end
-                       dctx.open_stage
-                       dctx.stage(". Definition in:")
-                       for lc in crhe.greaters do
-                               if lc.mmmodule.toplevel_owner != m then continue
-                               dctx.add(" {lc.mmmodule.html_link(dctx)}")
-                               assert lc isa MMSrcLocalClass
-                               var n = lc.node
-                               if n != null then
-                                       dctx.show_source(n.location)
-                               end
-                       end
-                       dctx.close_stage
-                       dctx.add("</p>\n")
-               end
-               dctx.add("</section>\n")
-
-               dctx.open_stage
-               dctx.stage("<section class=\"types\">\n")
-               dctx.stage("<h2>Formal and Virtual Types</h2>\n")
-               for i in [0..arity[ do
-                       var f = get_formal(i)
-                       f.full_documentation(dctx, self)
-               end
-               for p in props do
-                       if not p isa MMTypeProperty then continue
-                       p.full_documentation(dctx, self)
-               end
-               dctx.stage("</section>\n")
-               dctx.close_stage
-
-               dctx.open_stage
-               dctx.stage("<section class=\"constructors\">\n")
-               dctx.stage("<h2 class=\"section-header\">Constructors</h2>\n")
-               for p in props do
-                       if not p.global.is_init_for(self) then continue
-                       p.full_documentation(dctx, self)
-               end
-               dctx.stage("</section>\n")
-               dctx.close_stage
-
-               dctx.open_stage
-               dctx.stage("<section class=\"methods\">\n")
-               dctx.stage("<h2 class=\"section-header\">Methods</h2>\n")
-               for p in props do
-                       if p.global.is_init then continue
-                       if p.local_class.global != self.global then continue
-                       if not p isa MMMethod then continue
-                       p.full_documentation(dctx, self)
-               end
-               if not inhs.is_empty then
-                       dctx.open_stage
-                       dctx.stage("<h3>Inherited Methods</h3>\n")
-                       for lc in inhs do
-                               dctx.open_stage
-                               dctx.stage("<p>Defined in {lc.html_link(dctx)}:")
-                               for p in inh[lc] do
-                                       if p.global.is_init then continue
-                                       if not p isa MMMethod then continue
-                                       dctx.add(" {p.html_link(dctx)}")
-                               end
-                               dctx.stage("</p>")
-                               dctx.close_stage
-                       end
-                       dctx.close_stage
+       private fun tpl_modifiers: Template do
+               var tpl = new Template
+               for modifier in modifiers do
+                       if modifier == "public" then continue
+                       tpl.add "{modifier} "
                end
-               dctx.add("</section>\n")
-               dctx.close_stage
-               dctx.add("</div> <!-- end class {name} -->\n")
+               return tpl
        end
-end
 
-redef class MMSrcLocalClass
-       redef fun short_doc
-       do
-               var d = doc
-               if d != null then
-                       return d.short
-               else if global.intro == self then
-                       return "&nbsp;"
-               else
-                       var bc = global.intro
-                       return bc.short_doc
+       private fun tpl_short_definition: TplDefinition do
+               var tpl = new TplDefinition
+               tpl.namespace = mclassdef.tpl_namespace
+               if mdoc != null then
+                       tpl.comment = mdoc.tpl_short_comment
                end
+               return tpl
        end
 
-       redef fun doc
-       do
-               var n = node
-               if not n isa AStdClassdef then
-                       return null
-               end
-               var d = n.n_doc
-               if d == null then
-                       return null
-               end
-               if d.n_comment.is_empty then
-                       return null
-               else
-                       return d
+       private fun tpl_definition: TplDefinition do
+               var tpl = new TplDefinition
+               tpl.namespace = mclassdef.tpl_namespace
+               if mdoc != null then
+                       tpl.comment = mdoc.tpl_comment
                end
+               return tpl
        end
 end
 
-redef class MMSignature
-       # Htlm transcription of the signature (with nested links)
-       fun to_html(dctx: DocContext, with_closure: Bool): String
-       do
-               var res = new Buffer
-               if arity > 0 then
-                       res.append("(")
-                       res.append(self.params[0].name.to_s)
-                       res.append(": ")
-                       res.append(self[0].html_link(dctx))
-                       for i in [1..arity[ do
-                               res.append(", ")
-                               res.append(self.params[i].name.to_s)
-                               res.append(": ")
-                               res.append(self[i].html_link(dctx))
-                       end
-                       res.append(")")
-               end
-               if return_type != null then
-                       res.append(": ")
-                       res.append(return_type.html_link(dctx))
-               end
-               if with_closure then
-                       for c in closures do
-                               res.append(" ")
-                               if c.is_optional then res.append("[")
-                               if c.is_break then res.append("break ")
-                               res.append("!{c.name}")
-                               res.append(c.signature.to_html(dctx, false))
-                               if c.is_optional then res.append("]")
+redef class MSignature
+       private fun tpl_signature: Template do
+               var tpl = new Template
+               if not mparameters.is_empty then
+                       tpl.add "("
+                       for i in [0..mparameters.length[ do
+                               tpl.add mparameters[i].tpl_link
+                               if i < mparameters.length - 1 then tpl.add ", "
                        end
+                       tpl.add ")"
                end
-               return res.to_s
+               if return_mtype != null then
+                       tpl.add ": "
+                       tpl.add return_mtype.tpl_link
+               end
+               return tpl
        end
 end
 
-redef class MMType
-       # Htlm transcription of the type (with nested links)
-       fun html_link(dctx: DocContext): String do return to_s
-end
-
-redef class MMTypeSimpleClass
-       redef fun html_link(dctx) do return local_class.html_link(dctx)
+redef class MParameter
+       private fun tpl_link: Template do
+               var tpl = new Template
+               tpl.add "{name}: "
+               tpl.add mtype.tpl_link
+               if is_vararg then tpl.add "..."
+               return tpl
+       end
 end
 
-redef class MMTypeGeneric
-       redef fun html_link(dctx)
-       do
-               var res = new Buffer
-               res.append(local_class.html_link(dctx))
-               res.append("[")
-               res.append(params[0].html_link(dctx))
-               for i in [1..params.length[ do
-                       res.append(", ")
-                       res.append(params[i].html_link(dctx))
-               end
-               res.append("]")
-               return res.to_s
+redef class Location
+       fun github(gitdir: String): String do
+               var base_dir = getcwd.join_path(gitdir).simplify_path
+               var file_loc = getcwd.join_path(file.filename).simplify_path
+               var gith_loc = file_loc.substring(base_dir.length + 1, file_loc.length)
+               return "{gith_loc}:{line_start},{column_start}--{line_end},{column_end}"
        end
 end
 
-redef class MMTypeFormalParameter
-       fun html_anchor: String
-       do
-               return "FT_{local_class}_{cmangle(name)}"
+redef class MDoc
+       private fun short_comment: String do
+               return content.first.html_escape
        end
-       redef fun html_link(dctx)
-       do
-               return "<a href=\"#{html_anchor}\">{name}</a>"
+
+       private fun full_comment: String do
+               return content.join("\n").html_escape
        end
-       fun full_documentation(dctx: DocContext, lc: MMLocalClass)
-       do
-               dctx.add("<article id=\"{html_anchor}\">\n")
-               dctx.add("<h3 class=\"signature\">{name}: {bound.html_link(dctx)}</h3>\n")
-               dctx.add("<div class=\"info\">")
-               dctx.add("formal generic type")
-               dctx.add("</div>")
-               dctx.add("</article>")
+
+       private fun tpl_short_comment: TplShortComment do
+               return new TplShortComment(short_markdown)
        end
-end
 
-redef class MMNullableType
-       redef fun html_link(dctx) do return "nullable " + as_notnull.html_link(dctx)
+       private fun tpl_comment: TplComment do
+               return new TplComment(full_markdown)
+       end
 end
 
-redef class MMVirtualType
-       redef fun html_link(dctx) do return property.html_link(dctx)
-end
+var nitdoc = new NitdocContext
+nitdoc.generate_nitdoc
 
-var c = new DocContext
-c.exec_cmd_line