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 # Render the DocModel pages as HTML pages.
17 # FIXME this module is all f*cked up to maintain compatibility with
18 # the original `doc_templates` and `doc_model` modules.
19 # This will change in further refactorings.
23 import doc_hierarchies
24 import doc_intros_redefs
28 redef class ToolContext
30 # File pattern used to link documentation to source code.
31 var opt_source
= new OptionString("link for source (%f for filename, " +
32 "%l for first line, %L for last line)", "--source")
34 # Directory where the CSS and JS is stored.
35 var opt_sharedir
= new OptionString("directory containing nitdoc assets", "--sharedir")
37 # Use a shareurl instead of copy shared files.
39 # This is usefull if you don't want to store the Nitdoc templates with your
41 var opt_shareurl
= new OptionString("use shareurl instead of copy shared files", "--shareurl")
43 # Use a custom title for the homepage.
44 var opt_custom_title
= new OptionString("custom title for homepage", "--custom-title")
46 # Display a custom brand or logo in the documentation top menu.
47 var opt_custom_brand
= new OptionString("custom link to external site", "--custom-brand")
49 # Display a custom introduction text before the packages overview.
50 var opt_custom_intro
= new OptionString("custom intro text for homepage", "--custom-overview-text")
51 # Display a custom footer on each documentation page.
53 # Generally used to display the documentation or product version.
54 var opt_custom_footer
= new OptionString("custom footer text", "--custom-footer-text")
58 # If you want to monitor your visitors.
59 var opt_piwik_tracker
= new OptionString("Piwik tracker URL (ex: nitlanguage.org/piwik/)", "--piwik-tracker")
61 # Piwik tracker site id.
62 var opt_piwik_site_id
= new OptionString("Piwik site ID", "--piwik-site-id")
64 # These options are not currently used in Nitdoc.
66 # FIXME redo the plugin
67 var opt_github_upstream
= new OptionString("Git branch where edited commits will be pulled into (ex: user:repo:branch)", "--github-upstream")
68 # FIXME redo the plugin
69 var opt_github_base_sha1
= new OptionString("Git sha1 of base commit used to create pull request", "--github-base-sha1")
70 # FIXME redo the plugin
71 var opt_github_gitdir
= new OptionString("Git working directory used to resolve path name (ex: /home/me/mypackage/)", "--github-gitdir")
73 # Do not produce HTML files
74 var opt_no_render
= new OptionBool("do not render HTML files", "--no-render")
79 option_context
.add_option
(
80 opt_source
, opt_sharedir
, opt_shareurl
, opt_custom_title
,
81 opt_custom_footer
, opt_custom_intro
, opt_custom_brand
,
82 opt_github_upstream
, opt_github_base_sha1
, opt_github_gitdir
,
83 opt_piwik_tracker
, opt_piwik_site_id
,
87 redef fun process_options
(args
) do
89 var upstream
= opt_github_upstream
90 var base_sha
= opt_github_base_sha1
91 var git_dir
= opt_github_gitdir
92 var opts
= [upstream
.value
, base_sha
.value
, git_dir
.value
]
93 if not opts
.has_only
(null) and opts
.has
(null) then
94 print
"Option Error: options {upstream.names.first}, " +
95 "{base_sha.names.first} and {git_dir.names.first} " +
96 "are required to enable the GitHub plugin"
102 # Render the Nitdoc as a HTML website.
103 class RenderHTMLPhase
106 # Used to sort sidebar elements by name.
107 var name_sorter
= new MEntityNameSorter
110 if ctx
.opt_no_render
.value
then return
112 for page
in doc
.pages
.values
do
113 page
.render
(self, doc
).write_to_file
("{ctx.output_dir.to_s}/{page.html_url}")
117 # Creates the output directory and imports assets files form `resources/`.
118 fun init_output_dir
do
119 # create destination dir if it's necessary
120 var output_dir
= ctx
.output_dir
121 if not output_dir
.file_exists
then output_dir
.mkdir
123 var sharedir
= ctx
.opt_sharedir
.value
124 if sharedir
== null then
125 var dir
= ctx
.nit_dir
126 sharedir
= dir
/"share/nitdoc"
127 if not sharedir
.file_exists
then
128 print
"Error: cannot locate nitdoc share files. Uses --sharedir or envvar NIT_DIR"
133 if ctx
.opt_shareurl
.value
== null then
134 sys
.system
("cp -r -- {sharedir.to_s.escape_to_sh}/* {output_dir.to_s.escape_to_sh}/")
136 sys
.system
("cp -r -- {sharedir.to_s.escape_to_sh}/resources/ {output_dir.to_s.escape_to_sh}/resources/")
141 # Returns a HTML link for a given `location`.
142 fun html_source_link
(location
: nullable Location): nullable String
144 if location
== null then return null
145 var source
= ctx
.opt_source
.value
146 if source
== null then
147 var url
= location
.file
.filename
.simplify_path
148 return "<a target='_blank' title='Show source' href=\"{url.html_escape}\
">View Source</a>"
150 # THIS IS JUST UGLY ! (but there is no replace yet)
151 var x
= source
.split_with
("%f")
152 source
= x
.join
(location
.file
.filename
.simplify_path
)
153 x
= source
.split_with
("%l")
154 source
= x
.join
(location
.line_start
.to_s
)
155 x
= source
.split_with
("%L")
156 source
= x
.join
(location
.line_end
.to_s
)
157 source
= source
.simplify_path
158 return "<a target='_blank' title='Show source' href=\"{source.to_s.html_escape}\
">View Source</a>"
164 # Render the page as a html template.
165 private fun render
(v
: RenderHTMLPhase, doc
: DocModel): Writable do
167 if v
.ctx
.opt_shareurl
.value
!= null then
168 shareurl
= v
.ctx
.opt_shareurl
.value
.as(not null)
172 self.shareurl
= shareurl
173 self.footer
= v
.ctx
.opt_custom_footer
.value
174 self.body_attrs
.add
(new TagAttribute("data-bootstrap-share", shareurl
))
183 var tracker_url
= v
.ctx
.opt_piwik_tracker
.value
184 var site_id
= v
.ctx
.opt_piwik_site_id
.value
185 if tracker_url
!= null and site_id
!= null then
186 self.scripts
.add
new TplPiwikScript(tracker_url
, site_id
)
192 # all properties below are roughly copied from `doc_pages`
194 # Build page title string
195 fun init_title
(v
: RenderHTMLPhase, doc
: DocModel) do end
197 # Build top menu template if any.
198 fun init_topmenu
(v
: RenderHTMLPhase, doc
: DocModel) do
199 topmenu
= new DocTopMenu
200 topmenu
.brand
= v
.ctx
.opt_custom_brand
.value
201 var title
= "Overview"
202 if v
.ctx
.opt_custom_title
.value
!= null then
203 title
= v
.ctx
.opt_custom_title
.value
.to_s
205 topmenu
.add_li
new ListItem(new Link("index.html", title
))
206 topmenu
.add_li
new ListItem(new Link("search.html", "Index"))
207 topmenu
.active_item
= topmenu
.items
.first
210 # Build page sidebar if any.
211 fun init_sidebar
(v
: RenderHTMLPhase, doc
: DocModel) do
212 sidebar
= new DocSideBar
213 sidebar
.boxes
.add
new DocSideBox("Summary", html_toc
)
216 # Build page content template.
217 fun init_content
(v
: RenderHTMLPhase, doc
: DocModel) do
218 root
.init_html_render
(v
, doc
, self)
222 redef class OverviewPage
223 redef var html_url
= "index.html"
225 redef fun init_title
(v
, doc
) do
227 if v
.ctx
.opt_custom_title
.value
!= null then
228 title
= v
.ctx
.opt_custom_title
.value
.to_s
233 redef class SearchPage
234 redef var html_url
= "search.html"
235 redef fun init_title
(v
, doc
) do title
= "Index"
237 redef fun init_topmenu
(v
, doc
) do
239 topmenu
.active_item
= topmenu
.items
.last
242 redef fun init_sidebar
(v
, doc
) do end
245 redef class MEntityPage
246 redef var html_url
is lazy
do
247 if mentity
isa MGroup and mentity
.mdoc
!= null then
248 return "api_{mentity.nitdoc_url}"
250 return mentity
.nitdoc_url
253 redef fun init_title
(v
, doc
) do title
= mentity
.html_name
256 # FIXME all clases below are roughly copied from `doc_pages` and adapted to new
257 # doc phases. This is to preserve the compatibility with the current
258 # `doc_templates` module.
260 redef class ReadmePage
261 redef var html_url
is lazy
do return mentity
.nitdoc_url
263 redef fun init_topmenu
(v
, doc
) do
265 var mpackage
= mentity
.mpackage
266 if not mentity
.is_root
then
267 topmenu
.add_li
new ListItem(new Link(mpackage
.nitdoc_url
, mpackage
.html_name
))
269 topmenu
.add_li
new ListItem(new Link(html_url
, mpackage
.html_name
))
270 topmenu
.active_item
= topmenu
.items
.last
273 redef fun init_sidebar
(v
, doc
) do
275 var api_lnk
= """<a href="api_{{{mentity.nitdoc_url}}}">Go to API</a>"""
276 sidebar
.boxes
.unshift
new DocSideBox(api_lnk
, "")
280 redef class MGroupPage
281 redef fun init_topmenu
(v
, doc
) do
283 var mpackage
= mentity
.mpackage
284 if not mentity
.is_root
then
285 topmenu
.add_li
new ListItem(new Link(mpackage
.nitdoc_url
, mpackage
.html_name
))
287 topmenu
.add_li
new ListItem(new Link(html_url
, mpackage
.html_name
))
288 topmenu
.active_item
= topmenu
.items
.last
291 redef fun init_sidebar
(v
, doc
) do
294 if mentity
.mdoc
!= null then
295 var doc_lnk
= """<a href="{{{mentity.nitdoc_url}}}">Go to README</a>"""
296 sidebar
.boxes
.unshift
new DocSideBox(doc_lnk
, "")
299 var mclasses
= new HashSet[MClass]
300 mclasses
.add_all intros
301 mclasses
.add_all redefs
302 if mclasses
.is_empty
then return
303 var list
= new UnorderedList
304 list
.css_classes
.add
"list-unstyled list-labeled"
305 var sorted
= mclasses
.to_a
306 v
.name_sorter
.sort
(sorted
)
307 for mclass
in sorted
do
308 list
.add_li tpl_sidebar_item
(mclass
)
310 sidebar
.boxes
.add
new DocSideBox("All classes", list
)
311 sidebar
.boxes
.last
.is_open
= false
314 private fun tpl_sidebar_item
(def
: MClass): ListItem do
315 var classes
= def
.intro
.css_classes
316 if intros
.has
(def
) then
321 var lnk
= new Template
322 lnk
.add
new DocHTMLLabel.with_classes
(classes
)
323 lnk
.add def
.html_link
324 return new ListItem(lnk
)
328 redef class MModulePage
329 redef fun init_topmenu
(v
, doc
) do
331 var mpackage
= mentity
.mpackage
332 topmenu
.add_li
new ListItem(new Link(mpackage
.nitdoc_url
, mpackage
.html_name
))
333 topmenu
.add_li
new ListItem(new Link(mentity
.nitdoc_url
, mentity
.html_name
))
334 topmenu
.active_item
= topmenu
.items
.last
337 # Class list to display in sidebar
338 redef fun init_sidebar
(v
, doc
) do
341 var mclasses
= new HashSet[MClass]
342 mclasses
.add_all mentity
.collect_intro_mclasses
(v
.ctx
.min_visibility
)
343 mclasses
.add_all mentity
.collect_redef_mclasses
(v
.ctx
.min_visibility
)
344 if mclasses
.is_empty
then return
345 var list
= new UnorderedList
346 list
.css_classes
.add
"list-unstyled list-labeled"
348 var sorted
= mclasses
.to_a
349 v
.name_sorter
.sort
(sorted
)
350 for mclass
in sorted
do
351 list
.add_li tpl_sidebar_item
(mclass
)
353 sidebar
.boxes
.add
new DocSideBox("All classes", list
)
354 sidebar
.boxes
.last
.is_open
= false
357 private fun tpl_sidebar_item
(def
: MClass): ListItem do
358 var classes
= def
.intro
.css_classes
359 if def
.intro_mmodule
== self.mentity
then
364 var lnk
= new Template
365 lnk
.add
new DocHTMLLabel.with_classes
(classes
)
366 lnk
.add def
.html_link
367 return new ListItem(lnk
)
371 redef class MClassPage
373 redef fun init_topmenu
(v
, doc
) do
375 var mpackage
= mentity
.intro_mmodule
.mgroup
.mpackage
376 topmenu
.add_li
new ListItem(new Link(mpackage
.nitdoc_url
, mpackage
.html_name
))
377 topmenu
.add_li
new ListItem(new Link(html_url
, mentity
.html_name
))
378 topmenu
.active_item
= topmenu
.items
.last
381 redef fun init_sidebar
(v
, doc
) do
383 var by_kind
= new PropertiesByKind.with_elements
(mclass_inherited_mprops
(v
, doc
))
384 var summary
= new UnorderedList
385 summary
.css_classes
.add
"list-unstyled"
387 by_kind
.sort_groups
(v
.name_sorter
)
388 for g
in by_kind
.groups
do tpl_sidebar_list
(g
, summary
)
389 sidebar
.boxes
.add
new DocSideBox("All properties", summary
)
390 sidebar
.boxes
.last
.is_open
= false
393 private fun tpl_sidebar_list
(mprops
: PropertyGroup[MProperty], summary
: UnorderedList) do
394 if mprops
.is_empty
then return
395 var list
= new UnorderedList
396 list
.css_classes
.add
"list-unstyled list-labeled"
397 for mprop
in mprops
do
398 list
.add_li tpl_sidebar_item
(mprop
)
400 var content
= new Template
401 content
.add mprops
.title
403 var li
= new ListItem(content
)
407 private fun tpl_sidebar_item
(mprop
: MProperty): ListItem do
408 var classes
= mprop
.intro
.css_classes
409 if not mprop_is_local
(mprop
) then
410 classes
.add
"inherit"
411 var cls_url
= mprop
.intro
.mclassdef
.mclass
.nitdoc_url
412 var def_url
= "{cls_url}#{mprop.nitdoc_id}.definition"
413 var lnk
= new Link(def_url
, mprop
.html_name
)
414 var mdoc
= mprop
.intro
.mdoc_or_fallback
415 if mdoc
!= null then lnk
.title
= mdoc
.synopsis
416 var item
= new Template
417 item
.add
new DocHTMLLabel.with_classes
(classes
)
419 return new ListItem(item
)
421 if mpropdefs
.has
(mprop
.intro
) then
426 var def
= select_mpropdef
(mprop
)
427 var anc
= def
.html_link_to_anchor
428 anc
.href
= "#{def.nitdoc_id}.definition"
429 var lnk
= new Template
430 lnk
.add
new DocHTMLLabel.with_classes
(classes
)
432 return new ListItem(lnk
)
435 # Get the mpropdef contained in `self` page for a mprop.
437 # FIXME this method is used to translate a mprop into a mpropdefs for
438 # section linking. A better page structure should avoid this...
439 private fun select_mpropdef
(mprop
: MProperty): MPropDef do
440 for mclassdef
in mentity
.mclassdefs
do
441 for mpropdef
in mclassdef
.mpropdefs
do
442 if mpropdef
.mproperty
== mprop
then return mpropdef
445 abort # FIXME is there a case where the prop is not found?
448 private fun mclass_inherited_mprops
(v
: RenderHTMLPhase, doc
: DocModel): Set[MProperty] do
449 var res
= new HashSet[MProperty]
450 var local
= mentity
.collect_local_mproperties
(v
.ctx
.min_visibility
)
451 for mprop
in mentity
.collect_inherited_mproperties
(v
.ctx
.min_visibility
) do
452 if local
.has
(mprop
) then continue
453 #if mprop isa MMethod and mprop.is_init then continue
454 if mprop
.intro
.mclassdef
.mclass
.name
== "Object" and
455 (mprop
.visibility
== protected_visibility
or
456 mprop
.intro
.mclassdef
.mmodule
.name
!= "kernel") then continue
463 private fun mprop_is_local
(mprop
: MProperty): Bool do
464 for mpropdef
in mprop
.mpropdefs
do
465 if self.mpropdefs
.has
(mpropdef
) then return true
471 redef class MPropertyPage
472 redef fun init_title
(v
, doc
) do
473 title
= "{mentity.html_name}{mentity.html_short_signature.write_to_string}"
476 redef fun init_topmenu
(v
, doc
) do
478 var mmodule
= mentity
.intro_mclassdef
.mmodule
479 var mpackage
= mmodule
.mgroup
.mpackage
480 var mclass
= mentity
.intro_mclassdef
.mclass
481 topmenu
.add_li
new ListItem(new Link(mpackage
.nitdoc_url
, mpackage
.html_name
))
482 topmenu
.add_li
new ListItem(new Link(mclass
.nitdoc_url
, mclass
.html_name
))
483 topmenu
.add_li
new ListItem(new Link(html_url
, mentity
.html_name
))
484 topmenu
.active_item
= topmenu
.items
.last
488 redef class DocComposite
489 # Prepares the HTML rendering for this element.
491 # This visit is mainly used to set template attributes before rendering.
492 fun init_html_render
(v
: RenderHTMLPhase, doc
: DocModel, page
: DocPage) do
493 for child
in children
do child
.init_html_render
(v
, doc
, page
)
497 # FIXME hideous hacks to avoid diff
498 redef class MEntitySection
499 redef fun init_html_render
(v
, doc
, page
) do
500 if not page
isa MEntityPage then return
501 var mentity
= self.mentity
502 if mentity
isa MGroup and mentity
.is_root
then
503 html_title
= mentity
.mpackage
.html_name
504 html_subtitle
= mentity
.mpackage
.html_declaration
505 else if mentity
isa MProperty then
506 var title
= new Template
507 title
.add mentity
.html_name
508 title
.add mentity
.html_signature
510 html_subtitle
= mentity
.html_namespace
511 html_toc_title
= mentity
.html_name
517 # FIXME hideous hacks to avoid diff
518 redef class ConcernSection
519 redef fun init_html_render
(v
, doc
, page
) do
520 if not page
isa MEntityPage then return
521 var mentity
= self.mentity
522 if page
isa MGroupPage then
524 html_toc_title
= mentity
.html_name
525 is_toc_hidden
= false
526 else if page
.mentity
isa MModule and mentity
isa MModule then
527 var title
= new Template
528 if mentity
== page
.mentity
then
530 html_toc_title
= "in {mentity.html_name}"
533 html_toc_title
= "from {mentity.html_name}"
535 title
.add mentity
.html_namespace
537 else if (page
.mentity
isa MClass and mentity
isa MModule) or
538 (page
.mentity
isa MProperty and mentity
isa MModule) then
539 var title
= new Template
541 title
.add mentity
.html_namespace
543 html_toc_title
= "in {mentity.html_name}"
550 redef class IntroArticle
551 redef fun init_html_render
(v
, doc
, page
) do
552 var mentity
= self.mentity
553 if mentity
isa MModule then
554 html_source_link
= v
.html_source_link
(mentity
.location
)
555 else if mentity
isa MClassDef then
556 html_source_link
= v
.html_source_link
(mentity
.location
)
557 else if mentity
isa MPropDef then
558 html_source_link
= v
.html_source_link
(mentity
.location
)
563 # FIXME less hideous hacks...
564 redef class DefinitionArticle
565 redef fun init_html_render
(v
, doc
, page
) do
566 var mentity
= self.mentity
567 if mentity
isa MPackage or mentity
isa MModule then
568 var title
= new Template
569 title
.add mentity
.html_icon
570 title
.add mentity
.html_namespace
572 html_toc_title
= mentity
.html_name
573 if mentity
isa MModule then
574 html_source_link
= v
.html_source_link
(mentity
.location
)
576 else if mentity
isa MClassDef then
577 var title
= new Template
579 title
.add mentity
.mmodule
.html_namespace
580 html_title
= mentity
.html_declaration
581 html_subtitle
= title
582 html_toc_title
= "in {mentity.html_name}"
583 html_source_link
= v
.html_source_link
(mentity
.location
)
584 if page
isa MEntityPage and mentity
.is_intro
and mentity
.mmodule
!= page
.mentity
then
585 is_short_comment
= true
587 if page
isa MModulePage then is_toc_hidden
= true
588 else if mentity
isa MPropDef then
589 if page
isa MClassPage then
590 var title
= new Template
591 title
.add mentity
.html_icon
592 title
.add mentity
.html_declaration
594 html_subtitle
= mentity
.html_namespace
595 html_toc_title
= mentity
.html_name
597 var title
= new Template
599 title
.add mentity
.mclassdef
.html_link
601 html_toc_title
= "in {mentity.mclassdef.html_name}"
603 html_source_link
= v
.html_source_link
(mentity
.location
)
605 if page
isa MGroupPage and mentity
isa MModule then
612 redef class HomeArticle
613 redef fun init_html_render
(v
, doc
, page
) do
614 if v
.ctx
.opt_custom_title
.value
!= null then
615 self.html_title
= v
.ctx
.opt_custom_title
.value
.to_s
616 self.html_toc_title
= v
.ctx
.opt_custom_title
.value
.to_s
618 self.content
= v
.ctx
.opt_custom_intro
.value
623 redef class GraphArticle
624 redef fun init_html_render
(v
, doc
, page
) do
625 var path
= v
.ctx
.output_dir
/ graph_id
626 var file
= new FileWriter.open
("{path}.dot")
629 var proc
= new ProcessReader("dot", "-Tsvg", "-Tcmapx", "{path}.dot")
632 while not proc
.eof
do
634 if i
< 6 then continue # skip dot default header
635 svg
.append proc
.read_line
638 self.svg
= svg
.write_to_string