X-Git-Url: http://nitlanguage.org diff --git a/src/doc/doc_templates.nit b/src/doc/doc_templates.nit index dc05b33..c23e880 100644 --- a/src/doc/doc_templates.nit +++ b/src/doc/doc_templates.nit @@ -17,12 +17,13 @@ module doc_templates import template +import json::static # A documentation page class TplPage super Template - # Page title in HTML header + # The unescaped page title to put in the HTML header. var title: String is writable, noinit # Page url @@ -44,13 +45,11 @@ class TplPage var sections = new Array[TplSection] # Footer content if any - var footer: nullable Streamable = null is writable + var footer: nullable Writable = null is writable # JS scripts to append at the end of the body var scripts = new Array[TplScript] - init do end - # Add a section to this page fun add_section(section: TplSection) do sections.add section @@ -58,28 +57,31 @@ class TplPage # Render the html header private fun render_head do - add "" - add "" - add " " - add " " - add " " - add " " - add " " - add " " - add " " - add " " - add " {title}" - add "" + 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 "" + addn ">" end # Render the topmenu template private fun render_topmenu do - add "
" + addn "
" add topmenu - add "
" + addn "
" end # Render the sidebar @@ -97,49 +99,52 @@ class TplPage private fun render_content do for section in sections do add section if footer != null then - add "" end end # Render JS scripts private fun render_footer do - add "" - add "" - add "" - add "" + 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 - add """""" - add "" - add "" + addn "" + addn "" end # Render the whole page redef fun rendering do render_head - add "
" + addn "
" render_topmenu - add "
" + addn "
" if sidebar != null then - add "
" + addn "
" render_sidebar - add "
" - add "
" + addn "
" + addn "
" render_content - add "
" + addn "
" else - add "
" + addn "
" render_content - add "
" + addn "
" end - add "
" - add "
" + addn "
" + addn "
" render_footer end end @@ -153,19 +158,15 @@ class TplTopMenu super Template # Brand link to display in first position of the top menu - private var brand: nullable Streamable = null is writable + private var brand: nullable Writable = null is writable # Elements of the topmenu - private var elts = new Array[Streamable] + private var elts = new Array[Writable] # The page url where the top menu is displayed. # # Used to select the active link. private var current_url: String - init(current_url: String) do - self.current_url = current_url - end - # Add a new link to the menu. fun add_link(content: TplLink) do var is_active = content.href == current_url @@ -173,7 +174,7 @@ class TplTopMenu end # Add a content between `
  • ` tags - fun add_item(content: Streamable, is_active: Bool) do + fun add_item(content: Writable, is_active: Bool) do var tpl = new Template tpl.add "" tpl.add content - tpl.add "
  • " + tpl.addn "" add_raw(tpl) end # Add a raw content to the menu - fun add_raw(content: Streamable) do + fun add_raw(content: Writable) do elts.add content end redef fun rendering do if brand == null and elts.is_empty then return - add "" end end @@ -232,15 +233,17 @@ class TplSidebar redef fun rendering do if boxes.is_empty then return order_boxes - add "" end end # Comparator used to sort boxes by order private class OrderComparator - super Comparator[TplSidebarElt] + super Comparator + + redef type COMPARED: TplSidebarElt redef fun compare(a, b) do if a.order < b.order then return -1 @@ -270,22 +273,21 @@ class TplSideBox # Box HTML id # equals to `title.to_cmangle` by default # Used for collapsing - var id: String + var id: String is noinit # Content to display in the box # box will not be rendered if the content is null - var content: nullable Streamable writable + var content: nullable Writable = null is writable # Is the box opened by default # otherwise, the user will have to clic on the title to display the content - var is_open writable = false + var is_open = false is writable - init(title: String) do - self.title = title + init do self.id = title.to_cmangle end - init with_content(title: String, content: Streamable) do + init with_content(title: String, content: Writable) do init(title) self.content = content end @@ -294,16 +296,16 @@ class TplSideBox if content == null then return var open = "" if is_open then open = "in" - add "
    " - add "
    " + addn "
    " + addn "
    " add " " add title - add " " - add "
    " - add "
    " + addn " " + addn "
    " + addn "
    " add content.as(not null) - add "
    " - add "
    " + addn "
    " + addn "
    " end end @@ -330,18 +332,18 @@ class TplSummary redef fun rendering do if children.is_empty then return - add "
    " - add "
    " + addn "
    " + addn "
    " add " " add "Summary" - add " " - add "
    " - add "
    " - add "
    " + addn "
    " + addn " " - add "
    " - add "
    " + addn " " + addn "
    " + addn "
    " end end @@ -350,25 +352,23 @@ class TplSummaryEntry super TplSummaryElt # Text to display - var text: Streamable + var text: Writable # Children of this entry # Will be displayed as a tree var children = new Array[TplSummaryElt] - init(text: Streamable) do self.text = text - redef fun add_child(child) do children.add child redef fun rendering do add "
  • " add text if not children.is_empty then - add "" end - add "
  • " + addn "" end end @@ -383,14 +383,14 @@ class TplSectionElt # Title to display if any # if both `title` and `summary_title` are null then # the section will not appear in the summary - var title: nullable Streamable writable + var title: nullable Writable = null is writable # Subtitle to display if any - var subtitle: nullable Streamable writable + var subtitle: nullable Writable = null is writable # Title that appear in the summary # if null use `title` instead - var summary_title: nullable String writable + var summary_title: nullable String = null is writable # CSS classes to apply on the section element var css_classes = new Array[String] @@ -399,11 +399,9 @@ class TplSectionElt var title_classes = new Array[String] # Parent article/section if any - var parent: nullable TplSectionElt + var parent: nullable TplSectionElt = null - init(id: String) do self.id = id - - init with_title(id: String, title: Streamable) do + init with_title(id: String, title: Writable) do init(id) self.title = title end @@ -446,23 +444,23 @@ class TplSection super TplSectionElt redef fun rendering do - add "
    " + addn "
    " if title != null then var lvl = hlvl if lvl == 2 then title_classes.add "well well-sm" - add "" - add title.as(not null) - add "" + addn "" + addn title.as(not null) + addn "" end if subtitle != null then - add "
    " - add subtitle.as(not null) - add "
    " + addn "
    " + addn subtitle.as(not null) + addn "
    " end for child in children do add child end - add "
    " + addn "
    " end end @@ -471,10 +469,10 @@ class TplArticle super TplSectionElt # Content for this article - var content: nullable Streamable writable = null - var source_link: nullable Streamable writable = null + var content: nullable Writable = null is writable + var source_link: nullable Writable = null is writable - init with_content(id: String, title: Streamable, content: Streamable) do + init with_content(id: String, title: Writable, content: Writable) do with_title(id, title) self.content = content end @@ -490,23 +488,23 @@ class TplArticle redef fun rendering do if is_empty then return - add "
    " + addn "
    " if source_link != null then add "" + addn "" end if title != null then var lvl = hlvl if lvl == 2 then title_classes.add "well well-sm" add "" add title.as(not null) - add "" + addn "" end if subtitle != null then add "
    " add subtitle.as(not null) - add "
    " + addn "" end if content != null then add content.as(not null) @@ -514,7 +512,7 @@ class TplArticle for child in children do add child end - add """
    """ + addn """
    """ end redef fun is_empty: Bool do @@ -527,16 +525,16 @@ class TplDefinition super Template # Comment to display - var comment: nullable Streamable = null is writable + var comment: nullable Writable = null is writable # Namespace for this definition - var namespace: nullable Streamable = null is writable + var namespace: nullable Writable = null is writable # Location link to display - var location: nullable Streamable = null is writable + var location: nullable Writable = null is writable private fun render_info do - add "
    " + addn "
    " if namespace != null then if comment == null then add "no comment for " @@ -547,7 +545,7 @@ class TplDefinition add " " add location.as(not null) end - add "
    " + addn "
    " end private fun render_comment do @@ -555,10 +553,10 @@ class TplDefinition end redef fun rendering do - add "
    " + addn "
    " render_comment render_info - add "
    " + addn "
    " end end @@ -569,23 +567,21 @@ class TplClassDefinition var intros = new Array[TplListElt] var redefs = new Array[TplListElt] - init do end - redef fun rendering do - add "
    " + addn "
    " render_comment render_info render_list("Introduces", intros) render_list("Redefines", redefs) - add "
    " + addn "
    " end private fun render_list(name: String, elts: Array[TplListElt]) do if elts.is_empty then return - add "
    {name}
    " - add "" end end @@ -593,53 +589,53 @@ end class TplSearchPage super TplSectionElt - var modules = new Array[Streamable] - var classes = new Array[Streamable] - var props = new Array[Streamable] + var modules = new Array[Writable] + var classes = new Array[Writable] + var props = new Array[Writable] redef fun rendering do var title = self.title - if title != null then add "

    {title}

    " - add "
    " - add "
    " + if title != null then addn "

    {title.to_s.html_escape}

    " + addn "
    " + addn "
    " if not modules.is_empty then - add "
    " - add "

    Modules

    " - add "
      " + addn "
      " + addn "

      Modules

      " + addn "
        " for m in modules do add "
      • " add m - add "
      • " + addn "" end - add "
      " - add "
      " + addn "
    " + addn "
    " end if not classes.is_empty then - add "
    " - add "

    Classes

    " - add "
      " + addn "
      " + addn "

      Classes

      " + addn "
        " for c in classes do add "
      • " add c - add "
      • " + addn "" end - add "
      " - add "
      " + addn "
    " + addn "
    " end if not props.is_empty then - add "
    " - add "

    Properties

    " - add "
      " + addn "
      " + addn "

      Properties

      " + addn "
        " for p in props do add "
      • " add p - add "
      • " + addn "" end - add "
      " - add "
      " + addn "
    " + addn "
    " end - add "
    " - add "
    " + addn "
    " + addn "
    " end end @@ -652,18 +648,13 @@ class TplLink super Template # Link href - var href: String writable - - # Text to display in the link - var text: Streamable writable + var href: String is writable - # Optional title - var title: nullable String writable + # The raw HTML content to display in the link + var text: Writable is writable - init(href, text: String) do - self.href = href - self.text = text - end + # The unescaped optional title. + var title: nullable String = null is writable init with_title(href, text, title: String) do init(href, text) @@ -672,11 +663,11 @@ class TplLink redef fun rendering do add "" @@ -699,17 +690,15 @@ class TplList # Add content wrapped in a
  • element fun add_li(item: TplListItem) do elts.add item - init do end - init with_classes(classes: Array[String]) do self.css_classes = classes fun is_empty: Bool do return elts.is_empty redef fun rendering do if elts.is_empty then return - add "
      " + addn "
        " for elt in elts do add elt - add "
      " + addn "
    " end end @@ -728,23 +717,21 @@ class TplListItem # CSS classes of the
  • element var css_classes = new Array[String] - init do end - - init with_content(content: Streamable) do append(content) + init with_content(content: Writable) do append(content) - init with_classes(content: Streamable, classes: Array[String]) do + init with_classes(content: Writable, classes: Array[String]) do with_content(content) css_classes = classes end # Append `content` to the item # similar to `self.content.add` - fun append(content: Streamable) do self.content.add content + fun append(content: Writable) do self.content.add content redef fun rendering do add "
  • " add content - add "
  • " + addn "" end end @@ -762,9 +749,9 @@ class TplTab var css_classes = new Array[String] redef fun rendering do - add "
    " + addn "
    " for panel in panels do add panel - add "
    " + addn "
    " end end @@ -778,21 +765,21 @@ class TplTabPanel # The panel id. # # Used to show/hide panel. - var id: String + var id: String is noinit # The panel name. # # Displayed in the tab header or in the pointing link. - var name: Streamable + var name: Writable # Is the panel visible by default? var is_active = false is writable # Body of the panel - var content: nullable Streamable = null is writable + var content: nullable Writable = null is writable # Get a link pointing to this panel. - fun tpl_link_to: Streamable do + fun tpl_link_to: Writable do var lnk = new Template lnk.add "
    " lnk.add name @@ -803,9 +790,9 @@ class TplTabPanel redef fun rendering do add "
    " + addn "' id='{id}'>" if content != null then add content.as(not null) - add "
    " + addn "" end end @@ -814,12 +801,12 @@ class TplLabel super Template # Content of the label if any - var content: nullable Streamable = null is writable + var content: nullable Writable = null is writable # CSS classes of the element var css_classes = new Array[String] - init with_content(content: Streamable) do self.content = content + init with_content(content: Writable) do self.content = content init with_classes(classes: Array[String]) do self.css_classes = classes redef fun rendering do @@ -848,23 +835,29 @@ end # A HTML tag attribute # `` +# +# ~~~nit +# var attr: TagAttribute +# +# attr = new TagAttribute("foo", null) +# assert attr.write_to_string == " foo=\"\"" +# +# attr = new TagAttribute("foo", "bar<>") +# assert attr.write_to_string == " foo=\"bar<>\"" +# ~~~ class TagAttribute super Template var name: String var value: nullable String - init(name: String, value: nullable String) do - self.name = name - self.value = value - end - redef fun rendering do var value = self.value if value == null then - add(" {name}") + # SEE: http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes + add " {name.html_escape}=\"\"" else - add(" {name}=\"{value}\"") + add " {name.html_escape}=\"{value.html_escape}\"" end end end @@ -874,7 +867,7 @@ class TplScript super Template var attrs = new Array[TagAttribute] - var content: nullable Streamable = null is writable + var content: nullable Writable = null is writable init do attrs.add(new TagAttribute("type", "text/javascript")) @@ -887,9 +880,9 @@ class TplScript redef fun rendering do add "" + addn ">" render_content - add "" + addn "" end end @@ -900,24 +893,23 @@ class TplPiwikScript var tracker_url: String var site_id: String - init(tracker_url, site_id: String) do - super - self.tracker_url = tracker_url - self.site_id = site_id - end - redef fun render_content do - add "" - add "var _paq = _paq || [];" - add " _paq.push([\"trackPageView\"]);" - add " _paq.push([\"enableLinkTracking\"]);" - add "(function() \{" - add " var u=((\"https:\" == document.location.protocol) ? \"https\" : \"http\") + \"://{tracker_url}\";" - add " _paq.push([\"setTrackerUrl\", u+\"piwik.php\"]);" - add " _paq.push([\"setSiteId\", \"{site_id}\"]);" - add " var d=document, g=d.createElement(\"script\"), s=d.getElementsByTagName(\"script\")[0]; g.type=\"text/javascript\";" - add " g.defer=true; g.async=true; g.src=u+\"piwik.js\"; s.parentNode.insertBefore(g,s);" - add "\})();" + var site_id = self.site_id.to_json + var tracker_url = self.tracker_url.trim + if tracker_url.chars.last != '/' then tracker_url += "/" + tracker_url = "://{tracker_url}".to_json + + addn "" + addn "var _paq = _paq || [];" + addn " _paq.push([\"trackPageView\"]);" + addn " _paq.push([\"enableLinkTracking\"]);" + addn "(function() \{" + addn " var u=((\"https:\" == document.location.protocol) ? \"https\" : \"http\") + {tracker_url};" + addn " _paq.push([\"setTrackerUrl\", u+\"piwik.php\"]);" + addn " _paq.push([\"setSiteId\", {site_id}]);" + addn " var d=document, g=d.createElement(\"script\"), s=d.getElementsByTagName(\"script\")[0]; g.type=\"text/javascript\";" + addn " g.defer=true; g.async=true; g.src=u+\"piwik.js\"; s.parentNode.insertBefore(g,s);" + addn "\})();" end end