1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Introduces templates that compose the documentation HTML rendering.
19 import html
::bootstrap
20 import doc_phases
::doc_structure
21 import doc_phases
::doc_hierarchies
22 import doc_phases
::doc_graphs
23 import doc_phases
::doc_intros_redefs
24 import doc_phases
::doc_lin
26 # Renders the page as HTML.
31 var html_url
: String is writable, noinit
33 # Directory where css, js and other assets can be found.
34 var shareurl
: String is writable, noinit
36 # Attributes of the body tag element.
37 var body_attrs
= new Array[TagAttribute]
39 # Top menu template if any.
40 var topmenu
: DocTopMenu is writable, noinit
42 # Sidebar template if any.
43 var sidebar
: nullable DocSideBar = null is writable
45 # Footer content if any.
46 var footer
: nullable Writable = null is writable
48 # JS scripts to append at the end of the body
49 var scripts
= new Array[TplScript]
51 # Renders the html `<head>`.
52 private fun render_head
do
53 var css
= (self.shareurl
/ "css").html_escape
54 var vendors
= (self.shareurl
/ "vendors").html_escape
56 addn
"<!DOCTYPE html>"
58 addn
" <meta charset='utf-8'/>"
59 addn
" <!--link rel='stylesheet' href='{css}/Nitdoc.UI.css' type='text/css'/-->"
60 addn
" <link rel='stylesheet' href='{vendors}/bootstrap/css/bootstrap.min.css'/>"
61 addn
" <link rel='stylesheet' href='{css}/nitdoc.bootstrap.css'/>"
62 addn
" <link rel='stylesheet' href='{css}/nitdoc.css'/>"
63 addn
" <link rel='stylesheet' href='{css}/Nitdoc.QuickSearch.css'/>"
64 addn
" <link rel='stylesheet' href='{css}/Nitdoc.ModalBox.css'/>"
65 addn
" <link rel='stylesheet' href='{css}/Nitdoc.GitHub.css'/>"
66 addn
" <title>{title.html_escape}</title>"
69 for attr
in body_attrs
do add attr
73 # Renders the footer and content.
74 private fun render_content
do
76 if footer
!= null then
77 addn
"<div class='well footer'>"
78 add footer
.as(not null)
84 private fun render_footer
do
85 var vendors
= (self.shareurl
/ "vendors").html_escape
86 var js
= (self.shareurl
/ "js").html_escape
88 addn
"<script src='{vendors}/jquery/jquery-1.11.1.min.js'></script>"
89 addn
"<script src='{vendors}/jquery/jquery-ui-1.10.4.custom.min.js'></script>"
90 addn
"<script src='{vendors}/bootstrap/js/bootstrap.min.js'></script>"
91 addn
"<script data-main='{js}/nitdoc' src='{js}/lib/require.js'></script>"
92 for script
in scripts
do add script
95 $("[data-toggle='tooltip']").tooltip();
96 $("[data-toggle='popover']").popover();
103 # Render the whole page
104 redef fun rendering
do
106 addn
"<div class='container-fluid'>"
107 addn
" <div class='row'>"
110 addn
" <div class='row' id='content'>"
111 var sidebar
= self.sidebar
112 if sidebar
!= null then
113 addn
"<div class='col col-xs-3 col-lg-2'>"
116 addn
"<div class='col col-xs-9 col-lg-10' data-spy='scroll' data-target='.summary'>"
120 addn
"<div class='col col-xs-12'>"
129 # Render table of content for this page.
130 fun html_toc
: UnorderedList do
131 var lst
= new UnorderedList
132 lst
.css_classes
.add
"nav"
133 for child
in root
.children
do
134 child
.render_toc_item
(lst
)
140 # Top menu bar template.
142 # FIXME should be a Bootstrap component template
143 # At this moment, the topmenu structure stills to specific to Nitdoc to use the
148 # Brand link to display in first position of the top menu.
150 # This is where you want to put your logo.
151 var brand
: nullable Writable is noinit
, writable
155 # Depends on the current page, this allows to hilighted the current item.
157 # FIXME should be using Boostrap breadcrumbs component.
158 # This will still like this to avoid diff and be changed in further fixes
159 # when we will modify the output.
160 var active_item
: nullable ListItem is noinit
, writable
162 redef fun rendering
do
163 addn
"<nav id='topmenu' class='navbar navbar-default navbar-fixed-top' role='navigation'>"
164 addn
" <div class='container-fluid'>"
165 addn
" <div class='navbar-header'>"
166 add
" <button type='button' class='navbar-toggle' "
167 addn
" data-toggle='collapse' data-target='#topmenu-collapse'>"
168 addn
" <span class='sr-only'>Toggle menu</span>"
169 addn
" <span class='icon-bar'></span>"
170 addn
" <span class='icon-bar'></span>"
171 addn
" <span class='icon-bar'></span>"
173 if brand
!= null then
174 add
"<span class='navbar-brand'>"
175 add brand
.write_to_string
179 addn
" <div class='collapse navbar-collapse' id='topmenu-collapse'>"
180 addn
" <ul class='nav navbar-nav'>"
182 if item
== active_item
then item
.css_classes
.add
"active"
183 add item
.write_to_string
192 # Nitdoc sidebar template.
196 # Sidebar contains `DocSideBox`.
197 var boxes
= new Array[DocSideBox]
199 redef fun rendering
do
200 if boxes
.is_empty
then return
201 addn
"<div id='sidebar'>"
202 for box
in boxes
do add box
207 # Something that can be put in a DocSideBar.
211 # Box HTML id, used for Bootstrap collapsing feature.
213 # Use `html_title.to_cmangle` by default.
214 var id
: String is lazy
do return title
.write_to_string
.to_cmangle
216 # Title of the box to display.
219 # Content to display in the box.
220 var content
: Writable
222 # Is the box opened by default?
224 # Otherwise, the user will have to clic on the title to display the content.
227 var is_open
= true is writable
229 redef fun rendering
do
231 if is_open
then open
= "in"
232 addn
"<div class='panel'>"
233 addn
" <div class='panel-heading'>"
234 add
" <a data-toggle='collapse' data-parent='#sidebar'"
235 add
" data-target='#box_{id}' href='#'>"
239 addn
" <div id='box_{id}' class='summary panel-body collapse {open}'>"
246 redef class DocComposite
250 var html_id
: String is writable, lazy
do return id
252 # Title to display if any.
254 # This title can be decorated with HTML.
255 var html_title
: nullable Writable is writable, lazy
do return title
257 # Subtitle to display if any.
258 var html_subtitle
: nullable Writable is noinit
, writable
260 # Render the element title and subtitle.
261 private fun render_title
do
262 if html_title
!= null then
263 var header
= new Header(hlvl
, html_title
.write_to_string
)
264 header
.css_classes
.add
"signature"
265 if hlvl
== 2 then header
.css_classes
.add
"well well-sm"
268 if html_subtitle
!= null then
269 addn
"<div class='info subtitle'>"
270 addn html_subtitle
.write_to_string
275 # Render the element body.
276 private fun render_body
do
277 for child
in children
do addn child
.write_to_string
280 redef fun rendering
do
281 if is_hidden
then return
286 # Level <hX> for HTML heading.
287 private fun hlvl
: Int do return depth
289 # A short, undecorated title that goes in the table of contents.
291 # By default, returns `html_title.to_s`, subclasses should redefine it.
292 var html_toc_title
: nullable String is lazy
, writable do
293 if html_title
== null then return toc_title
294 return html_title
.write_to_string
297 # Is `self` hidden in the table of content?
298 var is_toc_hidden
= false is writable
300 # Render this element in a table of contents.
301 private fun render_toc_item
(lst
: UnorderedList) do
302 if is_toc_hidden
then return
304 var content
= new Template
305 content
.add
new Link("#{html_id}", html_toc_title
.to_s
)
306 if not children
.is_empty
then
307 var sublst
= new UnorderedList
308 sublst
.css_classes
.add
"nav"
309 for child
in children
do
310 child
.render_toc_item
(sublst
)
314 lst
.add_li
new ListItem(content
)
317 # ID used in HTML tab labels.
319 # We sanitize it for Boostrap JS panels that do not like ":" and "." in ids.
320 var html_tab_id
: String is lazy
do
321 var id
= html_id
.replace
(":", "")
322 id
= id
.replace
(".", "")
328 redef fun rendering
do
329 for child
in children
do addn child
.write_to_string
333 redef class DocSection
336 redef fun rendering
do
338 addn
"<a id=\"{html_id}\
"></a>"
341 addn
"<section{render_css_classes} id=\"{html_id}\
">"
348 redef class DocArticle
351 redef fun rendering
do
352 if is_hidden
then return
353 addn
"<article{render_css_classes} id=\"{html_id}\
">"
360 redef class TabbedGroup
361 redef fun render_body
do
362 var tabs
= new DocTabs("{html_id}.tabs", "")
363 for child
in children
do
364 if child
.is_hidden
then continue
365 var title
= child
.html_toc_title
or else child
.toc_title
or else ""
366 tabs
.add_panel
new DocTabPanel(child
.html_tab_id
, title
, child
)
372 redef class PanelGroup
373 redef var html_title
= null
374 redef var toc_title
is lazy
do return title
or else ""
375 redef var is_toc_hidden
= true
378 redef class HomeArticle
379 redef var html_title
= "Overview"
381 # HTML content to display on the home page.
383 # This attribute is set by the `doc_render` phase who knows the context.
384 var content
: nullable String is noinit
, writable
386 redef fun render_body
do
387 var content
= self.content
388 if content
!= null then add content
393 redef class IndexArticle
394 redef var html_title
= "Index"
396 redef fun render_body
do
397 addn
"<div class='container-fluid'>"
398 addn
" <div class='row'>"
399 render_list
("Modules", mmodules
)
400 render_list
("Classes", mclasses
)
401 render_list
("Properties", mprops
)
406 # Displays a list from the content of `mentities`.
407 private fun render_list
(title
: String, mentities
: Array[MEntity]) do
408 if mentities
.is_empty
then return
409 addn
"<div class='col-xs-4'>"
410 addn
new Header(3, title
)
411 var lst
= new UnorderedList
412 for mentity
in mentities
do
413 if mentity
isa MProperty then
414 var tpl
= new Template
415 tpl
.add mentity
.intro
.html_link
417 tpl
.add mentity
.intro
.mclassdef
.mclass
.html_link
419 lst
.add_li
new ListItem(tpl
)
421 lst
.add_li
new ListItem(mentity
.html_link
)
429 redef class ProjectsSection
430 redef var html_title
= "Projects"
433 redef class MEntityComposite
434 redef var html_title
is lazy
do return mentity
.nitdoc_name
437 redef class MEntitySection
438 redef var html_title
is lazy
do return mentity
.html_name
439 redef var html_subtitle
is lazy
do return mentity
.html_declaration
442 redef class ConstructorsSection
443 redef var html_title
= "Constructors"
444 redef var html_subtitle
= null
445 redef fun is_toc_hidden
do return is_hidden
448 redef class ConcernSection
449 redef var html_title
is lazy
do return "in {mentity.nitdoc_name}"
450 redef fun is_toc_hidden
do return is_hidden
453 redef class ImportationListSection
454 redef var html_title
is lazy
do return "Dependencies"
457 redef class InheritanceListSection
458 redef var html_title
is lazy
do return "Inheritance"
461 redef class IntroArticle
462 redef var html_title
= null
463 redef var is_toc_hidden
= true
465 # Link to source to display if any.
466 var html_source_link
: nullable Writable is noinit
, writable
468 redef fun render_body
do
469 var tabs
= new DocTabs("{html_id}.tabs", "")
470 var comment
= mentity
.html_comment
471 if comment
!= null then
472 tabs
.add_panel
new DocTabPanel("{html_tab_id}-comment", "Comment", comment
)
474 for child
in children
do
475 if child
.is_hidden
then continue
476 var title
= child
.html_toc_title
or else child
.toc_title
or else ""
477 tabs
.add_panel
new DocTabPanel(child
.html_tab_id
, title
, child
)
479 var lnk
= html_source_link
481 tabs
.drop_list
.items
.add
new ListItem(lnk
)
487 redef class ConcernsArticle
488 redef var html_title
= "Concerns"
489 redef fun render_body
do add concerns
.html_list
492 redef class DefinitionListArticle
493 redef var html_title
is lazy
do
494 var title
= new Template
495 title
.add mentity
.html_icon
496 title
.add mentity
.html_link
500 redef var html_subtitle
is lazy
do return mentity
.html_namespace
501 redef var html_toc_title
is lazy
do return mentity
.html_name
504 redef class DefinitionArticle
505 redef var html_title
is lazy
do return mentity
.html_name
506 redef var html_subtitle
is lazy
do return mentity
.html_declaration
508 # Does `self` display only it's title and no body?
511 var is_no_body
: Bool = false is writable
513 # Does `self` display only the short content as definition?
516 var is_short_comment
: Bool = false is writable
518 # Link to source to display if any.
519 var html_source_link
: nullable Writable is noinit
, writable
521 redef fun render_body
do
522 var tabs
= new DocTabs("{html_id}.tabs", "")
523 if not is_no_body
then
525 if is_short_comment
then
526 comment
= mentity
.html_short_comment
528 comment
= mentity
.html_comment
530 if comment
!= null then
531 tabs
.add_panel
new DocTabPanel("{html_tab_id}-comment", "Comment", comment
)
534 for child
in children
do
535 if child
.is_hidden
then continue
536 var title
= child
.html_toc_title
or else child
.toc_title
or else ""
537 tabs
.add_panel
new DocTabPanel(child
.html_tab_id
, title
, child
)
539 var lnk
= html_source_link
541 tabs
.drop_list
.items
.add
new ListItem(lnk
)
547 redef class HierarchyListArticle
548 redef var is_toc_hidden
= true
550 redef fun render_body
do
551 var lst
= new UnorderedList
552 lst
.css_classes
.add
"list-unstyled list-definition"
553 for mentity
in mentities
do
554 lst
.add_li mentity
.html_list_item
560 redef class IntrosRedefsSection
561 redef var toc_title
do return "Intros / Redefs"
562 redef var html_title
= null
563 redef var html_subtitle
= null
564 redef var is_toc_hidden
= true
567 redef class IntrosRedefsListArticle
568 redef var is_toc_hidden
= true
570 redef fun render_body
do
571 var lst
= new UnorderedList
572 lst
.css_classes
.add
"list-unstyled list-labeled"
573 for mentity
in mentities
do
574 lst
.add_li mentity
.html_list_item
580 redef class DefinitionLinArticle
581 redef var html_title
is lazy
do return "Linearization"
582 redef var is_toc_hidden
= true
584 redef fun render_body
do
585 var lst
= new UnorderedList
586 lst
.css_classes
.add
"list-unstyled list-labeled"
587 for mentity
in mentities
do
588 if not mentity
isa MPropDef then continue # TODO handle all mentities
589 var tpl
= new Template
590 tpl
.add mentity
.mclassdef
.html_namespace
591 var comment
= mentity
.mclassdef
.html_short_comment
592 if comment
!= null then
596 var li
= new ListItem(tpl
)
597 li
.css_classes
.add
"signature"
604 redef class GraphArticle
605 redef var html_title
= null
606 redef var is_toc_hidden
= true
608 # HTML map used to display link.
610 # This attribute is set by the `doc_render` phase who knows the context.
611 var map
: String is noinit
, writable
613 redef fun render_body
do
614 addn
"<div class=\"text-center\
">"
615 addn
" <img src='{graph_id}.png' usemap='#{graph_id}' style='margin:auto'"
616 addn
" alt='{title or else ""}'/>"