Beispiel #1
0
    def make_toc_entry(self, element, component, parent=None):
        type_ = element.tag.split('}', 1)[-1]
        id_ = element.get('id')

        if type_ == 'doc':
            # component, get the title from the alias
            heading = element.find('./{*}meta//{*}FRBRalias')
            if heading is not None:
                heading = heading.get('value')
            else:
                # eg. schedule1 -> Schedule 1
                m = self.component_id_re.match(component)
                if m:
                    typ, num = m.groups()
                    heading = '%s %s' % (_(typ.capitalize()), num)
                else:
                    heading = _(component.capitalize())
        else:
            try:
                heading = _collect_string_content(element.heading)
            except AttributeError:
                heading = None

        try:
            num = element.num
        except AttributeError:
            num = None

        num = num.text if num else None

        if type_ == "doc":
            subcomponent = None
        else:
            # if we have a chapter/part as a child of a chapter/part, we need to include
            # the parent as context because they aren't unique, eg: part/1/chapter/2
            if type_ in self.toc_non_unique_components and parent and parent.type in self.toc_non_unique_components:
                subcomponent = parent.subcomponent + "/"
            else:
                subcomponent = ""

            # eg. 'preamble' or 'chapter/2'
            subcomponent += type_

            if num:
                subcomponent += '/' + num.strip('.()')

        toc_item = TOCElement(element,
                              component,
                              type_,
                              heading=heading,
                              id_=id_,
                              num=num,
                              subcomponent=subcomponent,
                              parent=parent)
        toc_item.title = self.friendly_title(toc_item)

        return toc_item
Beispiel #2
0
    def element(self, element, component, parent=None):
        type_ = element.tag.split("}", 1)[-1]
        id_ = element.get("id")

        if type_ == "doc":
            # component, get the title from the alias
            heading = element.find("./{*}meta//{*}FRBRalias")
            if heading is not None:
                heading = heading.get("value")
            else:
                # eg. schedule1 -> Schedule 1
                m = self.component_id_re.match(component)
                if m:
                    heading = " ".join(m.groups()).capitalize()
                else:
                    heading = component.capitalize()
        else:
            try:
                heading = _collect_string_content(element.heading)
            except AttributeError:
                heading = None

        try:
            num = element.num
        except AttributeError:
            num = None

        num = num.text if num else None

        if type_ == "doc":
            subcomponent = None
        else:
            # if we have a chapter/part as a child of a chapter/part, we need to include
            # the parent as context because they aren't unique, eg: part/1/chapter/2
            if type_ in self.toc_non_unique_components and parent and parent.type in self.toc_non_unique_components:
                subcomponent = parent.subcomponent + "/"
            else:
                subcomponent = ""

            # eg. 'preamble' or 'chapter/2'
            subcomponent += type_

            if num:
                subcomponent += "/" + num.strip(".()")

        return TOCElement(
            element, component, type_, heading=heading, id_=id_, num=num, subcomponent=subcomponent, parent=parent
        )