WhakerPy 0.8

https://sourceforge.net/projects/whakerpy/

Module whakerpy.htmlmaker

Class HTMLHeadNode

Description

Convenient class to represent the head node of an HTML tree.

Children of a "head" node are limited to the ones of HEAD_TAGS list.

Constructor

Create the head node.

Parameters
  • parent
View Source
def __init__(self, parent):
    """Create the head node."""
    super(HTMLHeadNode, self).__init__(parent, 'head', 'head')

Public functions

append_child

Append a child node.

Parameters
  • node: (Node)
Raises
  • NodeChildTagError: if invalid child tag (not in HEAD_TAGS list)
View Source
def append_child(self, node) -> None:
    """Append a child node.

        :param node: (Node)
        :raise: NodeChildTagError: if invalid child tag (not in HEAD_TAGS list)

        """
    if node.tag not in HEAD_TAGS:
        raise NodeChildTagError(node.tag)
    HTMLNode.append_child(self, node)

insert_child

Insert a child node at the given index.

Parameters
  • pos: (int) Index position
  • node: (Node)
View Source
def insert_child(self, pos, node) -> None:
    """Insert a child node at the given index.

        :param pos: (int) Index position
        :param node: (Node)

        """
    if node.tag not in HEAD_TAGS:
        raise NodeChildTagError(node.tag)
    HTMLNode.insert_child(self, pos, node)

title

Set the title to the header.

Parameters
  • title: (str) The page title (expected short!)
View Source
def title(self, title) -> None:
    """Set the title to the header.

        :param title: (str) The page title (expected short!)

        """
    for child in self._children:
        if child.identifier == 'title':
            child.set_value(title)
            break

meta

Append a new meta tag to the header.

Parameters
  • metadict: (dict)
View Source
def meta(self, metadict) -> None:
    """Append a new meta tag to the header.

        :param metadict: (dict)

        """
    if isinstance(metadict, dict) is False:
        raise TypeError('Expected a dict.')
    child_node = EmptyNode(self.identifier, None, 'meta', attributes=metadict)
    self._children.append(child_node)

link

Add a link tag to the header.

Parameters
  • rel: (str)
  • href: (str) Path and/or name of the link reference
  • link_type: (str) Mimetype of the link file
View Source
def link(self, rel: str, href: str, link_type: str=None) -> None:
    """Add a link tag to the header.

        :param rel: (str)
        :param href: (str) Path and/or name of the link reference
        :param link_type: (str) Mimetype of the link file

        """
    d = dict()
    d['rel'] = rel
    if len(href) > 0 and href[0].isalpha():
        d['href'] = os.sep + href
    else:
        d['href'] = href
    if link_type is not None:
        d['type'] = link_type
    child_node = EmptyNode(self.identifier, None, 'link', attributes=d)
    self._children.append(child_node)

script

Add a meta tag to the header.

Parameters
  • src: (str) Script source file or Script content
  • script_type: (str) Script type or None if script content
View Source
def script(self, src, script_type) -> None:
    """Add a meta tag to the header.

        :param src: (str) Script source file or Script content
        :param script_type: (str) Script type or None if script content

        """
    if script_type is not None:
        d = dict()
        d['type'] = script_type
        if len(src) > 0 and src[0].isalpha():
            d['src'] = os.sep + src
        else:
            d['src'] = src
        child_node = HTMLNode(self.identifier, None, 'script', attributes=d)
        self._children.append(child_node)
    else:
        child_node = HTMLNode(self.identifier, None, 'script', value=str(src))
        self._children.append(child_node)

css

Append css style content.

Parameters
  • script_content: (str) CSS content
View Source
def css(self, css_content) -> None:
    """Append css style content.

        :param script_content: (str) CSS content

        """
    child_node = HTMLNode(self.identifier, None, 'style', value=str(css_content))
    self._children.append(child_node)