nitdoc: introduce `depth` service for DocComposite
[nit.git] / src / doc / html_templates / html_templates.nit
index 1aa3c02..7ffcdb0 100644 (file)
@@ -21,6 +21,7 @@ import doc_phases::doc_structure
 import doc_phases::doc_hierarchies
 import doc_phases::doc_graphs
 import doc_phases::doc_intros_redefs
+import doc_phases::doc_lin
 
 # Renders the page as HTML.
 redef class DocPage
@@ -41,22 +42,12 @@ redef class DocPage
        # Sidebar template if any.
        var sidebar: nullable DocSideBar = null is writable
 
-       # Content of the page in form a TplSection.
-       # TODO remove when other templates are migrated.
-       var sections = new Array[TplSection]
-
        # Footer content if any.
        var footer: nullable Writable = null is writable
 
        # JS scripts to append at the end of the body
        var scripts = new Array[TplScript]
 
-       # Adds a section to this page.
-       # TODO remove when other templates are migrated.
-       fun add_section(section: TplSection) do
-               sections.add section
-       end
-
        # Renders the html `<head>`.
        private fun render_head do
                var css = (self.shareurl / "css").html_escape
@@ -81,7 +72,7 @@ redef class DocPage
 
        # Renders the footer and content.
        private fun render_content do
-               for section in sections do add section
+               add root
                if footer != null then
                        addn "<div class='well footer'>"
                        add footer.as(not null)
@@ -256,7 +247,7 @@ redef class DocComposite
        super Template
 
        # HTML anchor id
-       var html_id: String is noinit
+       var html_id: String is noinit, writable
 
        # Title to display if any.
        #
@@ -269,7 +260,10 @@ redef class DocComposite
        # Render the element title and subtitle.
        private fun render_title do
                if html_title != null then
-                       addn new Header(hlvl, html_title.write_to_string)
+               var header = new Header(hlvl, html_title.write_to_string)
+               header.css_classes.add "signature"
+               if hlvl == 2 then header.css_classes.add "well well-sm"
+               addn header
                end
                if html_subtitle != null then
                        addn "<div class='info subtitle'>"
@@ -279,7 +273,9 @@ redef class DocComposite
        end
 
        # Render the element body.
-       private fun render_body do end
+       private fun render_body do
+               for child in children do addn child.write_to_string
+       end
 
        redef fun rendering do
                if is_hidden then return
@@ -288,10 +284,7 @@ redef class DocComposite
        end
 
        # Level <hX> for HTML heading.
-       private fun hlvl: Int do
-               if parent == null then return 1
-               return parent.hlvl + 1
-       end
+       private fun hlvl: Int do return depth
 
        # Is `self` not displayed in the page.
        #
@@ -333,18 +326,84 @@ redef class DocSection
                        addn "<a id=\"{html_id}\"></a>"
                        return
                end
+               addn "<section{render_css_classes} id=\"{html_id}\">"
+               render_title
                render_body
+               addn "</section>"
        end
 end
 
 redef class DocArticle
        super BSComponent
 
-       # Never displays the title for article.
+       redef fun rendering do
+               if is_hidden then return
+               addn "<article{render_css_classes} id=\"{html_id}\">"
+               render_title
+               render_body
+               addn "</article>"
+       end
+end
+
+redef class HomeArticle
+       redef var html_id = "article:home"
+       redef var html_title = "Overview"
+
+       # HTML content to display on the home page.
        #
-       # This is to maintain compatibility with old components, this may change
-       # without notice in further version.
-       redef fun render_title do end
+       # This attribute is set by the `doc_render` phase who knows the context.
+       var content: nullable String is noinit, writable
+
+       redef fun render_body do
+               var content = self.content
+               if content != null then add content
+               super
+       end
+end
+
+redef class IndexArticle
+       redef var html_id = "article:index"
+       redef var html_title = "Index"
+       redef fun is_empty do
+               return mmodules.is_empty and mclasses.is_empty and mprops.is_empty
+       end
+
+       redef fun render_body do
+               addn "<div class='container-fluid'>"
+               addn " <div class='row'>"
+               render_list("Modules", mmodules)
+               render_list("Classes", mclasses)
+               render_list("Properties", mprops)
+               addn "</div>"
+               addn "</div>"
+       end
+
+       # Displays a list from the content of `mentities`.
+       private fun render_list(title: String, mentities: Array[MEntity]) do
+               if mentities.is_empty then return
+               addn "<div class='col-xs-4'>"
+               addn new Header(3, title)
+               var lst = new UnorderedList
+               for mentity in mentities do
+                       if mentity isa MProperty then
+                               var tpl = new Template
+                               tpl.add mentity.intro.html_link
+                               tpl.add " ("
+                               tpl.add mentity.intro.mclassdef.mclass.html_link
+                               tpl.add ")"
+                               lst.add_li new ListItem(tpl)
+                       else
+                               lst.add_li new ListItem(mentity.html_link)
+                       end
+               end
+               addn lst
+               addn "</div>"
+       end
+end
+
+redef class ProjectsSection
+       redef var html_id = "section:projects"
+       redef var html_title = "Projects"
 end
 
 redef class MEntityComposite
@@ -352,28 +411,47 @@ redef class MEntityComposite
        redef var html_title is lazy do return mentity.nitdoc_name
 end
 
+redef class MEntitySection
+       redef var html_id is lazy do return "section:{mentity.nitdoc_name}"
+       redef var html_title is lazy do return mentity.html_name
+       redef var html_subtitle is lazy do return mentity.html_declaration
+end
+
 redef class ConcernSection
-       redef var html_id is lazy do return "section_concerns_{mentity.nitdoc_id}"
+       redef var html_id is lazy do return "concern:{mentity.nitdoc_id}"
        redef var html_title is lazy do return "in {mentity.nitdoc_name}"
        redef fun is_toc_hidden do return is_empty
 end
 
 redef class ImportationListSection
-       redef var html_id is lazy do return "section_dependancies_{mentity.nitdoc_id}"
+       redef var html_id is lazy do return "section:{mentity.nitdoc_id}.importation"
        redef var html_title is lazy do return "Dependencies"
 end
 
 redef class InheritanceListSection
-       redef var html_id is lazy do return "section_inheritance_{mentity.nitdoc_id}"
+       redef var html_id is lazy do return "section:{mentity.nitdoc_id}.inheritance"
        redef var html_title is lazy do return "Inheritance"
 end
 
 redef class IntroArticle
-       redef var html_id is lazy do return "article_intro_{mentity.nitdoc_id}"
-       redef var html_title is lazy do return null
+       redef var html_id is lazy do return "article:{mentity.nitdoc_id}.intro"
+       redef var html_title = null
        redef var is_hidden = false
        redef var is_toc_hidden = true
 
+       # Link to source to display if any.
+       var html_source_link: nullable Writable is noinit, writable
+
+       redef fun render_title do
+               var lnk = html_source_link
+               if lnk != null then
+                       add "<div class='source-link'>"
+                       add lnk
+                       addn "</div>"
+               end
+               super
+       end
+
        redef fun render_body do
                var comment = mentity.html_comment
                if comment != null then addn comment
@@ -382,27 +460,57 @@ redef class IntroArticle
 end
 
 redef class ConcernsArticle
-       redef var html_id is lazy do return "article_concerns_{mentity.nitdoc_id}"
+       redef var html_id is lazy do return "article:{mentity.nitdoc_id}.concerns"
        redef var html_title = "Concerns"
        redef fun is_hidden do return concerns.is_empty
        redef fun render_body do add concerns.html_list
 end
 
 redef class DefinitionArticle
-       redef var html_id is lazy do return "article_definition_{mentity.nitdoc_id}"
+       redef var html_id is lazy do return "article:{mentity.nitdoc_id}.definition"
        redef var html_title is lazy do return mentity.html_name
        redef var html_subtitle is lazy do return mentity.html_declaration
        redef var is_hidden = false
 
+       # Does `self` display only it's title and no body?
+       #
+       # FIXME diff hack
+       var is_no_body: Bool = false is writable
+
+       # Does `self` display only the short content as definition?
+       #
+       # FIXME diff hack
+       var is_short_comment: Bool = false is writable
+
+       # Link to source to display if any.
+       var html_source_link: nullable Writable is noinit, writable
+
+       redef fun render_title do
+               var lnk = html_source_link
+               if lnk != null then
+                       add "<div class='source-link'>"
+                       add lnk
+                       addn "</div>"
+               end
+               super
+       end
+
        redef fun render_body do
-               var comment = mentity.html_comment
-               if comment != null then addn comment
+               if not is_no_body then
+                       var comment
+                       if is_short_comment then
+                               comment = mentity.html_short_comment
+                       else
+                               comment = mentity.html_comment
+                       end
+                       if comment != null then addn comment
+               end
                super
        end
 end
 
 redef class HierarchyListArticle
-       redef var html_id is lazy do return "article_hierarchy_{list_title}_{mentity.nitdoc_id}"
+       redef var html_id is lazy do return "article:{list_title}_{mentity.nitdoc_id}.hierarchy"
        redef var html_title is lazy do return list_title
        redef fun is_empty do return mentities.is_empty
        redef fun is_toc_hidden do return mentities.is_empty
@@ -418,7 +526,7 @@ redef class HierarchyListArticle
 end
 
 redef class IntrosRedefsListArticle
-       redef var html_id is lazy do return "article_intros_redefs_{mentity.nitdoc_id}"
+       redef var html_id is lazy do return "article:{mentity.nitdoc_id}.intros_redefs"
        redef var html_title is lazy do return list_title
        redef fun is_hidden do return mentities.is_empty
        redef var is_toc_hidden = true
@@ -433,8 +541,34 @@ redef class IntrosRedefsListArticle
        end
 end
 
+redef class DefinitionLinArticle
+       redef var html_id is lazy do return "article:{mentity.nitdoc_id}.lin"
+       redef var html_title is lazy do return "Linearization"
+       redef fun is_hidden do return mentities.is_empty
+       redef var is_toc_hidden = true
+
+       redef fun render_body do
+               var lst = new UnorderedList
+               lst.css_classes.add "list-unstyled list-labeled"
+               for mentity in mentities do
+                       if not mentity isa MPropDef then continue # TODO handle all mentities
+                       var tpl = new Template
+                       tpl.add mentity.mclassdef.html_namespace
+                       var comment = mentity.mclassdef.html_short_comment
+                       if comment != null then
+                               tpl.add ": "
+                               tpl.add comment
+                       end
+                       var li = new ListItem(tpl)
+                       li.css_classes.add "signature"
+                       lst.add_li li
+               end
+               add lst
+       end
+end
+
 redef class GraphArticle
-       redef var html_id is lazy do return "article_graph_{mentity.nitdoc_id}"
+       redef var html_id is lazy do return "article:{mentity.nitdoc_id}.graph"
        redef var html_title = null
        redef var toc_title do return "Graph"
        redef var is_hidden = false