nitcatalog: add a new class Person
[nit.git] / src / nitcatalog.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Basic catalog generator for Nit packages
16 #
17 # See: <http://nitlanguage.org/catalog/>
18 #
19 # The tool scans packages and generates the HTML files of a catalog.
20 #
21 # See `catalog` for details
22 module nitcatalog
23
24 import loader # Scan&load packages, groups and modules
25 import doc::doc_down # Display mdoc
26 import catalog
27
28 # A HTML page in a catalog
29 #
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.
32 class CatalogPage
33 super Template
34
35 # The associated catalog, used to groups options and other global data
36 var catalog: Catalog
37
38 # Placeholder to include additional things before the `</head>`.
39 var more_head = new Template
40
41 # Relative path to the root directory (with the index file).
42 #
43 # Use "" for pages in the root directory
44 # Use ".." for pages in a subdirectory
45 var rootpath: String
46
47 redef init
48 do
49 add """
50 <!DOCTYPE html>
51 <html>
52 <head>
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"}}}">
56 """
57 add more_head
58
59 add """
60 </head>
61 <body>
62 <div class='container-fluid'>
63 <div class='row'>
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>
72 </button>
73 <span class='navbar-brand'><a href="http://nitlanguage.org/">Nitlanguage.org</a></span>
74 </div>
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>
78 </ul>
79 </div>
80 </div>
81 </nav>
82 </div>
83 """
84 end
85
86 # Inject piwik HTML code if required
87 private fun add_piwik
88 do
89 var tracker_url = catalog.piwik_tracker
90 if tracker_url == null then return
91
92 var site_id = catalog.piwik_site_id
93
94 tracker_url = tracker_url.trim
95 if tracker_url.chars.last != '/' then tracker_url += "/"
96 add """
97 <!-- Piwik -->
98 <script type="text/javascript">
99 var _paq = _paq || [];
100 _paq.push(['trackPageView']);
101 _paq.push(['enableLinkTracking']);
102 (function() {
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);
108 })();
109
110 </script>
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 -->
113 """
114
115 end
116
117 redef fun rendering
118 do
119 add """
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>
124 """
125 add_piwik
126 add """
127
128 </body>
129 </html>
130 """
131 end
132 end
133
134 redef class Catalog
135 # Return a empty `CatalogPage`.
136 fun new_page(rootpath: String): CatalogPage
137 do
138 return new CatalogPage(self, rootpath)
139 end
140
141 # Recursively generate a level in the file tree of the *content* section
142 private fun gen_content_level(ot: OrderedTree[MConcern], os: Array[Object], res: Template)
143 do
144 res.add "<ul>\n"
145 for o in os do
146 res.add "<li>"
147 if o isa MGroup then
148 var d = ""
149 var mdoc = o.mdoc
150 if mdoc != null then d = ": {mdoc.html_synopsis.write_to_string}"
151 res.add "<strong>{o.name}</strong>{d} ({o.filepath.to_s})"
152 else if o isa MModule then
153 var d = ""
154 var mdoc = o.mdoc
155 if mdoc != null then d = ": {mdoc.html_synopsis.write_to_string}"
156 res.add "<strong>{o.name}</strong>{d} ({o.filepath.to_s})"
157 else
158 abort
159 end
160 var subs = ot.sub.get_or_null(o)
161 if subs != null then gen_content_level(ot, subs, res)
162 res.add "</li>\n"
163 end
164 res.add "</ul>\n"
165 end
166
167 # Generate a full HTML page for a package
168 fun generate_page(mpackage: MPackage): Writable
169 do
170 var res = new_page("..")
171 var name = mpackage.name.html_escape
172 res.more_head.add """<title>{{{name}}}</title>"""
173
174 res.add """
175 <div class="content">
176 <h1 class="package-name">{{{name}}}</h1>
177 """
178 var mdoc = mpackage.mdoc_or_fallback
179 if mdoc != null then res.add mdoc.html_documentation
180
181 res.add "<h2>Content</h2>"
182 var ot = new OrderedTree[MConcern]
183 for g in mpackage.mgroups do
184 var pa = g.parent
185 if g.is_interesting then
186 ot.add(pa, g)
187 pa = g
188 end
189 for mp in g.mmodules do
190 ot.add(pa, mp)
191 end
192 end
193 ot.sort_with(alpha_comparator)
194 gen_content_level(ot, ot.roots, res)
195
196
197 res.add """
198 </div>
199 <div class="sidebar">
200 <ul class="box">
201 """
202 var tryit = mpackage.metadata("upstream.tryit")
203 if tryit != null then
204 var e = tryit.html_escape
205 res.add "<li><a href=\"{e}\">Try<span style=\"color:white\">n</span>it!</a></li>\n"
206 end
207 var apk = mpackage.metadata("upstream.apk")
208 if apk != null then
209 var e = apk.html_escape
210 res.add "<li><a href=\"{e}\">Android apk</a></li>\n"
211 end
212
213 res.add """</ul>\n<ul class="box">\n"""
214
215 var homepage = mpackage.metadata("upstream.homepage")
216 if homepage != null then
217 var e = homepage.html_escape
218 res.add "<li><a href=\"{e}\">{e}</a></li>\n"
219 end
220 for maintainer in mpackage.maintainers do
221 res.add "<li>{maintainer.to_html}</li>"
222 end
223 var license = mpackage.metadata("package.license")
224 if license != null then
225 var e = license.html_escape
226 res.add "<li><a href=\"http://opensource.org/licenses/{e}\">{e}</a> license</li>\n"
227 end
228 res.add "</ul>\n"
229
230 res.add "<h3>Source Code</h3>\n<ul class=\"box\">\n"
231 var browse = mpackage.metadata("upstream.browse")
232 if browse != null then
233 var e = browse.html_escape
234 res.add "<li><a href=\"{e}\">{e}</a></li>\n"
235 end
236 var git = mpackage.metadata("upstream.git")
237 if git != null then
238 var e = git.html_escape
239 res.add "<li><tt>{e}</tt></li>\n"
240 end
241 var last_date = mpackage.last_date
242 if last_date != null then
243 var e = last_date.html_escape
244 res.add "<li>most recent commit: {e}</li>\n"
245 end
246 var first_date = mpackage.first_date
247 if first_date != null then
248 var e = first_date.html_escape
249 res.add "<li>oldest commit: {e}</li>\n"
250 end
251 var commits = commits[mpackage]
252 if commits != 0 then
253 res.add "<li>{commits} commits</li>\n"
254 end
255 res.add "</ul>\n"
256
257 res.add "<h3>Tags</h3>\n"
258 var ts2 = new Array[String]
259 for t in mpackage.tags do
260 t = t.html_escape
261 ts2.add "<a href=\"../index.html#tag_{t}\">{t}</a>"
262 end
263 res.add_list(ts2, ", ", ", ")
264
265 if deps.has(mpackage) then
266 var reqs = deps[mpackage].greaters.to_a
267 reqs.remove(mpackage)
268 alpha_comparator.sort(reqs)
269 res.add "<h3>Requirements</h3>\n"
270 if reqs.is_empty then
271 res.add "none"
272 else
273 var list = new Array[String]
274 for r in reqs do
275 var direct = deps.has_direct_edge(mpackage, r)
276 var s = "<a href=\"{r}.html\">"
277 if direct then s += "<strong>"
278 s += r.to_s
279 if direct then s += "</strong>"
280 s += "</a>"
281 list.add s
282 end
283 res.add_list(list, ", ", " and ")
284 end
285
286 reqs = deps[mpackage].smallers.to_a
287 reqs.remove(mpackage)
288 alpha_comparator.sort(reqs)
289 res.add "<h3>Clients</h3>\n"
290 if reqs.is_empty then
291 res.add "none"
292 else
293 var list = new Array[String]
294 for r in reqs do
295 var direct = deps.has_direct_edge(r, mpackage)
296 var s = "<a href=\"{r}.html\">"
297 if direct then s += "<strong>"
298 s += r.to_s
299 if direct then s += "</strong>"
300 s += "</a>"
301 list.add s
302 end
303 res.add_list(list, ", ", " and ")
304 end
305 end
306
307 var contributors = mpackage.contributors
308 if not contributors.is_empty then
309 res.add "<h3>Contributors</h3>\n<ul class=\"box\">"
310 for c in contributors do
311 res.add "<li>{c.to_html}</li>"
312 end
313 res.add "</ul>"
314 end
315
316 res.add """
317 <h3>Stats</h3>
318 <ul class="box">
319 <li>{{{mmodules[mpackage]}}} modules</li>
320 <li>{{{mclasses[mpackage]}}} classes</li>
321 <li>{{{mmethods[mpackage]}}} methods</li>
322 <li>{{{loc[mpackage]}}} lines of code</li>
323 </ul>
324 """
325
326 res.add """
327 </div>
328 """
329 return res
330 end
331
332 # Return a short HTML sequence for a package
333 #
334 # Intended to use in lists.
335 fun li_package(p: MPackage): String
336 do
337 var res = ""
338 var f = "p/{p.name}.html"
339 res += "<a href=\"{f}\">{p}</a>"
340 var d = p.mdoc_or_fallback
341 if d != null then res += " - {d.html_synopsis.write_to_string}"
342 return res
343 end
344
345 # List packages by group.
346 #
347 # For each key of the `map` a `<h3>` is generated.
348 # Each package is then listed.
349 #
350 # The list of keys is generated first to allow fast access to the correct `<h3>`.
351 # `id_prefix` is used to give an id to the `<h3>` element.
352 fun list_by(map: MultiHashMap[Object, MPackage], id_prefix: String): Template
353 do
354 var res = new Template
355 var keys = map.keys.to_a
356 alpha_comparator.sort(keys)
357 var list = [for x in keys do "<a href=\"#{id_prefix}{x.to_s.html_escape}\">{x.to_s.html_escape}</a>"]
358 res.add_list(list, ", ", " and ")
359
360 for k in keys do
361 var projs = map[k].to_a
362 alpha_comparator.sort(projs)
363 var e = k.to_s.html_escape
364 res.add "<h3 id=\"{id_prefix}{e}\">{e} ({projs.length})</h3>\n<ul>\n"
365 for p in projs do
366 res.add "<li>"
367 res.add li_package(p)
368 res.add "</li>"
369 end
370 res.add "</ul>"
371 end
372 return res
373 end
374
375 # List the 10 best packages from `cpt`
376 fun list_best(cpt: Counter[MPackage]): Template
377 do
378 var res = new Template
379 res.add "<ul>"
380 var best = cpt.sort
381 for i in [1..10] do
382 if i > best.length then break
383 var p = best[best.length-i]
384 res.add "<li>"
385 res.add li_package(p)
386 # res.add " ({cpt[p]})"
387 res.add "</li>"
388 end
389 res.add "</ul>"
390 return res
391 end
392
393 # Produce a HTML table containig information on the packages
394 #
395 # `package_page` must have been called before so that information is computed.
396 fun table_packages(mpackages: Array[MPackage]): Template
397 do
398 alpha_comparator.sort(mpackages)
399 var res = new Template
400 res.add "<table data-toggle=\"table\" data-sort-name=\"name\" data-sort-order=\"desc\" width=\"100%\">\n"
401 res.add "<thead><tr>\n"
402 res.add "<th data-field=\"name\" data-sortable=\"true\">name</th>\n"
403 res.add "<th data-field=\"maint\" data-sortable=\"true\">maint</th>\n"
404 res.add "<th data-field=\"contrib\" data-sortable=\"true\">contrib</th>\n"
405 if deps.not_empty then
406 res.add "<th data-field=\"reqs\" data-sortable=\"true\">reqs</th>\n"
407 res.add "<th data-field=\"dreqs\" data-sortable=\"true\">direct<br>reqs</th>\n"
408 res.add "<th data-field=\"cli\" data-sortable=\"true\">clients</th>\n"
409 res.add "<th data-field=\"dcli\" data-sortable=\"true\">direct<br>clients</th>\n"
410 end
411 res.add "<th data-field=\"mod\" data-sortable=\"true\">modules</th>\n"
412 res.add "<th data-field=\"cla\" data-sortable=\"true\">classes</th>\n"
413 res.add "<th data-field=\"met\" data-sortable=\"true\">methods</th>\n"
414 res.add "<th data-field=\"loc\" data-sortable=\"true\">lines</th>\n"
415 res.add "<th data-field=\"score\" data-sortable=\"true\">score</th>\n"
416 res.add "</tr></thead>"
417 for p in mpackages do
418 res.add "<tr>"
419 res.add "<td><a href=\"p/{p.name}.html\">{p.name}</a></td>"
420 var maint = "?"
421 if p.maintainers.not_empty then maint = p.maintainers.first.name.html_escape
422 res.add "<td>{maint}</td>"
423 res.add "<td>{p.contributors.length}</td>"
424 if deps.not_empty then
425 res.add "<td>{deps[p].greaters.length-1}</td>"
426 res.add "<td>{deps[p].direct_greaters.length}</td>"
427 res.add "<td>{deps[p].smallers.length-1}</td>"
428 res.add "<td>{deps[p].direct_smallers.length}</td>"
429 end
430 res.add "<td>{mmodules[p]}</td>"
431 res.add "<td>{mclasses[p]}</td>"
432 res.add "<td>{mmethods[p]}</td>"
433 res.add "<td>{loc[p]}</td>"
434 res.add "<td>{score[p]}</td>"
435 res.add "</tr>\n"
436 end
437 res.add "</table>\n"
438 return res
439 end
440
441 # Piwik tracker URL, if any
442 var piwik_tracker: nullable String = null
443
444 # Piwik site ID
445 # Used when `piwik_tracker` is set
446 var piwik_site_id: Int = 1
447 end
448
449 var model = new Model
450 var tc = new ToolContext
451
452 var opt_dir = new OptionString("Directory where the HTML files are generated", "-d", "--dir")
453 var opt_no_git = new OptionBool("Do not gather git information from the working directory", "--no-git")
454 var opt_no_parse = new OptionBool("Do not parse nit files (no importation information)", "--no-parse")
455 var opt_no_model = new OptionBool("Do not analyse nit files (no class/method information)", "--no-model")
456
457 # Piwik tracker URL.
458 # If you want to monitor your visitors.
459 var opt_piwik_tracker = new OptionString("Piwik tracker URL (ex: `nitlanguage.org/piwik/`)", "--piwik-tracker")
460 # Piwik tracker site id.
461 var opt_piwik_site_id = new OptionString("Piwik site ID", "--piwik-site-id")
462
463 tc.option_context.add_option(opt_dir, opt_no_git, opt_no_parse, opt_no_model, opt_piwik_tracker, opt_piwik_site_id)
464
465 tc.process_options(sys.args)
466 tc.keep_going = true
467
468 var modelbuilder = new ModelBuilder(model, tc)
469 var catalog = new Catalog(modelbuilder)
470
471 catalog.piwik_tracker = opt_piwik_tracker.value
472 var piwik_site_id = opt_piwik_site_id.value
473 if piwik_site_id != null then
474 if catalog.piwik_tracker == null then
475 print_error "Warning: ignored `{opt_piwik_site_id}` because `{opt_piwik_tracker}` is not set."
476 else if piwik_site_id.is_int then
477 print_error "Warning: ignored `{opt_piwik_site_id}`, an integer is required."
478 else
479 catalog.piwik_site_id = piwik_site_id.to_i
480 end
481 end
482
483
484 # Get files or groups
485 var args = tc.option_context.rest
486 if opt_no_parse.value then
487 modelbuilder.scan_full(args)
488 else
489 modelbuilder.parse_full(args)
490 end
491
492 # Scan packages and compute information
493 for p in model.mpackages do
494 var g = p.root
495 assert g != null
496 modelbuilder.scan_group(g)
497
498 # Load the module to process importation information
499 if opt_no_parse.value then continue
500
501 catalog.deps.add_node(p)
502 for gg in p.mgroups do for m in gg.mmodules do
503 for im in m.in_importation.direct_greaters do
504 var ip = im.mpackage
505 if ip == null or ip == p then continue
506 catalog.deps.add_edge(p, ip)
507 end
508 end
509 end
510
511 if not opt_no_git.value then for p in model.mpackages do
512 catalog.git_info(p)
513 end
514
515 # Run phases to modelize classes and properties (so we can count them)
516 if not opt_no_model.value then
517 modelbuilder.run_phases
518 end
519
520 var out = opt_dir.value or else "catalog.out"
521 (out/"p").mkdir
522
523 # Generate the css (hard coded)
524 var css = """
525 body {
526 margin-top: 15px;
527 background-color: #f8f8f8;
528 }
529
530 a {
531 color: #0D8921;
532 text-decoration: none;
533 }
534
535 a:hover {
536 color: #333;
537 text-decoration: none;
538 }
539
540 h1 {
541 font-weight: bold;
542 color: #0D8921;
543 font-size: 22px;
544 }
545
546 h2 {
547 color: #6C6C6C;
548 font-size: 18px;
549 border-bottom: solid 3px #CCC;
550 }
551
552 h3 {
553 color: #6C6C6C;
554 font-size: 15px;
555 border-bottom: solid 1px #CCC;
556 }
557
558 ul {
559 list-style-type: square;
560 }
561
562 dd {
563 color: #6C6C6C;
564 margin-top: 1em;
565 margin-bottom: 1em;
566 }
567
568 pre {
569 border: 1px solid #CCC;
570 font-family: Monospace;
571 color: #2d5003;
572 background-color: rgb(250, 250, 250);
573 }
574
575 code {
576 font-family: Monospace;
577 color: #2d5003;
578 }
579
580 footer {
581 margin-top: 20px;
582 }
583
584 .container {
585 margin: 0 auto;
586 padding: 0 20px;
587 }
588
589 .content {
590 float: left;
591 margin-top: 40px;
592 width: 65%;
593 }
594
595 .sidebar {
596 float: right;
597 margin-top: 40px;
598 width: 30%
599 }
600
601 .sidebar h3 {
602 color: #0D8921;
603 font-size: 18px;
604 border-bottom: 0px;
605 }
606
607 .box {
608 margin: 0;
609 padding: 0;
610 }
611
612 .box li {
613 line-height: 2.5;
614 white-space: nowrap;
615 overflow: hidden;
616 text-overflow: ellipsis;
617 padding-right: 10px;
618 border-bottom: 1px solid rgba(0,0,0,0.2);
619 }
620 """
621 css.write_to_file(out/"style.css")
622
623 # PAGES
624
625 for p in model.mpackages do
626 # print p
627 var f = "p/{p.name}.html"
628 catalog.package_page(p)
629 catalog.generate_page(p).write_to_file(out/f)
630 end
631
632 # INDEX
633
634 var index = catalog.new_page("")
635 index.more_head.add "<title>Packages in Nit</title>"
636
637 index.add """
638 <div class="content">
639 <h1>Packages in Nit</h1>
640 """
641
642 index.add "<h2>Highlighted Packages</h2>\n"
643 index.add catalog.list_best(catalog.score)
644
645 if catalog.deps.not_empty then
646 index.add "<h2>Most Required</h2>\n"
647 var reqs = new Counter[MPackage]
648 for p in model.mpackages do
649 reqs[p] = catalog.deps[p].smallers.length - 1
650 end
651 index.add catalog.list_best(reqs)
652 end
653
654 index.add "<h2>By First Tag</h2>\n"
655 index.add catalog.list_by(catalog.cat2proj, "cat_")
656
657 index.add "<h2>By Any Tag</h2>\n"
658 index.add catalog.list_by(catalog.tag2proj, "tag_")
659
660 index.add """
661 </div>
662 <div class="sidebar">
663 <h3>Stats</h3>
664 <ul class="box">
665 <li>{{{model.mpackages.length}}} packages</li>
666 <li>{{{catalog.maint2proj.length}}} maintainers</li>
667 <li>{{{catalog.contrib2proj.length}}} contributors</li>
668 <li>{{{catalog.tag2proj.length}}} tags</li>
669 <li>{{{catalog.mmodules.sum}}} modules</li>
670 <li>{{{catalog.mclasses.sum}}} classes</li>
671 <li>{{{catalog.mmethods.sum}}} methods</li>
672 <li>{{{catalog.loc.sum}}} lines of code</li>
673 </ul>
674 </div>
675 """
676
677 index.write_to_file(out/"index.html")
678
679 # PEOPLE
680
681 var page = catalog.new_page("")
682 page.more_head.add "<title>People of Nit</title>"
683 page.add """<div class="content">\n<h1>People of Nit</h1>\n"""
684 page.add "<h2>By Maintainer</h2>\n"
685 page.add catalog.list_by(catalog.maint2proj, "maint_")
686 page.add "<h2>By Contributor</h2>\n"
687 page.add catalog.list_by(catalog.contrib2proj, "contrib_")
688 page.add "</div>\n"
689 page.write_to_file(out/"people.html")
690
691 # TABLE
692
693 page = catalog.new_page("")
694 page.more_head.add "<title>Projets of Nit</title>"
695 page.add """<div class="content">\n<h1>People of Nit</h1>\n"""
696 page.add "<h2>Table of Projets</h2>\n"
697 page.add catalog.table_packages(model.mpackages)
698 page.add "</div>\n"
699 page.write_to_file(out/"table.html")