From e7e68fcf04b74c360c13415c5830ef0c923d0a09 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 17 Feb 2015 20:27:37 +0100 Subject: [PATCH] src/doc: clean DocPage rendering API. Signed-off-by: Alexandre Terrasa --- src/doc/doc_phases/doc_html.nit | 135 +++++++++++++--------------- src/doc/html_templates/html_components.nit | 127 -------------------------- src/doc/html_templates/html_templates.nit | 132 +++++++++++++++++++++++++++ 3 files changed, 192 insertions(+), 202 deletions(-) diff --git a/src/doc/doc_phases/doc_html.nit b/src/doc/doc_phases/doc_html.nit index 58d5ae7..a11e187 100644 --- a/src/doc/doc_phases/doc_html.nit +++ b/src/doc/doc_phases/doc_html.nit @@ -105,7 +105,7 @@ class RenderHTMLPhase redef fun apply do init_output_dir for page in doc.pages do - page.render(self, doc).write_to_file("{ctx.output_dir.to_s}/{page.page_url}") + page.render(self, doc).write_to_file("{ctx.output_dir.to_s}/{page.html_url}") end end @@ -163,15 +163,16 @@ redef class DocPage shareurl = v.ctx.opt_shareurl.value.as(not null) end - # build page - self.title = tpl_title(v, doc) - self.url = page_url + # init page options self.shareurl = shareurl - self.topmenu = tpl_topmenu(v, doc) - self.add_section tpl_content(v, doc) self.footer = v.ctx.opt_custom_footer.value self.body_attrs.add(new TagAttribute("data-bootstrap-share", shareurl)) - self.sidebar = tpl_sidebar(v, doc) + + # build page + init_title(v, doc) + init_sidebar(v, doc) + init_topmenu(v, doc) + init_content(v, doc) # piwik tracking var tracker_url = v.ctx.opt_piwik_tracker.value @@ -185,23 +186,12 @@ redef class DocPage # FIXME diff hack # all properties below are roughly copied from `doc_pages` - # URL to this page. - fun page_url: String is abstract - - # Build page sidebar if any - fun tpl_sidebar(v: RenderHTMLPhase, doc: DocModel): nullable TplSidebar do return null - # Build page title string - fun tpl_title(v: RenderHTMLPhase, doc: DocModel): String do - if v.ctx.opt_custom_title.value != null then - return v.ctx.opt_custom_title.value.to_s - end - return "Nitdoc" - end + fun init_title(v: RenderHTMLPhase, doc: DocModel) is abstract - # Build top menu template - fun tpl_topmenu(v: RenderHTMLPhase, doc: DocModel): TplTopMenu do - var topmenu = new TplTopMenu(page_url) + # Build top menu template if any. + fun init_topmenu(v: RenderHTMLPhase, doc: DocModel) do + topmenu = new TplTopMenu(html_url) var brand = v.ctx.opt_custom_brand.value if brand != null then var tpl = new Template @@ -212,28 +202,31 @@ redef class DocPage end topmenu.add_link new TplLink("index.html", "Overview") topmenu.add_link new TplLink("search.html", "Index") - return topmenu end - # Build page content template - fun tpl_content(v: RenderHTMLPhase, doc: DocModel): TplSection is abstract + # Build page sidebar if any. + fun init_sidebar(v: RenderHTMLPhase, doc: DocModel) do + sidebar = new TplSidebar + end + + # Build page content template. + fun init_content(v: RenderHTMLPhase, doc: DocModel) do end end redef class OverviewPage - redef fun page_url do return "index.html" + redef var html_url = "index.html" - redef fun tpl_title(v, doc) do + redef fun init_title(v, doc) do + title = "Overview" if v.ctx.opt_custom_title.value != null then - return v.ctx.opt_custom_title.value.to_s - else - return "Overview" + title = v.ctx.opt_custom_title.value.to_s end end # TODO this should be done in StructurePhase. - redef fun tpl_content(v, doc) do + redef fun init_content(v, doc) do # intro text - var section = new TplSection.with_title("overview", tpl_title(v, doc)) + var section = new TplSection.with_title("overview", title) var article = new TplArticle("intro") if v.ctx.opt_custom_intro.value != null then article.content = v.ctx.opt_custom_intro.value.to_s @@ -255,18 +248,19 @@ redef class OverviewPage ssection.add_child sarticle end section.add_child ssection - return section + self.add_section section end - redef fun tpl_sidebar(v, doc) do return new TplSidebar + redef fun init_sidebar(v, doc) do sidebar = new TplSidebar end redef class SearchPage - redef fun page_url do return "search.html" - redef fun tpl_title(v, doc) do return "Index" + redef var html_url = "search.html" + redef fun init_title(v, doc) do title = "Index" + redef fun init_sidebar(v, doc) do end # TODO this should be done in StructurePhase. - redef fun tpl_content(v, doc) do + redef fun init_content(v, doc) do var tpl = new TplSearchPage("search_all") var section = new TplSection("search") # title @@ -289,7 +283,7 @@ redef class SearchPage tpl.props.add m end section.add_child tpl - return section + self.add_section section end # Extract mmodule list to display (sorted by name) @@ -319,9 +313,9 @@ redef class SearchPage end redef class MEntityPage - redef fun page_url do return mentity.nitdoc_url - redef fun tpl_title(v, doc) do return mentity.nitdoc_name - redef fun tpl_content(v, doc) do return root.start_rendering(v, doc, self) + redef var html_url is lazy do return mentity.nitdoc_url + redef fun init_title(v, doc) do title = mentity.nitdoc_name + redef fun init_content(v, doc) do add_section root.start_rendering(v, doc, self) end # FIXME all clases below are roughly copied from `doc_pages` and adapted to new @@ -329,22 +323,21 @@ end # `doc_templates` module. redef class MGroupPage - redef fun tpl_topmenu(v, doc) do - var topmenu = super + redef fun init_topmenu(v, doc) do + super var mproject = mentity.mproject if not mentity.is_root then topmenu.add_link new TplLink(mproject.nitdoc_url, mproject.nitdoc_name) end - topmenu.add_link new TplLink(page_url, mproject.nitdoc_name) - return topmenu + topmenu.add_link new TplLink(html_url, mproject.nitdoc_name) end - redef fun tpl_sidebar(v, doc) do - var sidebar = new TplSidebar + redef fun init_sidebar(v, doc) do + super var mclasses = new HashSet[MClass] mclasses.add_all intros mclasses.add_all redefs - if mclasses.is_empty then return sidebar + if mclasses.is_empty then return var list = new TplList.with_classes(["list-unstyled", "list-labeled"]) var sorted = mclasses.to_a @@ -353,7 +346,6 @@ redef class MGroupPage list.add_li tpl_sidebar_item(mclass) end sidebar.boxes.add new TplSideBox.with_content("All classes", list) - return sidebar end private fun tpl_sidebar_item(def: MClass): TplListItem do @@ -371,22 +363,21 @@ redef class MGroupPage end redef class MModulePage - redef fun tpl_topmenu(v, doc) do - var topmenu = super + redef fun init_topmenu(v, doc) do + super var mproject = mentity.mproject topmenu.add_link new TplLink(mproject.nitdoc_url, mproject.nitdoc_name) topmenu.add_link new TplLink(mentity.nitdoc_url, mentity.nitdoc_name) - return topmenu end # Class list to display in sidebar - redef fun tpl_sidebar(v, doc) do + redef fun init_sidebar(v, doc) do # TODO filter here? - var sidebar = new TplSidebar + super var mclasses = new HashSet[MClass] mclasses.add_all mentity.filter_intro_mclasses(v.ctx.min_visibility) mclasses.add_all mentity.filter_redef_mclasses(v.ctx.min_visibility) - if mclasses.is_empty then return sidebar + if mclasses.is_empty then return var list = new TplList.with_classes(["list-unstyled", "list-labeled"]) var sorted = mclasses.to_a @@ -395,7 +386,6 @@ redef class MModulePage list.add_li tpl_sidebar_item(mclass) end sidebar.boxes.add new TplSideBox.with_content("All classes", list) - return sidebar end private fun tpl_sidebar_item(def: MClass): TplListItem do @@ -414,27 +404,25 @@ end redef class MClassPage - redef fun tpl_title(v, doc) do - return "{mentity.nitdoc_name}{mentity.tpl_signature.write_to_string}" + redef fun init_title(v, doc) do + title = "{mentity.nitdoc_name}{mentity.tpl_signature.write_to_string}" end - redef fun tpl_topmenu(v, doc) do - var topmenu = super + redef fun init_topmenu(v, doc) do + super var mproject = mentity.intro_mmodule.mgroup.mproject topmenu.add_link new TplLink("{mproject.nitdoc_url}", "{mproject.nitdoc_name}") - topmenu.add_link new TplLink(page_url, mentity.nitdoc_name) - return topmenu + topmenu.add_link new TplLink(html_url, mentity.nitdoc_name) end - redef fun tpl_sidebar(v, doc) do - var sidebar = new TplSidebar + redef fun init_sidebar(v, doc) do + super var by_kind = new PropertiesByKind.with_elements(mclass_inherited_mprops(v, doc)) var summary = new TplList.with_classes(["list-unstyled"]) by_kind.sort_groups(v.name_sorter) for g in by_kind.groups do tpl_sidebar_list(g, summary) sidebar.boxes.add new TplSideBox.with_content("All properties", summary) - return sidebar end private fun tpl_sidebar_list(mprops: PropertyGroup[MProperty], summary: TplList) do @@ -497,22 +485,19 @@ redef class MClassPage end redef class MPropertyPage - redef fun tpl_topmenu(v, doc) do - var topmenu = super + redef fun init_title(v, doc) do + title = "{mentity.nitdoc_name}{mentity.tpl_signature.write_to_string}" + end + + redef fun init_topmenu(v, doc) do + super var mmodule = mentity.intro_mclassdef.mmodule var mproject = mmodule.mgroup.mproject var mclass = mentity.intro_mclassdef.mclass topmenu.add_link new TplLink("{mproject.nitdoc_url}", "{mproject.nitdoc_name}") topmenu.add_link new TplLink("{mclass.nitdoc_url}", "{mclass.nitdoc_name}") - topmenu.add_link new TplLink(page_url, mentity.nitdoc_name) - return topmenu + topmenu.add_link new TplLink(html_url, mentity.nitdoc_name) end - - redef fun tpl_title(v, doc) do - return "{mentity.nitdoc_name}{mentity.tpl_signature.write_to_string}" - end - - redef fun tpl_sidebar(v, doc) do return new TplSidebar end redef class DocComposite diff --git a/src/doc/html_templates/html_components.nit b/src/doc/html_templates/html_components.nit index 5194e08..bc83f96 100644 --- a/src/doc/html_templates/html_components.nit +++ b/src/doc/html_templates/html_components.nit @@ -20,133 +20,6 @@ import doc_base import template import json::static -# A documentation page -redef class DocPage - super Template - - # Page url - var url: String is writable, noinit - - # Directory where css, js and other assets can be found - var shareurl: String is writable, noinit - - # Attributes of the body tag element - var body_attrs = new Array[TagAttribute] - - # Top menu template if any - var topmenu: TplTopMenu is writable, noinit - - # Sidebar template if any - var sidebar: nullable TplSidebar = null is writable - - # Content of the page in form a TplSection - 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] - - # Add a section to this page - fun add_section(section: TplSection) do - sections.add section - end - - # Render the html header - private fun render_head do - var css = (self.shareurl / "css").html_escape - var vendors = (self.shareurl / "vendors").html_escape - - addn "" - addn "" - addn " " - addn " " - addn " " - addn " " - addn " " - addn " " - addn " " - addn " " - addn " {title.html_escape}" - addn "" - add "" - end - - # Render the topmenu template - private fun render_topmenu do - addn "
" - add topmenu - addn "
" - end - - # Render the sidebar - # Sidebar is automatically populated with a summary of all sections - fun render_sidebar do - if sidebar == null then return - var summary = new TplSummary.with_order(0) - for section in sections do - section.render_summary summary - end - sidebar.boxes.add summary - add sidebar.as(not null) - end - # Render the footer and content - private fun render_content do - for section in sections do add section - if footer != null then - addn "" - end - end - - # Render JS scripts - private fun render_footer do - var vendors = (self.shareurl / "vendors").html_escape - var js = (self.shareurl / "js").html_escape - - addn "" - addn "" - addn "" - addn "" - for script in scripts do add script - addn """""" - addn "" - addn "" - end - - # Render the whole page - redef fun rendering do - render_head - addn "
" - render_topmenu - addn "
" - if sidebar != null then - addn "
" - render_sidebar - addn "
" - addn "
" - render_content - addn "
" - else - addn "
" - render_content - addn "
" - end - addn "
" - addn "
" - render_footer - end -end - ######################### # general layout elements ######################### diff --git a/src/doc/html_templates/html_templates.nit b/src/doc/html_templates/html_templates.nit index ab019c8..83c1842 100644 --- a/src/doc/html_templates/html_templates.nit +++ b/src/doc/html_templates/html_templates.nit @@ -16,3 +16,135 @@ module html_templates import html_model + +# Renders the page as HTML. +redef class DocPage + super Template + + # Page url. + var html_url: String is writable, noinit + + # Directory where css, js and other assets can be found. + var shareurl: String is writable, noinit + + # Attributes of the body tag element. + var body_attrs = new Array[TagAttribute] + + # Top menu template if any. + var topmenu: TplTopMenu is writable, noinit + + # Sidebar template if any. + var sidebar: nullable TplSidebar = 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 ``. + private fun render_head do + var css = (self.shareurl / "css").html_escape + var vendors = (self.shareurl / "vendors").html_escape + + addn "" + addn "" + addn " " + addn " " + addn " " + addn " " + addn " " + addn " " + addn " " + addn " " + addn " {title.html_escape}" + addn "" + add "" + end + + # Renders the topmenu template. + private fun render_topmenu do + addn "
" + add topmenu + addn "
" + end + + # Renders the sidebar template. + # + # Sidebar is automatically populated with a summary of all sections + # TODO remove summary generation when other templates are migrated. + private fun render_sidebar do + if sidebar == null then return + var summary = new TplSummary.with_order(0) + for section in sections do + section.render_summary summary + end + sidebar.boxes.add summary + add sidebar.as(not null) + end + + # Renders the footer and content. + private fun render_content do + for section in sections do add section + if footer != null then + addn "" + end + end + + # Render JS scripts + private fun render_footer do + var vendors = (self.shareurl / "vendors").html_escape + var js = (self.shareurl / "js").html_escape + + addn "" + addn "" + addn "" + addn "" + for script in scripts do add script + addn """""" + addn "" + addn "" + end + + # Render the whole page + redef fun rendering do + render_head + addn "
" + render_topmenu + addn "
" + if sidebar != null then + addn "
" + render_sidebar + addn "
" + addn "
" + render_content + addn "
" + else + addn "
" + render_content + addn "
" + end + addn "
" + addn "
" + render_footer + end +end -- 1.7.9.5