Exemple #1
0
    def run(self, doc):
        # Get a list of id attributes
        used_ids = set()
        for el in doc.iter():
            if "id" in el.attrib:
                used_ids.add(el.attrib["id"])

        toc_tokens = []
        for el in doc.iter():
            if isinstance(el.tag, str) and self.header_rgx.match(el.tag):
                self.set_level(el)
                text = get_name(el)

                # Do not override pre-existing ids
                if "id" not in el.attrib:
                    innertext = unescape(stashedHTML2text(text, self.md))
                    el.attrib["id"] = unique(self.slugify(innertext, self.sep), used_ids)  # noqa: E501

                if int(el.tag[-1]) >= self.toc_top and int(el.tag[-1]) <= self.toc_bottom:  # noqa: E501
                    toc_tokens.append({
                        'level': int(el.tag[-1]),
                        'id': el.attrib["id"],
                        'name': unescape(stashedHTML2text(
                            code_escape(el.attrib.get('data-toc-label', text)),
                            self.md, strip_entities=False
                        ))
                    })

                # Remove the data-toc-label attribute as it is no longer needed
                if 'data-toc-label' in el.attrib:
                    del el.attrib['data-toc-label']

                if self.use_anchors:
                    self.add_anchor(el, el.attrib["id"])
                if self.use_permalinks not in [False, None]:
                    self.add_permalink(el, el.attrib["id"])

        toc_tokens = nest_toc_tokens(toc_tokens)
        div = self.build_toc_div(toc_tokens)
        div.attrib["c2c:role"] = "toc"
        del div.attrib["class"]

        if self.marker:
            self.replace_marker(doc, div)

        # serialize and attach to markdown instance.
        toc = self.md.serializer(div)
        for pp in self.md.postprocessors:
            toc = pp.run(toc)
        self.md.toc_tokens = toc_tokens
        self.md.toc = toc
Exemple #2
0
    def run(self, doc):
        # Get a list of id attributes
        used_ids = set()
        for el in doc.iter():
            if "id" in el.attrib:
                used_ids.add(el.attrib["id"])

        toc_items = []
        for el in doc.iter():
            if isinstance(el.tag, string_type) and re.match("[hH]2", el.tag):
                self.set_level(el)
                text = ''.join(el.itertext()).strip()

                # Do not override pre-existing ids
                if "id" not in el.attrib:
                    innertext = stashedHTML2text(text, self.markdown)
                    el.attrib["id"] = unique(self.slugify(innertext, self.sep),
                                             used_ids)

                toc_items.append({'id': el.attrib["id"], 'name': text})

                if self.use_anchors:
                    self.add_anchor(el, el.attrib["id"])
                if self.use_permalinks:
                    self.add_permalink(el, el.attrib["id"])

        self.markdown.toc_items = toc_items
    def run(self, root):
        """Add header anchors."""

        self.get_settings()

        # Get a list of id attributes
        used_ids = set()
        for tag in root.iter():
            if "id" in tag.attrib:
                used_ids.add(tag.attrib["id"])

        for tag in root.iter():
            if tag.tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'):
                if "id" in tag.attrib:
                    id_attr = tag.get('id')
                else:
                    id_attr = stashedHTML2text(''.join(tag.itertext()),
                                               self.md)
                    id_attr = unique(self.slugify(id_attr, self.separator),
                                     used_ids)
                    tag.set('id', id_attr)
                tag.text = self.markdown.htmlStash.store(
                    LINK % {"id": id_attr},
                    safe=True) + tag.text if tag.text is not None else ''
        return root
Exemple #4
0
    def run(self, doc):
        start_level, force_id = self._get_meta()
        slugify = self.config['slugify']
        sep = self.config['separator']
        for elem in doc:
            if elem.tag in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
                if force_id:
                    if "id" in elem.attrib:
                        id = elem.get('id')
                    else:
                        id = stashedHTML2text(''.join(elem.itertext()),
                                              self.md)
                        id = slugify(id, sep)
                    id = unique(id, self.IDs)

                    elem.set('id', id)
                    anchor = Element('a')
                    anchor.set('href', '#' + id)
                    anchor.set('class', 'anchor')
                    elem.append(anchor)
                if start_level:
                    level = int(elem.tag[-1]) + start_level
                    if level > 6:
                        level = 6
                    elem.tag = 'h%d' % level
Exemple #5
0
    def run(self, doc):
        def not_emphasis(elt):
            return elt.attrib.get("c2c:role") != "header-emphasis"

        # Get a list of id attributes
        used_ids = set()
        for el in doc.iter():
            if "id" in el.attrib:
                used_ids.add(el.attrib["id"])

        toc_tokens = []
        for el in doc.iter():
            if isinstance(el.tag, string_type) and \
                    self.header_rgx.match(el.tag):
                self.set_level(el)

                # modification for camptocamp
                # orginal line was :
                # text = ''.join(el.itertext()).strip()

                text = ''.join([
                    elt.text for elt in el.iter()
                    if elt.text and not_emphasis(elt)
                ])

                # Do not override pre-existing ids
                if "id" not in el.attrib:
                    innertext = stashedHTML2text(text, self.markdown)
                    el.attrib["id"] = unique(self.slugify(innertext, self.sep),
                                             used_ids)

                level = int(el.tag[-1])
                if level < 5:  # test for camptocamp
                    toc_tokens.append({
                        'level': level,
                        'id': el.attrib["id"],
                        'name': text
                    })

                if self.use_anchors:
                    self.add_anchor(el, el.attrib["id"])
                if self.use_permalinks:
                    self.add_permalink(el, el.attrib["id"])

        div = self.build_toc_div(nest_toc_tokens(toc_tokens))
        div.attrib["c2c:role"] = "toc"
        del div.attrib["class"]
        if self.marker:
            self.replace_marker(doc, div)

        # serialize and attach to markdown instance.
        toc = self.markdown.serializer(div)
        for pp in self.markdown.postprocessors.values():
            toc = pp.run(toc)
        self.markdown.toc = toc
    def run(self, doc):
        start_level, force_id = self._get_meta()
        slugify = self.config['slugify']
        sep = self.config['separator']
        for elem in doc:
            if elem.tag in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
                if force_id:
                    if "id" in elem.attrib:
                        id = elem.get('id')
                    else:
                        id = stashedHTML2text(''.join(elem.itertext()), self.md)
                        id = slugify(id, sep)
                    id = unique(id, self.IDs)

                    elem.set('id', id)
                    anchor = Element('a')
                    anchor.set('href', '#' + id)
                    anchor.set('class', 'anchor')
                    elem.append(anchor)
                if start_level:
                    level = int(elem.tag[-1]) + start_level
                    if level > 6:
                        level = 6
                    elem.tag = 'h%d' % level
    def run(self, root):
        """Add header anchors."""

        self.get_settings()

        # Get a list of id attributes
        used_ids = set()
        for tag in root.iter():
            if "id" in tag.attrib:
                used_ids.add(tag.attrib["id"])

        for tag in root.getiterator():
            if tag.tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'):
                if "id" in tag.attrib:
                    id_attr = tag.get('id')
                else:
                    id_attr = stashedHTML2text(''.join(tag.itertext()), self.md)
                    id_attr = unique(self.slugify(id_attr, self.separator), used_ids)
                    tag.set('id', id_attr)
                tag.text = self.markdown.htmlStash.store(
                    LINK % {"id": id_attr},
                    safe=True
                ) + tag.text if tag.text is not None else ''
        return root