nitdoc: Shorten redundant IDs.
[nit.git] / src / doc / doc_model.nit
index a1233db..c25c7dc 100644 (file)
 module doc_model
 
 import model_utils
-import docdown
+import doc_down
 import doc_templates
 import ordered_tree
 import model_ext
 
-
-################################################################################
-# Additions to Nit entities.
-
-redef class MDoc
-       # Comment synopsys HTML escaped
-       fun short_comment: String do return content.first.html_escape
-
-       # Full comment HTML escaped
-       fun full_comment: String do return content.join("\n").html_escape
-
-       # Synopsys in a template
-       fun tpl_short_comment: Streamable do return short_markdown
-
-       # Full comment in a template
-       fun tpl_comment: Streamable do return full_markdown
-end
-
 redef class Location
        # Github url based on this location
        fun github(gitdir: String): String do
@@ -53,10 +35,18 @@ redef class MEntity
        # HTML Escaped name
        fun nitdoc_name: String is abstract
 
-       # Used as HTML unique ids
+       # ID used as a HTML unique ID and in file names.
+       #
+       # **Must** match the following (POSIX ERE) regular expression:
+       #
+       # ~~~POSIX ERE
+       # ^[A-Za-z_][A-Za-z0-9._-]*$
+       # ~~~
+       #
+       # That way, the ID is always a valid URI component and a valid XML name.
        fun nitdoc_id: String is abstract
 
-       # URL of this entity Nitdoc page
+       # URL of this entity’s Nitdoc page.
        fun nitdoc_url: String is abstract
 
        # A template link to the mentity `nitdoc_id`
@@ -112,7 +102,7 @@ redef class MEntity
                lnk.add tpl_link
                if mdoc != null then
                        lnk.add ": "
-                       lnk.add mdoc.short_markdown
+                       lnk.add mdoc.tpl_short_comment
                end
                return new TplListItem.with_content(lnk)
        end
@@ -142,15 +132,15 @@ redef class MConcern
                lnk.add tpl_anchor
                if mdoc != null then
                        lnk.add ": "
-                       lnk.add mdoc.short_markdown
+                       lnk.add mdoc.tpl_short_comment
                end
                return new TplListItem.with_content(lnk)
        end
 end
 
 redef class MProject
+       redef var nitdoc_id = name.to_cmangle is lazy
        redef fun nitdoc_name do return name.html_escape
-       redef fun nitdoc_id do return nitdoc_name
        redef fun nitdoc_url do return root.nitdoc_url
 
        redef fun mdoc do
@@ -185,11 +175,11 @@ end
 redef class MGroup
        redef fun nitdoc_name do return name.html_escape
 
-       redef fun nitdoc_id do
+       redef var nitdoc_id is lazy do
                if parent != null then
-                       return "{parent.nitdoc_id}__{nitdoc_name}"
+                       return "{parent.nitdoc_id}__{name.to_cmangle}"
                end
-               return "{mproject.nitdoc_id}__{nitdoc_name}"
+               return name.to_cmangle
        end
 
        redef fun nitdoc_url do return "group_{nitdoc_id}.html"
@@ -225,11 +215,15 @@ end
 redef class MModule
        redef fun nitdoc_name do return name.html_escape
 
-       redef fun nitdoc_id do
+       redef var nitdoc_id is lazy do
                if mgroup != null then
-                       return "{mgroup.nitdoc_id}__{nitdoc_name}"
+                       if mgroup.mmodules.length == 1 then
+                               return "{mgroup.nitdoc_id}-"
+                       else
+                               return "{mgroup.nitdoc_id}__{name.to_cmangle}"
+                       end
                end
-               return nitdoc_name
+               return name.to_cmangle
        end
 
        redef fun nitdoc_url do return "module_{nitdoc_id}.html"
@@ -237,7 +231,7 @@ redef class MModule
        redef fun tpl_declaration do
                var tpl = new Template
                tpl.add "<span>module "
-               tpl.add tpl_link
+               tpl.add tpl_namespace
                tpl.add "</span>"
                return tpl
        end
@@ -265,7 +259,7 @@ end
 
 redef class MClass
        redef fun nitdoc_name do return name.html_escape
-       redef fun nitdoc_id do return "{intro_mmodule.mgroup.mproject}__{name.to_cmangle}"
+       redef var nitdoc_id = "{intro_mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
        redef fun nitdoc_url do return "class_{nitdoc_id}.html"
        redef fun mdoc do return intro.mdoc
 
@@ -315,7 +309,7 @@ end
 
 redef class MClassDef
        redef fun nitdoc_name do return mclass.nitdoc_name
-       redef fun nitdoc_id do return "{mmodule.nitdoc_id}__{name.to_cmangle}"
+       redef var nitdoc_id = "{mmodule.nitdoc_id}__{name.to_cmangle}" is lazy
        redef fun nitdoc_url do return "{mclass.nitdoc_url}#{nitdoc_id}"
 
        redef fun tpl_namespace do
@@ -405,10 +399,10 @@ redef class MClassDef
                lnk.add tpl_link
                if mdoc != null then
                        lnk.add ": "
-                       lnk.add mdoc.short_markdown
+                       lnk.add mdoc.tpl_short_comment
                else if mclass.intro.mdoc != null then
                        lnk.add ": "
-                       lnk.add mclass.intro.mdoc.short_markdown
+                       lnk.add mclass.intro.mdoc.tpl_short_comment
                end
                return new TplListItem.with_content(lnk)
        end
@@ -435,8 +429,8 @@ redef class MClassDef
 end
 
 redef class MProperty
+       redef var nitdoc_id = "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}" is lazy
        redef fun nitdoc_name do return name.html_escape
-       redef fun nitdoc_id do return "{intro_mclassdef.mclass.nitdoc_id}__{name.to_cmangle}"
        redef fun nitdoc_url do return "property_{nitdoc_id}.html"
 
        redef fun mdoc do return intro.mdoc
@@ -463,7 +457,7 @@ end
 
 redef class MPropDef
        redef fun nitdoc_name do return mproperty.nitdoc_name
-       redef fun nitdoc_id do return "{mclassdef.nitdoc_id}__{name.to_cmangle}"
+       redef var nitdoc_id = "{mclassdef.nitdoc_id}__{name.to_cmangle}" is lazy
        redef fun nitdoc_url do return "{mproperty.nitdoc_url}#{nitdoc_id}"
 
        redef fun tpl_anchor: TplLink do
@@ -552,10 +546,10 @@ redef class MPropDef
                lnk.add anchor
                if mdoc != null then
                        lnk.add ": "
-                       lnk.add mdoc.short_markdown
+                       lnk.add mdoc.tpl_short_comment
                else if mproperty.intro.mdoc != null then
                        lnk.add ": "
-                       lnk.add mproperty.intro.mdoc.short_markdown
+                       lnk.add mproperty.intro.mdoc.tpl_short_comment
                end
                return new TplListItem.with_content(lnk)
        end
@@ -570,7 +564,7 @@ redef class MPropDef
                lnk.add anchor
                if mdoc != null then
                        lnk.add ": "
-                       lnk.add mdoc.short_markdown
+                       lnk.add mdoc.tpl_short_comment
                end
                var li = new TplListItem.with_content(lnk)
                li.css_classes.add "signature"
@@ -578,6 +572,17 @@ redef class MPropDef
        end
 end
 
+redef class MAttributeDef
+       redef fun tpl_signature do
+               var tpl = new Template
+               if static_mtype != null then
+                       tpl.add ": "
+                       tpl.add static_mtype.tpl_signature
+               end
+               return tpl
+       end
+end
+
 redef class MMethod
        redef fun tpl_signature do
                var tpl = new Template
@@ -740,3 +745,25 @@ redef class MRawType
                return tpl
        end
 end
+
+redef class MInnerClass
+       redef fun nitdoc_url do return inner.nitdoc_url
+       redef fun tpl_signature do return inner.tpl_signature
+end
+
+redef class MInnerClassDef
+       redef fun nitdoc_url do return inner.nitdoc_url
+
+       redef fun tpl_anchor do return inner.tpl_anchor
+       redef fun tpl_link do return inner.tpl_link
+       redef fun tpl_signature do return inner.tpl_signature
+
+       redef fun tpl_definition do
+               var tpl = new TplClassDefinition
+               tpl.namespace = mclassdef.tpl_namespace
+               if mdoc != null then
+                       tpl.comment = mdoc.tpl_comment
+               end
+               return tpl
+       end
+end