lib/markdown: merge processor and emitter
[nit.git] / src / doc / html_templates / html_templates.nit
index b79c87b..a7a5b9a 100644 (file)
@@ -22,6 +22,8 @@ import doc_phases::doc_hierarchies
 import doc_phases::doc_graphs
 import doc_phases::doc_intros_redefs
 import doc_phases::doc_lin
+import doc_phases::doc_readme
+intrude import doc_down
 
 # Renders the page as HTML.
 redef class DocPage
@@ -88,7 +90,10 @@ redef class DocPage
                addn "<script src='{vendors}/jquery/jquery-1.11.1.min.js'></script>"
                addn "<script src='{vendors}/jquery/jquery-ui-1.10.4.custom.min.js'></script>"
                addn "<script src='{vendors}/bootstrap/js/bootstrap.min.js'></script>"
-               addn "<script data-main='{js}/nitdoc' src='{js}/lib/require.js'></script>"
+               addn "<script src='{js}/lib/utils.js'></script>"
+               addn "<script src='{js}/plugins/filtering.js'></script>"
+               addn "<script src='quicksearch-list.js'></script>"
+               addn "<script src='{js}/plugins/quicksearch.js'></script>"
                for script in scripts do add script
                addn """<script>
                        $(function () {
@@ -247,12 +252,12 @@ redef class DocComposite
        super Template
 
        # HTML anchor id
-       var html_id: String is noinit, writable
+       var html_id: String is writable, lazy do return id
 
        # Title to display if any.
        #
        # This title can be decorated with HTML.
-       var html_title: nullable Writable is noinit, writable
+       var html_title: nullable Writable is writable, lazy do return title
 
        # Subtitle to display if any.
        var html_subtitle: nullable Writable is noinit, writable
@@ -262,7 +267,6 @@ redef class DocComposite
                if html_title != null then
                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
@@ -284,31 +288,22 @@ redef class DocComposite
        end
 
        # Level <hX> for HTML heading.
-       private fun hlvl: Int do
-               if parent == null then return 0
-               return parent.hlvl + 1
-       end
-
-       # Is `self` not displayed in the page.
-       #
-       # By default, empty elements are hidden.
-       fun is_hidden: Bool do return is_empty
+       private fun hlvl: Int do return depth
 
        # A short, undecorated title that goes in the table of contents.
        #
        # By default, returns `html_title.to_s`, subclasses should redefine it.
-       var toc_title: String is lazy, writable do return html_title.to_s
-
-       # Is `self` hidden in the table of content?
-       var is_toc_hidden = false is writable
+       var html_toc_title: nullable String is lazy, writable do
+               if html_title == null then return toc_title
+               return html_title.write_to_string
+       end
 
        # Render this element in a table of contents.
        private fun render_toc_item(lst: UnorderedList) do
-               if is_toc_hidden then return
+               if is_toc_hidden or html_toc_title == null then return
 
                var content = new Template
-               content.add new Link("#{html_id}", toc_title)
-
+               content.add new Link("#{html_id}", html_toc_title.to_s)
                if not children.is_empty then
                        var sublst = new UnorderedList
                        sublst.css_classes.add "nav"
@@ -330,6 +325,12 @@ redef class DocComposite
        end
 end
 
+redef class DocRoot
+       redef fun rendering do
+               for child in children do addn child.write_to_string
+       end
+end
+
 redef class DocSection
        super BSComponent
 
@@ -362,21 +363,20 @@ redef class TabbedGroup
                var tabs = new DocTabs("{html_id}.tabs", "")
                for child in children do
                        if child.is_hidden then continue
-                       tabs.add_panel new DocTabPanel(child.html_tab_id, child.toc_title, child)
+                       var title = child.html_toc_title or else child.toc_title or else ""
+                       tabs.add_panel new DocTabPanel(child.html_tab_id, title, child)
                end
                addn tabs
        end
 end
 
 redef class PanelGroup
-       redef var html_id is lazy do return "group:{group_title.to_lower.to_snake_case}"
        redef var html_title = null
-       redef var toc_title is lazy do return group_title
+       redef var toc_title is lazy do return title or else ""
        redef var is_toc_hidden = true
 end
 
 redef class HomeArticle
-       redef var html_id = "article:home"
        redef var html_title = "Overview"
 
        # HTML content to display on the home page.
@@ -392,11 +392,7 @@ redef class HomeArticle
 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'>"
@@ -431,74 +427,53 @@ redef class IndexArticle
        end
 end
 
-redef class ProjectsSection
-       redef var html_id = "section:projects"
-       redef var html_title = "Projects"
-end
-
 redef class MEntityComposite
-       redef var html_id is lazy do return mentity.nitdoc_id
        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 "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:{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:{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:{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
+       redef fun render_body do
+               var tabs = new DocTabs("{html_id}.tabs", "")
+               var comment = mentity.html_documentation
+               if mentity isa MPackage then
+                       comment = mentity.html_synopsis
+               end
+               if comment != null then
+                       tabs.add_panel new DocTabPanel("{html_tab_id}-comment", "Comment", comment)
+               end
+               for child in children do
+                       if child.is_hidden then continue
+                       var title = child.html_toc_title or else child.toc_title or else ""
+                       tabs.add_panel new DocTabPanel(child.html_tab_id, title, child)
+               end
                var lnk = html_source_link
                if lnk != null then
-                       add "<div class='source-link'>"
-                       add lnk
-                       addn "</div>"
+                       tabs.drop_list.items.add new ListItem(lnk)
                end
-               super
-       end
-
-       redef fun render_body do
-               var comment = mentity.html_comment
-               if comment != null then addn comment
-               super
+               addn tabs
        end
 end
 
 redef class ConcernsArticle
-       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 DefinitionListArticle
-       redef var html_id is lazy do return "article:{mentity.nitdoc_id}.definition-list"
-
        redef var html_title is lazy do
                var title = new Template
                title.add mentity.html_icon
@@ -507,14 +482,12 @@ redef class DefinitionListArticle
        end
 
        redef var html_subtitle is lazy do return mentity.html_namespace
-       redef var toc_title is lazy do return mentity.html_name
+       redef var html_toc_title is lazy do return mentity.html_name
 end
 
 redef class DefinitionArticle
-       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?
        #
@@ -529,24 +502,14 @@ redef class DefinitionArticle
        # 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 tabs = new DocTabs("{html_id}.tabs", "")
                if not is_no_body then
                        var comment
-                       if is_short_comment then
-                               comment = mentity.html_short_comment
+                       if is_short_comment or mentity isa MPackage then
+                               comment = mentity.html_synopsis
                        else
-                               comment = mentity.html_comment
+                               comment = mentity.html_documentation
                        end
                        if comment != null then
                                tabs.add_panel new DocTabPanel("{html_tab_id}-comment", "Comment", comment)
@@ -554,58 +517,29 @@ redef class DefinitionArticle
                end
                for child in children do
                        if child.is_hidden then continue
-                       tabs.add_panel new DocTabPanel(child.html_tab_id, child.toc_title, child)
+                       var title = child.html_toc_title or else child.toc_title or else ""
+                       tabs.add_panel new DocTabPanel(child.html_tab_id, title, child)
+               end
+               var lnk = html_source_link
+               if lnk != null then
+                       tabs.drop_list.items.add new ListItem(lnk)
                end
                addn tabs
        end
 end
 
-redef class HierarchyListArticle
-       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 var is_toc_hidden = true
-
+redef class MEntitiesListArticle
        redef fun render_body do
                var lst = new UnorderedList
                lst.css_classes.add "list-unstyled list-definition"
                for mentity in mentities do
                        lst.add_li mentity.html_list_item
                end
-               addn lst
-       end
-end
-
-redef class IntrosRedefsSection
-       redef var html_id is lazy do return "article:{mentity.nitdoc_id}.intros_redefs"
-       redef var toc_title do return "Intros / Redefs"
-       redef var html_title = null
-       redef var html_subtitle = null
-       redef var is_toc_hidden = true
-end
-
-redef class IntrosRedefsListArticle
-       redef var html_id is lazy do return "article:{list_title}_{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
-
-       redef fun render_body do
-               var lst = new UnorderedList
-               lst.css_classes.add "list-unstyled list-labeled"
-               for mentity in mentities do
-                       lst.add_li mentity.html_list_item
-               end
                add lst
        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"
@@ -613,7 +547,7 @@ redef class DefinitionLinArticle
                        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
+                       var comment = mentity.mclassdef.html_synopsis
                        if comment != null then
                                tpl.add ": "
                                tpl.add comment
@@ -627,22 +561,63 @@ redef class DefinitionLinArticle
 end
 
 redef class GraphArticle
-       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
-       redef var is_toc_hidden = true
 
-       # HTML map used to display link.
+       # Graph in SVG with clickable map.
        #
        # This attribute is set by the `doc_render` phase who knows the context.
-       var map: String is noinit, writable
+       var svg: nullable String = null is writable
 
        redef fun render_body do
                addn "<div class=\"text-center\">"
-               addn " <img src='{id}.png' usemap='#{id}' style='margin:auto'"
-               addn "  alt='{graph_title}'/>"
-               add map
+               var svg = self.svg
+               if svg != null then add svg
                addn "</div>"
        end
 end
+
+redef class ReadmeSection
+       redef var html_id is lazy do
+               return markdown_processor.decorator.strip_id(html_title.as(not null).to_s)
+       end
+
+       redef var html_title is lazy do
+               return markdown_processor.process(title.as(not null))
+       end
+end
+
+redef class ReadmeArticle
+       redef var html_id = ""
+       redef var html_title = null
+       redef var is_toc_hidden = true
+
+       redef fun render_body do
+               add markdown_processor.process(md.trim.write_to_string)
+       end
+end
+
+redef class DocumentationArticle
+       redef var html_title is lazy do
+               var synopsis = mentity.html_synopsis
+               if synopsis == null then return mentity.html_link
+               return "{mentity.html_link.write_to_string} &ndash; {synopsis.write_to_string}"
+       end
+
+       redef var html_subtitle is lazy do return null
+       redef var html_toc_title is lazy do return mentity.html_name
+       redef var is_toc_hidden is lazy do return depth > 3
+
+       redef fun render_body do
+               var tabs = new DocTabs("{html_id}.tabs", "")
+               var comment = mentity.html_comment
+               if comment != null then
+                       tabs.add_panel new DocTabPanel("{html_tab_id}-comment", "Comment", comment)
+               end
+               for child in children do
+                       if child.is_hidden then continue
+                       var title = child.html_toc_title or else child.toc_title or else ""
+                       tabs.add_panel new DocTabPanel(child.html_tab_id, title, child)
+               end
+               addn tabs
+       end
+end