wikitextparser/_wikitext.py
def tags(self, name=None) -> List['Tag']:
        """Return all tags with the given name."""
        lststr = self._lststr
        type_to_spans = self._type_to_spans
        if name:
            if name in _tag_extensions:
                string = lststr[0]
                return [
                    Tag(lststr, type_to_spans, span, 'ExtensionTag')
                    for span in type_to_spans['ExtensionTag']
                    if string.startswith('<' + name, span[0])]
            tags = []  # type: List['Tag']
        else:
            # There is no name, add all extension tags. Before using shadow.
            tags = [
                Tag(lststr, type_to_spans, span, 'ExtensionTag')
                for span in type_to_spans['ExtensionTag']]
        tags_append = tags.append
        # Get the left-most start tag, match it to right-most end tag
        # and so on.
        ss = self._span[0]
        shadow = self._shadow
        if name:
            # There is a name but it is not in TAG_EXTENSIONS.
            reversed_start_matches = reversed([m for m in regex_compile(
                START_TAG_PATTERN.replace(
                    rb'{name}', rb'(?P<name>' + name.encode() + rb')')
            ).finditer(shadow)])
            end_search = regex_compile(END_TAG_PATTERN .replace(
                b'{name}', name.encode())).search
        else:
            reversed_start_matches = reversed(
                [m for m in START_TAG_FINDITER(shadow)])
        shadow_copy = shadow[:]
        spans = type_to_spans.setdefault('Tag', [])
        span_tuple_to_span_get = {(s[0], s[1]): s for s in spans}.get
        spans_append = spans.append
        for start_match in reversed_start_matches:
            if start_match['self_closing']:
                # Don't look for the end tag
                s, e = start_match.span()
                span = [ss + s, ss + e]
            else:
                # look for the end-tag
                if name:
                    # the end_search is already available
                    # noinspection PyUnboundLocalVariable
                    end_match = end_search(shadow_copy, start_match.end())
                else:
                    # build end_search according to start tag name
                    end_match = search(
                        END_TAG_PATTERN.replace(
                            b'{name}', start_match['name']),
                        shadow_copy)
                if end_match:
                    s, e = end_match.span()
                    shadow_copy[s:e] = b'_' * (e - s)
                    span = [ss + start_match.start(), ss + e]
                else:
                    # Assume start-only tag.
                    s, e = start_match.span()
                    span = [ss + s, ss + e]
            old_span = span_tuple_to_span_get((span[0], span[1]))
            if old_span is None:
                spans_append(span)
            else:
                span = old_span
            tags_append(Tag(lststr, type_to_spans, span, 'Tag'))
        return sorted(tags, key=attrgetter('_span'))
def tags(self, name=None) -> List['Tag']:
        """Return all tags with the given name."""
        lststr = self._lststr
        type_to_spans = self._type_to_spans
        if name:
            if name in _tag_extensions:
                string = lststr[0]
                return [
                    Tag(lststr, type_to_spans, span, 'ExtensionTag')
                    for span in type_to_spans['ExtensionTag']
                    if string.startswith('<' + name, span[0])]
            tags = []  # type: List['Tag']
        else:
            # There is no name, add all extension tags. Before using shadow.
            tags = [
                Tag(lststr, type_to_spans, span, 'ExtensionTag')
                for span in type_to_spans['ExtensionTag']]
        tags_append = tags.append
        # Get the left-most start tag, match it to right-most end tag
        # and so on.
        ss = self._span[0]
        shadow = self._shadow
        if name:
            # There is a name but it is not in TAG_EXTENSIONS.
            reversed_start_matches = reversed([m for m in regex_compile(
                START_TAG_PATTERN.replace(
                    rb'{name}', rb'(?P<name>' + name.encode() + rb')')
            ).finditer(shadow)])
            end_search = regex_compile(END_TAG_PATTERN .replace(
                b'{name}', name.encode())).search
        else:
            reversed_start_matches = reversed(
                [m for m in START_TAG_FINDITER(shadow)])
        shadow_copy = shadow[:]
        spans = type_to_spans.setdefault('Tag', [])
        span_tuple_to_span_get = {(s[0], s[1]): s for s in spans}.get
        spans_append = spans.append
        for start_match in reversed_start_matches:
            if start_match['self_closing']:
                # Don't look for the end tag
                s, e = start_match.span()
                span = [ss + s, ss + e]
            else:
                # look for the end-tag
                if name:
                    # the end_search is already available
                    # noinspection PyUnboundLocalVariable
                    end_match = end_search(shadow_copy, start_match.end())
                else:
                    # build end_search according to start tag name
                    end_match = search(
                        END_TAG_PATTERN.replace(
                            b'{name}', start_match['name']),
                        shadow_copy)
                if end_match:
                    s, e = end_match.span()
                    shadow_copy[s:e] = b'_' * (e - s)
                    span = [ss + start_match.start(), ss + e]
                else:
                    # Assume start-only tag.
                    s, e = start_match.span()
                    span = [ss + s, ss + e]
            old_span = span_tuple_to_span_get((span[0], span[1]))
            if old_span is None:
                spans_append(span)
            else:
                span = old_span
            tags_append(Tag(lststr, type_to_spans, span, 'Tag'))
        return sorted(tags, key=attrgetter('_span'))
def tags ( self , name = None ) -> List [ 'Tag' ] : lststr = self . _lststr type_to_spans = self . _type_to_spans if name : if name in _tag_extensions : string = lststr [ 0 ] return [ Tag ( lststr , type_to_spans , span , 'ExtensionTag' ) for span in type_to_spans [ 'ExtensionTag' ] if string . startswith ( '<' + name , span [ 0 ] ) ] tags = [ ] # type: List['Tag'] else : # There is no name, add all extension tags. Before using shadow. tags = [ Tag ( lststr , type_to_spans , span , 'ExtensionTag' ) for span in type_to_spans [ 'ExtensionTag' ] ] tags_append = tags . append # Get the left-most start tag, match it to right-most end tag # and so on. ss = self . _span [ 0 ] shadow = self . _shadow if name : # There is a name but it is not in TAG_EXTENSIONS. reversed_start_matches = reversed ( [ m for m in regex_compile ( START_TAG_PATTERN . replace ( rb'{name}' , rb'(?P<name>' + name . encode ( ) + rb')' ) ) . finditer ( shadow ) ] ) end_search = regex_compile ( END_TAG_PATTERN . replace ( b'{name}' , name . encode ( ) ) ) . search else : reversed_start_matches = reversed ( [ m for m in START_TAG_FINDITER ( shadow ) ] ) shadow_copy = shadow [ : ] spans = type_to_spans . setdefault ( 'Tag' , [ ] ) span_tuple_to_span_get = { ( s [ 0 ] , s [ 1 ] ) : s for s in spans } . get spans_append = spans . append for start_match in reversed_start_matches : if start_match [ 'self_closing' ] : # Don't look for the end tag s , e = start_match . span ( ) span = [ ss + s , ss + e ] else : # look for the end-tag if name : # the end_search is already available # noinspection PyUnboundLocalVariable end_match = end_search ( shadow_copy , start_match . end ( ) ) else : # build end_search according to start tag name end_match = search ( END_TAG_PATTERN . replace ( b'{name}' , start_match [ 'name' ] ) , shadow_copy ) if end_match : s , e = end_match . span ( ) shadow_copy [ s : e ] = b'_' * ( e - s ) span = [ ss + start_match . start ( ) , ss + e ] else : # Assume start-only tag. s , e = start_match . span ( ) span = [ ss + s , ss + e ] old_span = span_tuple_to_span_get ( ( span [ 0 ] , span [ 1 ] ) ) if old_span is None : spans_append ( span ) else : span = old_span tags_append ( Tag ( lststr , type_to_spans , span , 'Tag' ) ) return sorted ( tags , key = attrgetter ( '_span' ) )
Return all tags with the given name.
Return all tags with the given name .