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 # Basic catalog generator for Nit packages
17 # See: <http://nitlanguage.org/catalog/>
19 # The tool scans packages and generates the HTML files of a catalog.
21 # See `catalog` for details
24 import loader
# Scan&load packages, groups and modules
25 import doc
::doc_down
# Display mdoc
28 # A HTML page in a catalog
30 # This is just a template with the header pre-filled and the footer injected at rendering.
31 # Therefore, once instantiated, the content can just be added to it.
35 # The associated catalog, used to groups options and other global data
38 # Placeholder to include additional things before the `</head>`.
39 var more_head
= new Template
41 # Relative path to the root directory (with the index file).
43 # Use "" for pages in the root directory
44 # Use ".." for pages in a subdirectory
53 <meta charset="utf-8">
54 <link rel="stylesheet" media="all" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
55 <link rel="stylesheet" media="all" href="{{{rootpath / "style.css"}}}">
62 <div class='container-fluid'>
64 <nav id='topmenu' class='navbar navbar-default navbar-fixed-top' role='navigation'>
65 <div class='container-fluid'>
66 <div class='navbar-header'>
67 <button type='button' class='navbar-toggle' data-toggle='collapse' data-target='#topmenu-collapse'>
68 <span class='sr-only'>Toggle menu</span>
69 <span class='icon-bar'></span>
70 <span class='icon-bar'></span>
71 <span class='icon-bar'></span>
73 <span class='navbar-brand'><a href="http://nitlanguage.org/">Nitlanguage.org</a></span>
75 <div class='collapse navbar-collapse' id='topmenu-collapse'>
76 <ul class='nav navbar-nav'>
77 <li><a href="{{{rootpath / "index.html"}}}">Catalog</a></li>
86 # Inject piwik HTML code if required
89 var tracker_url
= catalog
.piwik_tracker
90 if tracker_url
== null then return
92 var site_id
= catalog
.piwik_site_id
94 tracker_url
= tracker_url
.trim
95 if tracker_url
.chars
.last
!= '/' then tracker_url
+= "/"
98 <script type="text/javascript">
99 var _paq = _paq || [];
100 _paq.push(['trackPageView']);
101 _paq.push(['enableLinkTracking']);
103 var u=(("https:" == document.location.protocol) ? "https" : "http") + "://{{{tracker_url.escape_to_c}}}";
104 _paq.push(['setTrackerUrl', u+'piwik.php']);
105 _paq.push(['setSiteId', {{{site_id}}}]);
106 var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
107 g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
111 <noscript><p><img src="http://{{{tracker_url.html_escape}}}piwik.php?idsite={{{site_id}}}" style="border:0;" alt="" /></p></noscript>
112 <!-- End Piwik Code -->
120 </div> <!-- container-fluid -->
121 <script src='https://code.jquery.com/jquery-latest.min.js'></script>
122 <script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js'></script>
123 <script src='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table-all.min.js'></script>
134 redef class NitdocDecorator
135 redef fun add_image
(v
, link
, name
, comment
)
137 # Keep absolute links as is
138 if link
.has_prefix
("http://") or link
.has_prefix
("https://") then
144 # Get the directory of the doc object to deal with the relative link
145 var mdoc
= current_mdoc
146 if mdoc
== null then break
147 var source
= mdoc
.location
.file
148 if source
== null then break
149 var path
= source
.filename
150 var stat
= path
.file_stat
151 if stat
== null then break
152 if not stat
.is_dir
then path
= path
.dirname
154 # Get the full path to the local resource
155 var fulllink
= path
/ link
.to_s
156 stat
= fulllink
.file_stat
157 if stat
== null then break
159 # Get a collision-free catalog name for the resource
160 var hash
= fulllink
.md5
161 var ext
= fulllink
.file_extension
162 if ext
!= null then hash
= hash
+ "." + ext
164 # Copy the local resource in the resource directory of the catalog
165 var res
= catalog
.outdir
/ "res" / hash
166 fulllink
.file_copy_to
(res
)
168 # Hijack the link in the html.
169 link
= ".." / "res" / hash
170 super(v
, link
, name
, comment
)
175 catalog
.modelbuilder
.toolcontext
.error
(current_mdoc
.location
, "Error: cannot find local image `{link}`")
179 # The registered catalog
181 # It is used to deal with relative links in images.
182 var catalog
: Catalog is noautoinit
188 # Register `self` to the global NitdocDecorator
189 # FIXME this is ugly. But no better idea at the moment.
190 modelbuilder
.model
.nitdoc_md_processor
.decorator
.as(NitdocDecorator).catalog
= self
193 # The output directory where to generate pages
194 var outdir
: String is noautoinit
196 # Return a empty `CatalogPage`.
197 fun new_page
(rootpath
: String): CatalogPage
199 return new CatalogPage(self, rootpath
)
202 # Recursively generate a level in the file tree of the *content* section
203 private fun gen_content_level
(ot
: OrderedTree[MConcern], os
: Array[Object], res
: Template)
211 if mdoc
!= null then d
= ": {mdoc.html_synopsis.write_to_string}"
212 res
.add
"<strong>{o.name}</strong>{d} ({o.filepath.to_s})"
213 else if o
isa MModule then
216 if mdoc
!= null then d
= ": {mdoc.html_synopsis.write_to_string}"
217 res
.add
"<strong>{o.name}</strong>{d} ({o.filepath.to_s})"
221 var subs
= ot
.sub
.get_or_null
(o
)
222 if subs
!= null then gen_content_level
(ot
, subs
, res
)
228 # Generate a full HTML page for a package
229 fun generate_page
(mpackage
: MPackage): Writable
231 var res
= new_page
("..")
232 var name
= mpackage
.name
.html_escape
233 res
.more_head
.add
"""<title>{{{name}}}</title>"""
235 res
.add
"""<div class="content">"""
237 var mdoc
= mpackage
.mdoc_or_fallback
239 res
.add
"""<h1 class="package-name">{{{name}}}</h1>"""
242 <div style="float: left">
243 <h1 class="package-name">{{{name}}} - </h1>
246 res
.add mdoc
.html_documentation
249 res
.add
"<h2>Content</h2>"
250 var ot
= new OrderedTree[MConcern]
251 for g
in mpackage
.mgroups
do
253 if g
.is_interesting
then
257 for mp
in g
.mmodules
do
261 ot
.sort_with
(alpha_comparator
)
262 gen_content_level
(ot
, ot
.roots
, res
)
267 <div class="sidebar">
270 var tryit
= mpackage
.metadata
.metadata
("upstream.tryit")
271 if tryit
!= null then
272 var e
= tryit
.html_escape
273 res
.add
"<li><a href=\"{e}\
">Try<span style=\"color
:white\
">n</span>it!</a></li>\n"
275 var apk
= mpackage
.metadata
.metadata
("upstream.apk")
277 var e
= apk
.html_escape
278 res
.add
"<li><a href=\"{e}\
">Android apk</a></li>\n"
281 res
.add
"""</ul>\n<ul class="box">\n"""
283 var homepage
= mpackage
.metadata
.metadata
("upstream.homepage")
284 if homepage
!= null then
285 var e
= homepage
.html_escape
286 res
.add
"<li><a href=\"{e}\
">{e}</a></li>\n"
288 for maintainer
in mpackage
.metadata
.maintainers
do
289 res
.add
"<li>{maintainer.to_html}</li>"
291 var license
= mpackage
.metadata
.metadata
("package.license")
292 if license
!= null then
293 var e
= license
.html_escape
294 res
.add
"<li><a href=\"http
://opensource
.org
/licenses
/{e}\
">{e}</a> license</li>\n"
298 res
.add
"<h3>Source Code</h3>\n<ul class=\"box\
">\n"
299 var browse
= mpackage
.metadata
.metadata
("upstream.browse")
300 if browse
!= null then
301 var e
= browse
.html_escape
302 res
.add
"<li><a href=\"{e}\
">{e}</a></li>\n"
304 var git
= mpackage
.metadata
.metadata
("upstream.git")
306 var e
= git
.html_escape
307 res
.add
"<li><tt>{e}</tt></li>\n"
309 var last_date
= mpackage
.metadata
.last_date
310 if last_date
!= null then
311 var e
= last_date
.html_escape
312 res
.add
"<li>most recent commit: {e}</li>\n"
314 var first_date
= mpackage
.metadata
.first_date
315 if first_date
!= null then
316 var e
= first_date
.html_escape
317 res
.add
"<li>oldest commit: {e}</li>\n"
319 var commits
= commits
[mpackage
]
321 res
.add
"<li>{commits} commits</li>\n"
325 res
.add
"<h3>Quality</h3>\n<ul class=\"box\
">\n"
326 var errors
= errors
[mpackage
]
328 res
.add
"<li>{errors} errors</li>\n"
330 res
.add
"<li>{warnings[mpackage]} warnings ({warnings_per_kloc[mpackage]}/kloc)</li>\n"
331 res
.add
"<li>{documentation_score[mpackage]}% documented</li>\n"
334 res
.add
"<h3>Tags</h3>\n"
335 var ts2
= new Array[String]
336 for t
in mpackage
.metadata
.tags
do
338 ts2
.add
"<a href=\"../index
.html
#tag_{t}\">{t}</a>"
340 res
.add_list
(ts2
, ", ", ", ")
342 if deps
.has
(mpackage
) then
343 var reqs
= deps
[mpackage
].greaters
.to_a
344 reqs
.remove
(mpackage
)
345 alpha_comparator
.sort
(reqs
)
346 res
.add
"<h3>Requirements</h3>\n"
347 if reqs
.is_empty
then
350 var list
= new Array[String]
352 var direct
= deps
.has_direct_edge
(mpackage
, r
)
353 var s
= "<a href=\"{r}.html\
">"
354 if direct
then s
+= "<strong>"
356 if direct
then s
+= "</strong>"
360 res
.add_list
(list
, ", ", " and ")
363 reqs
= deps
[mpackage
].smallers
.to_a
364 reqs
.remove
(mpackage
)
365 alpha_comparator
.sort
(reqs
)
366 res
.add
"<h3>Clients</h3>\n"
367 if reqs
.is_empty
then
370 var list
= new Array[String]
372 var direct
= deps
.has_direct_edge
(r
, mpackage
)
373 var s
= "<a href=\"{r}.html\
">"
374 if direct
then s
+= "<strong>"
376 if direct
then s
+= "</strong>"
380 res
.add_list
(list
, ", ", " and ")
384 var contributors
= mpackage
.metadata
.contributors
385 if not contributors
.is_empty
then
386 res
.add
"<h3>Contributors</h3>\n<ul class=\"box\
">"
387 for c
in contributors
do
388 res
.add
"<li>{c.to_html}</li>"
396 <li>{{{mmodules[mpackage]}}} modules</li>
397 <li>{{{mclasses[mpackage]}}} classes</li>
398 <li>{{{mmethods[mpackage]}}} methods</li>
399 <li>{{{loc[mpackage]}}} lines of code</li>
409 # Return a short HTML sequence for a package
411 # Intended to use in lists.
412 fun li_package
(p
: MPackage): String
415 var f
= "p/{p.name}.html"
416 res
+= "<a href=\"{f}\
">{p}</a>"
417 var d
= p
.mdoc_or_fallback
418 if d
!= null then res
+= " - {d.html_synopsis.write_to_string}"
422 # List packages by group.
424 # For each key of the `map` a `<h3>` is generated.
425 # Each package is then listed.
427 # The list of keys is generated first to allow fast access to the correct `<h3>`.
428 # `id_prefix` is used to give an id to the `<h3>` element.
429 fun list_by
(map
: MultiHashMap[Object, MPackage], id_prefix
: String): Template
431 var res
= new Template
432 var keys
= map
.keys
.to_a
433 alpha_comparator
.sort
(keys
)
434 var list
= [for x
in keys
do "<a href=\"#{id_prefix}{x.to_s.html_escape}\">{x.to_s.html_escape}</a>"]
435 res
.add_list
(list
, ", ", " and ")
438 var projs
= map
[k
].to_a
439 alpha_comparator
.sort
(projs
)
440 var e
= k
.to_s
.html_escape
441 res
.add
"<h3 id=\"{id_prefix}{e}\
">{e} ({projs.length})</h3>\n<ul>\n"
444 res
.add li_package
(p
)
452 # List the 10 best packages from `cpt`
453 fun list_best
(cpt
: Counter[MPackage]): Template
455 var res
= new Template
459 if i
> best
.length
then break
460 var p
= best
[best
.length-i
]
462 res
.add li_package
(p
)
463 # res.add " ({cpt[p]})"
470 # Produce a HTML table containig information on the packages
472 # `package_page` must have been called before so that information is computed.
473 fun table_packages
(mpackages
: Array[MPackage]): Template
475 alpha_comparator
.sort
(mpackages
)
476 var res
= new Template
477 res
.add
"<table data-toggle=\"table\
" data-sort-name=\"name\
" data-sort-order=\"desc\
" width=\"100%\
">\n"
478 res
.add
"<thead><tr>\n"
479 res
.add
"<th data-field=\"name\
" data-sortable=\"true\
">name</th>\n"
480 res
.add
"<th data-field=\"maint\
" data-sortable=\"true\
">maint</th>\n"
481 res
.add
"<th data-field=\"contrib\
" data-sortable=\"true\
">contrib</th>\n"
482 if deps
.not_empty
then
483 res
.add
"<th data-field=\"reqs\
" data-sortable=\"true\
">reqs</th>\n"
484 res
.add
"<th data-field=\"dreqs\
" data-sortable=\"true\
">direct<br>reqs</th>\n"
485 res
.add
"<th data-field=\"cli\
" data-sortable=\"true\
">clients</th>\n"
486 res
.add
"<th data-field=\"dcli\
" data-sortable=\"true\
">direct<br>clients</th>\n"
488 res
.add
"<th data-field=\"mod\
" data-sortable=\"true\
">modules</th>\n"
489 res
.add
"<th data-field=\"cla\
" data-sortable=\"true\
">classes</th>\n"
490 res
.add
"<th data-field=\"met\
" data-sortable=\"true\
">methods</th>\n"
491 res
.add
"<th data-field=\"loc\
" data-sortable=\"true\
">lines</th>\n"
492 res
.add
"<th data-field=\"score\
" data-sortable=\"true\
">score</th>\n"
493 res
.add
"<th data-field=\"errors\
" data-sortable=\"true\
">errors</th>\n"
494 res
.add
"<th data-field=\"warnings\
" data-sortable=\"true\
">warnings</th>\n"
495 res
.add
"<th data-field=\"warnings_per_kloc\
" data-sortable=\"true\
">w/kloc</th>\n"
496 res
.add
"<th data-field=\"doc\
" data-sortable=\"true\
">doc</th>\n"
497 res
.add
"</tr></thead>"
498 for p
in mpackages
do
500 res
.add
"<td><a href=\"p
/{p.name}.html\
">{p.name}</a></td>"
502 if p
.metadata
.maintainers
.not_empty
then maint
= p
.metadata
.maintainers
.first
.name
.html_escape
503 res
.add
"<td>{maint}</td>"
504 res
.add
"<td>{p.metadata.contributors.length}</td>"
505 if deps
.not_empty
then
506 res
.add
"<td>{deps[p].greaters.length-1}</td>"
507 res
.add
"<td>{deps[p].direct_greaters.length}</td>"
508 res
.add
"<td>{deps[p].smallers.length-1}</td>"
509 res
.add
"<td>{deps[p].direct_smallers.length}</td>"
511 res
.add
"<td>{mmodules[p]}</td>"
512 res
.add
"<td>{mclasses[p]}</td>"
513 res
.add
"<td>{mmethods[p]}</td>"
514 res
.add
"<td>{loc[p]}</td>"
515 res
.add
"<td>{score[p]}</td>"
516 res
.add
"<td>{errors[p]}</td>"
517 res
.add
"<td>{warnings[p]}</td>"
518 res
.add
"<td>{warnings_per_kloc[p]}</td>"
519 res
.add
"<td>{documentation_score[p]}</td>"
526 # Piwik tracker URL, if any
527 var piwik_tracker
: nullable String = null
530 # Used when `piwik_tracker` is set
531 var piwik_site_id
: Int = 1
534 var model
= new Model
535 var tc
= new ToolContext
537 var opt_dir
= new OptionString("Directory where the HTML files are generated", "-d", "--dir")
538 var opt_no_git
= new OptionBool("Do not gather git information from the working directory", "--no-git")
539 var opt_no_parse
= new OptionBool("Do not parse nit files (no importation information)", "--no-parse")
540 var opt_no_model
= new OptionBool("Do not analyse nit files (no class/method information)", "--no-model")
543 # If you want to monitor your visitors.
544 var opt_piwik_tracker
= new OptionString("Piwik tracker URL (ex: `nitlanguage.org/piwik/`)", "--piwik-tracker")
545 # Piwik tracker site id.
546 var opt_piwik_site_id
= new OptionString("Piwik site ID", "--piwik-site-id")
548 tc
.option_context
.add_option
(opt_dir
, opt_no_git
, opt_no_parse
, opt_no_model
, opt_piwik_tracker
, opt_piwik_site_id
)
550 tc
.process_options
(sys
.args
)
553 var modelbuilder
= new ModelBuilder(model
, tc
)
554 var catalog
= new Catalog(modelbuilder
)
556 catalog
.piwik_tracker
= opt_piwik_tracker
.value
557 var piwik_site_id
= opt_piwik_site_id
.value
558 if piwik_site_id
!= null then
559 if catalog
.piwik_tracker
== null then
560 print_error
"Warning: ignored `{opt_piwik_site_id}` because `{opt_piwik_tracker}` is not set."
561 else if piwik_site_id
.is_int
then
562 print_error
"Warning: ignored `{opt_piwik_site_id}`, an integer is required."
564 catalog
.piwik_site_id
= piwik_site_id
.to_i
569 # Get files or groups
570 var args
= tc
.option_context
.rest
572 if opt_no_parse
.value
then
573 mmodules
= modelbuilder
.scan_full
(args
)
575 mmodules
= modelbuilder
.parse_full
(args
)
577 var mpackages
= new Set[MPackage]
580 if p
!= null then mpackages
.add p
583 # Scan packages and compute information
584 for p
in model
.mpackages
do
587 modelbuilder
.scan_group
(g
)
589 # Load the module to process importation information
590 if opt_no_parse
.value
then continue
592 catalog
.deps
.add_node
(p
)
593 for gg
in p
.mgroups
do for m
in gg
.mmodules
do
594 for im
in m
.in_importation
.direct_greaters
do
596 if ip
== null or ip
== p
then continue
597 catalog
.deps
.add_edge
(p
, ip
)
602 if not opt_no_git
.value
then for p
in mpackages
do
606 # Run phases to modelize classes and properties (so we can count them)
607 if not opt_no_model
.value
then
608 modelbuilder
.run_phases
611 var out
= opt_dir
.value
or else "catalog.out"
617 # Generate the css (hard coded)
621 background-color: #f8f8f8;
626 text-decoration: none;
631 text-decoration: none;
643 border-bottom: solid 3px #CCC;
649 border-bottom: solid 1px #CCC;
653 list-style-type: square;
663 border: 1px solid #CCC;
664 font-family: Monospace;
666 background-color: rgb(250, 250, 250);
670 font-family: Monospace;
710 text-overflow: ellipsis;
712 border-bottom: 1px solid rgba(0,0,0,0.2);
715 css
.write_to_file
(out
/"style.css")
719 for p
in mpackages
do
721 var f
= "p/{p.name}.html"
722 catalog
.package_page
(p
)
723 catalog
.generate_page
(p
).write_to_file
(out
/f
)
728 var index
= catalog
.new_page
("")
729 index
.more_head
.add
"<title>Packages in Nit</title>"
732 <div class="content">
733 <h1>Packages in Nit</h1>
736 index
.add
"<h2>Highlighted Packages</h2>\n"
737 index
.add catalog
.list_best
(catalog
.score
)
739 if catalog
.deps
.not_empty
then
740 index
.add
"<h2>Most Required</h2>\n"
741 var reqs
= new Counter[MPackage]
742 for p
in mpackages
do
743 reqs
[p
] = catalog
.deps
[p
].smallers
.length
- 1
745 index
.add catalog
.list_best
(reqs
)
748 index
.add
"<h2>By First Tag</h2>\n"
749 index
.add catalog
.list_by
(catalog
.cat2proj
, "cat_")
751 index
.add
"<h2>By Any Tag</h2>\n"
752 index
.add catalog
.list_by
(catalog
.tag2proj
, "tag_")
756 <div class="sidebar">
759 <li>{{{mpackages.length}}} packages</li>
760 <li>{{{catalog.maint2proj.length}}} maintainers</li>
761 <li>{{{catalog.contrib2proj.length}}} contributors</li>
762 <li>{{{catalog.tag2proj.length}}} tags</li>
763 <li>{{{catalog.mmodules.sum}}} modules</li>
764 <li>{{{catalog.mclasses.sum}}} classes</li>
765 <li>{{{catalog.mmethods.sum}}} methods</li>
766 <li>{{{catalog.loc.sum}}} lines of code</li>
771 index
.write_to_file
(out
/"index.html")
775 var page
= catalog
.new_page
("")
776 page
.more_head
.add
"<title>People of Nit</title>"
777 page
.add
"""<div class="content">\n<h1>People of Nit</h1>\n"""
778 page
.add
"<h2>By Maintainer</h2>\n"
779 page
.add catalog
.list_by
(catalog
.maint2proj
, "maint_")
780 page
.add
"<h2>By Contributor</h2>\n"
781 page
.add catalog
.list_by
(catalog
.contrib2proj
, "contrib_")
783 page
.write_to_file
(out
/"people.html")
787 page
= catalog
.new_page
("")
788 page
.more_head
.add
"<title>Projets of Nit</title>"
789 page
.add
"""<div class="content">\n<h1>People of Nit</h1>\n"""
790 page
.add
"<h2>Table of Projets</h2>\n"
791 page
.add catalog
.table_packages
(mpackages
.to_a
)
793 page
.write_to_file
(out
/"table.html")