ClammingPy 3.1

https://github.com/brigitte-bigi/ClammingPy

Module clamming

Class ExportOptions

Description

Store the options and content for an export to documented files.

ExportOptions is a data class, used to store options and content for exporting a documented file. It provides methods to set and get various information such as software name, copyright, icon, title, favicon, and theme. It also allows setting the names of the next and previous classes or modules for generating a table of contents (HTML only).

Example
 >>> h = ExportOptions()
 >>> h.software = "Clamming"
 >>> h.theme = "light"
 >>> html_head = h.get_head()
 >>> html_nav = h.get_nav()
 >>> html_footer = h.get_footer()

Constructor

__init__

Create a documentation export system for a ClamsPack.

Main functionalities:

  • Store options and content for exporting a standalone file;
  • Set and get HTML information such as software name, copyright, icon, title, favicon, and theme;
  • Set the names of the next and previous classes or modules for generating a table of contents.
View Source
def __init__(self):
    """Create a documentation export system for a ClamsPack.

    Main functionalities:

    - Store options and content for exporting a standalone file;
    - Set and get HTML information such as software name, copyright, icon, title, favicon, and theme;
    - Set the names of the next and previous classes or modules for generating a table of contents.

    """
    self.__readme = True
    self.__software = ExportOptions.DEFAULT_SOFTWARE
    self.__copyright = ExportOptions.DEFAULT_COPYRIGHT
    self.__url = ExportOptions.DEFAULT_URL
    self.__icon = ExportOptions.DEFAULT_ICON
    self.__title = ExportOptions.DEFAULT_TITLE
    self.__favicon = ExportOptions.DEFAULT_FAVICON
    self.__theme = ExportOptions.DEFAULT_THEME
    self.__lang = ExportOptions.DEFAULT_LANG
    self.__statics = ExportOptions.DEFAULT_STATICS
    self.__wexa_statics = ExportOptions.DEFAULT_WEXA_STATICS
    self.__descr = 'Python class documentation'
    self.__aside_toc = ExportOptions.DEFAULT_ASIDE_TOC
    self.__css_theme = ExportOptions.DEFAULT_CSS_THEME
    self.__next_class = None
    self.__prev_class = None
    self.__next_pack = None
    self.__prev_pack = None

Public functions

get_add_readme

Return whether the README of library is added or not.

View Source
def get_add_readme(self) -> bool:
    """Return whether the README of library is added or not."""
    return self.__readme

set_add_readme

Set whether the README of library is added or not.

Parameters
  • readme: (bool) whether the README is added or not.
View Source
def set_add_readme(self, readme: bool) -> NoReturn:
    """Set whether the README of library is added or not.

        :param readme: (bool) whether the README is added or not.

        """
    self.__readme = bool(readme)

get_aside_toc

Return whether the table of contents is a collapsible aside or not.

View Source
def get_aside_toc(self) -> bool:
    """Return whether the table of contents is a collapsible aside or not."""
    return self.__aside_toc

set_aside_toc

Set whether the table of contents is a collapsible aside or not.

The table of contents is a fixed 'nav' panel by default. When it is an aside instead, 'book.js' hides it and adds a button to open and close it.

Parameters
  • aside_toc: (bool) whether the table of contents is an aside or not.
View Source
def set_aside_toc(self, aside_toc: bool) -> NoReturn:
    """Set whether the table of contents is a collapsible aside or not.

        The table of contents is a fixed 'nav' panel by default. When it is an
        aside instead, 'book.js' hides it and adds a button to open and close it.

        :param aside_toc: (bool) whether the table of contents is an aside or not.

        """
    self.__aside_toc = bool(aside_toc)

get_css_theme

Return the filename of the CSS theme of the documented software.

View Source
def get_css_theme(self) -> str:
    """Return the filename of the CSS theme of the documented software."""
    return self.__css_theme

set_css_theme

Set the filename of the CSS theme, in the statics folder.

A theme is a stylesheet defining colors only. When a name is given, the pages are opening with it, and the high-contrast theme of Whakerexa can be activated instead. When the name is empty, no theme is declared at all: the pages are the ones ClammingPy created before themes existed.

Example
 >>> h = ExportOptions()
 >>> h.css_theme = "clamming_theme.css"
Parameters
  • name: (str) Name of the theme file, or an empty string
Raises
  • TypeError: Given name is not a string
View Source
def set_css_theme(self, name: str=DEFAULT_CSS_THEME) -> NoReturn:
    """Set the filename of the CSS theme, in the statics folder.

        A theme is a stylesheet defining colors only. When a name is given, the
        pages are opening with it, and the high-contrast theme of Whakerexa can
        be activated instead. When the name is empty, no theme is declared at
        all: the pages are the ones ClammingPy created before themes existed.

        :example:
        >>> h = ExportOptions()
        >>> h.css_theme = "clamming_theme.css"

        :param name: (str) Name of the theme file, or an empty string
        :raises: TypeError: Given name is not a string

        """
    if isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the ExportOptions.css_theme. Got {} instead.".format(name))
    self.__css_theme = name

get_software

Return the name of the software.

View Source
def get_software(self) -> str:
    """Return the name of the software."""
    return self.__software

set_software

Set a software name.

Parameters
  • name: (str) Name of the documented software
Raises
  • TypeError: Given name is not a string
View Source
def set_software(self, name: str=DEFAULT_SOFTWARE) -> NoReturn:
    """Set a software name.

        :param name: (str) Name of the documented software
        :raises: TypeError: Given name is not a string

        """
    if isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the HTMLDocExport.software. Got {} instead.".format(name))
    self.__software = name

get_url

Return the url of the software.

View Source
def get_url(self) -> str:
    """Return the url of the software."""
    return self.__url

set_url

Set a software url.

Parameters
  • name: (str) URL of the documented software
Raises
  • TypeError: Given name is not a string
View Source
def set_url(self, name: str='') -> NoReturn:
    """Set a software url.

        :param name: (str) URL of the documented software
        :raises: TypeError: Given name is not a string

        """
    if isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the HTMLDocExport.url. Got {} instead.".format(name))
    self.__url = name

get_copyright

Return the copyright of the HTML page.

View Source
def get_copyright(self) -> str:
    """Return the copyright of the HTML page."""
    return self.__copyright

set_copyright

Set a copyright text, added to the footer of the page.

Parameters
  • text: (str) Copyright of the documented software
Raises
  • TypeError: Given text is not a string
View Source
def set_copyright(self, text: str=DEFAULT_COPYRIGHT) -> NoReturn:
    """Set a copyright text, added to the footer of the page.

        :param text: (str) Copyright of the documented software
        :raises: TypeError: Given text is not a string

        """
    if isinstance(text, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the HTMLDocExport.copyright. Got {} instead.".format(text))
    self.__copyright = text

get_icon

Return the icon filename of the software.

View Source
def get_icon(self) -> str:
    """Return the icon filename of the software."""
    return self.__icon

set_icon

Set an icon filename.

Parameters
  • name: (str) Filename of the icon of the documented software
Raises
  • TypeError: Given name is not a string
View Source
def set_icon(self, name: str=DEFAULT_ICON) -> NoReturn:
    """Set an icon filename.

        :param name: (str) Filename of the icon of the documented software
        :raises: TypeError: Given name is not a string

        """
    if isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the HTMLDocExport.icon. Got {} instead.".format(name))
    self.__icon = name

get_title

Return the title of the HTML page.

View Source
def get_title(self) -> str:
    """Return the title of the HTML page."""
    return self.__title

set_title

Set a title to the output HTML pages.

Parameters
  • text: (str) Title of the HTML pages
Raises
  • TypeError: Given text is not a string
View Source
def set_title(self, text: str=DEFAULT_TITLE) -> NoReturn:
    """Set a title to the output HTML pages.

        :param text: (str) Title of the HTML pages
        :raises: TypeError: Given text is not a string

        """
    if isinstance(text, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the HTMLDocExport.title. Got {} instead.".format(text))
    self.__title = text

get_statics

Return the static path of the CSS, JS, etc.

View Source
def get_statics(self) -> str:
    """Return the static path of the CSS, JS, etc."""
    return self.__statics

set_statics

Set the static path of the customs CSS, JS, etc.

Parameters
  • name: (str) Path of the static elements
Raises
  • TypeError: Given name is not a string
View Source
def set_statics(self, name: str=DEFAULT_STATICS) -> NoReturn:
    """Set the static path of the customs CSS, JS, etc.

        :param name: (str) Path of the static elements
        :raises: TypeError: Given name is not a string

        """
    if isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the HTMLDocExport.statics. Got {} instead.".format(name))
    self.__statics = name

get_wexa_statics

Return the static path of the CSS, JS, etc. of Whakerexa.

View Source
def get_wexa_statics(self) -> str:
    """Return the static path of the CSS, JS, etc. of Whakerexa. """
    return self.__wexa_statics

set_wexa_statics

Set the static path of the customs CSS, JS, etc. of Whakerexa.

Parameters
  • name: (str) Path of the static elements
Raises
  • TypeError: Given name is not a string
View Source
def set_wexa_statics(self, name: str=DEFAULT_WEXA_STATICS) -> NoReturn:
    """Set the static path of the customs CSS, JS, etc. of Whakerexa.

        :param name: (str) Path of the static elements
        :raises: TypeError: Given name is not a string

        """
    if isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the HTMLDocExport.wexa_statics. Got {} instead.".format(name))
    self.__wexa_statics = name

get_favicon

Return the favicon filename of the HTML pages.

View Source
def get_favicon(self) -> str:
    """Return the favicon filename of the HTML pages."""
    return self.__favicon

set_favicon

Set a favicon to the output HTML pages.

Parameters
  • name: (str) Favicon of the HTML pages
Raises
  • TypeError: Given name is not a string
View Source
def set_favicon(self, name: str=DEFAULT_FAVICON) -> NoReturn:
    """Set a favicon to the output HTML pages.

        :param name: (str) Favicon of the HTML pages
        :raises: TypeError: Given name is not a string

        """
    if isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the HTMLDocExport.favicon. Got {} instead.".format(name))
    self.__favicon = name

get_theme

Return the theme of the HTML page.

View Source
def get_theme(self) -> str:
    """Return the theme of the HTML page."""
    return self.__theme

set_theme

Set a theme name.

Parameters
  • name: (str) Name of the theme of the HTML pages
Raises
  • TypeError: Given name is not a string
View Source
def set_theme(self, name: str=DEFAULT_THEME) -> NoReturn:
    """Set a theme name.

        :param name: (str) Name of the theme of the HTML pages
        :raises: TypeError: Given name is not a string

        """
    if isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the HTMLDocExport.theme. Got {} instead.".format(name))
    self.__theme = name

get_root_class

Return the 'class' attribute value of the HTML root element.

Since Whakerexa 3.0, the color mode is a class of ':root' -- it was a class of 'body' before. The light mode being the default one, it is represented by an empty class.

View Source
def get_root_class(self) -> str:
    """Return the 'class' attribute value of the HTML root element.

        Since Whakerexa 3.0, the color mode is a class of ':root' -- it was a
        class of 'body' before. The light mode being the default one, it is
        represented by an empty class.

        """
    if self.__theme in ExportOptions.ROOT_COLOR_MODES:
        return self.__theme
    return ''

get_lang

Return the language code of the HTML pages.

View Source
def get_lang(self) -> str:
    """Return the language code of the HTML pages."""
    return self.__lang

set_lang

Set the language code of the HTML pages.

Parameters
  • name: (str) BCP 47 language tag, e.g. 'en', 'fr', 'es'
Raises
  • TypeError: Given name is not a string
View Source
def set_lang(self, name: str=DEFAULT_LANG) -> NoReturn:
    """Set the language code of the HTML pages.

        :param name: (str) BCP 47 language tag, e.g. 'en', 'fr', 'es'
        :raises: TypeError: Given name is not a string

        """
    if isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the ExportOptions.lang. Got {} instead.".format(name))
    self.__lang = name

get_description

Return the 160 chars description of the HTML page.

View Source
def get_description(self) -> str:
    """Return the 160 chars description of the HTML page."""
    return self.__descr

set_description

Set a 160 chars max description text.

Parameters
  • descr: (str) Description of the documented document
Raises
  • TypeError: Given descr is not a string
View Source
def set_description(self, descr: str='') -> NoReturn:
    """Set a 160 chars max description text.

        :param descr: (str) Description of the documented document
        :raises: TypeError: Given descr is not a string

        """
    if isinstance(descr, (str, bytes)) is False:
        raise TypeError("Expected a 'str' for the HTMLDocExport.descr. Got {} instead.".format(descr))
    descr = descr.replace('\n', ' ')
    descr = descr.replace("'", ' ')
    descr = descr.replace('"', ' ')
    if len(descr) < 90:
        logging.warning(f'Given description is a little bit shorted than the 90 expected characters: {descr}')
        descr = 'Python Class Documentation of ' + descr
    if len(descr) > 160:
        logging.warning(f'Given description is longer than 160 characters: {descr}.')
    self.__descr = descr[:160]

get_next_class

Return the name of the next documented class.

View Source
def get_next_class(self) -> str:
    """Return the name of the next documented class."""
    return self.__next_class

set_next_class

Set the name of the next documented class.

Parameters
  • name: (str|None) Name of the next documented class
Raises
  • TypeError: Given name is not a string
View Source
def set_next_class(self, name: str | None=None) -> NoReturn:
    """Set the name of the next documented class.

        :param name: (str|None) Name of the next documented class
        :raises: TypeError: Given name is not a string

        """
    if name is not None and isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' or None for the HTMLDocExport.next_class. Got {} instead.".format(name))
    self.__next_class = name

get_prev_class

Return the name of the previous documented class, for the ToC.

View Source
def get_prev_class(self) -> str:
    """Return the name of the previous documented class, for the ToC."""
    return self.__prev_class

set_prev_class

Set the name of the previous documented class.

Parameters
  • name: (str|None) Name of the previous documented class
Raises
  • TypeError: Given name is not a string
View Source
def set_prev_class(self, name: str | None=None) -> NoReturn:
    """Set the name of the previous documented class.

        :param name: (str|None) Name of the previous documented class
        :raises: TypeError: Given name is not a string

        """
    if name is not None and isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' or None for the HTMLDocExport.prev_class. Got {} instead.".format(name))
    self.__prev_class = name

get_next_module

Return the name of the next documented module.

View Source
def get_next_module(self) -> str:
    """Return the name of the next documented module."""
    return self.__next_pack

set_next_module

Set the name of the next documented module.

Parameters
  • name: (str|None) Name of the next documented module
Raises
  • TypeError: Given name is not a string
View Source
def set_next_module(self, name: str | None=None) -> NoReturn:
    """Set the name of the next documented module.

        :param name: (str|None) Name of the next documented module
        :raises: TypeError: Given name is not a string

        """
    if name is not None and isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' or None for the HTMLDocExport.next_module. Got {} instead.".format(name))
    self.__next_pack = name

get_prev_module

Return the name of the previous documented module, for the ToC.

View Source
def get_prev_module(self) -> str:
    """Return the name of the previous documented module, for the ToC."""
    return self.__prev_pack

set_prev_module

Set the name of the previous documented module.

Parameters
  • name: (str|None) Name of the previous documented module
Raises
  • TypeError: Given name is not a string
View Source
def set_prev_module(self, name: str | None=None) -> NoReturn:
    """Set the name of the previous documented module.

        :param name: (str|None) Name of the previous documented module
        :raises: TypeError: Given name is not a string

        """
    if name is not None and isinstance(name, (str, bytes)) is False:
        raise TypeError("Expected a 'str' or None for the HTMLDocExport.prev_module. Got {} instead.".format(name))
    self.__prev_pack = name

get_head

Return the HTML 'head' of the page.

View Source
def get_head(self) -> str:
    """Return the HTML 'head' of the page."""
    return ExportOptions.HTML_HEAD.format(TITLE=self.__title, FAVICON=self.__favicon, THEME=self.__theme, STATICS=self.__statics, WEXA_STATICS=self.__wexa_statics, META_DESCRIPTION=self.__descr, THEME_LINK=self.__theme_part(ExportOptions.HTML_THEME_LINK), THEME_BUNDLE=self.__theme_part(ExportOptions.HTML_THEME_BUNDLE), THEME_MODULE=self.__theme_part(ExportOptions.HTML_THEME_MODULE))

get_header

Return the 'header' of the HTML->body of the page.

View Source
def get_header(self) -> str:
    """Return the 'header' of the HTML->body of the page."""
    h = list()
    h.append('    <header>')
    theme_button = ''
    if len(self.__css_theme) > 0:
        theme_button = ExportOptions.HTML_THEME_BUTTON
    h.append(ExportOptions.HTML_BUTTONS_ACCESSIBILITY.format(THEME_BUTTON=theme_button))
    if len(self.__software) > 0:
        h.append('    <h1>{SOFTWARE}</h1>'.format(SOFTWARE=self.__software))
    if len(self.__icon) > 0:
        h.append('        <p><img class="small-logo" src="{STATICS}/{ICON}" alt="Software logo"/></p>'.format(STATICS=self.__statics, ICON=self.__icon))
    if len(self.__url) > 0:
        h.append('        <p><a class="external-link" href="{URL}">{URL}</a></p>'.format(URL=self.__url))
    h.append('    </header>')
    return '\n'.join(h)

get_nav

Return the 'nav' of the HTML->body of the page.

View Source
def get_nav(self) -> str:
    """Return the 'nav' of the HTML->body of the page."""
    nav = list()
    if self.__aside_toc is True:
        tag_name = 'aside'
        class_name = 'book-toc-aside'
    else:
        tag_name = 'nav'
        class_name = 'book-toc'
    nav.append('<{TAG} id="nav-book" class="{CLASS}" aria-label="Table of contents">'.format(TAG=tag_name, CLASS=class_name))
    if self.__aside_toc is True:
        nav.append('    <h1>Table of Contents</h1>')
    else:
        if self.__software == ExportOptions.DEFAULT_SOFTWARE:
            nav.append('    <h1>Documentation</h1>')
        else:
            nav.append('    <h1>{SOFTWARE}</h1>'.format(SOFTWARE=self.__software))
        if len(self.__icon) > 0:
            nav.append('    <img class="small-logo center" src="{STATICS}/{ICON}" alt=""/>'.format(STATICS=self.__statics, ICON=self.__icon))
        if len(self.__url) > 0:
            nav.append('        <p><a class="external-link" href="{URL}">{URL}</a></p>'.format(URL=self.__url))
    nav.append('    <ul>')
    nav.append(ExportOptions.__nav_link('&crarr; Prev. Module', self.__prev_pack, 'nav-prev-module'))
    nav.append(ExportOptions.__nav_link('&uarr; Prev. Class', self.__prev_class, 'nav-prev-class'))
    nav.append(ExportOptions.__nav_link('&#8962; Index', 'index.html', 'nav-index'))
    nav.append(ExportOptions.__nav_link('&darr; Next Class', self.__next_class, 'nav-next-class'))
    nav.append(ExportOptions.__nav_link('&rdsh; Next Module', self.__next_pack, 'nav-next-module'))
    nav.append('    </ul>')
    if self.__aside_toc is False:
        nav.append('    <h2>Table of Contents</h2>')
    nav.append('    <ul id="toc"></ul>')
    nav.append('    <hr>')
    nav.append('    <p><small>Automatically created</small></p><p><small>by <a class="external-link" href="https://github.com/brigitte-bigi/ClammingPy">ClammingPy</a></small></p>')
    nav.append('</{TAG}>'.format(TAG=tag_name))
    return '\n'.join(nav)

get_footer

Return the 'footer' of the HTML->body of the page.

View Source
def get_footer(self) -> str:
    """Return the 'footer' of the HTML->body of the page."""
    return ExportOptions.HTML_FOOTER.format(COPYRIGHT=self.__copyright)

Protected functions

__theme_part

Return the given part of the 'head' filled with the theme information.

The name a theme is registered with is the name of its file, without the extension: it is what the browser address shows when the reader switched.

Parameters
  • template: (str) One of the HTMLTHEME* templates
Returns
  • (str) The filled template, or an empty string if no theme is set
View Source
def __theme_part(self, template: str) -> str:
    """Return the given part of the 'head' filled with the theme information.

        The name a theme is registered with is the name of its file, without the
        extension: it is what the browser address shows when the reader switched.

        :param template: (str) One of the HTML_THEME_* templates
        :return: (str) The filled template, or an empty string if no theme is set

        """
    if len(self.__css_theme) == 0:
        return ''
    theme_name = self.__css_theme
    if '.' in theme_name:
        theme_name = theme_name[:theme_name.rindex('.')]
    return template.format(STATICS=self.__statics, WEXA_STATICS=self.__wexa_statics, CSS_THEME=self.__css_theme, THEME_NAME=theme_name)

__nav_link

Return a link of the navigation.

The 'wexa-link' class is the one the page collects to hand its links to 'LinkController': a link handled by it carries the framework parameters -- the theme, the contrast and the color -- to the page it opens.

Parameters
  • text: (str) Content of the link
  • link: (str|None) Target of the link, or None for a disabled one
  • identifier: (str) Identifier of the link, required by 'LinkController'
Returns
  • (str) HTML code
View Source
@staticmethod
def __nav_link(text: str, link: str | None, identifier: str) -> str:
    """Return a link of the navigation.

        The 'wexa-link' class is the one the page collects to hand its links to
        'LinkController': a link handled by it carries the framework parameters
        -- the theme, the contrast and the color -- to the page it opens.

        :param text: (str) Content of the link
        :param link: (str|None) Target of the link, or None for a disabled one
        :param identifier: (str) Identifier of the link, required by 'LinkController'
        :return: (str) HTML code

        """
    if link is None:
        a = 'aria-disabled="true"'
    else:
        a = 'href="{:s}" class="wexa-link" data-target="_self"'.format(link)
    return '<li><a role="button" tabindex="0" id="{ID}" {LINK}> {TEXT}</a></li>'.format(ID=identifier, LINK=a, TEXT=text)