File: _meta_parser.py

package info (click to toggle)
sphinxext-opengraph 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,200 kB
  • sloc: python: 1,130; makefile: 11; sh: 8
file content (29 lines) | stat: -rw-r--r-- 818 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from __future__ import annotations

from html.parser import HTMLParser


def get_meta_description(meta_tags: str) -> bool:
    htp = HTMLTextParser()
    htp.feed(meta_tags)
    htp.close()

    return htp.meta_description


class HTMLTextParser(HTMLParser):
    """Parse HTML into text."""

    def __init__(self) -> None:
        super().__init__()
        self.meta_description = None

    def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
        # For example:
        # attrs = [("content", "My manual description"), ("name", "description")]
        if ('name', 'description') in attrs:
            self.meta_description = True
            for name, value in attrs:
                if name == 'content':
                    self.meta_description = value
                    break