|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
| 3 | +from html import escape as html_escape |
3 | 4 | import re |
4 | 5 | from os import getenv, listdir |
5 | 6 | import pickle |
6 | 7 | import importlib |
| 8 | +import pkgutil |
7 | 9 |
|
8 | 10 | from mathics import settings |
9 | | -from html import escape as html_escape |
10 | 11 |
|
11 | 12 | from mathics import builtin |
12 | 13 | from mathics.builtin import get_module_doc |
|
110 | 111 | except IOError: |
111 | 112 | xml_data = {} |
112 | 113 |
|
| 114 | +def get_submodule_names(object): |
| 115 | + modpkgs = [] |
| 116 | + if hasattr(object, '__path__'): |
| 117 | + for importer, modname, ispkg in pkgutil.iter_modules(object.__path__): |
| 118 | + modpkgs.append(modname) |
| 119 | + modpkgs.sort() |
| 120 | + return modpkgs |
| 121 | + |
| 122 | + |
113 | 123 |
|
114 | 124 | def filter_comments(doc): |
115 | 125 | return "\n".join( |
@@ -477,6 +487,7 @@ def post_sub(text, post_substitutions): |
477 | 487 | return text |
478 | 488 |
|
479 | 489 |
|
| 490 | +# FIXME: can we replace this with Python 3's html.escape ? |
480 | 491 | def escape_html(text, verbatim_mode=False, counters=None, single_line=False): |
481 | 492 | def repl_python(match): |
482 | 493 | return ( |
@@ -617,6 +628,7 @@ def repl_subsection(match): |
617 | 628 | text = text.replace("\\" + key, xml) |
618 | 629 |
|
619 | 630 | if not single_line: |
| 631 | + # FIXME: linebreaks() is not defined |
620 | 632 | # text = linebreaks(text) |
621 | 633 | text = text.replace("<br />", "\n").replace("<br>", "<br />") |
622 | 634 |
|
@@ -657,7 +669,7 @@ def get_prev_next(self): |
657 | 669 | return prev, next |
658 | 670 |
|
659 | 671 | def get_title_html(self): |
660 | | - return html_escape(self.title, single_line=True) |
| 672 | + return escape_html(self.title, single_line=True) |
661 | 673 |
|
662 | 674 |
|
663 | 675 | class Documentation(DocElement): |
@@ -791,21 +803,36 @@ def __init__(self): |
791 | 803 | title, text = get_module_doc(module) |
792 | 804 | chapter = DocChapter(builtin_part, title, Doc(text)) |
793 | 805 | builtins = builtins_by_module[module.__name__] |
794 | | - for instance in builtins: |
| 806 | + |
| 807 | + if module.__file__.endswith("__init__.py"): |
| 808 | + section_names = get_submodule_names(module) |
| 809 | + else: |
| 810 | + section_names = builtins |
| 811 | + |
| 812 | + for instance in section_names: |
795 | 813 | installed = True |
796 | 814 | for package in getattr(instance, "requires", []): |
797 | 815 | try: |
798 | 816 | importlib.import_module(package) |
799 | 817 | except ImportError: |
800 | 818 | installed = False |
801 | 819 | break |
802 | | - section = DocSection( |
803 | | - chapter, |
804 | | - strip_system_prefix(instance.get_name()), |
805 | | - instance.__doc__ or "", |
806 | | - operator=instance.get_operator(), |
807 | | - installed=installed, |
808 | | - ) |
| 820 | + if isinstance(instance, str): |
| 821 | + section = DocSection( |
| 822 | + chapter, |
| 823 | + instance, |
| 824 | + "", |
| 825 | + None, |
| 826 | + installed=installed, |
| 827 | + ) |
| 828 | + else: |
| 829 | + section = DocSection( |
| 830 | + chapter, |
| 831 | + strip_system_prefix(instance.get_name()), |
| 832 | + instance.__doc__ or "", |
| 833 | + operator=instance.get_operator(), |
| 834 | + installed=installed, |
| 835 | + ) |
809 | 836 | chapter.sections.append(section) |
810 | 837 | builtin_part.chapters.append(chapter) |
811 | 838 | self.parts.append(builtin_part) |
@@ -1170,7 +1197,7 @@ def latex(self, output): |
1170 | 1197 |
|
1171 | 1198 | def html(self): |
1172 | 1199 | counters = {} |
1173 | | - return escape_html( |
| 1200 | + return html_escape( |
1174 | 1201 | "\n".join( |
1175 | 1202 | item.html(counters) for item in self.items if not item.is_private() |
1176 | 1203 | ) |
|
0 commit comments